๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[๊ฐœ๋…] DFS์™€ BFS

by nitronium102 2021. 8. 24.

๐Ÿ” ์ •์˜

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

๋ฃจํŠธ ๋…ธํŠธ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•.

๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

์Šคํƒ(Stack) / ์žฌ๊ท€ํ•จ์ˆ˜ ์‚ฌ์šฉ, ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€(isVisited[idx]) ์‚ฌ์šฉ

 

์žฅ์ 

  - ๋‹จ์ง€ ํ˜„ ๊ฒฝ๋กœ์ƒ์˜ ๋…ธ๋“œ๋งŒ์„ ๊ธฐ์–ตํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ €์žฅ๊ณต๊ฐ„์˜ ์ˆ˜์š”๊ฐ€ ๋น„๊ต์  ์ ๋‹ค.

  - ๋ชฉํ‘œ๋…ธ๋“œ๊ฐ€ ๊นŠ์€ ๋‹จ๊ณ„์— ์žˆ์„ ๊ฒฝ์šฐ ํ•ด๋ฅผ ๋นจ๋ฆฌ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋‹จ์ 

  - ํ•ด๊ฐ€ ์—†๋Š” ๊ฒฝ๋กœ์— ๊นŠ์ด ๋น ์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹ค์ œ์˜ ๊ฒฝ์šฐ ๋ฏธ๋ฆฌ ์ง€์ •ํ•œ ์ž„์˜์˜ ๊นŠ์ด๊นŒ์ง€๋งŒ ํƒ์ƒ‰ํ•˜๊ณ  ๋ชฉํ‘œ๋…ธ๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋‹ค์Œ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

-> ํƒ์ƒ‰ ๊ณผ์ •์ด ๋„ˆ๋ฌด ๊นŠ๊ฒŒ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ๊นŠ์ด ์ œํ•œ์„ ์‚ฌ์šฉํ•œ๋‹ค.

  - ์–ป์–ด์ง„ ํ•ด๊ฐ€ ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๋œ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†๋‹ค. ์ด๋Š” ๋ชฉํ‘œ์— ์ด๋ฅด๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋‹ค์ˆ˜์ธ ๋ฌธ์ œ์— ๋Œ€ํ•ด DFS๋Š” ํ•ด์— ๋‹ค๋‹ค๋ฅด๋ฉด ํƒ์ƒ‰์„ ๋๋‚ด๋ฒ„๋ฆฌ๋ฏ€๋กœ, ์ด๋•Œ ์–ป์–ด์ง„ ํ•ด๋Š” ์ตœ์ ์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

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

๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ or ์ž„์˜์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
ํ(Queue), ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€(isVisited[idx]) ์ด์šฉ

 

 

์žฅ์ 

  - ์ถœ๋ฐœ๋…ธ๋“œ์—์„œ ๋ชฉํ‘œ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ธธ์ด ๊ฒฝ๋กœ๋ฅผ ๋ณด์žฅ

 

๋‹จ์ 

  - ๊ฒฝ๋กœ๊ฐ€ ๋งค์šฐ ๊ธธ ๊ฒฝ์šฐ์—๋Š” ํƒ์ƒ‰ ๊ฐ€์ง€๊ฐ€ ๊ธ‰๊ฒฉํžˆ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ๋ณด๋‹ค ๋งŽ์€ ๊ธฐ์–ต ๊ณต๊ฐ„์„ ํ•„์š”

  - ํ•ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์œ ํ•œ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ, ๋ชจ๋“  ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•œ ํ›„์— ์‹คํŒจ๋กœ ๋๋‚œ๋‹ค.

  - ๋ฌดํ•œ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฒฐ์ฝ” ํ•ด๋ฅผ ์ฐพ์ง€๋„ ๋ชปํ•˜๊ณ , ๋๋‚ด์ง€๋„ ๋ชปํ•œ๋‹ค.

 


๐Ÿ’ป ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

01. DFS

์Šคํƒ

 1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

 2. ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ๊ทธ ๋…ธ๋“œ๋ฅผ ์Šคํƒ์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

    ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์Šคํƒ์—์„œ ์ตœ์ƒ๋‹จ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ธ๋‹ค.

 3. 2๋ฒˆ์˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

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

