[Algorithm]DFS์ BFS
๐ฆฅ ๋ณธ๋ฌธ
https://www.acmicpc.net/problem/1260

์คํ์ผ๋ก๋ก ํผ DFS
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
| package backjoon_1260;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_withStack {
static StringBuilder sb = new StringBuilder();
static boolean[] check; // ๋ฐฉ๋ฌธ ์ฒดํฌ
static ArrayList<Integer>[] adjList; // ์ธ์ ๋ฆฌ์คํธ
static int node, line, start; // ๋
ธ๋ ์, ๊ฐ์ ์, ์์ ๋
ธ๋
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
node = Integer.parseInt(st.nextToken());
line = Integer.parseInt(st.nextToken());
start = Integer.parseInt(st.nextToken());
// ์ธ์ ๋ฆฌ์คํธ ์ด๊ธฐํ
adjList = new ArrayList[node + 1];
for (int i = 1; i <= node; i++) {
adjList[i] = new ArrayList<>();
}
// ๊ทธ๋ํ ์
๋ ฅ
for (int i = 0; i < line; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
adjList[a].add(b);
adjList[b].add(a);
}
// ๋ฐฉ๋ฌธ ์์๋ฅผ ์ํด ์ ๋ ฌ
for (int i = 1; i <= node; i++) {
Collections.sort(adjList[i]);
}
// DFS (์คํ ์ด์ฉ)
check = new boolean[node + 1];
dfsWithStack(start);
// BFS
sb.append("\n");
check = new boolean[node + 1];
bfs(start);
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
System.out.println(sb);
}
// ์คํ์ ์ด์ฉํ DFS
private static void dfsWithStack(int start) {
Stack<Integer> stack = new Stack<>();
stack.push(start); // ์์ ๋
ธ๋ ์ฝ์
while (!stack.isEmpty()) {
int cur = stack.pop(); // ์คํ์์ ๋
ธ๋ ๊บผ๋
if (!check[cur]) { // ๋ฐฉ๋ฌธํ์ง ์์ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
check[cur] = true;
sb.append(cur).append(" ");
// ์ธ์ ๋
ธ๋๋ฅผ ์คํ์ ์ถ๊ฐ (์ญ์์ผ๋ก ์ถ๊ฐํด ์์ ๋ฒํธ ์ฐ์ ํ์)
for (int i = adjList[cur].size() - 1; i >= 0; i--) {
int next = adjList[cur].get(i);
if (!check[next]) {
stack.push(next);
}
}
}
}
}
// ํ๋ฅผ ์ด์ฉํ BFS
private static void bfs(int start) {
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
check[start] = true;
while (!queue.isEmpty()) {
int cur = queue.poll();
sb.append(cur).append(" ");
for (int next : adjList[cur]) {
if (!check[next]) {
queue.add(next);
check[next] = true;
}
}
}
}
}
|
์ฌ๊ท๋ก ํผ DFS
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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static boolean[] check;
static int[][] arr;
static int node,line,start;
static Queue<Integer> q= new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
node = Integer.parseInt(st.nextToken());
line = Integer.parseInt(st.nextToken());
start = Integer.parseInt(st.nextToken());
arr = new int[node+1][node+1];
check = new boolean[node+1];
for(int i = 0 ; i < line ; i++ ){
StringTokenizer str = new StringTokenizer(br.readLine());
int a = Integer.parseInt(str.nextToken());
int b = Integer.parseInt(str.nextToken());
arr[a][b] = arr[b][a] = 1;
}
dfs(start);
sb.append("\n");
check = new boolean[node+1];
bfs(start);
System.out.println(sb);
}
private static void dfs(int start) {
check[start] = true;
sb.append(start + " ");
for(int i = 1 ; i <= node ; i++){
if(arr[start][i] == 1 && check[i] == false){
dfs(i);
}
}
}
private static void bfs(int start) {
q.add(start);
check[start] = true;
while(!q.isEmpty()){
start= q.poll();
sb.append(start + " ");
for(int i = 1 ; i <= node ; i++){
if(arr[start][i] == 1 && check[i] == false){
q.add(i);
check[i] = true;
}
}
}
}
}
|
๋๋น ์ฐ์ ํ์(BFS,Breadth-First Search)
- ์์ ์ ์ ๋ฐฉ๋ฌธ ํ ๊ฐ๊น์ด ์ ์ ์ฐ์ ๋ฐฉ๋ฌธ
- ๋๊ฒ ํ์ํ๋ ๋ฐฉ๋ฒ
- ๋ ๋
ธ๋ ์ฌ์ด ์ต๋จ๊ฑฐ๋ฆฌ , ์ต๋จ ๊ฒฝ๋ก ๊ตฌํ ๋ ์์ฃผ ์ฌ์ฉ
- ์ฅ์
- ๋จ์
- ์ต์ ์คํ๋ณด๋ค ์ค๋ ๊ฑธ๋ฆฝ๋๋ค.
๊ธฐ๋ณธ ๊ตฌํ
- ์ ์ v์ ๋ฐฉ๋ฌธํ๋ค.
2.์ ์ v์ ์ธ์ ํ ์ ์ ์ค ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์ ์ฐจ๋ก๋ก ๋ฐฉ๋ฌธํ๋ฉด์ ํ์ ๋ฃ๋๋ค.
3.์ธ์ ํฉ ์ ์ ๋ชจ๋ ๋ฐฉ๋ถํ๋ค๋ฉด ํ์์ dequeueํ์ฌ ๋ฐ์ ๊ฐ ์ ์ v๋ก ์ค์ ํ๊ณ 2๋ฅผ ๋ฐ๋ณตํ๋ค.
4.ํ๊ฐ ๊ณต๋ฐฑ์ด๋ผ๋ฉด ํ์ ์๋ฃํ ๊ฒ์ด๋ค.
static boolean[] visit;
static LinkedList<Integer>[] graph;
static int[][] graph;
//์์ ์ ์ v
Public static void bfs(int v){
Queue<Integer> queue = new LinkedList<>();
queue.add(v);
visit[v] = true;
while(!queue.isEmpty()){
int temp = queue.poll();
System.out.println(temp);
for(int nextV: graph[temp]){
if(!visit[nextV]){
queue.add(nextV);
visit[nextV] = true;
}
}
}
}
๊น์ด ์ฐ์ ํ์(DFS,Dreadth-First Search)
- ์์ ์ ์ ๋ฐฉ๋ฌธ ํ ๋ค์ ๋ถ๊ธฐ๋ก ๋์ด๊ฐ๊ธฐ ์ ํด๋น ๋ถ๊ธฐ ์๋ฒฝํ๊ฒ ํ์
- ํธ๋ฆฌ์์ ๋ณด๋ฉด ๋
ธ๋ ๋ฐฉ๋ฌธ ํ ์์ ๋
ธ๋ ์ฐ์ ๋ฐฉ๋ฌธ
- ๊น์ด ํ์ํ๋ ๋ฐฉ๋ฒ
- ์ฅ์
- ์ต์ ์ ๊ฒฝ์ฐ ๋น ๋ฅด๊ฒ ํด๋ฅผ ์ฐพ์
- ๋จ์
- ์ฐพ์ ํด๊ฐ ์ต์ ํด๊ฐ ์๋ ๊ฐ๋ฅ์ฑ ์์
- ์ต์
์ ๊ฒฝ์ฐ ํด ์ฐพ๋๋ฐ ๊ฐ์ฅ ์ค๋ ์๊ฐ ๊ฑธ๋ฆผ
๊ธฐ๋ณธ ๊ตฌํ
1.์ ์ v๋ฅผ ๋ฐฉ๋ถํ๋ค.
2.์ ์ v์์ ์ธ์ ํ ์ ์ ์ค์ ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ w๊ฐ ์๋ค๋ฉด w๋ฅผ v๋ก ํ์ฌ 1๋ถํฐ ๋ฐ๋ณตํ๋ค(์ฌ๊ทํจ์ ํธ์ถ)
3.์ธ์ ํ ์ ์ ๋ชจ๋ ๋ฐฉ๋ฌธํ๋ค๋ฉด ์คํ์์ ์ ์ ์ ๊บผ๋ด ์๋ฅผ ๋ฐ๋ณตํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
| //์์ ์ ์ v
static boolean[] visit;
//์ฐ๊ฒฐ ๋ฆฌ์คํธ, ํ๋ ฌ ๊ทธ๋ํ ์ค ์ ํ
static LinkedList<Integer>[] graph;
static int[][]graph;
public static void dfs(int v){
visit[v] = true;
System.out.println(v);
for(int nextV: graph[v]){
if(!visit[nextV]defs (nextV));
}
}
|
์๊ฐ ๋ณต์ก๋
- BFS DFS ๋์ ์๊ฐ ๋ณต์ก๋๋ ๋์ผํ๋ฉฐ , ๊ทธ๋ํ ๊ตฌํ์ ๋ฐ๋ผ ์๊ฐ ๋ณต์ก๋๊ฐ ๋ฌ๋ผ์ง
- ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ๊ตฌํํ ๊ทธ๋ํ:O(n+m)
- ํ๋ ฌ๋ก ๊ตฌํํ ๊ทธ๋ํ : O(n^2)
Leave a comment