A Stackless Floodfill Automaton

t3x.org

49 points by andsoitis 2 days ago


hyperhello - 14 hours ago

The algorithm uses about half a byte per pixel and saves the path it follows. That kind of implies that the stack is still there and present, it’s just bounded to the number of pixels, and could actually be modified to just use a bounded area of the heap. But conceptually it’s easier to follow the way the paper was written. Neat.

zimpenfish - 14 hours ago

Huh, that's useful because I had to write a floodfill a couple of days ago and it was a pain in the arse.

I'll be converting my code to the stackless version toot sweet.

hatthew - 8 hours ago

What's the utility of this? It's arguably still using a stack, just encoded in the original data structure itself. If your original data structure somehow uses colors but is also fine with the algorithm overwriting with its own colors, I guess it works? But those are very unusual preconditions. For any real world scenario I can imagine, I feel like there's a better solution.

If the colors in your original data are important, then you can't really just overwrite them and/or assume that the algorithm can use its own colors. You're going to have to add a stack.

If your original data doesn't have colors, adding a stack is better than integrating metadata into the original data structure.

If for some reason your original data has colors but they aren't considered important, then just remove the colors and add a stack.

If your concern is about dynamic memory, preallocate static memory equal to the size of the original data structure.

voidUpdate - 14 hours ago

How does this deal with one pixel gaps? The algorithm can enter it just fine but there's no space to go back out again, and there doesn't seem to be a system for backtracking, unless I've misunderstood the algorithm

fatlotus - 11 hours ago

Another option if you find yourself with this problem (flood fill, no recursion) is union find/connected components. Like the algorithm proposed in the article, you still need to store O(N) things, and runtime is still O(N).

amelius - 10 hours ago

Curious if Claude could come up with this when asked to implement a flood fill without stack. Alas, out of tokens.

NooneAtAll5 - 9 hours ago

[pdf]