which flood-fill algorithm is better for performance?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Flood-fill algorithms are widely used in computer graphics, image processing, and game development to determine the area connected to a particular node in a multi-dimensional array. This article compares the performance of two popular flood-fill algorithms: Depth-First Search (DFS) and Breadth-First Search (BFS), providing technical insights, examples, and a detailed comparison using markdown formatting.
Understanding Flood-Fill Algorithms
Flood-fill algorithms operate by filling a connected region in a multi-dimensional space. The most common applications are in paint bucket tools in image editing software and boundary filling in game terrain generation.
Depth-First Search (DFS)
DFS is a recursive algorithm that explores as far as possible along a single branch before backtracking. It is implemented using a stack data structure, either explicitly with a user-defined stack or implicitly through function calls.
Breadth-First Search (BFS)
BFS explores the neighbor nodes at the present depth before moving on to nodes at the next depth level. It uses a queue data structure, which stores nodes in a First-In-First-Out (FIFO) manner.
Algorithm Performance
Depth-First Search (DFS)
- Time Complexity: , where is the number of rows and is the number of columns. This complexity arises because DFS potentially visits every cell in the 2D space.
- Space Complexity: in the worst case, due to the stack used to store the recursive call stack. This can be expensive in terms of memory usage.
- Strengths: Efficient in scenarios where the area to be filled is compact, with relatively few recursive calls made before hitting a boundary.
- Weaknesses: Susceptible to stack overflow if the fill area is large, due to the depth of recursion.
Breadth-First Search (BFS)
- Time Complexity: , similar to DFS because each cell may need to be visited once to determine connectivity.
- Space Complexity: due to the need to store all nodes in the current layer for the next level of exploration.
- Strengths: Handles large contiguous regions effectively without the risk of a stack overflow since it maintains a queue to manage nodes.
- Weaknesses: May require significant memory overhead due to the storage of all neighbor nodes, leading to increased space complexity.
Example Comparisons
Consider the following pseudo-code implementations and performance analysis for both algorithms.
DFS Example
Related reading
- Which is better On log n or On2
- Which is better way to calculate nCr
- Which is faster, \`Hash\` lookup or Binary search?
- Which is the best method between Genetic Algorithm and Dynamic Programming to solve classic 0-1 knapsack?
- Which is better in python, del or delattr?
- Which is faster clear collection or instantiate new
- Which is the fastest algorithm to find prime numbers?
- Which is the fastest way to get the absolute value of a number

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.