For a randomized spanning tree, edges should be generated with a random weight, and Prim's algorithm extracts them from the frontier ordered by weight. This creates a very different tree structure that is much more similar to the uniform spanning tree generated by Wilson's algorithm.
(This comment is based on the code from this page: http://bl.ocks.org/mbostock/11159599)
http://weblog.jamisbuck.org/2011/1/10/maze-generation-prim-s...
EDIT: Looks like you’re right! It makes a huge difference if you assign each edge a random weight once, rather than simply pulling a random edge of the frontier. Seems like the reference I used has this bug? Here is a fixed version:
> My last post was about using Kruskal’s algorithm to generate random mazes. This article is about using another minimal spanning tree algorithm to do the same: Prim’s algorithm.
But his implementation is NOT the same! (His implementation of Kruskal's algorithm does look correct.) Extracting elements at a random index is not equivalent to assigning random weights to elements and extracting the minimum-weight elements.
Is this is a flood-fill algorithm?
Cause what I remember from back in the day when you could still watch MS Paint do the flood fill, a more efficient version fills out horizontal spans all at once, instead of growing like an ink blot.
But it looks cool.
EDIT: maybe this is one of those "use the entire RGB colour space" type of renderings? (see http://allrgb.com )
Quick screencast: https://docs.google.com/file/d/0B4MGwWE_SNv0elNfUVdmalp5NVE
http://updates.html5rocks.com/2011/12/Transferable-Objects-L...
[1] http://people.renci.org/~borland/pdfs/RainbowColorMap_VisVie...
Each tree is selected uniformly amoug the HUGE set of possible spanning trees of this graph.
The color patterns in this algorithm vs. Prim algorithm are very different. Unlike Prim algorithm, this pattern is random and has a fractal structure.
Is there a good reason a programmer might need each UST to be equally likely?
// Pick a location that’s not yet in the maze (if any).
do if ((index0 = remaining.pop()) == null) return true;
while (cells[index0] >= 0);
// Perform a random walk starting at this location,
previous[index0] = index0;
walk: while (true) {
i = index0 % width;
j = index0 / width | 0;
// picking a legal random direction at each step.
direction = Math.random() * 4 | 0;
if (direction === 0) { if (j <= 0) continue walk; --j; }
else if (direction === 1) { if (j >= height - 1) continue walk; ++j; }
else if (direction === 2) { if (i <= 0) continue walk; --i; }
else { if (i >= width - 1) continue walk; ++i; }
note the braceless do while and the labeled jump statementsThe braceless do-whiles are isolated by blank lines and have leading comments.
I'd prefer bostock's code over the average uncommented code with cryptic abbreviated var names.
Seriously, the style is going to generate errors.