์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋ฐฑ์ค€(BOJ)] 1260๋ฒˆ: DFS์™€ BFS (C++)

PROGRAMMING/Algorithm

by koharin 2021. 1. 22. 23:32

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ–จ ์ž…์ถœ๋ ฅ


  • ์ž…๋ ฅ: N(์ •์ ์˜ ๊ฐœ์ˆ˜), M(๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜), V(์ฒ˜์Œ ๋ฐฉ๋ฌธํ•  ๋ฒˆํ˜ธ), ์„œ๋กœ ์—ฐ๊ฒฐ๋œ ๋ฒˆํ˜ธ์Œ

  • ์ถœ๋ ฅ: DFS ๋ฐฉ๋ฌธ ์ˆœ์„œ, BFS ๋ฐฉ๋ฌธ ์ˆœ์„œ

 

 

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


#include <bits/stdc++.h>

using namespace std;

void DFS(int start, deque<int> *graph, bool *check, deque<int> *DFS_result){
    stack<int> s;
    check[start] = true;
    s.push(start);
    (*DFS_result).push_back(start);

    while(!s.empty()){
        int current = s.top(); 
        s.pop(); 
        for(int next: graph[current]){
            if(check[next] == false){
                s.push(current);
                s.push(next); 
                check[next] = true;
                (*DFS_result).push_back(next);
                break;
            }
        }

    }
}
void BFS(int start, deque<int> *graph, bool *check, deque<int> *BFS_result){
    queue<int> q;
    check[start] = true;
    q.push(start);
    
    while(!q.empty()){
        int front = q.front(); 
        (*BFS_result).push_back(front);
        q.pop();
        for(int num: graph[front]){
            if(check[num] == false){
                q.push(num);
                check[num] = true;
            }
        }
    }
}

int main(){
    int N, M, V; //N: ์ •์ ์˜ ์ˆ˜, M: ๊ฐ„์„ ์˜ ์ˆ˜, V: ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ

    scanf("%d %d %d", &N, &M, &V); 

    deque<int> graph[N];
    deque<int> DFS_result;
    deque<int> BFS_result;
    bool DFS_check[N];
    bool BFS_check[N];
    fill(DFS_check, DFS_check+N, false);
    fill(BFS_check, BFS_check+N, false);

    for(int i=0; i<M; i++){
        int u,v;
        scanf("%d %d", &u, &v);
        graph[u-1].push_back(v-1);
        graph[v-1].push_back(u-1);
    }

    for(int i=0; i<N; i++) sort(graph[i].begin(), graph[i].end());

    DFS(V-1, graph, DFS_check, &DFS_result);
    BFS(V-1, graph, BFS_check, &BFS_result);

    for(int i: DFS_result) printf("%d ", i+1);
    printf("\n");
    for(int i: BFS_result) printf("%d ", i+1);

}

 

 

 

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •: DFS & BFS ๊ตฌํ˜„


#include <iostream>
#include <stdio.h>
#include <deque>
#include <queue>
#include <algorithm>
#include <stack>

using namespace std;

void DFS(int start, deque<int> *graph, bool *check, deque<int> *DFS_result){
    stack<int> s;
    check[start] = true;
    s.push(start);
    printf("[+] %d VISITED\n", start);

    while(!s.empty()){
        int front = s.top(); 
        s.pop(); printf("[+] POPED %d\n", front);
        (*DFS_result).push_back(front);
        for(int num: graph[front]){
            if(check[num] == false){
                s.push(num); 
                check[num] = true;
                printf("[+] %d VISITED\n", num);
            }
        }

    }
}
void BFS(int start, deque<int> *graph, bool *check, deque<int> *BFS_result){
    queue<int> q;
    check[start] = true;
    q.push(start);
    printf("[+] %d VISITED\n", start);
    
    while(!q.empty()){
        int front = q.front(); printf("[+] POPED %d\n", front);
        (*BFS_result).push_back(front);
        q.pop();
        for(int num: graph[front]){
            if(check[num] == false){
                q.push(num);
                check[num] = true;
                printf("[+] %d VISITED\n", num);
            }
        }
    }
}

int main(){
    int N, M, V; //N: ์ •์ ์˜ ์ˆ˜, M: ๊ฐ„์„ ์˜ ์ˆ˜, V: ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ

    scanf("%d %d %d", &N, &M, &V); 

    deque<int> graph[N];
    deque<int> DFS_result;
    deque<int> BFS_result;
    bool DFS_check[N];
    bool BFS_check[N];
    
    fill(DFS_check, DFS_check+N, false);
    fill(BFS_check, BFS_check+N, false);

    for(int i=0; i<M; i++){
        int u,v;
        scanf("%d %d", &u, &v);
        graph[u-1].push_back(v-1);
        graph[v-1].push_back(u-1);
    }

    for(int i=0; i<N; i++) sort(graph[i].begin(), graph[i].end());

    DFS(V-1, graph, DFS_check, &DFS_result);
    BFS(V-1, graph, BFS_check, &BFS_result);

    printf("RESULT OF DFS: ");
    for(int i: DFS_result) printf("%d ", i);

    printf("\nRESULT OF BFS: ");
    for(int i: BFS_result) printf("%d ", i);

}

