P4 - Mazes: Disjoint Sets
- Scope
UnionBySizeCompressingDisjointSets(this page),KruskalMinimumSpanningTreeFinderandKruskalMazeCarver(Kruskal's Algorithm)- Check-in due
- Wednesday, November 25
Recommended by M1:
- Disjoint sets tests pass
- Kruskal tests pass (tree, cycle, disconnected, and self-loop graphs)
- In the GUI, “Run (randomized) Kruskal” works and is fast
The check-in is a few short survey questions, graded for completion. You get the points for honest answers even if some of your tests aren’t passing yet, but please make sure to submit it on time (details).
Disjoint Sets
| Signature | Description |
|---|---|
void makeSet(T item) |
Creates a new set containing just the given item and with a new integer id. |
int findSet(T item) |
Returns the integer id of the set containing the given item. |
boolean union(T item1, T item2) |
If the given items are in different sets, merges those sets and
returns true. Otherwise does nothing and returns
false. |
The disjointsets package contains the disjoint sets data
structures required by Kruskal’s algorithm. We include a default,
inefficient implementation in the skeleton.
The inefficient implementation will be replaced by the optimized
UnionBySizeCompressingDisjointSets implementation to
generate mazes.
An Optimized Implementation
Task
Implement UnionBySizeCompressingDisjointSets using
union-by-size, path compression, and array representation
optimizations.
Implementation notes:
- We recommend reviewing the lecture slides and section materials before implementing your disjoint sets.
- The Gradescope tests will directly inspect the
pointersfield so be sure to store the array representation of your disjoint sets there. If a test fails, see tracing disjoint sets by hand.
In the next section, you will use your optimized disjoint sets
implementation to speed up
KruskalMinimumSpanningTreeFinder.