Skip to content

Commit ebeda7f

Browse files
Fleury Algo added in Java (iuliagroza#70)
* Fleury Algo added in Java * Required changes fixed * Update Fleury.java --------- Co-authored-by: Riyazul555 <riyazulislam2003@gmail.com>
1 parent be98321 commit ebeda7f

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Fleury/Fleury.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This code is contributed by Riyazul555
2+
3+
// Time Complexity = O(V + E) where V is the number of vertices and E is the number of edges.
4+
// Space Complexity = O(V + E) where V is the number of vertices and E is the number of edges.
5+
6+
import java.io.*;
7+
import java.util.*;
8+
9+
public class EulerPath {
10+
11+
static void euler(int nod, ArrayList<Pair<Integer, Integer>>[] G, ArrayList<Integer> e, BitSet viz) {
12+
while (!G[nod].isEmpty()) {
13+
Pair<Integer, Integer> it = G[nod].remove(G[nod].size() - 1);
14+
if (!viz.get(it.second)) {
15+
viz.set(it.second);
16+
euler(it.first, G, e, viz);
17+
}
18+
}
19+
e.add(nod);
20+
}
21+
22+
public static void main(String[] args) throws IOException {
23+
BufferedReader fin = new BufferedReader(new FileReader(args[0]));
24+
BufferedWriter fout = new BufferedWriter(new FileWriter(args[1]));
25+
26+
StringTokenizer st = new StringTokenizer(fin.readLine());
27+
int n = Integer.parseInt(st.nextToken());
28+
int m = Integer.parseInt(st.nextToken());
29+
30+
ArrayList<Pair<Integer, Integer>>[] G = new ArrayList[n + 1];
31+
for (int i = 1; i <= n; i++) {
32+
G[i] = new ArrayList<>();
33+
}
34+
35+
for (int i = 1; i <= m; i++) {
36+
st = new StringTokenizer(fin.readLine());
37+
int x = Integer.parseInt(st.nextToken());
38+
int y = Integer.parseInt(st.nextToken());
39+
G[x].add(new Pair<>(y, i));
40+
G[y].add(new Pair<>(x, i));
41+
}
42+
43+
ArrayList<Integer> e = new ArrayList<>();
44+
BitSet viz = new BitSet(m);
45+
euler(1, G, e, viz);
46+
47+
for (int i = e.size() - 1; i > 0; i--) {
48+
fout.write(e.get(i) + " ");
49+
}
50+
51+
fin.close();
52+
fout.close();
53+
}
54+
55+
static class Pair<A, B> {
56+
A first;
57+
B second;
58+
59+
public Pair(A first, B second) {
60+
this.first = first;
61+
this.second = second;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)