-
Dynamic connectivity
-
Quick Find
-
Quick Union
-
Improvements
-
Applications
This project implements the Union Find algorithm in Java.
Union Find is a Disjoint-Set data structure. It represents a collection of disjoint (non-overlapping) sets.
The two main operations performed on these sets are:
Union- Merge two sets into one.Find- Determine which set a particular element belongs to.
Union Find can be used to solve connectivity problems, detect cycles in graphs and perform hierarchical clustering.
The implementation uses two classes:
Node- Represents a node in the Union Find sets. It contains:data- The data stored in the nodeparent- Points to the parent node in the set
UnionFind- Contains the main Union Find logic:union(int p, int q)- Unites the sets containing p and qfind(int p)- Finds the representative of the set that p belongs toconnected(int p, int q)- Checks if p and q are in the same set
The
find()operation uses path compression to optimize the time complexity.Time complexity:
find(): O(α(n)) amortized due to path compressionunion(): O(1)connected(): O(1)
Space complexity: O(n)
- Compile:
javac UnionFind.java - Run:
java UnionFind
This will run some sample test cases and demonstrate the Union Find operations.
FearLessXT/UnionFind-Algorithm
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|