1. DFS ๊ตฌํ˜„

  • recursive์™€ stack์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฐฑ์ค€ 2606: ๋ฐ”์ด๋Ÿฌ์Šค ๋ฌธ์ œ์—์„œ recursive๋กœ ๊ตฌํ˜„ํ–ˆ์–ด์„œ, ์ด๋ฒˆ์—๋Š” stack์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค.

  • stack์˜ ๊ฒฝ์šฐ, top์—์„œ๋งŒ ๋„ฃ๊ณ  ๊บผ๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋„ฃ์€ ์›์†Œ๋ถ€ํ„ฐ popํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ํ•ด์ฃผ๊ณ , stack์— ๋„ฃ๋Š”๋‹ค. ๋‹ค์‹œ ์ด ์›์†Œ๋ฅผ pop์„ ํ•ด์„œ ํ•ด๋‹น ๋ฒˆํ˜ธ์— ์—ฐ๊ฒฐ๋œ ์›์†Œ๋“ค๋ฅผ stack์— ๋„ฃ๋Š”๋‹ค. 

  • ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์›์†Œ๋Š” stack์— ๋„ฃ์ง€ ์•Š๋Š”๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์›์†Œ์— ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ์›์†Œ๋„ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๋‹ค์‹œ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ stack์— ๋„ฃ์€ ํ›„, ๋ฐฉ๋ฌธํ•œ ์ฒซ ๋ฒˆ์งธ ์›์†Œ์— ์—ฐ๊ฒฐ๋œ ์›์†Œ๋ฅผ stack์— ๋„ฃ๋Š”๋‹ค.

  • ๋‹ค์Œ์œผ๋กœ๋Š” top์— ์žˆ๋Š” ์•„๊นŒ ๋„ฃ์—ˆ๋˜ ์›์†Œ๋ฅผ popํ•œ ํ›„, top์˜ ์›์†Œ๋ฅผ stack์— ๋„ฃ์€ ํ›„ ์ด top ์›์†Œ์— ์—ฐ๊ฒฐ๋œ ์›์†Œ๋ฅผ stack์— ๋„ฃ๋Š”๋‹ค. 

  • ์œ„์˜ ๊ณผ์ •์„ stack์ด empty๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ ํ›„ ์ข…๋ฃŒ๋œ๋‹ค.

  • stack์—์„œ popํ•œ ์ˆœ์„œ๊ฐ€ DFS์—์„œ์˜ ๋ฐฉ๋ฌธ์ˆœ์„œ์ด๋‹ค.

  • DFS์˜ ๊ฒฝ์šฐ, result์— ๋„ฃ์„ ๋•Œ BFS๋‚˜ ๊ธฐ์กด recursive ๊ตฌํ˜„ ๋•Œ์ฒ˜๋Ÿผ while๋ฌธ ์ดˆ๋ฐ˜์— ๋„ฃ์œผ๋ฉด ๋งจ ์ฒ˜์Œ ์›์†Œ๊ฐ€ ์•ˆ ๋“ค์–ด๊ฐ€์„œ start ์›์†Œ๋ฅผ pushํ•ด์ฃผ๋Š” ๊ฒƒ์„ while๋ฌธ ์ „์— ๋„ฃ์–ด์คฌ๋‹ค.

2. BFS ๊ตฌํ˜„

  • queue๋กœ ๊ตฌํ˜„ (FIFO)

  • queue์˜ ๊ฒฝ์šฐ, front๋ฅผ popํ•˜๊ณ  front์— ์—ฐ๊ฒฐ๋œ ์›์†Œ์„ queue ๋’ค์— ์ฐจ๋ก€๋กœ ๋„ฃ๋Š” ๊ณผ์ •์„ empty๊ฐ€ ์•„๋‹ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

  • ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์›์†Œ๋Š” queue์— ๋„ฃ์ง€ ์•Š๋Š”๋‹ค.

  • queue์—์„œ ์›์†Œ๋ฅผ ๋นผ๋Š” ์ˆœ์„œ๊ฐ€ ๋ฐฉ๋ฌธ ์ˆœ์„œ์ด๋‹ค.

  • ์ฒ˜์Œ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฒˆํ˜ธ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ V-1๋กœ ์คฌ๋‹ค.

 

 

๐Ÿ‘ ๊ฒฐ๊ณผ


 

728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