-
Notifications
You must be signed in to change notification settings - Fork 20k
Hamiltonian cycle implementation #3133
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
Closed
itsAkshayDubey
wants to merge
6
commits into
TheAlgorithms:master
from
itsAkshayDubey:Hamiltonian-cycle-implementation
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9dd9dff
feat: Add hamiltonian cycle implementation
itsAkshayDubey df37ca9
refactor: Add unit test for hamiltonian cycle
itsAkshayDubey 57d46aa
refactor: Remove extra whitespaces and newlines
itsAkshayDubey 7c343a9
refactor: Improve flow and rename solve method
itsAkshayDubey a128b33
Merge branch 'TheAlgorithms:master' into Hamiltonian-cycle-implementa…
itsAkshayDubey 872abff
fix: findHamiltonianCycle method will return a cycle
itsAkshayDubey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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[] 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. | ||
*/ | ||
public int[] findHamiltonianCycle(int[][] graph){ | ||
this.V = graph.length; | ||
this.cycle = new int[this.V+1]; | ||
|
||
//Initialize path array with -1 value | ||
for(int i=0 ; i<this.cycle.length ; i++) { | ||
this.cycle[i] = -1; | ||
} | ||
|
||
this.graph = graph; | ||
|
||
this.cycle[0] = 0; | ||
this.pathCount = 1; | ||
if(!isPathFound(0)) { | ||
for(int i=0 ; i<this.cycle.length ; i++) { | ||
this.cycle[i] = -1; | ||
} | ||
} | ||
else { | ||
this.cycle[this.cycle.length-1] = this.cycle[0]; | ||
} | ||
|
||
return cycle; | ||
} | ||
|
||
/** function to find paths recursively | ||
* Find paths recursively from given vertex | ||
* @param vertex Vertex from which path is to be found | ||
* @returns true if path is found false otherwise | ||
*/ | ||
public boolean isPathFound(int vertex) { | ||
if (this.graph[vertex][0] == 1 && this.pathCount == this.V) { | ||
return true; | ||
} | ||
|
||
/** all vertices selected but last vertex not linked to 0 **/ | ||
if (this.pathCount == this.V) { | ||
return false; | ||
} | ||
|
||
for (int v = 0; v < this.V; v++){ | ||
/** if connected **/ | ||
if (this.graph[vertex][v] == 1 ){ | ||
/** add to path **/ | ||
this.cycle[this.pathCount++] = v; | ||
|
||
/** remove connection **/ | ||
this.graph[vertex][v] = 0; | ||
this.graph[v][vertex] = 0; | ||
|
||
/** if vertex not already selected solve recursively **/ | ||
if (!isPresent(v)) { | ||
return isPathFound(v); | ||
} | ||
|
||
/** restore connection **/ | ||
this.graph[vertex][v] = 1; | ||
this.graph[v][vertex] = 1; | ||
|
||
/** remove path **/ | ||
this.cycle[--this.pathCount] = -1; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** function to check if path is already selected | ||
* Check if path is already selected | ||
* @param vertex Starting vertex | ||
*/ | ||
public boolean isPresent(int vertex){ | ||
|
||
for (int i = 0; i < pathCount - 1; i++) { | ||
if (cycle[i] == vertex) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's called
findCycle
, it should return a cycle, not just a path. Please rename everything tofind path
, notfind cycle
if that's what it's supposed to do