Live data from Hacker News

Color Flood: Wilson's Algorithm

bl.ocks.org

11–20 of 27 posts

Re: Color Flood: Wilson's Algorithm

#11
the point of Wilson's algorithm is that each spanning is equally likely.

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?

Re: Color Flood: Wilson's Algorithm

#12
That code does NOT implement Prim's algorithm on a graph with randomly-weighted edges. Instead it just does a randomized breadth-first search of the area (starting from the corner), which is the same algorithm used for colouring, which is why the radial pattern occurs.

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)

Re: Color Flood: Wilson's Algorithm

#13
post #10
post #7

Warning for the link to Prim's Algorithm: it's a beast and will make FF fairly unresponsive.

What version/OS are you on? I'm running 29 (Aurora? Beta? Whatever it's called :)) on OS X and it's perfectly smooth, low CPU usage.

I've got the same problem with version 28.0 on Linux.

Re: Color Flood: Wilson's Algorithm

#14
I love mike bostock's stuff, but his code often makes me feel sorry for whoever has to deal with his code after him

   // 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 = height - 1) continue walk; ++j; }
      else if (direction === 2) { if (i = width - 1) continue walk; ++i; }
note the braceless do while and the labeled jump statements

Re: Color Flood: Wilson's Algorithm

#15
post #14

I love mike bostock's stuff, but his code often makes me feel sorry for whoever has to deal with his code after him // 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 r…

Those labelled jumps are equivalent to common unlabeled continues, but safer.

The 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.

Re: Color Flood: Wilson's Algorithm

#16
post #15
post #14

I love mike bostock's stuff, but his code often makes me feel sorry for whoever has to deal with his code after him // 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 r…

Those labelled jumps are equivalent to common unlabeled continues, but safer. The 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.

These would basically be "time for serious feedback" at any place I've worked in the last 25 years.

Seriously, the style is going to generate errors.

Re: Color Flood: Wilson's Algorithm

#17
post #12

That code does NOT implement Prim's algorithm on a graph with randomly-weighted edges. Instead it just does a randomized breadth-first search of the area (starting from the corner), which is the same algorithm used for colouring, which is why the radial pattern occurs. 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…

I’d be happy if you could explain more, but the edges here are intentionally unweighted, so I believe the algorithm is correct. It’s based on the description here:

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:

http://bl.ocks.org/mbostock/11159599

Re: Color Flood: Wilson's Algorithm

#19
post #14

I love mike bostock's stuff, but his code often makes me feel sorry for whoever has to deal with his code after him // 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 r…

Yep, I just a couple of hours messing around with it and got tripped up many times by this stuff.

Re: Color Flood: Wilson's Algorithm

#20

It would be cool to visualize the depth with a colormap that makes it visually possible to compare depths, e.g. Matlab's Hot. HSV colormaps are pretty but make direct visual comparisons difficult [1]. [1] http://people.renci.org/~borland/pdfs/RainbowColorMap_VisVie...

Yes, this is not intended to be a visualization; it is merely something pretty. D3 supports Lab and HCL color spaces [1] which are perceptually uniform; I could use those to improve the accuracy of the distance encoding, but I’d have to sacrifice the current garish aesthetic.

[1] http://bl.ocks.org/mbostock/3014589

Post reply on HN