From 9dd9dff90290e84ea964e555c0f009aa4f30523b Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Sun, 12 Jun 2022 14:31:32 +0530 Subject: [PATCH 1/5] feat: Add hamiltonian cycle implementation --- .../graphs/HamiltonianCycle.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java new file mode 100644 index 000000000000..02f68c5f198f --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java @@ -0,0 +1,127 @@ +package com.thealgorithms.datastructures.graphs; + +/** + * Java program for Hamiltonian Cycle (https://en.wikipedia.org/wiki/Hamiltonian_path). + * @author Akshay Dubey (https://github.com/itsAkshayDubey) + */ +public class HamiltonianCycle { + + private int V, pathCount; + private int[] path; + private int[][] graph; + + /** + * Find hamiltonian cycle for given graph G(V,E) + * + * @param graph Adjacency matrix of a graph G(V, E) + * for which hamiltonian path is to be found + * @return Array containing hamiltonian cycle else returns 1D array with value -1. + */ + public int[] findHamiltonianCycle(int[][] graph){ + + this.V = graph.length; + this.path = new int[this.V]; + + //Initialize path array with -1 value + for(int i=0 ; i Date: Mon, 13 Jun 2022 13:13:04 +0530 Subject: [PATCH 2/5] refactor: Add unit test for hamiltonian cycle --- .../graphs/HamiltonianCycleTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java new file mode 100644 index 000000000000..b7bf5c51459b --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java @@ -0,0 +1,40 @@ +package com.thealgorithms.datastructures.graphs; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class HamiltonianCycleTest { + + private HamiltonianCycle hamiltonianCycle = new HamiltonianCycle(); + + @Test + void testFindHamiltonianCycleShouldReturnHamiltonianPath() { + int[] expectedArray = {0,1,2,4,3}; + int[][] inputArray = { + {0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 1}, + {0, 1, 1, 1, 0}, + }; + + assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray)); + } + + @Test + void testFindHamiltonianCycleShouldReturnInfinityArray() { + int[] expectedArray = {-1,-1,-1,-1,-1}; + + int[][] inputArray = { + {0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 0}, + {0, 1, 1, 0, 0}, + }; + + assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray)); + } + +} From 57d46aa5c1896209afe45875bd2670b33ba1d1dd Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Fri, 17 Jun 2022 11:44:53 +0530 Subject: [PATCH 3/5] refactor: Remove extra whitespaces and newlines --- .../graphs/HamiltonianCycle.java | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java index 02f68c5f198f..782891d577e3 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java @@ -18,7 +18,6 @@ public class HamiltonianCycle { * @return Array containing hamiltonian cycle else returns 1D array with value -1. */ public int[] findHamiltonianCycle(int[][] graph){ - this.V = graph.length; this.path = new int[this.V]; @@ -38,9 +37,7 @@ public int[] findHamiltonianCycle(int[][] graph){ for(int i=0 ; i Date: Fri, 17 Jun 2022 13:01:46 +0530 Subject: [PATCH 4/5] refactor: Improve flow and rename solve method - Rename solve method to isPathFound method which now returns true if path is found false otherwise, earlier the return type of this method was void - Improve flow by removing throw statement by using boolean return type - Replace all tab spaces with whitespaces --- .../graphs/HamiltonianCycle.java | 183 +++++++++--------- 1 file changed, 87 insertions(+), 96 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java index 782891d577e3..df0d8c8962d7 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java @@ -6,100 +6,91 @@ */ public class HamiltonianCycle { - private int V, pathCount; - private int[] path; - private int[][] graph; - - /** - * Find hamiltonian cycle for given graph G(V,E) - * - * @param graph Adjacency matrix of a graph G(V, E) - * for which hamiltonian path is to be found - * @return Array containing hamiltonian cycle else returns 1D array with value -1. - */ - public int[] findHamiltonianCycle(int[][] graph){ - this.V = graph.length; - this.path = new int[this.V]; - - //Initialize path array with -1 value - for(int i=0 ; i Date: Sun, 19 Jun 2022 15:47:10 +0530 Subject: [PATCH 5/5] fix: findHamiltonianCycle method will return a cycle --- .../graphs/HamiltonianCycle.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java index df0d8c8962d7..e0f373bf0610 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java @@ -1,41 +1,45 @@ package com.thealgorithms.datastructures.graphs; /** - * Java program for Hamiltonian Cycle (https://en.wikipedia.org/wiki/Hamiltonian_path). + * Java program for Hamiltonian Cycle (https://en.wikipedia.org/wiki/Hamiltonian_path) * @author Akshay Dubey (https://github.com/itsAkshayDubey) */ public class HamiltonianCycle { private int V, pathCount; - private int[] path; + private int[] cycle; private int[][] graph; /** * Find hamiltonian cycle for given graph G(V,E) * @param graph Adjacency matrix of a graph G(V, E) * for which hamiltonian path is to be found - * @return Array containing hamiltonian cycle else returns 1D array with value -1. + * @return Array containing hamiltonian cycle + * else returns 1D array with value -1. */ public int[] findHamiltonianCycle(int[][] graph){ this.V = graph.length; - this.path = new int[this.V]; + this.cycle = new int[this.V+1]; //Initialize path array with -1 value - for(int i=0 ; i