Prim’s Algorithm
Prim’s Algorithm visualizes the construction of a minimum spanning tree on a grid of randomly weighted edges. A magenta frontier expands from the bottom-left corner, with d3.timer driving the step-by-step animation. Each iteration pops the lowest-weight edge from a custom minHeap, adds it to the tree, and updates the frontier on a canvas. The rendering uses manual Canvas 2D fillRect calls to draw cells and connections, with black indicating unexplored space and white showing the growing tree.
AI-generated descriptionPrim’s algorithm generates a minimum spanning tree from a graph with weighted edges. Starting in the bottom-left corner, the algorithm keeps a heap of the possible directions the maze could be extended (shown in pink). At each step, the maze is extended in the direction with the lowest weight, as long as doing so does not reconnect with another part of the maze. Here the edges are initialized with random weights.
Unlike Wilson’s algorithm, this does not result in a uniform spanning tree. Sometimes, random traversal is misleadingly referred to as randomized Prim’s algorithm; however, the two algorithms exhibit radically different behavior! The global structure of the maze can be more easily seen by flooding it with color.