Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #286 hamiltonian cycle #349

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}


}

73 changes: 73 additions & 0 deletions src/main/java/com/williamfiset/algorithms/sorting/CombSort.java
Original file line number Diff line number Diff line change
@@ -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<n-gap; i++)
{
if (arr[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<V;i++)
arr[i]=scan.nextInt();
ob.sort(arr);
System.out.println("sorted array");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");

}
}
Original file line number Diff line number Diff line change
@@ -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.getHamiltonCircuit(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.getHamiltonCircuit(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.getHamiltonCircuit(graph);
Assert.assertEquals("No solution",outContent.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.williamfiset.algorithms.sorting;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

import static com.google.common.truth.Truth.assertThat;

public class CombSortTest {
static Random random = new Random();

CombSort ob = new CombSort();

@Test
public void randomCombSort_smallNumbers() {
for (int size = 0; size < 1000; size++) {
int[] values = new int[size];
for (int i = 0; i < size; i++) {
values[i] = randInt(1, 50);
}
int[] copy = values.clone();

Arrays.sort(values);
ob.sort(copy); assertThat(values).isEqualTo(copy);
}
}

@Test
public void randomRadixSort_largeNumbers() {
for (int size = 0; size < 1000; size++) {
int[] values = new int[size];
for (int i = 0; i < size; i++) {
values[i] = randInt(1, Integer.MAX_VALUE);
}
int[] copy = values.clone();

Arrays.sort(values);
ob.sort(copy);

assertThat(values).isEqualTo(copy);
}
}

// return a random number between [min, max]
static int randInt(int min, int max) {
return random.nextInt((max - min) + 1) + min;
}
}