forked from gyoogle/tech-interview-for-developer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2020-02-05 [Algorithm] DFS & BFS Add
- Loading branch information
gyusuk
committed
Feb 5, 2020
1 parent
1a9ce25
commit a86d68b
Showing
1 changed file
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# DFS & BFS | ||
|
||
<br> | ||
|
||
๊ทธ๋ํ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก, ๋ฌธ์ ๋ฅผ ํ ๋ ์๋นํ ๋ง์ด ์ฌ์ฉํ๋ค. | ||
|
||
๊ฒฝ๋ก๋ฅผ ์ฐพ๋ ๋ฌธ์ ์, ์ํฉ์ ๋ง๊ฒ DFS์ BFS๋ฅผ ํ์ฉํ๊ฒ ๋๋ค. | ||
|
||
<br> | ||
|
||
### DFS | ||
|
||
> ๋ฃจํธ ๋ ธ๋ ํน์ ์์ ๋ ธ๋์์ **๋ค์ ๋ธ๋์น๋ก ๋์ด๊ฐ๊ธฐ ์ ์, ํด๋น ๋ธ๋์น๋ฅผ ๋ชจ๋ ํ์**ํ๋ ๋ฐฉ๋ฒ | ||
**์คํ or ์ฌ๊ทํจ์**๋ฅผ ํตํด ๊ตฌํํ๋ค. | ||
|
||
<br> | ||
|
||
- ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ๋ฐฉ๋ฌธํด์ผ ํ ๊ฒฝ์ฐ ์ฌ์ฉ์ ์ ํฉ | ||
|
||
<img src="https://upload.wikimedia.org/wikipedia/commons/7/7f/Depth-First-Search.gif" width="300"> | ||
|
||
##### ์๊ฐ ๋ณต์ก๋ | ||
|
||
- ์ธ์ ํ๋ ฌ : O(V^2) | ||
- ์ธ์ ๋ฆฌ์คํธ : O(V+E) | ||
|
||
> V๋ ์ ์ , E๋ ๊ฐ์ ์ ๋ปํ๋ค | ||
<br> | ||
|
||
##### Code | ||
|
||
```c | ||
#include <stdio.h> | ||
|
||
int map[1001][1001], dfs[1001]; | ||
|
||
void init(int *, int size); | ||
|
||
void DFS(int v, int N) { | ||
|
||
dfs[v] = 1; | ||
printf("%d ", v); | ||
|
||
for (int i = 1; i <= N; i++) { | ||
if (map[v][i] == 1 && dfs[i] == 0) { | ||
DFS(i, N); | ||
} | ||
} | ||
|
||
} | ||
|
||
int main(void) { | ||
|
||
init(map, sizeof(map) / 4); | ||
init(dfs, sizeof(dfs) / 4); | ||
|
||
int N, M, V; | ||
scanf("%d%d%d", &N, &M, &V); | ||
|
||
for (int i = 0; i < M; i++) | ||
{ | ||
int start, end; | ||
scanf("%d%d", &start, &end); | ||
map[start][end] = 1; | ||
map[end][start] = 1; | ||
} | ||
|
||
DFS(V, N); | ||
|
||
return 0; | ||
} | ||
|
||
void init(int *arr, int size) { | ||
for (int i = 0; i < size; i++) | ||
{ | ||
arr[i] = 0; | ||
} | ||
} | ||
``` | ||
<br> | ||
<br> | ||
### BFS | ||
> ๋ฃจํธ ๋ ธ๋ ๋๋ ์์ ๋ ธ๋์์ **์ธ์ ํ ๋ ธ๋๋ถํฐ ๋จผ์ ํ์**ํ๋ ๋ฐฉ๋ฒ | ||
**ํ**๋ฅผ ํตํด ๊ตฌํํ๋ค. (ํด๋น ๋ ธ๋์ ์ฃผ๋ณ๋ถํฐ ํ์ํด์ผํ๊ธฐ ๋๋ฌธ) | ||
<br> | ||
- ์ต์ ๋น์ฉ(์ฆ, ๋ชจ๋ ๊ณณ์ ํ์ํ๋ ๊ฒ๋ณด๋ค ์ต์ ๋น์ฉ์ด ์ฐ์ ์ผ ๋)์ ์ ํฉ | ||
<img src="https://upload.wikimedia.org/wikipedia/commons/5/5d/Breadth-First-Search-Algorithm.gif" width="300"> | ||
##### ์๊ฐ ๋ณต์ก๋ | ||
- ์ธ์ ํ๋ ฌ : O(V^2) | ||
- ์ธ์ ๋ฆฌ์คํธ : O(V+E) | ||
##### Code | ||
```c | ||
#include <stdio.h> | ||
int map[1001][1001], bfs[1001]; | ||
int queue[1001]; | ||
void init(int *, int size); | ||
void BFS(int v, int N) { | ||
int front = 0, rear = 0; | ||
int pop; | ||
printf("%d ", v); | ||
queue[rear++] = v; | ||
bfs[v] = 1; | ||
while (front < rear) { | ||
pop = queue[front++]; | ||
for (int i = 1; i <= N; i++) { | ||
if (map[pop][i] == 1 && bfs[i] == 0) { | ||
printf("%d ", i); | ||
queue[rear++] = i; | ||
bfs[i] = 1; | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
int main(void) { | ||
init(map, sizeof(map) / 4); | ||
init(bfs, sizeof(bfs) / 4); | ||
init(queue, sizeof(queue) / 4); | ||
int N, M, V; | ||
scanf("%d%d%d", &N, &M, &V); | ||
for (int i = 0; i < M; i++) | ||
{ | ||
int start, end; | ||
scanf("%d%d", &start, &end); | ||
map[start][end] = 1; | ||
map[end][start] = 1; | ||
} | ||
BFS(V, N); | ||
return 0; | ||
} | ||
void init(int *arr, int size) { | ||
for (int i = 0; i < size; i++) | ||
{ | ||
arr[i] = 0; | ||
} | ||
} | ||
``` | ||
|
||
<br> | ||
|
||
**์ฐ์ต๋ฌธ์ ** : [[BOJ] DFS์ BFS](https://www.acmicpc.net/problem/1260) | ||
|
||
<br> | ||
|
||
##### [์ฐธ๊ณ ์๋ฃ] | ||
|
||
- [๋งํฌ](https://developer-mac.tistory.com/64) |