-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEulerTrail.cpp
More file actions
259 lines (205 loc) · 4.78 KB
/
EulerTrail.cpp
File metadata and controls
259 lines (205 loc) · 4.78 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// main.cpp
// EulerTrail
//
// Created by Sethupathi on 31/07/20.
//
#include <iostream>
#include <list>
#include <string.h>
#include <algorithm>
using namespace std;
class Graph
{
private:
int V;
list<int> *adj;
public:
Graph(int V){
this->V= V;
adj = new list<int>[V];
}
~Graph()
{
delete [] adj;
}
void addEdge(int u, int v)
{
adj[u]. push_back(v);
adj[v]. push_back(u);
}
void removeEdge(int u, int v);
void printEulerTour();
void printEulerUtil(int s);
//counting the no. of vertices reachable from v
int DFSCount(int v, bool visited[]);
//function to check if edge u-v is a valid next edge in euler trail
bool isValidNextEdge(int u, int v);
};
void Graph::printEulerTour()
{
int u=0;
for (int i=0;i<V;i++)
{
if(adj[i].size()&1)
{
u=i;
break;
}
}
printEulerUtil(u);
cout<<endl;
}
void Graph::printEulerUtil(int u)
{
list<int>:: iterator i;
for(i=adj[u].begin();i!=adj[u].end();++i)
{
int v = *i;
if(v!= -1 && isValidNextEdge(u, v))
{
cout<<u<<"-"<<v<<" ";
removeEdge(u, v);
printEulerUtil(v);
}
}
}
bool Graph::isValidNextEdge(int u, int v)
{
//The edge u-v is valid in one of the following two cases:
// 1) If v is the only adjacent vertex of u
int count=0; // to store count of adjacent edges
list<int>::iterator i;
for (i=adj[u].begin();i!=adj[u].end();++i)
{
if(*i!=-1)
count++;
}
if (count ==1)
return true;
// 2) If there are multiple adjacents, then u-v is not a bridge
// Do following steps to check if u-v is a bridge
// 2.a) count of vertices reachable from u
bool visited[V];
memset(visited,false,V);
int count1 = DFSCount(u, visited);
// 2.b) Remove edge (u, v) and after removing the edge, count
// vertices reachable from u
removeEdge(u, v);
memset(visited, false, V);
int count2 = DFSCount(u, visited);
// 2.c) Add the edge back to the graph
addEdge(u, v);
// 2.d) If count1 is greater, then edge (u, v) is a bridge
return (count1 > count2)? false: true;
}
void Graph:: removeEdge(int u, int v)
{
// Find v in adjacency list of u and replace it with -1
list<int>::iterator iv = find(adj[u].begin(),adj[u].end(),v);
*iv =-1;
list<int>::iterator iu = find(adj[v].begin(),adj[v].end(),u);
*iu =-1;
}
// A DFS based function to count reachable vertices from v
int Graph::DFSCount(int v, bool visited[])
{
visited[v] = true;
int count=1;
list<int>:: iterator i;
for(i=adj[v].begin();i!=adj[v].end();++i)
{
if(*i!=-1 && !visited[*i])
count += DFSCount(*i, visited);
}
return count;
}
// Driver program to test above function
int main()
{
int e,node1,node2;
//cout<<"Enter no. of edges";
cin>>e;
Graph g1(e);
for(int i=0;i<e;i++)
{
cin>>node1>>node2;
g1.addEdge(node1,node2);
}
g1.printEulerTour();
// Let us first create and test graphs shown in above figure
// Graph g1(4);
// g1.addEdge(0, 1);
// g1.addEdge(0, 2);
// g1.addEdge(1, 2);
// g1.addEdge(2, 3);
// g1.printEulerTour();
//
// Graph g2(3);
// g2.addEdge(0, 1);
// g2.addEdge(1, 2);
// g2.addEdge(0, 2);
// g2.printEulerTour();
//
// Graph g3(4);
// g3.addEdge(0, 1);
// g3.addEdge(1, 2);
// g3.addEdge(2, 3);
// g3.addEdge(3, 0);
//
// g3.printEulerTour();
//
// //starts from '0'
//
// Graph g4(6);
// g4.addEdge(1, 2);
// g4.addEdge(2, 4);
// g4.addEdge(4, 3);
// g4.addEdge(3, 1);
// g4.addEdge(4, 5);
// g4.addEdge(5, 4);
// g4.addEdge(7, 5);
// g4.addEdge(7, 0);
// g4.addEdge(6, 0);
// g4.addEdge(6, 0);
// g4.addEdge(5, 6);
// g4.addEdge(5, 6);
// g4.addEdge(0, 1);
// g4.addEdge(0, 1);
// g4.addEdge(1, 2);
// g4.addEdge(1, 2);
// g4.addEdge(2, 3);
// g4.addEdge(3, 0);
//
// g4.printEulerTour();
//
// Graph g5(5);
// g5.addEdge(0, 1);
// g5.addEdge(0, 1);
// g5.addEdge(2, 3);
// g5.addEdge(3, 4);
// g5.addEdge(1, 2);
// g5.printEulerTour();
//
//
// cout<<"Euler Path for the boolean function "<<endl;
//
// Graph g6(12);
// g6.addEdge(0, 6);
// g6.addEdge(0, 6);
// g6.addEdge(0, 7);
// g6.addEdge(5, 6);
// g6.addEdge(5, 6);
// g6.addEdge(5, 7);
//
// g6.addEdge(5, 4);
// g6.addEdge(5, 4);
// g6.addEdge(4, 3);
// g6.addEdge(3, 1);
// g6.addEdge(1, 2);
// g6.addEdge(2, 4);
//
// g6.printEulerTour();
//
return 0;
}