0
0
DSA Cprogramming~10 mins

Minimum Spanning Tree Prim's Algorithm in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Minimum Spanning Tree Prim's Algorithm
Start with any node
Add node to MST set
Update edges to nodes not in MST
Select edge with minimum weight to new node
Add new node to MST set
Repeat until all nodes included
Minimum Spanning Tree complete
Prim's algorithm starts from any node, grows the MST by adding the closest node not yet included, and repeats until all nodes are connected.
Execution Sample
DSA C
int primMST(int graph[V][V]) {
  int parent[V];
  int key[V];
  bool mstSet[V];
  // Initialize keys and mstSet
  // Pick minimum key vertex not in mstSet
  // Update keys and parent for adjacent vertices
  // Repeat until MST complete
}
This code finds the minimum spanning tree of a graph using Prim's algorithm.
Execution Table
StepOperationCurrent MST NodesEdge Selected (u-v)Updated KeysVisual MST State
1Start at node 0[0]None[0, ∞, ∞, ∞]0
2Select edge with min key[0,1]0-1 (2)[0,2, ∞, ∞]0 --2-- 1
3Select edge with min key[0,1,3]1-3 (6)[0,2, ∞,6]0 --2-- 1 | 6 3
4Select edge with min key[0,1,3,2]0-2 (3)[0,2,3,6]0 --2-- 1 | | 3 6 2 3
5All nodes included[0,1,3,2]None[0,2,3,6]MST Complete: 0 --2-- 1 | | 3 6 2 3
ExitAll nodes included in MST[0,1,3,2]None[0,2,3,6]Algorithm stops
💡 All nodes are included in MST, so the algorithm stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
parent[-1,-1,-1,-1][-1,0,-1,-1][-1,0,-1,1][-1,0,0,1][-1,0,0,1][-1,0,0,1]
key[∞,∞,∞,∞][0,∞,∞,∞][0,2,∞,∞][0,2,∞,6][0,2,3,6][0,2,3,6]
mstSet[false,false,false,false][true,false,false,false][true,true,false,false][true,true,false,true][true,true,true,true][true,true,true,true]
Key Moments - 3 Insights
Why do we update keys only for nodes not yet in MST?
Because nodes already in MST are connected; updating keys for them is unnecessary. See execution_table rows 2-4 where keys update only for nodes outside MST.
How do we pick the next node to add to MST?
We pick the node with the smallest key value not yet in MST, as shown in execution_table steps 2-4 selecting edges with minimum weights.
Why does the algorithm stop when all nodes are included?
Because MST must connect all nodes without cycles. When mstSet is true for all nodes (see variable_tracker final), MST is complete.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, which edge is selected to add the new node?
A1-3 (6)
B0-2 (3)
C0-1 (2)
D2-3 (∞)
💡 Hint
Check the 'Edge Selected (u-v)' column at Step 3 in execution_table.
At which step does the node 2 get included in the MST?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at 'Current MST Nodes' column in execution_table to see when node 2 appears.
If the edge 0-2 had weight 1 instead of 3, how would the key array change after Step 2?
A[0,2,3,6]
B[0,1,3,∞]
C[0,2,1,∞]
D[0,∞,∞,∞]
💡 Hint
Refer to 'Updated Keys' column in execution_table and how keys update with smaller edge weights.
Concept Snapshot
Prim's Algorithm for MST:
- Start from any node, add it to MST
- Update keys for adjacent nodes not in MST
- Pick node with smallest key to add next
- Repeat until all nodes included
- MST connects all nodes with minimum total edge weight
Full Transcript
Prim's algorithm builds a minimum spanning tree by starting from any node and adding the closest node not yet in the tree. It keeps track of the minimum edge weights to nodes outside the MST using a key array. At each step, it selects the node with the smallest key value and adds it to the MST set. The parent array records the MST structure. The algorithm stops when all nodes are included. The execution table shows step-by-step how nodes are added, keys updated, and edges selected. The variable tracker shows changes in parent, key, and mstSet arrays. Key moments clarify why keys update only for nodes outside MST, how the next node is chosen, and why the algorithm stops when all nodes are included. The visual quiz tests understanding of edge selection, node inclusion steps, and key updates with changed weights.