Click or drag to resize
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.

  • union(p, q) adds a connection between the two sites P and Q. If P and Q are in different components, then it replaces these two components with a new component that is the union of the two.
  • find(p) returns the component identifier of the component containing P.
  • connected(p, q) returns true if both P and Q are in the same component, and false otherwise.
  • count() returns the number of components.

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 .

Inheritance Hierarchy
SystemObject
  Algs4NetQuickFindUF

Namespace: Algs4Net
Assembly: Algs4Net (in Algs4Net.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
C#
public class QuickFindUF

The QuickFindUF type exposes the following members.

Constructors
  NameDescription
Public methodQuickFindUF
Initializes an empty union-find data structure with N sites 0 through N-1. Each site is initially in its own component.
Top
Properties
  NameDescription
Public propertyCount
Returns the number of components.
Top
Methods
  NameDescription
Public methodConnected
Returns true if the the two sites are in the same component.
Public methodFind
Returns the component identifier for the component containing site p.
Public methodStatic memberMainTest
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.
Public methodUnion
Merges the component containing site p with the the component containing site q.
Top
Remarks

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.

See Also