Link

Kruskal’s Algorithm

Implementing Kruskal’s algorithm to generate mazes.

Table of contents

  1. Generating Mazes
    1. MinimumSpanningTreeFinder
    2. KruskalGraph
    3. DisjointSets
  2. KruskalMinimumSpanningTreeFinder
  3. KruskalMazeCarver
  4. UnionBySizeCompressingDisjointSets
  5. Submission

Generating Mazes

Background
Understand the interfaces involved.

We saw earlier that the “remove random walls” algorithms usually ended up generating pretty poor mazes—they either removed too many walls and created trivial mazes, or removed too few and created impossible ones.

What we really want is an algorithm that:

  1. generates a random-looking maze
  2. makes sure the maze is actually solvable
  3. removes as few walls as possible

It turns out that we can use MST algorithms such as Prim’s and Kruskal’s to do exactly that! We’ll start this portion of the assignment by implementing Kruskal’s algorithm, and afterwards you’ll use it to generate better mazes.

MinimumSpanningTreeFinder

Background
Much like ShortestPathFinder, this interface describes an object that simply computes minimum spanning trees.

The interface also includes the same gross generic definitions as ShortestPathFinder, but once again, you should be able to safely ignore them—the important takeaway is that G is a Graph, V can be any object, and E is a BaseEdge.

SignatureDescription
MinimumSpanningTree<V, E>
findMinimumSpanningTree(G graph)
Finds and returns a minimum spanning tree for the given graph.

MinimumSpanningTree is another container for edges, but unlike ShortestPath, the edges are unordered (since the edges of an MST don’t have any particular ordering like the edges of a path do).

KruskalGraph

Background
Kruskal’s algorithm requires some extra functionality from its graphs beyond the basic Graph interface:
SignatureDescription
Collection<V> allVertices()Returns an unmodifiable collection of all vertices in the graph.
Collection<E> allEdges()Returns an unmodifiable collection of all edges in the graph.

DisjointSets

Background
Kruskal’s algorithm also uses the disjoint sets ADT:
SignatureDescription
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 skeleton includes a naive implementation, QuickFindDisjointSets, which you can use to start. You’ll write a faster implementation later.

KruskalMinimumSpanningTreeFinder

Task
Complete KruskalMinimumSpanningTreeFinder, using Kruskal’s algorithm to implement the MinimumSpanningTreeFinder interface.

Implementation notes:

  • The generic type bounds on this class require G to be a subtype of KruskalGraph.
  • The skeleton code includes a snippet of code that sorts the edges of the given graph based on their weights, so you don’t need to worry about figuring out how to do that.
  • Unlike the pseudocode from lecture, the findShortestPath must be able to detect when no MST exists and return the corresponding MinimumSpanningTree result.
Test graph diagrams:

Tree graph:

Ga a b b a--b 2 c c a--c 3 d d c--d 1

Graph with cycle:

Ga a b b a--b 1 c c a--c 3 d d a--d 2 e e b--e 6 e--c 5 c--d 4

Disconnected graph:

Ga a b b a--b 2 d d c c d--c 3 e e d--e 1 e--c 5

Graph with self-loop edge:

Ga a a--a 0 c c a--c 2 b b b--c 1

KruskalMazeCarver

Task
Implement KruskalMazeCarver using KruskalMinimumSpanningTreeFinder.

The MazeCarver requires subclasses to implement a single method:

SignatureDescription
Set<Wall>
chooseWallsToRemove(Set<Wall> walls)
Given a set of walls separating rooms in a maze base, returns a set of every wall that should be removed to form a maze.

Recall our criteria from above:

  1. generates a random-looking maze
  2. makes sure the maze is actually solvable
  3. removes as few walls as possible

Here’s the trick: we take the maze and treat each room as a vertex and each wall as an edge, much like we would when solving the maze (the only difference being that edges now represent walls instead of pathways). Then, we can assign each wall a random weight, and run any MST-finding algorithm.

By removing any wall that was a part of that MST, we end up satisfying all three criteria! By randomizing the wall weights, we remove random walls which satisfy criterion 1. An MST, by definition, will include a path from every vertex (every room) to every other one, satisfying criterion 2. And finally, because the MST will not have cycles, we avoid removing unnecessary edges and end up with a maze where there really is only one solution, satisfying criterion 3.

Implementation notes:

  • If you aren’t sure where to start your implementation, take a look at MazeGraph, Wall, and related classes. To reiterate, Rooms will be the vertices in the graph, and edges will represent Walls.
  • Use the Random object in the provided rand field to generate the random edge weights.
  • Use the MinimumSpanningTreeFinder object in the provided minimumSpanningTreeFinder field to find the minimum spanning tree.

After you finish, you can try using your code to generate some mazes by running the program and using the “Run (randomized) Kruskal” option. You should notice that although the mazes generated look much better than before, they take a bit longer to generate—we’ll address this by creating a faster disjoint sets implementation.

UnionBySizeCompressingDisjointSets

Task
Implement UnionBySizeCompressingDisjointSets, and use it to speed up KruskalMinimumSpanningTreeFinder.

Make sure that your implementation unions by size and uses path compression. Also make sure to store the array representation of your disjoint sets in the pointers field—the grader tests will inspect it directly.

After modifying your KruskalMinimumSpanningTreeFinder to use this class, you should notice that maze generation using KruskalMazeCarver becomes significantly faster—almost indistinguishable from the time required by the RandomMazeCarver.

Submission

Task
Commit and push your changes to GitLab before submitting to Gradescope.

After you’re done, remember to complete the individual feedback survey for extra credit, as described on the project main page.