๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Structure๐Ÿงถ

๊ทธ๋ž˜ํ”„ ์ด๋ก  - DFS์™€ BFS

by Jouureee 2021. 4. 21.

์„ ํ˜• ๊ตฌ์กฐ๊ฐ€ ์•„๋‹Œ ๋น„์„ ํ˜• ๊ตฌ์กฐ ํƒ์ƒ‰ ์œ„ํ•ด์„  ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)๊ณผ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์„ ์‚ฌ์šฉํ•œ๋‹ค.

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์€ ๋ธŒ๋ฃจํŠธ ํฌ์Šค์™€ ๊ด€๋ จ์ด ๊นŠ๊ณ  ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์€ ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ ๊ด€๋ จ์ด ๊นŠ๋‹ค.

 

DFS

๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์  ๋ฐฉ๋ฌธ

๋ง‰ํžŒ ์ •์ ์— ๋„๋‹ฌํ•˜์ง€ ์•Š๋Š” ํ•œ ๋’ค๋กœ ๋Œ์•„๊ฐ€์ง€ ์•Š๋Š”๋‹ค ์ฆ‰ ๋” ๋”ฐ๋ผ๊ฐˆ ๊ฐ„์„ ์ด ์—†์„ ๊ฒฝ์šฐ ์ด์ „์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค. ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ง€๊ธˆ๊นŒ์ง€ ๊ฑฐ์ณ ์˜จ ์ •์  ๋ชจ๋‘ ์ €์žฅ ํ•ด ๋‘ฌ์•ผ ํ•˜๋Š”๋ฐ ์žฌ๊ท€ ํ˜ธ์ถœ ์ด์šฉํ•˜๋ฉด ์ด ๊ฐ™์€ ์ผ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฌ๊ท€ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒํ•˜๋ฉด ํ˜ธ์ถœํ•œ ์œ„์น˜๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

DFS ํŠน์ง•

  1. ๋ฐฉ๋ฌธํ•œ ์ •์ ์„ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ ์ •์ ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ํ•„์š”
  2. ๋”์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ •์ ์ด ์—†์œผ๋ฉด ์ด์ „ ์ •์ ์œผ๋กœ ๋Œ์•„๊ฐ€์•ผ ํ•œ๋‹ค -> ์žฌ๊ท€, stack ์ด์šฉ
  3. ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆœ์„œ

  1. ์‹œ์ž‘ ๋…ธ๋“œ ๋ฐฉ๋ฌธ (with ๋ฐฉ๋ฌธ ํ‘œ์‹œ)
  2. ์‹œ์ž‘ ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ ๋ฐฉ๋ฌธ
  3. ์‹œ์ž‘ ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ b ๋ฐฉ๋ฌธ ์‹œ b์˜ ์ด์›ƒ ๋…ธ๋“œ ๋ฐฉ๋ฌธ
    1. b ๋…ธ๋“œ์— ๋Œ€ํ•ด dfs ํ•˜์—ฌ b์˜ ์ด์›ƒ ๋…ธ๋“œ ๋ชจ๋‘ ๋ฐฉ๋ฌธ
  4. b ๋…ธ๋“œ ์ด์›ƒ ๋…ธ๋“œ ๋‹ค ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ๋‹ค์‹œ a์— ์ธ์ ‘ํ•œ ์ •์ ๋“ค ์ค‘ ๋ฐฉ๋ฌธ์ด ์•ˆ๋œ ๋…ธ๋“œ ๋ฐฉ๋ฌธ
    1. ๋‹ค ๋ฐฉ๋ฌธ์‹œ ์ข…๋ฃŒ
    2. ์•„์ง ๋ฐฉ๋ฌธ์ด ์•ˆ๋œ ์ •์ ์ด ์žˆ์œผ๋ฉด ๊ทธ ์ •์ ์„ ์‹œ์ž‘ ์ •์ ์„ dfs

 

  • ์ธ์ ‘ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„๋œ ๊ทธ๋ž˜ํ”„ : O(N^2)
  • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„๋œ ๊ทธ๋ž˜ํ”„ : O(N+E)

 


 

 

BFS

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•, ๊ฐ€๊นŒ์šด ์ •์  ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ์ •์  ๋‚˜์ค‘์— ๋ฐฉ๋ฌธ

๋‘ ๋…ธ๋“œ ์‚ฌ์ด ์ตœ๋‹จ ๊ฒฝ๋กœ, ์ž„์˜์˜ ๊ฒฝ๋กœ ์ฐพ๊ณ  ์‹ถ์„ ๋•Œ  ์‚ฌ์šฉ

