-
Notifications
You must be signed in to change notification settings - Fork 368
/
Hierholzer_Algorithm.java
110 lines (89 loc) · 4.12 KB
/
Hierholzer_Algorithm.java
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
/*
------------------------------------- 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.
import java.util.*;
/**
* Hierholzer_Algorithm
*/
public class Hierholzer_Algorithm {
static void dfs(int curr_v, List<List<Integer>> adj, List<Integer> circuit, HashMap<Integer, Integer> edge_count, Stack<Integer> curr_path) {
while (edge_count.get(curr_v) != 0) {
curr_path.push(curr_v);
int next_v = adj.get(curr_v).remove(adj.get(curr_v).size() - 1);
edge_count.put(curr_v, edge_count.get(curr_v) - 1);
curr_v = next_v;
}
circuit.add(curr_v);
}
static void printCycle(List<List<Integer>> adj) {
HashMap<Integer, Integer> edge_count = new HashMap<>();
for (int i = 0; i < adj.size(); i++) {
edge_count.put(i, adj.get(i).size());
}
if (adj.isEmpty()) {
return;
}
Stack<Integer> curr_path = new Stack<>();
List<Integer> circuit = new ArrayList<>();
curr_path.push(0);
int curr_v = 0;
while (!curr_path.empty()) {
if (edge_count.get(curr_v) != 0) {
dfs(curr_v, adj, circuit, edge_count, curr_path);
curr_v = curr_path.peek();
curr_path.pop();
} else {
circuit.add(curr_v);
curr_v = curr_path.peek();
curr_path.pop();
}
}
for (int i = circuit.size() - 1; i >= 0; i--) {
System.out.print(circuit.get(i));
if (i > 0) {
System.out.print(" -> ");
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of vertices in the graph: ");
int vertices = scanner.nextInt();
System.out.print("Enter the number of edges in the graph: ");
int edges = scanner.nextInt();
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < vertices; i++) {
adj.add(new ArrayList<>());
}
System.out.println("Enter the edges in the format 'source destination':");
for (int i = 0; i < edges; i++) {
System.out.print("Source-" + (i + 1) + " : ");
int source = scanner.nextInt();
System.out.print("Destination-" + (i + 1) + " : ");
int destination = scanner.nextInt();
adj.get(source).add(destination);
}
System.out.println("\nEulerian Cycle: ");
printCycle(adj);
System.out.println("\n");
}
}