๐Ÿ“ Coding Test Study/C++

[Algorithm][C++] BFS/DFS

ibelieveinme 2023. 4. 25. 23:45
728x90

< BFS(Breath First Search): ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ >

 

Queue์™€ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•  ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(Queue๋Š” ์„ ์ž…์„ ์ถœ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ๋จผ์ € ๋„ฃ์€ ๊ฐ’์„ ๋จผ์ € ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋„“๊ฒŒ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋Š” BFS๋กœ ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค)

 

1. queue์— 0๋ฒˆ ๋…ธ๋“œ๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.

int visit []

0 0 0 0 0

queue<int>

0
 
 
 
 
 

 

2. ํ•œ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด visit ๋ฐฐ์—ด์— ํ˜„์žฌ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
0
0
0
0
0

queue<int>

0
 
 
 
 
 

 

3. queue์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์ธ 0๋ฒˆ๋…ธ๋“œ๋ฅผ ๋นผ์„œ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, 0๋ฒˆ ๋…ธ๋“œ์˜ ์ด์›ƒ ๋…ธ๋“œ์ธ 1๋ฒˆ, 2๋ฒˆ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋œ ์ ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ 

๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†์œผ๋ฉด queue์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
1
0
0
0

queue<int>

1
2
 
 
 
 

 

4. queue์˜ ๋งจ ์•ž ๊ฐ’์ธ 1๋ฒˆ ๋…ธ๋“œ๋ฅผ ๋นผ์„œ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

1๋ฒˆ ๋…ธ๋“œ์˜ ์ด์›ƒ๋…ธ๋“œ(3, 4)๋ฅผ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†์œผ๋ฉด queue์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
1
1
1
0

queue<int>

2
3
4
 
 
 

 

5. queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

 

[ C++ ์ฝ”๋“œ ]

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> v[1001];
int visit_bfs[1001] = { 0, };

void bfs(int s) {
	queue<int> q;
	q.push(s); //ํ์— ๋„ฃ๊ณ 
	visit_bfs[s] = true; //๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌ

	while (!q.empty()) { //ํ๊ฐ€ ๋ชจ๋‘ ๋น„์›Œ์งˆ ๋•Œ ๊นŒ์ง€
		int x = q.front(); //ํ์˜ ๋งจ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ๋นผ์„œ ํ™•์ธ
		q.pop(); //ํ์—์„œ ์ œ๊ฑฐ
		cout << x << " ";
		for (int i = 0; i < v[x].size(); i++) //ํ˜„์žฌ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ ํฌ๊ธฐ๊นŒ์ง€
		{
			int y = v[x][i]; //์ธ์ ‘๋…ธ๋“œ ๊ฐ’์„ ์ €์žฅ
			if (!visit_bfs[y]) { //์ธ์ ‘๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ ์•ˆํ•œ ๋…ธ๋“œ์ด๋ฉด
				q.push(y); //ํ์— ์‚ฝ์ž…
				visit_bfs[y] = true; //๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌ
			}
		}
	}
}

int main() {
	int V, E, S; //๋…ธ๋“œ, ์—ฃ์ง€, ์‹œ์ž‘๋…ธ๋“œ
	cin >> V >> E >> S;

	int v1, v2;
	for(int i = 1; i <= E; i++) {
		cin >> v1 >> v2; //์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฒกํ„ฐ๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ
		v[v1].push_back(v2);
		v[v2].push_back(v1);
	}
	for (int i = 1; i < V; i++) { //์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
		sort(v[i].begin(), v[i].end());
	}
	bfs(S);
}
 

 

< DFS(Depth First Search): ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ >

 

Stack or ์žฌ๊ท€ํ˜ธ์ถœ๊ณผ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•  ๋ฐฐ์—ด ์ด์šฉ

(๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์— ์ด์›ƒ๋…ธ๋“œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ๊นŠ์ด ์žˆ๊ฒŒ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ›„์ž…์„ ์ถœ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ Stack or ์žฌ๊ท€ํ˜ธ์ถœ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค)

 

1. ์Šคํƒ์— 0๋ฒˆ ๋…ธ๋“œ(์‹œ์ž‘๋…ธ๋“œ)๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ , 0๋ฒˆ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
0
0
0
0
0

stack <int>

0
 
 
 
 
 

 

2. 0๋ฒˆ ๋…ธ๋“œ์— ์ด์›ƒ๋…ธ๋“œ 1, 2๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์šฐ์„ ์ˆœ์œ„์— ์˜ํ•ด stack์— 1๋ฒˆ์„ ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
0
0
0
0

stack <int>

0
1
 
 
 
 

 

3. 1๋ฒˆ ๋…ธ๋“œ์— ์ด์›ƒํ•œ ๋…ธ๋“œ์ธ 3, 4๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์šฐ์„ ์ˆœ์œ„์— ์˜ํ•ด 3๋ฒˆ์„ stack์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
0
1
0
0
stack <int>
0
1
3
 
 
 

 