์žฌ๊ท€์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์–ด๋–ค ๋…ธ๋“œ ๋ฐฉ๋ฌธํ–ˆ์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ฒ€์‚ฌํ•ด์•ผ ํ•œ๋‹ค ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์งˆ ์œ„ํ—˜์„ฑ์„ ๊ฐ€์ง„๋‹ค.

๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ์ฐจ๋ก€๋กœ ์ €์žฅํ•œ ํ›„ ๊บผ๋‚ผ ์ˆ˜์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ํ ์‚ฌ์šฉํ•œ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ, ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์œ ์‚ฌํ•˜๋‹ค

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆœ์„œ

  1. ์‹œ์ž‘ ๋…ธ๋“œ ๋ฐฉ๋ฌธ + ๋ฐฉ๋ฌธ ๋…ธ๋“œ ์ฒดํฌ
    1. ํ์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ ์‚ฝ์ž…
    2. ์ดˆ๊ธฐ ์ƒํƒœ ํ๋Š” ์‹œ์ž‘ ๋…ธ๋“œ๋งŒ ์ €์žฅ
  2. ํ์—์„œ ๊บผ๋‚ธ ๋…ธ๋“œ์™€ ์ธ์ ‘ ๋…ธ๋“œ ์ฐจ๋ก€๋กœ ๋ฐฉ๋ฌธ
    1. ํ์—์„œ ๊บผ๋‚ธ ๋…ธ๋“œ ๋ฐฉ๋ฌธ
    2. ํ์—์„œ ๊บผ๋‚ธ ๋…ธ๋“œ์™€ ์ธ์ ‘ ๋…ธ๋“œ ๋ฐฉ๋ฌธ  - ์ธ์ ‘ ๋…ธ๋“œ ์—†์œผ๋ฉด ํ์—์„œ ๋…ธ๋“œ ๊บผ๋ƒ„
    3. ํ์—์„œ ๋ฐฉ๋ฌธ๋œ ๋…ธ๋“œ ์‚ฝ์ž…
  3. ํ ์†Œ์ง„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

 

  • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„๋œ ๊ทธ๋ž˜ํ”„ : O(N+E)
  • ์ธ์ ‘ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„๋œ ๊ทธ๋ž˜ํ”„ : O(N^2)

 

1) BFS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชฉ์ ์€ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ• ๋•Œ (๋‹จ, ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 1์ผ๋•Œ)

2) DFS์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชฉ์ ์€ ์‹œ์ž‘์ ์—์„œ ๋์ ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ๋กœ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ์–ป๊ณ  ์‹ถ์„ ๋•Œ

 

 

c++ ์˜ˆ์ œ

๋ฐฑ์ค€ 1260๋ฒˆ ๋ฌธ์ œ

//
//  1260_bfs_dfs.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/13.
//

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


using namespace std;
// dfs ์— ๋“ค์–ด์˜ค๋ฉด ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ ๋ฐฉ๋ฌธ ํŒ๋‹จ ์—ฌ๋ถ€๊ฐ€ ๋ฐ˜๋“œ์‹œ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค.

//recursion used
/*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); //recursion used
        }
    }
}
*/

//queue used
void bfs(int start, vector<int> bgraph[], bool bcheck[]){
    queue<int> q;
    q.push(start);
    bcheck[start] = true;
    while(!q.empty()){
        int tmp = q.front();
        q.pop();
        cout << tmp << ' ';
        for(int i=0; i<bgraph[tmp].size(); i++){
            // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
            if(bcheck[bgraph[tmp][i]] == false){
                // ํ์— ๋„ฃ์–ด์ฃผ๊ณ  ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ํ‘œ์‹œํ•œ๋‹ค.
                q.push(bgraph[tmp][i]);
                bcheck[bgraph[tmp][i]] = true;
            }
        }
    }
}

//stack used
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;
            }
        }
    }
}


int main(){
    int n, m, start;
    cin >> n >> m >> start;
    vector<int> graph[n+1];
    vector<int> regraph[n+1];
    bool check [n+1];
    bool recheck [n+1];
    
     // fill check ~ check+n index fill to check array
     fill(check, check+n+1, false);
     fill(recheck, recheck+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);
         regraph[u].push_back(v);
         regraph[v].push_back(u);
     }
     
     for(int i = 1; i<=n; i++){
         sort(graph[i].begin(), graph[i].end());
         sort(regraph[i].begin(), regraph[i].end());
     }
    
    dfs(start, graph, check);
    cout << '\n';
    int restart;
    restart = start;
    bfs(restart, regraph, recheck);
}

๋Œ“๊ธ€