-
Notifications
You must be signed in to change notification settings - Fork 0
/
HamiltonionPath.java
34 lines (32 loc) · 1023 Bytes
/
HamiltonionPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import utils.*;
import java.util.*;
class main{
static int hamiltonianPath(int src , ArrayList<Edge> graph[] , boolean visited[] , int count , String res){
res=res+"->"+src;
if(count==graph.length-1){
System.out.println(res);
return 1;
}
visited[src]=true;
int count_=0;
for(Edge e : graph[src]){
int neighbour = e.vertex;
if(!visited[neighbour]){
count+=hamiltonianPath(neighbour,graph,visited,count+1,res);
}
}
visited[src]=false;
return count_;
}
public static void main(String args[]){
Graph g = new Graph(4);
ArrayList<Edge> graph []= g.graph;
g.addEdge(0,1,0,true);
g.addEdge(1,2,0,true);
g.addEdge(2,3,0,true);
g.addEdge(3,0,0,true);
g.addEdge(0,2,0,true);
boolean visited[] = new boolean[graph.length];
System.out.println(hamiltonianPath(0,graph,visited,0,""));
}
}