Open In App

Flood-Fill Algorithm using JavaScript

Last Updated : 03 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a 2D screen arr[][] where each arr[i][j] is an integer representing the color of that pixel, also given the location of a pixel 
(X, Y) and a color C, the task is to replace the color of the given pixel and all the adjacent same-colored pixels with the given color.

Example:

Input: 

const grid = [
[1, 1, 0, 0, 0],
[1, 1, 0, 1, 1],
[0, 0, 0, 1, 0],
[0, 0, 1, 1, 0],
[1, 0, 0, 0, 1]
];

Starting point: (0,0)
TargetColor: 2

Output:

2 2 0 0 0
2 2 0 1 1
0 0 0 1 0
0 0 1 1 0
1 0 0 0 1

Explanation:

In the 2D grid, each value indicates the color of a pixel.
The starting point (0,0) indicates the coordinates from where
we begin the flood-fill. We replace all connected pixels
with the target color (2). So, all connected '1's starting
from the starting point are replaced with '2' in the grid.

Table of Content

Using DFS

Using Depth-First Search (DFS), the flood-fill algorithm recursively explores neighboring pixels from a starting point (x, y).

Approach:

  • Start at the given starting point (x, y) in the grid.
  • Check if the current pixel is within the grid boundaries and has the same color as the starting pixel.
  • If the current pixel is valid, replace its color with the target color.
  • Recursively explore the neighboring pixels of the current pixel.
  • For each neighboring pixel:
    • Check if it’s within the grid boundaries and has the same color as the starting pixel.
    • If valid, apply the replacement color and recursively explore its neighbors.
  • Repeat steps 4-5 until all connected pixels with the same color as the starting pixel are replaced with the target color.

Example: Implementation to implement Flood-fill Algorithm using DFS.

JavaScript
function floodFillRecursive(grid, x, y, targetColor, startColor) {
    if (x < 0 || x >= grid.length || y < 0
        || y >= grid[0].length ||
        grid[x][y] !== startColor) {
        return;
    }

    grid[x][y] = targetColor;

    floodFillRecursive(grid, x + 1, y, targetColor, startColor);
    floodFillRecursive(grid, x - 1, y, targetColor, startColor);
    floodFillRecursive(grid, x, y + 1, targetColor, startColor);
    floodFillRecursive(grid, x, y - 1, targetColor, startColor);
}

// Example usage
const grid = [
    [1, 1, 0, 0, 0],
    [1, 1, 0, 1, 1],
    [0, 0, 0, 1, 0],
    [0, 0, 1, 1, 0],
    [1, 0, 0, 0, 1]
];

const startX = 0;
const startY = 0;
const targetColor = 2;

floodFillRecursive(grid, startX, startY, targetColor, grid[startX][startY]);

console.log("Output Using DFS:");
console.log(grid);

Output
Output Using DFS:
[
  [ 2, 2, 0, 0, 0 ],
  [ 2, 2, 0, 1, 1 ],
  [ 0, 0, 0, 1, 0 ],
  [ 0, 0, 1, 1, 0 ],
  [ 1, 0, 0, 0, 1 ]
]

Time complexity: O(m*n)

Space Complexity: O(m+n), due to recursive call stack.

Using BFS

It explores neighboring pixels iteratively from a starting point in the grid.

Approach:

  • Initialize a queue (Q).
  • Push the starting location of the pixel as given in the input and apply the replacement color to it.
  • Iterate over the queue until it’s not empty.
  • Pop the front node from the queue.
  • Check the adjacent pixels of the current pixel.
  • If adjacent pixels are valid (within grid boundaries and have the same color as the starting pixel), push them into the queue and apply the replacement color.
  • Repeat steps 4-6 until all connected pixels with the same color as the starting pixel are replaced with the target color.

Example: Implementation to implement Flood-fill Algorithm using DFS.

JavaScript
function floodFillIterative(grid, startX, startY, targetColor) {
    const startColor = grid[startX][startY];
    const queue = [[startX, startY]];
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];

    while (queue.length > 0) {
        const [x, y] = queue.shift();
        grid[x][y] = targetColor;

        for (const [dx, dy] of directions) {
            const newX = x + dx;
            const newY = y + dy;
            if (newX >= 0 && newX < grid.length
                && newY >= 0 && newY < grid[0].length
                && grid[newX][newY] === startColor) {
                queue.push([newX, newY]);
            }
        }
    }
}

// Example usage
const grid = [
    [1, 1, 0, 0, 0],
    [1, 1, 0, 1, 1],
    [0, 0, 0, 1, 0],
    [0, 0, 1, 1, 0],
    [1, 0, 0, 0, 1]
];

const startX = 2;
const startY = 2;
const targetColor = 2;

floodFillIterative(grid, startX, startY, targetColor);

console.log("Output after BFS approach:");
console.log(grid);

Output
Output after BFS approach:
[
  [ 1, 1, 2, 2, 2 ],
  [ 1, 1, 2, 1, 1 ],
  [ 2, 2, 2, 1, 0 ],
  [ 2, 2, 1, 1, 0 ],
  [ 1, 2, 2, 2, 1 ]
]

Time Complexity: O(N*M)

Space Complexity: O(N*M)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads