Flood fill recursive algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Flood fill algorithms are fundamental in computer graphics and computer science. They are used primarily in the area of image processing to determine the area connected to a given node in a multi-dimensional array. The flood fill algorithm can be observed in many applications, such as the paint bucket tool in graphics editing software or in broader applications like maze solving.
Understanding the Flood Fill Recursive Algorithm
The recursive implementation of the flood fill algorithm is elegant and intuitive. It works by starting at a given node (or pixel) and exploring all neighbor nodes, either in 4-connectivity (up, down, left, right) or 8-connectivity (diagonals included). The recursion continues until a node not meeting a condition is reached.
Key Concepts
- Start Node: The initial point from where the filling starts.
- Target Color: The color of the pixels that need to be changed.
- Replacement Color: The new color that will replace the target color.
Recursive Approach
The recursive flood fill algorithm can be succinctly described in the following steps:
- Check if the current node has the target color.
- If yes, replace it with the replacement color.
- Recur for its neighboring pixels.
Recursive Algorithm Pseudocode
Here's a high-level pseudocode description for the flood fill recursive algorithm:
- Out of Bounds: If the current position is outside the bounds of the image, the function returns immediately.
- Unmatched Color: If the current node's color is not the target color, the function does not proceed further.
- Already Filled: If the current node has already been recolored with the replacement color, it does not need any further action.
- After filling the current pixel, the algorithm makes recursive calls to its neighboring pixels. This exploration continues until all connected target-colored pixel areas are filled.
1 1 1 0 1 1 0 0 1 0 1 1 0 0 1 0
2 2 2 0 2 2 0 0 2 0 1 1 0 0 1 0
- Recursion Depth Limit: Recursive solutions are limited by recursion depth, whereas iterative solutions can be managed more efficiently using explicit stacks or queues.
- State Memory Usage: Recursive solutions use implicit stack memory, while iterative solutions use explicit data structures.
Related reading
- Floyd–Rivest vs. Introselect algorithm performance
- FLP Impossiblity Result assumption of C_1 = e'(C_0)
- Followup Sorting colors by distinctiveness
- for_each that gives two or n adjacent elements
- For given two integers A and B, find a pair of numbers X and Y such that A XY and B X xor Y
- For parallel algorithm with N threads, can performance gain be more than N?
- Ford Fulkerson from Cormen et al
- Formally verifying the correctness of an algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.