Lecture 13: Kruskal's algorithm + Union-Find Data structure Flashcards
Kruskal’s algorithm and our assumptions
It’s a greedy algorithm
- Order all the edges by ascending weight into a list L. Se the MST to be empty
- Get the smallest edge in the list
a. If it forms a cycle, do not add it to the MST
b. Else, add the edge/vertex to the MST and remove it from list L
c. Repeat until done
O(V^2)
Assumptions
- Graph is connected (otherwise, we can adapt the algo to compute MSTs of each connected component - minimum spanning forest).
- Unique edge weights (guarantees a unique MST)
- Union-find data structure
- Union(0, 7) visualgo.net
Observation: Adding edge (u,v) creates a cycle if vertices u & v are in the same component
Let’s start with each node being in its own component (or equivalence class). We add the next edge
- if u and v are in the same component, adding the edge will create a cycle
- Otherwise, we can merge the two components from u and v into one bigger component
Union(0, 7)
1. FindSet(0): find the set leader for node 0 and perform path compression to make it easier later on to know the set leader
2. FindSet(7): do the same as above
3. the shorter rank tree gets updated during the actual union operation
Objective: to speed up Kruskal’s algorithm, we keep trees short, so we can determine if a cycle exists quickly
Using the Union-Find Data structure, what would be the maximum depth of our trees?
log(V)
where is the problem in a lazy union/find
The tree height may grow to O(V)
In the union-find algorithm, how about having every vertex/node store its set leader?
When we “union” two components using this approach, one group of nodes always gets assigned a new leader. In the worse case, this wold take O(n^2) work assuming n nodes.
Kruskal’s algorithm via Union/Find and run-time analysis
- Order edges by weight into a list L. Se the minimal spanning tree T = null
- for each smallest edge (u, v)
a. if find-set(u) != find-set(v)
Add the edge to MST
Union(u, v) – combining the two components w/ path compression - return MST
O(ElogV)
- Total for all find and union operations in O(E) using nearly constant time
- The bottleneck becomes sorting of edges O(ElogV)