Imagine a room full of strangers. Now and then two of them shake hands and merge their circles of friends into one. At any moment you might be asked: are these two people in the same circle? As the handshakes pile up, answering that quickly stops being obvious.
Union-Find â also called the disjoint-set structure â does exactly this. It supports two operations: union, which merges the groups of two elements, and find, which returns a representative for an element's group so you can test whether two elements are connected.
The astonishing part is the speed. With two small tricks â union by rank and path compression â each operation costs, in practice, less than a constant: for any input you could ever build, the amortized cost per operation is smaller than five. This is not a hard open problem; it is a solved one, and its solution is one of the most beautiful results in algorithm analysis.
Comments
Loading comments...