QuickFindUF Class |
The QuickFindUF class represents a Union-find data type (also known as the Disjoint-sets data type). It supports the Union and Find operations, along with a Connected operation for determining whether two sites are in the same component and a Count operation that returns the total number of components.
The union-find data type models connectivity among a set of N sites, named 0 through N - 1. The Is-connected-to relation must be an Equivalence relation (see text).
An equivalence relation partitions the sites into Equivalence classes (or Components). In this case, two sites are in the same component if and only if they are connected. Both sites and components are identified with integers between 0 and N - 1. Initially, there are N components, with each site in its own component. The Component identifier of a component (also known as the Root, Canonical element, Leader, or Set representative) is one of the sites in the component: two sites have the same component identifier if and only if they are in the same component.
The component identifier of a component can change only when the component itself changes during a call to Union; it cannot change during a call to Find, Connected, or Count.
This implementation uses quick find. Initializing a data structure with N sites takes linear time. Afterwards, the Find, Connected, and Count operations take constant time but the Union operation takes linear time.
For alternate implementations of the same API, see , , and .
Namespace: Algs4Net
public class QuickFindUF
The QuickFindUF type exposes the following members.
Name | Description | |
---|---|---|
![]() | QuickFindUF | Initializes an empty union-find data structure with N sites
0 through N-1. Each site is initially in its own
component. |
Name | Description | |
---|---|---|
![]() | Connected |
Returns true if the the two sites are in the same component. |
![]() | Find |
Returns the component identifier for the component containing site p. |
![]() ![]() | MainTest |
Reads in a sequence of pairs of integers (between 0 and N-1) from standard input,
where each integer represents some site;
if the sites are in different components, merge the two components
and print the pair to standard output. |
![]() | Union |
Merges the component containing site p with the
the component containing site q. |
For additional documentation, see Section 1.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
This class is a C# port from the original Java class QuickFindUF implementation by the respective authors.