-
Notifications
You must be signed in to change notification settings - Fork 368
/
Hierholzer_Algorithm.cpp
111 lines (91 loc) · 3.38 KB
/
Hierholzer_Algorithm.cpp
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
/*
------------------------------------- Hierholzer’s Algorithm for finding an Eulerian Cycle -------------------------------------
An Eulerian Cycle is a path in a graph that visits every edge exactly once and returns to the starting vertex.
To find an Eulerian Cycle using Hierholzer's Algorithm, we follow these steps:
1. Choose any starting vertex in the graph.
2. Follow a trail of edges from that vertex until returning to the starting vertex. Since the graph is directed, there will always be an unused edge leaving each visited vertex.
3. Repeat the process as long as there are vertices with unused edges connected to the current trail. Start a new trail from such vertices, following unused edges until returning to the starting vertex of the new trail.
4. If we get stuck (a vertex with no unused edges), backtrack to the nearest vertex in the current path that has unused edges.
5. Repeat steps 3 and 4 until all edges have been used.
6. Store the final path in a separate container.
For Example:
Consider a directed graph with the following edges:
0 -> 1 -> 2 -> 3 -> 0
1 -> 4 -> 1
2 -> 1
3 -> 2 -> 4 -> 0
To find the Eulerian Cycle using Hierholzer's Algorithm, we start from vertex 0 and traverse the graph by following unused edges until we return to vertex 0.
Then, we identify the remaining unused edges and start new trails from vertices 1, 2, and 3, respectively. Finally, we merge all the paths to obtain the Eulerian Cycle:
0 -> 1 -> 2 -> 3 -> 2 -> 1 -> 4 -> 1 -> 0.
*/
// This code is for that graph which is already an Eulerian graph.
#include <bits/stdc++.h>
using namespace std;
void dfs(int curr_v, vector< vector<int> >& adj, vector<int>& circuit, unordered_map<int,int>& edge_count, stack<int>& curr_path)
{
while (edge_count[curr_v])
{
curr_path.push(curr_v);
int next_v = adj[curr_v].back();
edge_count[curr_v]--;
adj[curr_v].pop_back();
curr_v = next_v;
}
circuit.push_back(curr_v);
}
void printCycle(vector< vector<int> >& adj)
{
unordered_map<int,int> edge_count;
for (int i = 0; i < adj.size(); i++)
edge_count[i] = adj[i].size();
if (!adj.size())
return;
stack<int> curr_path;
vector<int> circuit;
curr_path.push(0);
int curr_v = 0;
while (!curr_path.empty())
{
if (edge_count[curr_v])
{
dfs(curr_v, adj, circuit, edge_count, curr_path);
curr_v = curr_path.top();
curr_path.pop();
}
else
{
circuit.push_back(curr_v);
curr_v = curr_path.top();
curr_path.pop();
}
}
for (int i = circuit.size() - 1; i >= 0; i--)
{
cout << circuit[i];
if (i)
cout << " -> ";
}
}
int main()
{
int vertices, edges;
cout << "Enter the number of vertices in the graph: ";
cin >> vertices;
cout << "Enter the number of edges in the graph: ";
cin >> edges;
vector<vector<int>> adj(vertices);
cout << "Enter the edges in the format 'source -> destination':" << endl;
for (int i = 0; i < edges; i++)
{
int source, destination;
cout << "Source-" << i+1 << " : ";
cin >> source;
cout << "Destination-" << i+1 << " : ";
cin >> destination;
adj[source].push_back(destination);
}
cout << "\nEulerian Cycle: ";
printCycle(adj);
cout << endl;
return 0;
}