forked from ReciHub/FunnyAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluery.cpp
More file actions
132 lines (107 loc) · 3.01 KB
/
Copy pathfluery.cpp
File metadata and controls
132 lines (107 loc) · 3.01 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
#include <algorithm>
#include <iostream>
#include <list>
#include <string.h>
using namespace std;
class Graph {
int V; // vertices
list < int > * adj; // A dynamic array of adjacency lists
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 printEulerTour() {
// Find a vertex with odd degree
int u = 0;
for (int i = 0; i < V; i++)
if (adj[i].size() & 1) {
u = i;
break;
}
// Print tour starting from oddv
printEulerUtil(u);
cout << endl;
}
void printEulerUtil(int u) {
// Recur for all the vertices adjacent to this vertex
list < int > ::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i) {
int v = * i;
// If edge u-v is not removed and it's a a valid
// next edge
if (v != -1 && isValidNextEdge(u, v)) {
cout << u << "-" << v << " ";
rmvEdge(u, v);
printEulerUtil(v);
}
}
}
bool 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 vertices
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
rmvEdge(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 rmvEdge(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;
// Find u in adjacency list of v and replace it with -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 DFSCount(int v, bool visited[]) {
// Mark the current node as visited
visited[v] = true;
int count = 1;
// Recur for all adjacent vertices
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;
}
};
int main() {
Graph g3(5);
g3.addEdge(1, 0);
g3.addEdge(0, 2);
g3.addEdge(2, 1);
g3.addEdge(0, 3);
g3.addEdge(3, 4);
g3.addEdge(3, 2);
g3.addEdge(3, 1);
g3.addEdge(2, 4);
g3.printEulerTour();
return 0;
}