Non-recursive implementation of Flood Fill algorithm?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Flood Fill is an algorithm primarily used for operations in image processing, such as filling connected, similarly-colored areas. A classic use case is the "bucket" tool commonly found in graphic design software, which fills an enclosed area with a specific color. Traditionally, this algorithm is implemented using recursion, but due to limitations in computer stack size, a non-recursive implementation, commonly using an iterative approach with a stack or queue, is often favored.
Non-Recursive Implementation
The aim of the non-recursive approach is to avoid the stack overflow problems that arise with recursive methods, especially with large images or maps. We can achieve this using a stack (or queue) data structure to maintain the pixels that need to be checked and potentially filled.
Algorithm Explanation
- Initialize the Stack:
- Push the starting pixel's position onto the stack.
- Loop Until Stack is Empty:
- Pop the top pixel from the stack.
- If this pixel is outside the bounds or already filled, skip it.
- Otherwise, fill it with the new color and push its unfilled neighbors onto the stack.
- Continue until No More Pixels:
- The loop continues checking and filling pixels until there are no more unprocessed pixels remaining in the stack.
Example
Let's illustrate with an example in Python. Assume we have a 2D array representing the image and we want to fill all connected pixels of the same color starting from a given pixel.
Key Considerations
- Boundary Checks: It's crucial to include boundary checks to avoid accessing indices outside the bounds of the image array.
- Initial Color: Ensure that the starting pixel's color is different from the new fill color to prevent the algorithm from running indefinitely.
- Data Structure Choice: You may prefer a queue for breadth-first filling or a stack for depth-first, although a stack is the norm for flood fill.
Performance and Complexity
- Time Complexity: The algorithm typically runs in time, where is the total number of pixels. Every pixel might be pushed and popped from the stack once.
- Space Complexity: In the worst case, the stack might store the number of pixels in a potential connected area, also in the worst case but considerably less when fewer pixels need to be filled.
Advantages of Non-Recursive Approach
- Prevents Stack Overflow: Avoids maximum recursion limit issues, especially for large images.
- Iterative Process: Can be easier to understand for those unfamiliar with recursive thinking.
- Control Over Process Flow: More flexibility in managing which pixels to visit next without depending on the call stack.
| Key Point | Explanation |
| Algorithm Type | Non-recursive Flood Fill |
| Data Structure | Stack (or Queue for different behavior) |
| Space Use | O(N), where N is the number of pixels |
| Time Use | O(N), every pixel processed potentially once |
| Boundary Concern | Explicit boundary checking is necessary |
| Starting Condition | Ensure start color ≠ new color |
Through non-recursive implementation, the Flood Fill algorithm retains its functionality while reducing the risk of performance issues linked to recursion limits, making it more robust for various practical uses in digital image processing fields.
Related reading
- Nothing is being detected in Tensorflow Object detection API
- Object detection with R-CNN?
- One stage vs two stage object detection
- OpenCV2 imwrite is writing a black image
- Non-Recursive Merge Sort
- Non-recursive merge sort with two nested loops - how?
- Not able to connect mongo with replica set to mongo compass
- NotEnoughReplicasException The size of the current ISR Set(2) is insufficient to satisfy the min.isr requirement of 3

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.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.