-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.java
More file actions
60 lines (50 loc) · 1.42 KB
/
Program.java
File metadata and controls
60 lines (50 loc) · 1.42 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
package practice.bfs;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class Program {
public static void main(String[] args) {
/*
* (1)
* / | \
* (2) (3) (4)
* / \ / | \
* (5) (6)(7)(8)(9)
*/
final int[][] edges = { // 노드는 1번부터 시작
{1, 2}, {1, 3}, {1, 4},
{2, 5}, {2, 6},
{4, 7}, {4, 8}, {4, 9},
};
final int nodeCount = 9;
// 그래프를 자료형에 저장
List<List<Integer>> graph = new ArrayList<>();
for (int count = 0; count <= nodeCount; count++) graph.add(new ArrayList<>());
for (int[] edge : edges) graph.get(edge[0]).add(edge[1]);
// BFS
Queue<Integer> bfsQueue = new ArrayDeque<>();
bfsQueue.offer(1);
System.out.println("BFS");
while (!bfsQueue.isEmpty()) {
int nodeIndex = bfsQueue.poll();
System.out.println(nodeIndex);
for (int child : graph.get(nodeIndex)) {
bfsQueue.offer(child);
}
}
System.out.println();
// DFS
Stack<Integer> dfsStack = new Stack<>();
dfsStack.push(1);
System.out.println("DFS");
while (!dfsStack.isEmpty()) {
int nodeIndex = dfsStack.pop();
System.out.println(nodeIndex);
for (int child : graph.get(nodeIndex)) {
dfsStack.push(child);
}
}
}
}