-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphListNode.java
More file actions
93 lines (84 loc) · 1.46 KB
/
GraphListNode.java
File metadata and controls
93 lines (84 loc) · 1.46 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
package algorithm;
import java.util.*;
public class GraphListNode {
static int N;
static Node[] g;
static boolean[] v;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
int E = sc.nextInt();
g = new Node[N];
v = new boolean[N];
for (int i = 0; i < E; i++) {
int from = sc.nextInt();
int toto = sc.nextInt();
g[from] = new Node(toto, g[from]);
g[toto] = new Node(from, g[toto]);
}
for (Node a : g)
System.out.println(a);
System.out.println();
// bfs(0);
dfs(0);
sc.close();
}
static void bfs(int i) {
ArrayDeque<Integer> q = new ArrayDeque<>();
v[i] = true;
q.offer(i);
while (!q.isEmpty()) {
i = q.poll();
System.out.print((char) (i + 'A') + " ");
for (Node j = g[i]; j != null; j = j.link) {
if (!v[j.vertex]) {
v[j.vertex] = true;
q.offer(j.vertex);
}
}
}
}
static void dfs(int i) {
v[i] = true;
System.out.print((char) (i + 'A') + " ");
for (Node j = g[i]; j != null; j = j.link) {
if (!v[j.vertex])
dfs(j.vertex);
}
}
static class Node {
int vertex;
Node link;
Node(int vertex, Node link) {
this.vertex = vertex;
this.link = link;
}
@Override
public String toString() {
return (char) (vertex + 'A') + "(" + vertex + ")->" + link;
}
}
}
/*
* 7
* 8
* 0 1
* 0 2
* 1 3
* 1 4
* 2 4
* 3 5
* 4 5
* 5 6
*
* A0
* / \
* B1 C2
* / \ /
* D3 E4
* \ /
* F5 - G6
*
* =dfs========
* A B D F E C G
*/