Prim order
Warm-up · Trace the algorithm · about 5 min
Run Prim from A on this graph. First click the vertex that joins the tree second. Then put the other vertices in the order they join.
Example 1
Input:
{"n":6,"edges":[[0,1,4],[0,2,2],[1,2,5],[1,3,1],[2,4,6],[3,4,3],[3,5,8],[4,5,7],[2,3,9]],"source":0}
Output:
["A","C","B","D","E","F"]
The MST weighs 17.
Constraints
- Edge weights are all different.
Learn the idea first: Prim's algorithm, a visual lesson.
More Prim's algorithm problems
- Kruskal vs Prim Warm-up
- Implement Prim Standard
- Supply water to every house Standard
- Cost of the array version Standard
- MST on a dense matrix graph Challenge