-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0006_EulerianPathAndCircuit.cc
212 lines (188 loc) · 4.71 KB
/
0006_EulerianPathAndCircuit.cc
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
#include "../Headers/0003_Graph/0006_EulerianPathAndCircuit.h"
#include<stack>
#include<algorithm>
using namespace std;
namespace EulerianPathAndCircuit
{
Node::Node(int value)
{
this->data = value;
this->degree = 0;
this->inDegree = 0;
this->outDegree = 0;
this->visited = false;
}
// Graph Private Member Methods
Node* Graph::MakeOrFindNode(int value)
{
Node* node = nullptr;
if (this->_nodeMap.find(value) == this->_nodeMap.end())
{
node = new Node(value);
this->_nodeMap[value] = node;
}
else
{
node = this->_nodeMap[value];
}
return node;
}
void Graph::DepthFirstSearch(Node* nodeU)
{
nodeU->visited = true;
for (auto& nodeV : this->_adjlist[nodeU])
{
if (nodeV->visited == false)
{
this->DepthFirstSearch(nodeV);
}
}
}
bool Graph::IsConnected()
{
// Step-1 : Make the visited property of all nodes as false. It is already done in constructor.
// Step-2 : Find a node which do not have 0 degree.
Node* node = nullptr;
for (auto& iterator : this->_nodeMap)
{
if (iterator.second->degree != 0)
{
node = iterator.second;
break;
}
}
// Step-3 : If node is null, it means G.E is null, so G is connected, else call DFS to traverse the graph G.
if (node == nullptr)
{
return true;
}
this->DepthFirstSearch(node);
// Step-4 : Checking if all the non-zero degree vertices have been visited or not.
for (auto& iterator : this->_nodeMap)
{
if (iterator.second->visited == false && iterator.second->degree != 0)
{
return false;
}
}
return true;
}
void Graph::EulerianPathHierholzerAlgorithm(Node* startingNode)
{
stack<Node*> currentPath;
currentPath.push(startingNode);
while (!currentPath.empty())
{
Node* currentNode = currentPath.top();
if (!this->_adjlist[currentNode].empty())
{
Node* nextNode = this->_adjlist[currentNode].front();
this->_adjlist[currentNode].pop_front();
this->_adjlist[nextNode].remove(currentNode);
currentPath.push(nextNode);
}
else
{
currentPath.pop();
this->_eulerianPath.push_back(currentNode->data);
}
}
}
// Graph Public Member Methods
void Graph::PushUndirectedEdge(int valueU, int valueV)
{
Node* nodeU = this->MakeOrFindNode(valueU);
Node* nodeV = this->MakeOrFindNode(valueV);
this->_adjlist[nodeU].push_back(nodeV);
nodeU->degree++;
this->_adjlist[nodeV].push_back(nodeU);
nodeV->degree++;
}
void Graph::PushDirectedEdge(int valueU, int valueV)
{
Node* nodeU = this->MakeOrFindNode(valueU);
Node* nodeV = this->MakeOrFindNode(valueV);
this->_adjlist[nodeU].push_back(nodeV);
nodeU->outDegree++;
nodeV->inDegree++;
}
void Graph::PushSingleNode(int valueU)
{
Node* nodeU = this->MakeOrFindNode(valueU);
}
void Graph::FindEulerianPathAndCircuit()
{
// If the graph is not connected then graph G is Not-Eulerian.
if (this->IsConnected() == false)
{
this->_isEulerianPathPresent = false;
this->_isEulerianCircuitPresent = false;
return;
}
int oddDegreeVertexCount = 0;
for (auto& iterator : this->_nodeMap)
{
if (iterator.second->degree & 1)
{
oddDegreeVertexCount++;
}
}
// Check-1 : When no vertex with odd degree is present, then graph G is Eulerian.
if (oddDegreeVertexCount == 0)
{
this->_isEulerianPathPresent = true;
this->_isEulerianCircuitPresent = true;
return;
}
// Check-2 : When 2 vertices have odd degree, then graph G is Semi-Eulerian.
if (oddDegreeVertexCount == 2)
{
this->_isEulerianPathPresent = true;
this->_isEulerianCircuitPresent = false;
return;
}
// Check-3 : When more than 2 vertices have odd degree, then graph G is Not Eulerian.
if (oddDegreeVertexCount > 2)
{
this->_isEulerianPathPresent = false;
this->_isEulerianCircuitPresent = false;
return;
}
}
bool Graph::IsEulerianPathPresent()
{
return this->_isEulerianPathPresent;
}
bool Graph::IsEulerianCircuitPresent()
{
return this->_isEulerianCircuitPresent;
}
vector<int> Graph::UndirectedGraphGetEulerianPath()
{
// Case-3 : When more than 2 vertices have odd degree, then the graph G is not Eulerian.
// No Eulerian Path is posible.
if (this->_isEulerianPathPresent == false)
{
return {};
}
// Now 2 cases remains.
// Case-2 : When 2 vertices have odd degree. Choose any one of them.
Node* node = nullptr;
for (auto& iterator : this->_nodeMap)
{
if (iterator.second->degree & 1)
{
node = iterator.second;
break;
}
}
// Case-1 : When no vertex with odd degree is present. Choose any vertex as starting point.
if (node == nullptr)
{
node = this->_nodeMap[0];
}
this->EulerianPathHierholzerAlgorithm(node);
reverse(this->_eulerianPath.begin(), this->_eulerianPath.end());
return this->_eulerianPath;
}
}