4. 3๋ฒˆ๋…ธ๋“œ์— ์ด์›ƒํ•œ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ 3๋ฒˆ๋…ธ๋“œ๋ฅผ stack์—์„œ popํ•˜์—ฌ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
0
1
0
0
stack <int>
0
1
 
 
 
 

 

5. 1๋ฒˆ๋…ธ๋“œ์— ์ด์›ƒํ•œ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ 1๋ฒˆ๋…ธ๋“œ๋ฅผ stack์—์„œ popํ•˜์—ฌ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
0
1
0
0

stack <int>

0
 
 
 
 
 

 

6. 0๋ฒˆ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ด์›ƒ๋…ธ๋“œ 2๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ stack์— 2๋ฒˆ์„ ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.

 

int visit []

1
1
1
1
0
0

stack <int>

0
2
 
 
 
 

 

7. stack์ด ๋นŒ ๋•Œ๊นŒ์ง€ ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

 

[ C++ ์ฝ”๋“œ ]

1) Stack์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

vector<int> v[1001];//๋…ธ๋“œ ์ •๋ณด๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜
int visit_dfs[1001] = { 0, };//๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜

void dfs(int s) {
	stack<int> stk;

	visit_dfs[s] = true;//์‹œ์ž‘๋…ธ๋“œ ๋ฐฉ๋ฌธํ‘œ์‹œ!
	stk.push(s);  //stack์— ์‚ฝ์ž…

	cout << s << " ";

	while (!stk.empty()) { //stack์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
		int x = stk.top(); //stack์˜ ๋งจ ์œ„ ๋…ธ๋“œ ๊ฐ’์„ ํƒ์ƒ‰
		stk.pop(); //๋งจ ์œ„ ๋…ธ๋“œ ๋ฒ„๋ฆฌ๊ธฐ

		for (int i = 0; i < v[x].size(); i++) //x์˜ ์ด์›ƒ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊นŒ์ง€ ๋ฐ˜๋ณต
		{
			int y = v[x][i]; //์ด์›ƒ๋…ธ๋“œ๋ฅผ y๋กœ ์ง€์ •
			if (!visit_dfs[y]) { //y๋ฅผ ๋ฐฉ๋ฌธํ•œ์ ์ด ์—†์œผ๋ฉด
				cout << y << " ";
				visit_dfs[y] = true; //๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ์ฒดํฌ
				stk.push(x); //popํ–ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ stack์— x ์‚ฝ์ž…ํ•ด์„œ ๊ฒ€์‚ฌ(x์˜ ์ด์›ƒ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์•ผํ•˜๋‹ˆ๊น)
				stk.push(y); //stack์— y๋„ ์‚ฝ์ž…
				break;
			}
		}
	}
}

int main() {
	int V, E, S;//๋…ธ๋“œ, ์—ฃ์ง€, ์‹œ์ž‘๋…ธ๋“œ
	cin >> V >> E >> S;

	int v1, v2;
	for (int i = 1; i <= E; i++) {//์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฒกํ„ฐ๋กœ ์ด์–ด์ฃผ๊ธฐ
		cin >> v1 >> v2;
		v[v1].push_back(v2);
		v[v2].push_back(v1);
	}
	for (int i = 1; i < V; i++) {//์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
		sort(v[i].begin(), v[i].end());
	}
	dfs(S);
}
 

 

 

2) ์žฌ๊ท€ํ˜ธ์ถœ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

vector<int> graph[1001];
bool check[10001];

void dfs(int v) {
	check[v] = true;//๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌ
	cout << v << " ";

	for (int i = 0; i < graph[v].size(); i++) {//์ด์›ƒ ๋…ธ๋“œ ํƒ์ƒ‰
		int next = graph[v][i];
		if (check[next] == false) {//์ด์›ƒ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ์•ˆํ•œ ๋…ธ๋“œ๋ผ๋ฉด
			dfs(next);//๊ทธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•จ์œผ๋กœ์จ DFSํƒ์ƒ‰!
		}
	}
}

int main() {
	int V, E, S; //๊ฐ„์„  ์ˆ˜, ์—ฃ์ง€ ์ˆ˜, ์‹œ์ž‘๋…ธ๋“œ
	cin >> V >> E >> S;

	int v1, v2;
	for (int e = 0; e < E; e++) {//๋…ธ๋“œ ์—ฐ๊ฒฐ
		cin >> v1 >> v2;
		graph[v1].push_back(v2);
		graph[v2].push_back(v1);
	}
	for (int i = 1; i <= V; i++) {//์ˆœ์ฐจ์ ์ธ ๋…ธ๋“œ์ ‘๊ทผ์„ ์œ„ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ๊ธฐ
		sort(graph[i].begin(), graph[i].end());
	}
	dfs(S);
}
 
728x90