-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEulerian Path
173 lines (143 loc) · 4.46 KB
/
Eulerian Path
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
public class Main {
public static void main(String[] args) {
int noOfVertices=5;
int[][] adjMatrix=new int[][]{
{0,1,0,0,1},
{1,0,1,1,0},
{0,1,0,1,0},
{0,1,1,0,1},
{1,0,0,1,0}
};
isEuler(adjMatrix,noOfVertices);
System.out.println("");
adjMatrix=new int[][]{
{0,1,0,0,1},
{1,0,1,0,0},
{0,1,0,1,0},
{0,0,1,0,1},
{1,0,0,1,0}
};
isEuler(adjMatrix,noOfVertices);
System.out.println("");
adjMatrix=new int[][]{
{0,1,1,0,1},
{1,0,1,1,0},
{1,1,0,1,0},
{0,1,1,0,1},
{1,0,0,1,0}
};
isEuler(adjMatrix,noOfVertices);
}
static void isEuler(int[][] adjMatrix,int noOfVertices){
//if 0, then it is not a euler graph
//if 1, then it is a Euler path
//if 2, then it is a Euler Cycle
int flag = isEulerianGraph(adjMatrix,noOfVertices);
if(flag==0){
System.out.println("Graph is not Eulerian");
}
else if(flag==1){
System.out.println("Graph has Eulerian Path");
findEulerianPath(adjMatrix,noOfVertices);
}
else{
System.out.println("Graph has Eulerian Cycle");
findEulerianPath(adjMatrix,noOfVertices);
}
}
static int isEulerianGraph(int[][] adjMatrix,int noOfVertices){
int[] degreeOfVertices=findDegree(adjMatrix,noOfVertices);
boolean connected=isConnected(adjMatrix,noOfVertices,degreeOfVertices);
if(!connected){
return 0;
}
int noOfOddVertices=0;
for(int degree:degreeOfVertices){
if(degree%2!=0){
noOfOddVertices++;
}
}
if(noOfOddVertices>2){
return 0;
}
return noOfOddVertices == 2 ? 1 : 2;
}
static int[] findDegree(int[][] adjMatrix,int noOfVertices){
int[] degree=new int[noOfVertices];
for(int i=0;i<noOfVertices;i++){
for(int j=0;j<noOfVertices;j++){
if(i!=j && adjMatrix[i][j]==1){
degree[i]++;
}
}
}
return degree;
}
static boolean isConnected(int[][] adjMatrix,int noOfVertices,int[] degreeOfVertices){
boolean[] visited=new boolean[noOfVertices];
int i=0;
for(;i<noOfVertices;i++){
if(degreeOfVertices[i]>0){
break;
}
}
if(i==noOfVertices){
return true;
}
dfs(adjMatrix,visited,i,noOfVertices);
for(int vertex=0;vertex<noOfVertices;vertex++){
if(!visited[vertex] && degreeOfVertices[vertex]>0){
return false;
}
}
return true;
}
static void dfs(int[][] adjMatrix,boolean[] visited, int vertex,int noOfVertices){
visited[vertex]=true;
for(int i=0;i<noOfVertices;i++){
if(i!=vertex && adjMatrix[vertex][i]==1 && !visited[i]){
dfs(adjMatrix,visited,i,noOfVertices);
}
}
}
static void findEulerianPath(int[][] adjMatrix,int noOfVertices){
int[] degreeOfVertices=findDegree(adjMatrix,noOfVertices);
int curr=0;
for(int i=0;i<noOfVertices;i++){
if(degreeOfVertices[i]%2!=0){
curr=i;
}
}
Stack<Integer> st=new Stack<>();
List<Integer> path=new ArrayList<>();
while(!st.isEmpty() || hasNeighbour(adjMatrix[curr])){
if(!hasNeighbour(adjMatrix[curr])){
path.add(curr);
curr = st.pop();
}
else{
for(int i=0;i<noOfVertices;i++){
if(adjMatrix[curr][i]==1){
st.add(curr);
adjMatrix[curr][i]=0;
adjMatrix[i][curr]=0;
curr=i;
break;
}
}
}
}
for(int vertex:path){
System.out.print(vertex + " -> ");
}
System.out.println(curr);
}
static boolean hasNeighbour(int[] adjacent){
for(int i:adjacent){
if(i!=0){
return true;
}
}
return false;
}
}