Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 1 addition & 219 deletions Algorithm/BFS & DFS.md
Original file line number Diff line number Diff line change
@@ -1,219 +1 @@
## BFS & DFS

๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์ƒ๋‹นํžˆ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ๋…์ด๋‹ค.

๋ฌธ์ œ์˜ ์ƒํ™ฉ์— ๋งž๊ฒŒ BFS์™€ DFS๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ๋Š” ๋ฐฑ์ค€์— ์žˆ๋Š” [DFS์™€ BFS](https://www.acmicpc.net/problem/1260) ๋ฌธ์ œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค.

> V : ์ •์ , E : ๊ฐ„์„ 

#### BFS

- ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์ด๋ผ ํ•˜๋ฉฐ BFS(Breadth-First Search)๋ผ ๋ถ€๋ฅธ๋‹ค.
- ๋ฃจํŠธ ๋…ธ๋“œ ํ˜น์€ ์ž„์˜์˜ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•.
- ์‹œ์ž‘ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์ •์ ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š” ์ •์ ์„ ๋‚˜์ค‘์— ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœํšŒ ๋ฐฉ๋ฒ•์ด๋‹ค.
- ์ฆ‰, ๊นŠ๊ฒŒ ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ๋„“๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
- ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ฃผ๋ณ€๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.)
- ์ตœ์†Œ ๋น„์šฉ(์ฆ‰, ๋ชจ๋“  ๊ณณ์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ตœ์†Œ ๋น„์šฉ์ด ์šฐ์„ ์ผ ๋•Œ)์— ์ ํ•ฉํ•˜๋‹ค.

![img](https://camo.githubusercontent.com/b8073f26dfdf1644e8a92312fff100341987a8f5/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f352f35642f427265616474682d46697273742d5365617263682d416c676f726974686d2e676966)

- ์‹œ๊ฐ„ ๋ณต์žก๋„
- ์ธ์ ‘ ํ–‰๋ ฌ : O(V^2)
- ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ : O(V+E)



**[Code]**

```java
import java.io.*;
import java.util.*;

public class Main {
private static final String SPACE = " ";
private static ArrayList<Integer>[] a;
private static boolean[] visit;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String[] input = br.readLine().split(SPACE);
int n = convert(input[0]); // ์ •์ ์˜ ๊ฐœ์ˆ˜
int m = convert(input[1]); // ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
int start = convert(input[2]); // ์‹œ์ž‘ํ•  ์ •์  ๋ฒˆํ˜ธ

// ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”.
a = new ArrayList[n + 1];

for (int i = 1; i <= n; i++) {
a[i] = new ArrayList<>();
}

for (int j = 0; j < m; j++) {
String[] inputs = br.readLine().split(SPACE);
int u = convert(inputs[0]);
int v = convert(inputs[1]);

// ์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ผ ๊ฒฝ์šฐ ์–‘์ชฝ ๋‹ค ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
a[u].add(v);
a[v].add(u);
}

// ๋ฐฉ๋ฌธํ•  ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๋ ฌํ•œ๋‹ค.
for (int i = 1; i <= n; i++) {
Collections.sort(a[i]);
}

visit = new boolean[n + 1];
bfs(start);
System.out.println();
}

private static int convert(String command) {
return Integer.parseInt(command);
}

private static void bfs(int start) {
LinkedList<Integer> queue = new LinkedList<>();

visit[start] = true;
queue.add(start);

while (!queue.isEmpty()) {
int x = queue.remove(); // ํ์—์„œ ์ •์ ์„ ๋บ€๋‹ค.

System.out.print(x + SPACE);

for (int y : a[x]) {
// ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.
if (!visit[y]) {
// ํ•ด๋‹น ์ •์ ์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  true ๋กœ ์ฒดํฌํ•œ๋‹ค.
// ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์ •์ ์„ ํ์— ๋„ฃ๋Š”๋‹ค.
visit[y] = true;
queue.add(y);
}
}
}
}
}
// ์ž…๋ ฅ
5 5 3
5 4
5 2
1 2
3 4
3 1
// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
3 1 4 2 5
```



#### DFS

- ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์ด๋ฉฐ DFS(Depth-First Search)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
- ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•œ๋‹ค.
- ๋„“๊ฒŒ ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ๊นŠ๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
- ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฏธ๋กœ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊ณ„์† ๊ฐ€๋‹ค๊ฐ€ ๋” ์ด์ƒ ๊ฐˆ ์ˆ˜ ์—†๊ฒŒ ๋˜๋ฉด ๋‹ค์‹œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฐˆ๋ฆผ๊ธธ๋กœ ๋Œ์•„์™€์„œ ์ด๊ณณ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌํ•˜๋‹ค.
- ์Šคํƒ์ด๋‚˜ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•œ๋‹ค.
- ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ์— ์ ํ•ฉํ•˜๋‹ค.

![img](https://camo.githubusercontent.com/aaad9e39961daf34d967c616edeb50abf3bf1235/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f372f37662f44657074682d46697273742d5365617263682e676966)

- ์‹œ๊ฐ„ ๋ณต์žก๋„
- ์ธ์ ‘ ํ–‰๋ ฌ : O(V^2)
- ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ : O(V+E)



**[Code]**

```java
import java.io.*;
import java.util.*;

/**
* created by victory_woo on 02/04/2019
* DFS์™€ BFS ๋ณต์Šต
*/
public class BOJ1260_RE {
private static final String SPACE = " ";
private static ArrayList<Integer>[] a;
private static boolean[] visit;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String[] input = br.readLine().split(SPACE);
int n = convert(input[0]); // ์ •์ ์˜ ๊ฐœ์ˆ˜
int m = convert(input[1]); // ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
int start = convert(input[2]); // ์‹œ์ž‘ํ•  ์ •์  ๋ฒˆํ˜ธ

// ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”.
a = new ArrayList[n + 1];
visit = new boolean[n + 1];

for (int i = 1; i <= n; i++) {
a[i] = new ArrayList<>();
}

for (int j = 0; j < m; j++) {
String[] inputs = br.readLine().split(SPACE);
int u = convert(inputs[0]);
int v = convert(inputs[1]);

// ์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ผ ๊ฒฝ์šฐ ์–‘์ชฝ ๋‹ค ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
a[u].add(v);
a[v].add(u);
}

// ๋ฐฉ๋ฌธํ•  ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๋ ฌํ•œ๋‹ค.
for (int i = 1; i <= n; i++) {
Collections.sort(a[i]);
}
dfs(start);
}

private static int convert(String command) {
return Integer.parseInt(command);
}

private static void dfs(int x) {
// ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.
if (visit[x]) {
return;
}

visit[x] = true;
// ๋ฐฉ๋ฌธํ•œ ์ˆœ์„œ ์ถœ๋ ฅ
System.out.print(x+" ");

for (int y : a[x]) {
if (!visit[y]) {
dfs(y);
}
}

}
}
// ์ž…๋ ฅ
5 4 5
5 4
4 3
4 2
1 5
// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
5 1 4 2 3

// ์ž…๋ ฅ
5 5 3
5 4
5 2
1 2
3 4
3 1
// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
3 1 2 5 4
```
[MIT ๋ผ์ด์„ ์Šค์— ๋”ฐ๋ฅธ ์ถœ์ฒ˜ ํ‘œ๊ธฐ](https://github.com/WooVictory/Ready-For-Tech-Interview)