hn.today

A Stackless Floodfill Automaton

t3x.org36 points13 comments
Screenshot of A Stackless Floodfill Automaton

Commenters found the stackless flood-fill technique useful for avoiding recursion or an explicit stack, especially in memory-constrained settings. They described it as storing traversal state in special pixel values: the image itself becomes a linked path, with each pixel recording where the algorithm came from and which directions it has checked. One commenter planned to replace a recently written flood fill with this approach; another wondered whether an AI could invent it. Some found the backtracking explanation unclear, particularly how the algorithm handles narrow gaps, while another commenter explained that it retraces predecessor directions and resumes exploring at branch points.

The main disagreement concerned how to account for the algorithm’s memory cost. One commenter argued that saving a path is effectively a bounded stack, and suggested using a heap area instead. Another replied that the proposed implementation can reserve 16 of 256 color values as markers, costing only about 0.09 bits per pixel in reduced color capacity - far less than a four-bit-per-pixel heap representation, though restoring displaced colors could itself require substantial storage. Commenters also noted that this tradeoff depends on the image’s available colors: spare palette entries make the technique attractive, while using all 256 colors complicates matters. Union-find or connected-components labeling was offered as another non-recursive option, with O(N) storage and runtime.

Read on t3x.org13 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.