Prim's algorithm builds the minimum spanning tree by starting from one node and repeatedly adding the smallest edge that connects the tree to a new node.
Execution Sample
Data Structures Theory
Graph edges:
A-B:2, A-C:3, B-C:1, B-D:4, C-D:5
Start at A
Add smallest edge connecting MST to new node
Repeat until all nodes included
This example shows Prim's algorithm starting at node A and adding edges step-by-step to build the minimum spanning tree.
Analysis Table
Step
Operation
MST Nodes
Edges Considered
Edge Chosen
MST Edges
Visual State
1
Start at node A
[A]
Edges from A: A-B(2), A-C(3)
None
None
MST: A
2
Choose smallest edge from MST to non-MST
[A]
A-B(2), A-C(3)
A-B(2)
[A-B]
MST: A - B
3
Add node B to MST
[A, B]
Edges from A,B: A-C(3), B-C(1), B-D(4)
B-C(1)
[A-B, B-C]
MST: A - B - C
4
Add node C to MST
[A, B, C]
Edges from A,B,C: B-D(4), C-D(5)
B-D(4)
[A-B, B-C, B-D]
MST: A - B - C
|
D
5
Add node D to MST
[A, B, C, D]
All nodes included
None
[A-B, B-C, B-D]
MST complete: A-B, B-C, B-D
6
Stop
[A, B, C, D]
No edges left
None
[A-B, B-C, B-D]
All nodes connected, MST done
💡 All nodes are included in MST, no edges left to add
State Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
After Step 5
Final
MST Nodes
[]
[A]
[A, B]
[A, B, C]
[A, B, C, D]
[A, B, C, D]
Edges Considered
Edges from A
Edges from A,B
Edges from A,B,C
All nodes included
None
None
MST Edges
[]
[A-B]
[A-B, B-C]
[A-B, B-C, B-D]
[A-B, B-C, B-D]
[A-B, B-C, B-D]
Key Insights - 3 Insights
Why do we only consider edges that connect MST nodes to non-MST nodes?
Because Prim's algorithm grows the MST by adding new nodes connected by the smallest edge from the existing MST. Considering edges only between MST and non-MST nodes prevents cycles and ensures all nodes get connected.
What happens if we pick an edge that connects two nodes already in MST?
Picking such an edge would create a cycle, which is not allowed in a spanning tree. The execution_table shows edges chosen always connect MST to a new node.
Why does the algorithm stop when all nodes are included in MST?
Because the MST must include all nodes with the minimum total edge weight. Once all nodes are in MST, no more edges need to be added, as shown in the last step of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. Which edge is chosen to add node C to MST?
AA-C with weight 3
BB-C with weight 1
CB-D with weight 4
DC-D with weight 5
💡 Hint
Check the 'Edge Chosen' column at Step 3 in the execution_table.
At which step does the MST include all nodes?
AStep 5
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'MST Nodes' column to see when all nodes [A, B, C, D] are included.
If the edge B-C had weight 6 instead of 1, which edge would be chosen at Step 3?
AB-D with weight 4
BB-C with weight 6
CA-C with weight 3
DC-D with weight 5
💡 Hint
Compare edge weights in 'Edges Considered' at Step 3 and pick the smallest.
Concept Snapshot
Prim's Algorithm for MST:
- Start from any node
- Add smallest edge connecting MST to new node
- Repeat until all nodes included
- Avoid cycles by only connecting MST to non-MST nodes
- Result is a tree connecting all nodes with minimum total edge weight
Full Transcript
Prim's algorithm builds a minimum spanning tree by starting at any node and repeatedly adding the smallest edge that connects the current tree to a new node. At each step, it considers edges from nodes already in the MST to nodes not yet included. It picks the smallest such edge and adds the connected node to the MST. This process repeats until all nodes are included. The execution table shows step-by-step how nodes and edges are added, ensuring no cycles form and the total edge weight is minimized. Key points include only considering edges connecting MST to non-MST nodes and stopping when all nodes are included. The visual quiz tests understanding of edge choices and MST growth.