Lecture 13: Kruskal's algorithm + Union-Find Data structure Flashcards

1
Q

Kruskal’s algorithm and our assumptions

A

It’s a greedy algorithm

  1. Order all the edges by ascending weight into a list L. Se the MST to be empty
  2. 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  • Union-find data structure
  • Union(0, 7) visualgo.net
A

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

  1. if u and v are in the same component, adding the edge will create a cycle
  2. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Using the Union-Find Data structure, what would be the maximum depth of our trees?

A

log(V)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

where is the problem in a lazy union/find

A

The tree height may grow to O(V)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In the union-find algorithm, how about having every vertex/node store its set leader?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Kruskal’s algorithm via Union/Find and run-time analysis

A
  1. Order edges by weight into a list L. Se the minimal spanning tree T = null
  2. 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
  3. 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly