From 06416d49921ac571c134f5312d321f52690a3735 Mon Sep 17 00:00:00 2001 From: Nikhil Bisht <90137914+nikslyon19@users.noreply.github.com> Date: Wed, 9 Mar 2022 08:58:38 +0530 Subject: [PATCH 1/2] Fixes for FloodFill Algorithm (#2836) --- .../FloodFill.java | 45 +++++++---- .../backtracking/FloodFillTest.java | 78 +++++++++++++++++++ 2 files changed, 107 insertions(+), 16 deletions(-) rename src/main/java/com/thealgorithms/{dynamicprogramming => backtracking}/FloodFill.java (65%) create mode 100644 src/test/java/com/thealgorithms/backtracking/FloodFillTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/FloodFill.java b/src/main/java/com/thealgorithms/backtracking/FloodFill.java similarity index 65% rename from src/main/java/com/thealgorithms/dynamicprogramming/FloodFill.java rename to src/main/java/com/thealgorithms/backtracking/FloodFill.java index 8b7bd088da51..3435fb494c17 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/FloodFill.java +++ b/src/main/java/com/thealgorithms/backtracking/FloodFill.java @@ -1,4 +1,4 @@ -package com.thealgorithms.dynamicprogramming; +package com.thealgorithms.backtracking; /** * Java program for Flood fill algorithm. @@ -45,21 +45,25 @@ public static void putPixel(int[][] image, int x_co_ordinate, int y_co_ordinate, * @return */ public static void floodFill(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color, int old_color) { - if(x_co_ordinate >= 0 && y_co_ordinate >= 0 && getPixel(image, x_co_ordinate, y_co_ordinate) == old_color) { - - putPixel(image, x_co_ordinate, y_co_ordinate, new_color); - floodFill(image, x_co_ordinate + 1, y_co_ordinate, new_color, old_color); - floodFill(image, x_co_ordinate - 1, y_co_ordinate, new_color, old_color); - floodFill(image, x_co_ordinate, y_co_ordinate + 1, new_color, old_color); - floodFill(image, x_co_ordinate, y_co_ordinate - 1, new_color, old_color); - floodFill(image, x_co_ordinate + 1, y_co_ordinate - 1, new_color, old_color); - floodFill(image, x_co_ordinate - 1, y_co_ordinate + 1, new_color, old_color); - floodFill(image, x_co_ordinate + 1, y_co_ordinate + 1, new_color, old_color); - floodFill(image, x_co_ordinate - 1, y_co_ordinate - 1, new_color, old_color); - - - } - + + if(x_co_ordinate < 0 || x_co_ordinate >= image.length) return; + if(y_co_ordinate < 0 || y_co_ordinate >= image[x_co_ordinate].length) return; + if(getPixel(image, x_co_ordinate, y_co_ordinate) != old_color) return; + + putPixel(image, x_co_ordinate, y_co_ordinate, new_color); + + /* Recursively check for horizontally & vertically adjacent coordinates */ + floodFill(image, x_co_ordinate + 1, y_co_ordinate, new_color, old_color); + floodFill(image, x_co_ordinate - 1, y_co_ordinate, new_color, old_color); + floodFill(image, x_co_ordinate, y_co_ordinate + 1, new_color, old_color); + floodFill(image, x_co_ordinate, y_co_ordinate - 1, new_color, old_color); + + /* Recursively check for diagonally adjacent coordinates */ + floodFill(image, x_co_ordinate + 1, y_co_ordinate - 1, new_color, old_color); + floodFill(image, x_co_ordinate - 1, y_co_ordinate + 1, new_color, old_color); + floodFill(image, x_co_ordinate + 1, y_co_ordinate + 1, new_color, old_color); + floodFill(image, x_co_ordinate - 1, y_co_ordinate - 1, new_color, old_color); + } /** @@ -79,6 +83,15 @@ public static void printImageArray(int[][] image) { } } + + + /** Function for JUnit testing **/ + public static int[][] getFloodFillImage(int image[][], int pixelX, int pixelY, int newColor, int oldColor) + { + floodFill(image, pixelX, pixelY, newColor, oldColor); + return image; + } + // Driver Program public static void main(String[] args) { diff --git a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java new file mode 100644 index 000000000000..203ea83142a2 --- /dev/null +++ b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java @@ -0,0 +1,78 @@ +package com.thealgorithms.backtracking; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +class FloodFillTest { + + @Test + void testForEmptyImage() + { + int image[][] = {}; + int expected[][] = {}; + + int resultImage[][] = FloodFill.getFloodFillImage(image, 4, 5, 3, 2); + assertArrayEquals(expected, resultImage); + } + + + @Test + void testForSingleElementImage() + { + int image[][] = {{1}}; + int expected[][] = {{3}}; + + int resultImage[][] = FloodFill.getFloodFillImage(image, 0, 0, 3, 1); + assertArrayEquals(expected, resultImage); + } + + + @Test + void testForImage() + { + int image[][] = { + { 0,0,1,1,0,0,0 }, + { 1,1,3,3,3,0,0 }, + { 1,3,1,1,5,0,0 }, + { 0,3,1,1,5,5,3 }, + { 0,3,5,5,1,1,3 }, + { 0,0,0,5,1,1,3 }, + { 0,0,0,1,3,1,3 } + }; + + int expected[][] = { + { 0,0,2,2,0,0,0 }, + { 2,2,3,3,3,0,0 }, + { 2,3,2,2,5,0,0 }, + { 0,3,2,2,5,5,3 }, + { 0,3,5,5,2,2,3 }, + { 0,0,0,5,2,2,3 }, + { 0,0,0,2,3,2,3 } + }; + + int resultImage[][] = FloodFill.getFloodFillImage(image, 2, 2, 2, 1); + assertArrayEquals(expected, resultImage); + } + + + @Test + void testForImageTwo() + { + int image[][] = { + { 1,1,2,3,1,1,1 }, + { 1,0,0,1,0,0,1 }, + { 1,1,1,0,3,1,2 } + }; + + int expected[][] = { + { 4,4,2,3,4,4,4 }, + { 4,0,0,4,0,0,4 }, + { 4,4,4,0,3,4,2 }, + }; + + int resultImage[][] = FloodFill.getFloodFillImage(image, 0, 1, 4, 1); + assertArrayEquals(expected, resultImage); + } + +} \ No newline at end of file From d0c00fefbb3c468bd3e03cd232b18496f7144532 Mon Sep 17 00:00:00 2001 From: Nikhil Bisht <90137914+nikslyon19@users.noreply.github.com> Date: Sat, 12 Mar 2022 00:47:37 +0530 Subject: [PATCH 2/2] Implemented suggested changes (#2836) --- .../thealgorithms/backtracking/FloodFill.java | 108 ++++-------------- .../backtracking/FloodFillTest.java | 48 ++++++-- 2 files changed, 63 insertions(+), 93 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/FloodFill.java b/src/main/java/com/thealgorithms/backtracking/FloodFill.java index 3435fb494c17..d7842a6713dd 100644 --- a/src/main/java/com/thealgorithms/backtracking/FloodFill.java +++ b/src/main/java/com/thealgorithms/backtracking/FloodFill.java @@ -10,13 +10,13 @@ public class FloodFill { * Get the color at the given co-odrinates of a 2D image * * @param image The image to be filled - * @param x_co_ordinate The x co-ordinate of which color is to be obtained - * @param y_co_ordinate The y co-ordinate of which color is to be obtained + * @param x The x co-ordinate of which color is to be obtained + * @param y The y co-ordinate of which color is to be obtained */ - public static int getPixel(int[][] image, int x_co_ordinate, int y_co_ordinate) { + public static int getPixel(int[][] image, int x, int y) { - return image[x_co_ordinate][y_co_ordinate]; + return image[x][y]; } @@ -24,12 +24,12 @@ public static int getPixel(int[][] image, int x_co_ordinate, int y_co_ordinate) * Put the color at the given co-odrinates of a 2D image * * @param image The image to be filed - * @param x_co_ordinate The x co-ordinate at which color is to be filled - * @param y_co_ordinate The y co-ordinate at which color is to be filled + * @param x The x co-ordinate at which color is to be filled + * @param y The y co-ordinate at which color is to be filled */ - public static void putPixel(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color) { + public static void putPixel(int[][] image, int x, int y, int newColor) { - image[x_co_ordinate][y_co_ordinate] = new_color; + image[x][y] = newColor; } @@ -38,90 +38,32 @@ public static void putPixel(int[][] image, int x_co_ordinate, int y_co_ordinate, * Fill the 2D image with new color * * @param image The image to be filed - * @param x_co_ordinate The x co-ordinate at which color is to be filled - * @param y_co_ordinate The y co-ordinate at which color is to be filled - * @param new_color The new color which to be filled in the image - * @param old_color The old color which is to be replaced in the image + * @param x The x co-ordinate at which color is to be filled + * @param y The y co-ordinate at which color is to be filled + * @param newColor The new color which to be filled in the image + * @param oldColor The old color which is to be replaced in the image * @return */ - public static void floodFill(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color, int old_color) { + public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) { - if(x_co_ordinate < 0 || x_co_ordinate >= image.length) return; - if(y_co_ordinate < 0 || y_co_ordinate >= image[x_co_ordinate].length) return; - if(getPixel(image, x_co_ordinate, y_co_ordinate) != old_color) return; + if(x < 0 || x >= image.length) return; + if(y < 0 || y >= image[x].length) return; + if(getPixel(image, x, y) != oldColor) return; - putPixel(image, x_co_ordinate, y_co_ordinate, new_color); + putPixel(image, x, y, newColor); /* Recursively check for horizontally & vertically adjacent coordinates */ - floodFill(image, x_co_ordinate + 1, y_co_ordinate, new_color, old_color); - floodFill(image, x_co_ordinate - 1, y_co_ordinate, new_color, old_color); - floodFill(image, x_co_ordinate, y_co_ordinate + 1, new_color, old_color); - floodFill(image, x_co_ordinate, y_co_ordinate - 1, new_color, old_color); + floodFill(image, x + 1, y, newColor, oldColor); + floodFill(image, x - 1, y, newColor, oldColor); + floodFill(image, x, y + 1, newColor, oldColor); + floodFill(image, x, y - 1, newColor, oldColor); /* Recursively check for diagonally adjacent coordinates */ - floodFill(image, x_co_ordinate + 1, y_co_ordinate - 1, new_color, old_color); - floodFill(image, x_co_ordinate - 1, y_co_ordinate + 1, new_color, old_color); - floodFill(image, x_co_ordinate + 1, y_co_ordinate + 1, new_color, old_color); - floodFill(image, x_co_ordinate - 1, y_co_ordinate - 1, new_color, old_color); + floodFill(image, x + 1, y - 1, newColor, oldColor); + floodFill(image, x - 1, y + 1, newColor, oldColor); + floodFill(image, x + 1, y + 1, newColor, oldColor); + floodFill(image, x - 1, y - 1, newColor, oldColor); } - - /** - * This method will print the 2D image matrix - * - * @param image The image to be printed on the console - */ - public static void printImageArray(int[][] image) { - - for(int i=0 ; i - * 0 0 0 0 0 0 0 - 0 3 3 3 3 0 0 - 0 3 2 2 5 0 0 - 0 3 2 2 5 5 3 - 0 3 5 5 2 2 3 - 0 0 0 5 2 2 3 - 0 0 0 3 3 3 3 - * */ - - //print 2D image matrix - printImageArray(image); - } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java index 203ea83142a2..7b72e6b2f12d 100644 --- a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java +++ b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java @@ -12,8 +12,8 @@ void testForEmptyImage() int image[][] = {}; int expected[][] = {}; - int resultImage[][] = FloodFill.getFloodFillImage(image, 4, 5, 3, 2); - assertArrayEquals(expected, resultImage); + FloodFill.floodFill(image, 4, 5, 3, 2); + assertArrayEquals(expected, image); } @@ -23,13 +23,41 @@ void testForSingleElementImage() int image[][] = {{1}}; int expected[][] = {{3}}; - int resultImage[][] = FloodFill.getFloodFillImage(image, 0, 0, 3, 1); - assertArrayEquals(expected, resultImage); + FloodFill.floodFill(image, 0, 0, 3, 1); + assertArrayEquals(expected, image); } @Test - void testForImage() + void testForImageOne() + { + int image[][] = { + { 0,0,0,0,0,0,0 }, + { 0,3,3,3,3,0,0 }, + { 0,3,1,1,5,0,0 }, + { 0,3,1,1,5,5,3 }, + { 0,3,5,5,1,1,3 }, + { 0,0,0,5,1,1,3 }, + { 0,0,0,3,3,3,3 } + }; + + int expected[][] = { + { 0,0,0,0,0,0,0 }, + { 0,3,3,3,3,0,0 }, + { 0,3,2,2,5,0,0 }, + { 0,3,2,2,5,5,3 }, + { 0,3,5,5,2,2,3 }, + { 0,0,0,5,2,2,3 }, + { 0,0,0,3,3,3,3 } + }; + + FloodFill.floodFill(image,2,2,2,1); + assertArrayEquals(expected, image); + } + + + @Test + void testForImageTwo() { int image[][] = { { 0,0,1,1,0,0,0 }, @@ -51,13 +79,13 @@ void testForImage() { 0,0,0,2,3,2,3 } }; - int resultImage[][] = FloodFill.getFloodFillImage(image, 2, 2, 2, 1); - assertArrayEquals(expected, resultImage); + FloodFill.floodFill(image, 2, 2, 2, 1); + assertArrayEquals(expected, image); } @Test - void testForImageTwo() + void testForImageThree() { int image[][] = { { 1,1,2,3,1,1,1 }, @@ -71,8 +99,8 @@ void testForImageTwo() { 4,4,4,0,3,4,2 }, }; - int resultImage[][] = FloodFill.getFloodFillImage(image, 0, 1, 4, 1); - assertArrayEquals(expected, resultImage); + FloodFill.floodFill(image, 0, 1, 4, 1); + assertArrayEquals(expected, image); } } \ No newline at end of file