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..bb25f2476 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -0,0 +1,119 @@ +package com.williamfiset.algorithms.graphtheory; + +import java.util.List; +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; + + 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 + * 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, -1); + 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(); + } + } + + /** 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(); + } + } + 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