From 14a6994a580978ce54fa91725ad9d4a792b13f9e Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Sun, 23 Oct 2022 11:45:56 +1100 Subject: [PATCH 1/7] Do hamiltonian cycle checking --- .../graphtheory/HamiltonianCycle.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java new file mode 100644 index 000000000..b21f30d73 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -0,0 +1,108 @@ +package com.williamfiset.algorithms.graphtheory; + +import java.util.Scanner; +import java.util.Arrays; + +public class HamiltonianCycle { + /**elements used to store path and the graph **/ + private int V, pathCount; + private int[] path; + private int[][] graph; + + /** Function to find cycle + * Take the graph as input + * And will print "No solution" if the graph has no HamiltionianCycle + * Function solve will do actual find paths job + * **/ + public void findHamiltonianCycle(int[][] g) + { + V = g.length; + path = new int[V]; + Arrays.fill(path, -1); + graph = g; + try + { + path[0] = 0; + pathCount = 1; + solve(0); + System.out.println("No solution"); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + display(); + } + } + + /** function to find paths recursively + *Take int as input + *Will add coordinators into the path list. + * **/ + public void solve(int vertex) throws Exception + { + /** solution **/ + if (graph[vertex][0] == 1 && pathCount == V) + throw new Exception("Solution found"); + /** all vertices selected but last vertex not linked to 0 **/ + if (pathCount == V) + return; + for (int v = 0; v < V; v++) + { + /** if connected **/ + if (graph[vertex][v] == 1) + { + /** add to path **/ + path[pathCount++] = v; + /** remove connection **/ + graph[vertex][v] = 0; + graph[v][vertex] = 0; + /** if vertex not already selected solve recursively **/ + if (!isPresent(v)) + solve(v); + /** restore connection **/ + graph[vertex][v] = 1; + graph[v][vertex] = 1; + /** remove path **/ + path[--pathCount] = -1; + } + } + } + + /** function to check if path is already selected **/ + public boolean isPresent(int v) + { + for (int i = 0; i < pathCount - 1; i++) + if (path[i] == v) + return true; + return false; + } + + /** display solution **/ + public void display() + { + System.out.print("\nPath : "); + for (int i = 0; i <= V; i++) + System.out.print(path[i % V] + " "); + System.out.println(); + } + + /** Main function **/ + public static void main(String[] args) + { + Scanner scan = new Scanner(System.in); + /** Make an object of HamiltonianCycle class **/ + HamiltonianCycle hc = new HamiltonianCycle(); + /** Accept number of vertices **/ + System.out.println("Enter number of vertices"); + int V = scan.nextInt(); + /** get graph **/ + System.out.println("Enter adjacency matrix"); + int[][] graph = new int[V][V]; + for (int i = 0; i < V; i++) + for (int j = 0; j < V; j++) + graph[i][j] = scan.nextInt(); + hc.findHamiltonianCycle(graph); + scan.close(); + } + } + From 55cde931d129dfbe3806be16f930e5802cc5f76d Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Sun, 23 Oct 2022 13:21:28 +1100 Subject: [PATCH 2/7] Test for HamiltonianCycle --- .../graphtheory/HamiltonianCycle.java | 15 ++- .../graphtheory/HamiltonianCycleTest.java | 124 ++++++++++++++++++ 2 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java index b21f30d73..bb25f2476 100644 --- a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -1,5 +1,6 @@ package com.williamfiset.algorithms.graphtheory; +import java.util.List; import java.util.Scanner; import java.util.Arrays; @@ -9,6 +10,13 @@ public class HamiltonianCycle { private int[] path; private int[][] graph; + public void setGraph(int[][] graph1){ + this.graph=graph1; + } + + public int[] getPath(){ + return this.path; + } /** Function to find cycle * Take the graph as input * And will print "No solution" if the graph has no HamiltionianCycle @@ -16,8 +24,9 @@ public class HamiltonianCycle { * **/ public void findHamiltonianCycle(int[][] g) { + if(g==null)throw new IllegalArgumentException("Graph cannot be null."); V = g.length; - path = new int[V]; + path = new int[V+1]; Arrays.fill(path, -1); graph = g; try @@ -25,10 +34,11 @@ public void findHamiltonianCycle(int[][] g) path[0] = 0; pathCount = 1; solve(0); - System.out.println("No solution"); + System.out.print("No solution"); } catch (Exception e) { + path[V]=path[0]; System.out.println(e.getMessage()); display(); } @@ -66,6 +76,7 @@ public void solve(int vertex) throws Exception path[--pathCount] = -1; } } + } /** function to check if path is already selected **/ diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java new file mode 100644 index 000000000..d8baa7234 --- /dev/null +++ b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java @@ -0,0 +1,124 @@ +package com.williamfiset.algorithms.graphtheory; + +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.*; + +import static com.google.common.truth.Truth.assertThat; + +public class HamiltonianCycleTest { + + HamiltonianCycle solver; + + @Before + public void setUp() { + solver = null; + } + + // Initialize graph with 'n' nodes. + public static int[][] initializeEmptyGraph() { + int[][] graph = null; + return graph; + } + + // Add directed edge to graph. + public static void addDirectedEdge(int[][] graph, int a, int b) { + graph[a][b]=1; + } + + //test empty + @Test(expected = IllegalArgumentException.class) + public void testEmptyGraph() { + int[][] graph = initializeEmptyGraph(); + HamiltonianCycle solver; + solver = new HamiltonianCycle(); + solver.setGraph(graph); + solver.findHamiltonianCycle(graph); + } + + //valid test 1 + @Test + public void validHC1() { + int[][] graph = new int[5][5]; + int[] result= new int[6]; + result[0]=0;result[1]=1;result[2]=2;result[3]=4;result[4]=3;result[5]=0; + /** + * the example graph + * (0)--(1)--(2) + * | / \ | + * | / \ | + * | / \ | + * (3)-------(4) + * + * the 2D should be + * 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 + * + */ + graph [0][0]=0;graph [0][1]=1;graph [0][2]=0;graph [0][3]=1;graph [0][4]=0; + graph [1][0]=1;graph [1][1]=0;graph [1][2]=1;graph [1][3]=1;graph [1][4]=1; + graph [2][0]=0;graph [2][1]=1;graph [2][2]=0;graph [2][3]=0;graph [2][4]=1; + graph [3][0]=1;graph [3][1]=1;graph [3][2]=0;graph [3][3]=0;graph [3][4]=1; + graph [4][0]=0;graph [4][1]=1;graph [4][2]=1;graph [4][3]=1;graph [4][4]=0; + HamiltonianCycle solver; + solver = new HamiltonianCycle(); + solver.setGraph(graph); + solver.findHamiltonianCycle(graph); + assertThat(solver.getPath()).isEqualTo(result); + } + + //invalid test 1, generally same as valid 1, but change a little and make it invalid + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @After + public void restoreStreams() { + System.setOut(originalOut); + System.setErr(originalErr); + } + @Test + public void invalidHC1() { + int[][] graph = new int[5][5]; + int[] invalidresult= new int[6]; + Arrays.fill(invalidresult, -1); + /** + * the example graph + * (0)--(1)--(2) + * | / \ | + * | / \ | + * | / \ | + * (3) (4) + * + * the 2D should be + * 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 + * + */ + graph [0][0]=0;graph [0][1]=1;graph [0][2]=0;graph [0][3]=1;graph [0][4]=0; + graph [1][0]=1;graph [1][1]=0;graph [1][2]=1;graph [1][3]=1;graph [1][4]=1; + graph [2][0]=0;graph [2][1]=1;graph [2][2]=0;graph [2][3]=0;graph [2][4]=1; + graph [3][0]=1;graph [3][1]=1;graph [3][2]=0;graph [3][3]=0;graph [3][4]=0; + graph [4][0]=0;graph [4][1]=1;graph [4][2]=1;graph [4][3]=0;graph [4][4]=0; + HamiltonianCycle solver; + solver = new HamiltonianCycle(); + solver.setGraph(graph); + solver.findHamiltonianCycle(graph); + Assert.assertEquals("No solution",outContent.toString()); + } +} From 928be43729d02cc77422b7178d0a6fddc3bd0e58 Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Mon, 24 Oct 2022 16:36:00 +1100 Subject: [PATCH 3/7] Finish CombSort --- .../algorithms/sorting/CombSort.java | 74 +++++++++++++++++++ .../algorithms/sorting/CombSortTest.java | 61 +++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/sorting/CombSort.java create mode 100644 src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java diff --git a/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java new file mode 100644 index 000000000..ab04e1b27 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java @@ -0,0 +1,74 @@ +package com.williamfiset.algorithms.sorting; + +import java.util.Scanner; + +class CombSort +{ + // To find gap between elements + int getNextGap(int gap) + { + // Shrink gap by Shrink factor + gap = (gap*10)/13; + if (gap < 1) + return 1; + return gap; + } + + // Function to sort arr[] using Comb Sort + void sort(int arr[]) + { + int n = arr.length; + + // initialize gap + int gap = n; + + // Initialize swapped as true to make sure that + // loop runs + boolean swapped = true; + + // Keep running while gap is more than 1 and last + // iteration caused a swap + while (gap != 1 || swapped == true) + { + // Find next gap + gap = getNextGap(gap); + + // Initialize swapped as false so that we can + // check if swap happened or not + swapped = false; + + // Compare all elements with current gap + for (int i=0; i arr[i+gap]) + { + // Swap arr[i] and arr[i+gap] + int temp = arr[i]; + arr[i] = arr[i+gap]; + arr[i+gap] = temp; + + // Set swapped + swapped = true; + } + } + } + } + + // Driver method + public static void main(String args[]) + { + CombSort ob = new CombSort(); + Scanner scan = new Scanner(System.in); + System.out.println("Enter number of the numbers you want to sort"); + int V = scan.nextInt(); + System.out.println("Enter the numbers:"); + int[] arr = new int[V]; + for (int i=0;i Date: Mon, 24 Oct 2022 16:38:32 +1100 Subject: [PATCH 4/7] Update combsort Test --- .../algorithms/sorting/CombSortTest.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java b/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java index 78c09979c..8fb6a7aaf 100644 --- a/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java +++ b/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java @@ -10,20 +10,10 @@ public class CombSortTest { static Random random = new Random(); - @Test - public void testGetMax() { - int[] array = {5, 7, 1, 13, 1013, 287, 912}; - assertThat(RadixSort.getMax(array)).isEqualTo(1013); - } - - @Test - public void testCalculateNumberOfDigits() { - assertThat(RadixSort.calculateNumberOfDigits(1089)).isEqualTo(4); - assertThat(RadixSort.calculateNumberOfDigits(19)).isEqualTo(2); - } + CombSort ob = new CombSort(); @Test - public void randomRadixSort_smallNumbers() { + public void randomCombSort_smallNumbers() { for (int size = 0; size < 1000; size++) { int[] values = new int[size]; for (int i = 0; i < size; i++) { @@ -32,8 +22,7 @@ public void randomRadixSort_smallNumbers() { int[] copy = values.clone(); Arrays.sort(values); - RadixSort.radixSort(copy); - + ob.sort(copy); assertThat(values).isEqualTo(copy); } } @@ -48,7 +37,7 @@ public void randomRadixSort_largeNumbers() { int[] copy = values.clone(); Arrays.sort(values); - RadixSort.radixSort(copy); + ob.sort(copy); assertThat(values).isEqualTo(copy); } From 62ff491605a3b3534e7cfa4c0497af1ed52b05dd Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Thu, 27 Oct 2022 11:05:18 +1100 Subject: [PATCH 5/7] Delete comb sort, make it a branch only for Hamiltonian Cycle --- .../java/com/williamfiset/algorithms/sorting/CombSort.java | 1 - .../java/com/williamfiset/algorithms/sorting/CombSortTest.java | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java index ab04e1b27..f9eaac839 100644 --- a/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java +++ b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java @@ -32,7 +32,6 @@ void sort(int arr[]) { // Find next gap gap = getNextGap(gap); - // Initialize swapped as false so that we can // check if swap happened or not swapped = false; diff --git a/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java b/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java index 8fb6a7aaf..164499c82 100644 --- a/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java +++ b/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java @@ -22,8 +22,7 @@ public void randomCombSort_smallNumbers() { int[] copy = values.clone(); Arrays.sort(values); - ob.sort(copy); - assertThat(values).isEqualTo(copy); + ob.sort(copy); assertThat(values).isEqualTo(copy); } } From 67210152c0ae4cbe57f7a52b79ee760ffa1bedf8 Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Thu, 27 Oct 2022 11:14:14 +1100 Subject: [PATCH 6/7] Make some update to the HamiltonianCycle.java --- .../graphtheory/HamiltonianCycle.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java index bb25f2476..3c2c86659 100644 --- a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -6,10 +6,11 @@ public class HamiltonianCycle { /**elements used to store path and the graph **/ - private int V, pathCount; - private int[] path; - private int[][] graph; + int V, pathCount; + int[] path; + int[][] graph; + //used for testing public void setGraph(int[][] graph1){ this.graph=graph1; } @@ -27,7 +28,7 @@ public void findHamiltonianCycle(int[][] g) if(g==null)throw new IllegalArgumentException("Graph cannot be null."); V = g.length; path = new int[V+1]; - Arrays.fill(path, -1); + Arrays.fill(path, -100); graph = g; try { @@ -56,22 +57,27 @@ public void solve(int vertex) throws Exception /** all vertices selected but last vertex not linked to 0 **/ if (pathCount == V) return; - for (int v = 0; v < V; v++) + for (int i = 0; i < V; i++) { /** if connected **/ - if (graph[vertex][v] == 1) + if (graph[vertex][i] == 1) { /** add to path **/ - path[pathCount++] = v; + path[pathCount++] = i; /** remove connection **/ - graph[vertex][v] = 0; - graph[v][vertex] = 0; + graph[vertex][i] = 0; + graph[i][vertex] = 0; /** if vertex not already selected solve recursively **/ - if (!isPresent(v)) - solve(v); + boolean flag=false; + for (int j= 0; j < pathCount - 1; j++) + if (path[j] == i) + flag= true; + flag= false; + if (!flag) + solve(i); /** restore connection **/ - graph[vertex][v] = 1; - graph[v][vertex] = 1; + graph[vertex][i] = 1; + graph[i][vertex] = 1; /** remove path **/ path[--pathCount] = -1; } @@ -79,19 +85,11 @@ public void solve(int vertex) throws Exception } - /** function to check if path is already selected **/ - public boolean isPresent(int v) - { - for (int i = 0; i < pathCount - 1; i++) - if (path[i] == v) - return true; - return false; - } /** display solution **/ public void display() { - System.out.print("\nPath : "); + System.out.print("\nThe Hamiltonian Path is: "); for (int i = 0; i <= V; i++) System.out.print(path[i % V] + " "); System.out.println(); @@ -105,12 +103,12 @@ public static void main(String[] args) HamiltonianCycle hc = new HamiltonianCycle(); /** Accept number of vertices **/ System.out.println("Enter number of vertices"); - int V = scan.nextInt(); + int n = scan.nextInt(); /** get graph **/ System.out.println("Enter adjacency matrix"); - int[][] graph = new int[V][V]; - for (int i = 0; i < V; i++) - for (int j = 0; j < V; j++) + int[][] graph = new int[n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) graph[i][j] = scan.nextInt(); hc.findHamiltonianCycle(graph); scan.close(); From 2e43feb7c5eeca1e4e1ad1325dc98dc06b93f683 Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Thu, 27 Oct 2022 19:05:20 +1100 Subject: [PATCH 7/7] Write a new HamiltonianCycle.java and update the test unit, using a easier way. --- .../graphtheory/HamiltonianCycle.java | 178 ++++++++---------- .../graphtheory/HamiltonianCycleTest.java | 6 +- 2 files changed, 83 insertions(+), 101 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java index 3c2c86659..9cbf8b8aa 100644 --- a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -5,113 +5,95 @@ import java.util.Arrays; public class HamiltonianCycle { - /**elements used to store path and the graph **/ - int V, pathCount; - int[] path; - int[][] graph; + private int[] path; + private int[][] graph; + private boolean flag=false; + public void setGraph(int[][] graph1){ + this.graph=graph1; + } - //used for testing - public void setGraph(int[][] graph1){ - this.graph=graph1; - } + public int[] getPath(){ + return this.path; + } - public int[] getPath(){ - return this.path; + public void getHamiltonCircuit(int[][] g) { + if(g==null)throw new IllegalArgumentException("Graph cannot be null."); + //used to record the circuit + boolean[] used = new boolean[g.length]; + int[] path = new int[g.length]; + for(int i = 0;i < g.length;i++) { + //initialise + used[i] = false; + path[i] = -1; } - /** Function to find cycle - * Take the graph as input - * And will print "No solution" if the graph has no HamiltionianCycle - * Function solve will do actual find paths job - * **/ - public void findHamiltonianCycle(int[][] g) - { - if(g==null)throw new IllegalArgumentException("Graph cannot be null."); - V = g.length; - path = new int[V+1]; - Arrays.fill(path, -100); - graph = g; - try - { - path[0] = 0; - pathCount = 1; - solve(0); - System.out.print("No solution"); - } - catch (Exception e) - { - path[V]=path[0]; - System.out.println(e.getMessage()); - display(); - } + used[0] = true; + //the start point + path[0] = 0; + //deep search from the first point + dfs(g, path, used, 1); + if(!flag){ + System.out.print("No solution"); } - - /** function to find paths recursively - *Take int as input - *Will add coordinators into the path list. - * **/ - public void solve(int vertex) throws Exception - { - /** solution **/ - if (graph[vertex][0] == 1 && pathCount == V) - throw new Exception("Solution found"); - /** all vertices selected but last vertex not linked to 0 **/ - if (pathCount == V) - return; - for (int i = 0; i < V; i++) - { - /** if connected **/ - if (graph[vertex][i] == 1) - { - /** add to path **/ - path[pathCount++] = i; - /** remove connection **/ - graph[vertex][i] = 0; - graph[i][vertex] = 0; - /** if vertex not already selected solve recursively **/ - boolean flag=false; - for (int j= 0; j < pathCount - 1; j++) - if (path[j] == i) - flag= true; - flag= false; - if (!flag) - solve(i); - /** restore connection **/ - graph[vertex][i] = 1; - graph[i][vertex] = 1; - /** remove path **/ - path[--pathCount] = -1; + } + /* + * step means the current walked points' number + */ + public boolean dfs(int[][] g, int[] path, boolean[] used, int step) { + if(step == g.length) { //when finish all points + if(g[path[step - 1]][0] == 1) { + storeResult(path); + flag=true; + //the last step can reach the end + for(int i = 0;i < path.length;i++) + System.out.print(path[i]+">"); + System.out.print(path[0]); + System.out.println(); + return true; + } + return false; + } else { + storeResult(path); + for(int i = 0;i < g.length;i++) { + if(!used[i] && g[path[step - 1]][i] == 1) { + used[i] = true; + path[step] = i; + if(dfs(g, path, used, step + 1)) + return true; + else { + used[i] = false; + path[step] = -1; + } } } - } + return false; + } - - /** display solution **/ - public void display() - { - System.out.print("\nThe Hamiltonian Path is: "); - for (int i = 0; i <= V; i++) - System.out.print(path[i % V] + " "); - System.out.println(); + public void storeResult(int [] path){ + this.path=new int[path.length+1]; + for(int i=0;i< path.length;i++){ + this.path[i]=path[i]; } + this.path[path.length]=path[0]; + } - /** Main function **/ - public static void main(String[] args) - { - Scanner scan = new Scanner(System.in); - /** Make an object of HamiltonianCycle class **/ - HamiltonianCycle hc = new HamiltonianCycle(); - /** Accept number of vertices **/ - System.out.println("Enter number of vertices"); - int n = scan.nextInt(); - /** get graph **/ - System.out.println("Enter adjacency matrix"); - int[][] graph = new int[n][n]; - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - graph[i][j] = scan.nextInt(); - hc.findHamiltonianCycle(graph); - scan.close(); - } + public static void main(String[] args) { + HamiltonianCycle hc = new HamiltonianCycle(); + Scanner scan = new Scanner(System.in); + // Make an object of HamiltonianCycle class + // Accept number of vertices + System.out.println("Enter number of vertices"); + int n = scan.nextInt(); + // get graph + System.out.println("Enter adjacency matrix"); + int[][] graph = new int[n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + graph[i][j] = scan.nextInt(); + hc.getHamiltonCircuit(graph); + scan.close(); } + +} + diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java index d8baa7234..be07fa4a1 100644 --- a/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java +++ b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java @@ -35,7 +35,7 @@ public void testEmptyGraph() { HamiltonianCycle solver; solver = new HamiltonianCycle(); solver.setGraph(graph); - solver.findHamiltonianCycle(graph); + solver.getHamiltonCircuit(graph); } //valid test 1 @@ -68,7 +68,7 @@ public void validHC1() { HamiltonianCycle solver; solver = new HamiltonianCycle(); solver.setGraph(graph); - solver.findHamiltonianCycle(graph); + solver.getHamiltonCircuit(graph); assertThat(solver.getPath()).isEqualTo(result); } @@ -118,7 +118,7 @@ public void invalidHC1() { HamiltonianCycle solver; solver = new HamiltonianCycle(); solver.setGraph(graph); - solver.findHamiltonianCycle(graph); + solver.getHamiltonCircuit(graph); Assert.assertEquals("No solution",outContent.toString()); } }