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..9cbf8b8aa --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -0,0 +1,99 @@ +package com.williamfiset.algorithms.graphtheory; + +import java.util.List; +import java.util.Scanner; +import java.util.Arrays; + +public class HamiltonianCycle { + private int[] path; + private int[][] graph; + private boolean flag=false; + public void setGraph(int[][] graph1){ + this.graph=graph1; + } + + 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; + } + 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"); + } + } + /* + * 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; + } + + 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]; + } + + 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/main/java/com/williamfiset/algorithms/sorting/CombSort.java b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java new file mode 100644 index 000000000..f9eaac839 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java @@ -0,0 +1,73 @@ +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