-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS와DFS.java
More file actions
70 lines (61 loc) · 1.91 KB
/
BFS와DFS.java
File metadata and controls
70 lines (61 loc) · 1.91 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
import java.sql.Array;
import java.util.*;
import java.io.*;
public class BFS와DFS {
static ArrayList<Integer>[] A;
static Queue<Integer> que;
static boolean visited[];
static int N, M, v;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
N = Integer.parseInt(st.nextToken()); // 노드
M = Integer.parseInt(st.nextToken()); // 간선
v = Integer.parseInt(st.nextToken()); // 탐색 시작 번호
A = new ArrayList[N+1];
que = new LinkedList<>();
for(int i=1;i<=N;i++){
A[i] = new ArrayList<Integer>();
}
for(int i=0;i<M;i++){
st = new StringTokenizer(in.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
A[a].add(b);
A[b].add(a);
}
for(int i=1; i<=N; i++){
Collections.sort(A[i]);
}
visited = new boolean[N+1];
visited[v] = true;
dfs(0, v);
System.out.println();
visited = new boolean[N+1];
bfs(v);
}
private static void dfs(int depth, int start){
System.out.print(start+ " ");
for(int i : A[start]){
if(!visited[i]){
visited[i] = true;
dfs(depth +1, i);
}
}
}
private static void bfs(int start){
que.offer(start);
visited[start] = true;
while(!que.isEmpty()){
int now = que.poll();
System.out.print(now + " ");
for( int i : A[now]){
if(!visited[i]){
visited[i] = true;
que.offer(i);
}
}
}
}
}