// stack์— ๋“ค์–ด๊ฐ€๋ฉด ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ
// ํ•ด๋‹น ์œ„์น˜๋ฅผ true๋กœ ํ•ด์ค€๋‹ค.
void dfs(int start, vector<int> graph[], bool check[]){

	stack<int> s;
	s.push(start);
	check[start] = true;
	cout << start << " ";

	while(!s.empty()){

		int current_node = s.top();
		s.pop();
		for(int i=0; i<graph[current_node].size(); i++){
			int next_node = graph[current_node][i];
			if(check[next_node]==false){
				cout << next_node << " ";
				check[next_node] = true;
				// pop()์„ ํ–ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ current_node๋„ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค.
				s.push(current_node);
				s.push(next_node);
				break;
			}
		}
	}
}

 

์žฌ๊ท€

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

// dfs์— ๋“ค์–ด์˜ค๋ฉด '๋ฐฉ๋ฌธ'ํ•œ๊ฑฐ๋กœ ํŒ๋‹จ
// ํ•ด๋‹น ์œ„์น˜์— check true๋กœ ํ•ด์ค€๋‹ค.
void dfs(int start, vector<int> graph[], bool check[]){
	check[start]= true;
	cout << start << " ";

	for(int i=0; i < graph[start].size(); i++){
		int next = graph[start][i];
		// ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
		if(check[next]==false){
			// ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
			dfs(next, graph, check);
		}
	}
}

 

02. BFS

1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ

2. ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ธ ๋’ค์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘ ๋…ธ๋“œ ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ

3. 2๋ฒˆ ๋ฐ˜๋ณต

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

void bfs(int start, vector<int> graph[], bool check[]){
	queue<int> q;

	q.push(start);
	check[start] = true;

	while(!q.empty()){
		int tmp = q.front();
		q.pop();
		cout << tmp << " ";
		for(int i=0; i<graph[tmp].size(); i++){

			// ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
			if(check[graph[tmp][i]] == false){
				// ํ์— ๋„ฃ์–ด์ฃผ๊ณ  ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ํ‘œ์‹œํ•œ๋‹ค.
				q.push(graph[tmp][i]);
				check[graph[tmp][i]] = true;
			}
		}
	}

}

 

Main ํ•จ์ˆ˜

int main (){

	int n, m, start;
	cin >> n >> m >> start;

	// vscode ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๋ฐฐ์—ด์„ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•  ๋•Œ
	// vector<int> * graph = new vector<int>[n+1];
	vector<int> graph[n+1]; 
	bool check [n+1];
	fill(check, check+n+1, false);

	for(int i=0; i<m; i++){
		int u,v;
		cin >> u >> v;

		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	// Sorting
	// ๋‚˜์ค‘์— ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋‹ค์Œ์„ ํƒ์ƒ‰ํ•  ๋•Œ node ๊ฐ’์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ
	for(int i=1; i<=n; i++){
		sort(graph[i].begin(), graph[i].end());
	}

	dfs(start, graph, check);
	cout << "\n";

	return 0;
}

 



์ถœ์ฒ˜: 

https://sweetday-alice.tistory.com/176

 

[C++] DFS์™€ BFS ๊ตฌํ˜„ํ•˜๊ธฐ

1๏ธโƒฃDFS : ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (Depth-First Search) 1. What? : ๋ฃจํŠธ ๋…ธํŠธ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ• 2. When use? : ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ

sweetday-alice.tistory.com

https://twpower.github.io/73-how-to-implement-dfs-and-bfs-in-cpp

 

[Algorithm] C++์—์„œ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰(DFS์™€ BFS) ๊ตฌํ˜„ํ•˜๊ธฐ

Practice makes perfect!

twpower.github.io

https://better-tomorrow.tistory.com/entry/DFS-BFS-%EC%9D%B4%ED%95%B4%ED%95%98%EA%B8%B0

 

DFS & BFS ์ดํ•ดํ•˜๊ธฐ ๋ฐ ๊ตฌํ˜„(C++)

DFS : Depth First Search(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) - ๊ทธ๋ž˜ํ”„ ์ „์ฒด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜. (์™„๋ฒฝํžˆ ํƒ์ƒ‰) - ์‹œ์ž‘์ ๋ถ€ํ„ฐ ๋‹ค์Œ branch๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น branch๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๋ฐฉ๋ฒ•. - [์žฌ๊ท€ํ•จ์ˆ˜]

better-tomorrow.tistory.com

 

๋Œ“๊ธ€