How it works?
Lesson 3 of 9 in Coddy's Prim's Algorithm - Graph Algorithms course.
Keep a boolean inTree for each vertex. Start with only vertex 0 in the tree. The frontier is every edge with exactly one endpoint inside the tree, called a crossing edge.
Step-by-Step Process:
- Among all crossing edges, pick the one with the smallest weight.
- Add its outside endpoint to the tree and add its weight to the total.
- Repeat until the tree contains all
nvertices (that takesn - 1edges).
If at some point no crossing edge exists but vertices remain outside, the graph is disconnected.
Example with edges [0,1,1, 1,2,2, 2,3,3, 0,3,4]: from 0 take 0-1(1), then 1-2(2), then 2-3(3); total 6.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Prim's Algorithm - Graph Algorithms
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)