-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhamiltonian_path.java
87 lines (81 loc) · 2.34 KB
/
hamiltonian_path.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.io.*;
class q2
{
int adj[][];
void printHamiltonian(int visited[], int numVisited, int totNodes, int curNode, int path[], int pathl)
{
int i;
visited[curNode] = 1;
numVisited++;
if(numVisited==totNodes)
{
for(i=0;i<pathl;i++)
{
System.out.print((path[i]+1));
}
System.out.println();
}
else
{
for(i=0;i<totNodes;i++)
{
if(visited[i]==0 && adj[curNode][i]==1)
{
path[pathl] = i;
printHamiltonian(visited, numVisited, totNodes, i, path, pathl+1);
}
}
}
visited[curNode] = 0;
}
public static void main(String args[]) throws IOException
{
q2 ob = new q2();
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int i, n, j, e;
System.out.print("Enter the number of nodes : ");
n = Integer.parseInt(in.readLine());
ob.adj = new int[n][n];
int a[] = new int[2];
String ss[] = new String[2];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
ob.adj[i][j] = 0;
}
}
System.out.print("Enter the number of edges : ");
e = Integer.parseInt(in.readLine());
for(i=0;i<e;i++)
{
System.out.println("Enter the source and destination node for edge " + (i+1));
ss = in.readLine().split(" ");
a[0] = Integer.parseInt(ss[0]) - 1;
a[1] = Integer.parseInt(ss[1]) - 1;
ob.adj[a[0]][a[1]] = 1;
ob.adj[a[1]][a[0]] = 1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(ob.adj[i][j]);
}
System.out.println();
}
int v[] = new int[n];
for(i=0;i<n;i++)
{
v[i] = 0;
}
int path[] = new int[n];
System.out.println("Hamiltonian Paths :");
for(i=0;i<n;i++)
{
path[0] = i;
ob.printHamiltonian(v, 0, n, i, path, 1);
}
}
}