Minimum Spanning Trees
Kruskal
Greedily add edges ordered by their weights until all nodes belong to the same component (all are connected).
Hint: We can use Union-Find to check if all nodes are connected.
Time: O(E log V)
Prim
Prims works with an aribtrary node, and then always chooses the smallest edge that adds a new node. (Similar to Dijkstra, except Dijkstra selects to minimize weight from the start, not the current node).
Uses a Priority Queue.
Time: O(E + V log V)
Last updated