Algorithms: Flood Fill in Haskell
Flood fill is an algorithm used to work out which nodes are connected to a certain node in a multi dimensional array. In this case we’ll use a two dimensional array.
The idea is that we decide that we want to change the colour of one of the cells in the array and have its immediate neighbours who share its initial colour have their colour changed too i.e. the colour floods its way through the grid.
The algorithm is described on Wikipedia like so:
Flood-fill (node, target-color, replacement-color):
If the color of node is not equal to target-color, return.
Set the color of node to replacement-color.
Perform Flood-fill (one step to the west of node, target-color, replacement-color).
Perform Flood-fill (one step to the east of node, target-color, replacement-color).
Perform Flood-fill (one step to the north of node, target-color, replacement-color).
Perform Flood-fill (one step to the south of node, target-color, replacement-color).
Return.replacehttp://cvs.haskell.org/Hugs/pages/libraries/base/Data-Array.html#v%3AboundstoComplexArrayhttps://github.com/mneedham/haskell/blob/master/PrintArray.hsprintGridhttp://www.markhneedham.com/blog/2012/04/03/haskell-print-friendly-representation-of-an-array/[in my last post]my github haskell repository
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.