|
3 | 3 | import java.util.LinkedList; |
4 | 4 | import java.util.Queue; |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Implementation of the Flood Fill algorithm using an iterative BFS (Breadth-First Search) approach. |
| 8 | + * |
| 9 | + * <p>The Flood Fill algorithm is used to fill connected areas in an image with a new color, starting from a specified point. |
| 10 | + * This implementation uses an iterative BFS approach with a queue |
| 11 | + * instead of recursion to avoid stack overflow issues with large images.</p> |
| 12 | + * |
| 13 | + * <p><b>Implementation Features:</b></p> |
| 14 | + * <ul> |
| 15 | + * <li>Supports 8-connected filling (horizontal, vertical, and diagonal directions)</li> |
| 16 | + * <li>Uses BFS traversal through {@link java.util.Queue}</li> |
| 17 | + * <li>Includes nested {@code Point} class to represent pixel coordinates</li> |
| 18 | + * <li>Iterative approach avoids stack overflow for large images</li> |
| 19 | + * </ul> |
| 20 | + * |
| 21 | + * <p><b>Time Complexity:</b> O(M × N) where M and N are the dimensions of the image</p> |
| 22 | + * <p><b>Space Complexity:</b> O(M × N) in the worst case the queue stores every pixel</p> |
| 23 | + * |
| 24 | + * @see <a href="https://www.geeksforgeeks.org/dsa/flood-fill-algorithm">Flood Fill Algorithm - GeeksforGeeks</a> |
| 25 | + * @see <a href="https://en.wikipedia.org/wiki/Flood_fill">Flood Fill Algorithm - Wikipedia</a> |
| 26 | + */ |
6 | 27 | public final class IterativeFloodFill { |
7 | 28 | private IterativeFloodFill() { |
8 | 29 | } |
9 | 30 |
|
10 | | - /** |
11 | | - * Represents a point in 2D space with integer coordinates. |
12 | | - */ |
13 | | - private static class Point { |
14 | | - final int x; |
15 | | - final int y; |
16 | | - |
17 | | - Point(final int x, final int y) { |
18 | | - this.x = x; |
19 | | - this.y = y; |
20 | | - } |
21 | | - } |
22 | | - |
23 | | - /** |
24 | | - * Checks if a pixel should be skipped during flood fill operation. |
25 | | - * |
26 | | - * @param image The image to get boundaries |
27 | | - * @param x The x co-ordinate of pixel to check |
28 | | - * @param y The y co-ordinate of pixel to check |
29 | | - * @param oldColor The old color which is to be replaced in the image |
30 | | - * @return {@code true} if pixel should be skipped, else {@code false} |
31 | | - */ |
32 | | - private static boolean shouldSkipPixel(final int[][] image, final int x, final int y, final int oldColor) { |
33 | | - |
34 | | - if (x < 0 || x >= image.length || y < 0 || y >= image[0].length || image[x][y] != oldColor) { |
35 | | - return true; |
36 | | - } |
37 | | - |
38 | | - return false; |
39 | | - } |
40 | | - |
41 | 31 | /** |
42 | 32 | * Iteratively fill the 2D image with new color |
43 | 33 | * |
@@ -78,4 +68,35 @@ public static void floodFill(final int[][] image, final int x, final int y, fina |
78 | 68 | } |
79 | 69 | } |
80 | 70 | } |
| 71 | + |
| 72 | + /** |
| 73 | + * Represents a point in 2D space with integer coordinates. |
| 74 | + */ |
| 75 | + private static class Point { |
| 76 | + final int x; |
| 77 | + final int y; |
| 78 | + |
| 79 | + Point(final int x, final int y) { |
| 80 | + this.x = x; |
| 81 | + this.y = y; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Checks if a pixel should be skipped during flood fill operation. |
| 87 | + * |
| 88 | + * @param image The image to get boundaries |
| 89 | + * @param x The x co-ordinate of pixel to check |
| 90 | + * @param y The y co-ordinate of pixel to check |
| 91 | + * @param oldColor The old color which is to be replaced in the image |
| 92 | + * @return {@code true} if pixel should be skipped, else {@code false} |
| 93 | + */ |
| 94 | + private static boolean shouldSkipPixel(final int[][] image, final int x, final int y, final int oldColor) { |
| 95 | + |
| 96 | + if (x < 0 || x >= image.length || y < 0 || y >= image[0].length || image[x][y] != oldColor) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + return false; |
| 101 | + } |
81 | 102 | } |
0 commit comments