-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathhierholzer_algorithm.cpp
More file actions
124 lines (111 loc) · 2.99 KB
/
Copy pathhierholzer_algorithm.cpp
File metadata and controls
124 lines (111 loc) · 2.99 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
/*
* Algorithm: Hierholzer’s Algorithm (Eulerian Path / Circuit)
*
* Problem:
* Given a connected graph (directed or undirected), find an Eulerian Path or Circuit.
* - Eulerian Path: visits every edge exactly once.
* - Eulerian Circuit: Eulerian Path that starts and ends on the same vertex.
*
* Eulerian Path Conditions:
* - Undirected: At most 2 vertices have odd degree.
* - Directed: exactly 1 vertex has (out = in + 1), exactly 1 has (in = out + 1),
* all others: indegree == outdegree.
*
* Eulerian Circuit Conditions:
* - Undirected: all vertices have even degree.
* - Directed: indegree == outdegree for every vertex.
*
* Approach (Hierholzer’s Algorithm):
* 1. Pick starting vertex:
* - Circuit → any vertex.
* - Path → vertex with (outdeg > indeg).
* 2. Traverse edges using a stack, removing them as you go.
* 3. If stuck, backtrack → add vertex to final path.
* 4. Reverse final path → Eulerian Path / Circuit.
*
* Complexity:
* - O(E), since each edge is used exactly once.
*/
#include <bits/stdc++.h>
using namespace std;
class Eulerian
{
public:
vector<int> findEulerianPath(int n, vector<vector<int>> &edges)
{
vector<vector<int>> adj(n);
vector<int> indeg(n, 0), outdeg(n, 0);
// Build adjacency list
for (auto &e : edges)
{
int u = e[0], v = e[1];
adj[u].push_back(v);
outdeg[u]++;
indeg[v]++;
}
// Find start node (for path vs circuit)
int start = 0;
for (int i = 0; i < n; i++)
{
if (outdeg[i] - indeg[i] == 1)
start = i; // Eulerian path start
}
vector<int> path, stack = {start};
// Hierholzer’s Algorithm
while (!stack.empty())
{
int u = stack.back();
if (!adj[u].empty())
{
// Go deeper using available edge
int v = adj[u].back();
adj[u].pop_back(); // remove edge u→v
stack.push_back(v);
// 🔎 Visualization step
// cout << "Traverse edge " << u << " -> " << v << endl;
}
else
{
// No more edges → add to path (backtrack)
path.push_back(u);
stack.pop_back();
// 🔎 Visualization step
// cout << "Backtrack: add " << u << " to path" << endl;
}
}
reverse(path.begin(), path.end()); // final path
return path;
}
};
int main()
{
// Example Graph: Eulerian Circuit
int n = 4;
vector<vector<int>> edges = {
{0, 1}, {1, 2}, {2, 0}, {0, 3}, {3, 0}};
Eulerian solver;
vector<int> path = solver.findEulerianPath(n, edges);
cout << "Eulerian Path/Circuit: ";
for (int v : path)
cout << v << " ";
cout << endl;
/*
* Visualization for this graph:
*
* Edges: 0->1, 1->2, 2->0, 0->3, 3->0
*
* Step walk:
* Start at 0
* 0 -> 3
* 3 -> 0
* 0 -> 1
* 1 -> 2
* 2 -> 0
*
* Backtracking order:
* 0 (dead end) → 2 → 1 → 0 → 3 → 0
*
* Final Eulerian Circuit:
* 0 → 3 → 0 → 1 → 2 → 0
*/
}