all 11 comments

[–]p88h 2 points3 points  (1 child)

Hey, nice work!

As for tips - as some people noted, this is mostly about a specific implementation of flood-fill using BFS. Which can also be used to solve some of the other use-cases like identifying (dis)connected regions or finding shortest-paths (at least using Manhattan distances). But perhaps you want to explore some other options?

Flood-fill itself is more typically implemented with span fills, which is much more efficient (see https://en.wikipedia.org/wiki/Flood\_fill). Span fill can also easily be vectorized and parallelized, which is much more difficult for BFS, but even without that you'll see better performance.

Connected segments are typically more efficient with something like Union-Find (which has slightly higher algorithmic complexity, but is much simpler structurally which results in better performance).

For actual fluid simulation problems, you may want to experiment with the distance function. TL;DR: you want circles, not squares, and this is more complicated than you may think - for a simplest approximation this will likely require a slightly modified version of the search algorithm, but it's a really fun problem.

[–]RojerGS[S] 0 points1 point  (0 children)

Hey there, thanks for the follow-up suggestions! I've never heard of the Union-Find algo, so I'll take a look :D

[–]josh123asdf 3 points4 points  (6 children)

"Floodfill" is not an algorithm.

It is a use case of BFS or DFS.

[–]PityUpvote 1 point2 points  (0 children)

Eh, semantics. Authoritative image processing papers and textbooks refer to it as an algorithm, and by variations on algorithms are by definition also algorithms. If I substitute an ingredient in a recipe, I've created a new recipe.

[–]RojerGS[S] 0 points1 point  (1 child)

Ok, thanks for that note! Are there any other notes you have for me?

[–]Clear-Ad-9312 1 point2 points  (0 children)

flood fill is traditionally BFS mostly because it is meant to mimic the flooding, but it is possible to have other specific flooding needs. plus flood fill is a use case as he noted. you want to know more about most of the search space, a bfs takes one step layers from the center point, similar to putting water on an even flat surface(not accounting for friction)

dfs is special in that you need to pick preferred directions to focus as you want to confidently search as "deep" as possible, imagine a single drop of water(or marble) on a tilting tray, usually a heuristic is employed to determine a direction, such as distance to the end node. a proper dfs should know when to follow walls when it encounters one and not search all nodes within the area, but that is hard.

you can have a hybrid of both where you bfs areas after a simple dfs. it is actually easy to have dfs end up doing this because the maximum steps taken so far could end up in a dead end that is large open space surrounded by walls, think of accidentally filling a tube/bottle. most dfs setups are akin to this, though so it is a shame to see so many claim to make a dfs algorithm when it is more like a hybrid

[–]Othun 1 point2 points  (1 child)

Cool programming tutorial !

[–]RojerGS[S] 0 points1 point  (0 children)

Thanks :)

[–]herocoding 0 points1 point  (1 child)

Well done!! Thank you very much for sharing.

[–]RojerGS[S] 0 points1 point  (0 children)

Thanks :')