Union-find motivation [00P2]

Notice that for each edge e = (u,v), doing a DFS is sort of overkill, in the sense that it actually tries to find a path from u to v—but we don’t care about the path, only whether u and v are already connected or not. In this case—when we only care about which vertices are connected and not about the actual paths between them—it turns out that we can test for connectivity much faster.

The basic idea is to come up with some sort of data structure to maintain a set of connected components. Given such a data structure, the algorithm looks something like this:

  • Start every vertex in its own connected component.
  • For each edge, test whether its vertices are in the same component (which means it would form a cycle) or in different components.
  • If the endpoints are in different components, add the edge to T, and merge the two components into one.

This kind of data structure is called a union-find structure (or a disjoint-set data structure), and from the algorithm sketch above we can see what operations it needs to support:

  • \mathsf {MakeSets}(n): create a union-find structure containing n singleton sets labelled 0 \dots n-1.
  • \mathsf {Find}(v): return the name of the set containing v. (“Name” could be anything; typically we will use integers.) To check whether two vertices u and v are in the same connected component, we can test if \mathsf {Find}(u) = \mathsf {Find}(v).
  • \mathsf {Union}(x,y): merge the two sets containing x and y.

This kind of data structure actually has lots of applications besides implementing Kruskal's Algorithm; you can explore one such application on a homework problem.