Package dsa
Class QuickFindUF
java.lang.Object
dsa.QuickFindUF
- All Implemented Interfaces:
UF
This data type provides an implementation of the Union Find (UF) API in which the find, count, and connected
operations run in constant time, whereas the union operation runs in linear time.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
connected
(int p, int q) Returnstrue
if sitesp
andq
belong to the same component, andfalse
otherwise.int
count()
Returns the number of components.int
find
(int p) Returns the canonical site of the component containing sitep
.static void
Unit tests the data type.void
union
(int p, int q) Connects sitesp
andq
.
-
Constructor Details
-
QuickFindUF
public QuickFindUF(int n) Constructs an empty UF data structure with n sites.- Parameters:
n
- the number of sites.
-
-
Method Details
-
find
public int find(int p) Returns the canonical site of the component containing sitep
. -
count
public int count()Returns the number of components. -
connected
public boolean connected(int p, int q) Returnstrue
if sitesp
andq
belong to the same component, andfalse
otherwise. -
union
public void union(int p, int q) Connects sitesp
andq
. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-