๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐˜š๐˜ญ๐˜ฐ๐˜ธ ๐˜ฃ๐˜ถ๐˜ต ๐˜ด๐˜ต๐˜ฆ๐˜ข๐˜ฅ๐˜บ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ทธ๋ž˜ํ”„ ์ˆœํšŒ ๋ฌธ์ œ - 2. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS) ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ทธ๋ž˜ํ”„ ์ˆœํšŒ ๋ฌธ์ œ - 2. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)

.23 2021. 7. 22. 00:45

'์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ with C++' ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS) ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

2.2.3 ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ; Breadth-First Search

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์€ ์‹œ์ž‘ ์ •์ ๊ณผ ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ๋ฐ˜๋ณต์ ์œผ๋กœ ํƒ์ƒ‰ํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์„ ์˜๋ฏธํ•œ๋‹ค. 

๊ทธ๋ž˜ํ”„ ์˜ˆ์‹œ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ์–‘์˜ ๊ทธ๋ž˜ํ”„๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. 0๋ฒˆ์งธ ์ •์ ์„ ์‹œ์ž‘์œผ๋กœ ์ž‘์€ ๊ฐ’๋ถ€ํ„ฐ ์ฐพ์•„๊ฐ„๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰๋œ๋‹ค.

1๋‹จ๊ณ„ : 0๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ

2๋‹จ๊ณ„ : 0๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘ ๊ฐ€์žฅ ๊ฐ’์ด ์ž‘์€ 1๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ

3๋‹จ๊ณ„ : 0๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ , ๊ฐ€์žฅ ๊ฐ’์ด ์ž‘์€ 2๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ

4๋‹จ๊ณ„ : 0๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ 3๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ

5๋‹จ๊ณ„ : 0๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜์˜€์œผ๋ฏ€๋กœ ๋‹ค์Œ ์ •์ ์ธ 1๋ฒˆ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ ํ•จ. 1๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ , ๊ฐ€์žฅ ๊ฐ’์ด ์ž‘์€ 4๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ

6๋‹จ๊ณ„ : 1๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ 5๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ

7๋‹จ๊ณ„ : 1๋ฒˆ๊ณผ 2๋ฒˆ ์ •์ ์˜ ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜์˜€์œผ๋ฏ€๋กœ 3๋ฒˆ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ์ฐพ์Œ. 3๋ฒˆ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ 6๋ฒˆ ์ •์  ๋ฐฉ๋ฌธ. ๋”์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ BFS ์ข…๋ฃŒ.

๋”ฐ๋ผ์„œ ์œ„์˜ ๊ทธ๋ž˜ํ”„๋ฅผ DFS๋ฅผ ํ†ตํ•ด ์ˆœํšŒํ•˜์˜€์„ ๋•Œ 0 โžก๏ธ 1 โžก๏ธ 2 โžก๏ธ 3 โžก๏ธ 4 โžก๏ธ 5 โžก๏ธ 6 ์ˆœ์„œ๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค.

 

2.2.4 ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์ฝ”๋“œ

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ๋Š” DFS์™€ ๋‹ฌ๋ฆฌ ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹œ์ž‘ ์ •์ ์— ๊ฐ€๊นŒ์šด ์ •์ ์„ ๋ฉ€๋ฆฌ ์žˆ๋Š” ์ •์ ๋ณด๋‹ค ๋จผ์ € ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์ •์ ๊ณผ ๊ฐ„์„  ์ •๋ณด๊ฐ€ ๋‹ด๊ธด 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด ๋ฒกํ„ฐ graph์™€ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•˜๋Š” bool ๋ฒกํ„ฐ visited๋ฅผ ์ด์šฉํ•˜์—ฌ, ๋ฐฉ๋ฌธํ•  ์ •์ ์„ ์„ ํƒํ•œ ๋’ค BFS๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ํ์ธ q์— ์‹œ์ž‘ ์ •์  ์ •๋ณด๋ฅผ ๋„ฃ์€ ํ›„ q๊ฐ€ ๋นˆ ํ๊ฐ€ ๋  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณต์ ์œผ๋กœ ํƒ์ƒ‰์„ ํ•ด ๋‚˜๊ฐ„๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(V + E) (V : ์ •์ ์˜ ๊ฐœ์ˆ˜, E : ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜)๋กœ DFS์™€ ๋™์ผํ•˜๋‹ค.

์‹œ์ž‘ ์ •์ ๊ณผ ๊ฐ€๊นŒ์šด ์ •์ ์„ ์ฐพ๋Š” ๋ฐ ์œ ๋ฆฌํ•˜๊ณ , ์‹œ์ž‘ ์ •์ ์—์„œ ํ•ด๋‹น ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฒฝ๋กœ๊ฐ€ ๋ณด์žฅ๋œ๋‹ค.

void BFS(int start) {
    queue<int> q;
    q.push(start);
    visited[start] = true;

    while(!q.empty()) {
        int vertex = q.front();
        q.pop();
        printf("%d ", vertex);
        for(int i = 0; i < graph[vertex].size(); i++) {
            int next = graph[vertex][i];
            if(!visited[next]) {
                q.push(next);
                visited[next] = true;
            }
        }
    }
}

์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ •์ ๊ณผ ๊ฐ„์„  ์ž…๋ ฅ ๋ฐฉ์‹์€ ๋ฐฑ์ค€ 1260๋ฒˆ DFS์™€ BFS๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค.

๋”๋ณด๊ธฐ
#include <cstdio>
#include <vector>
#include <queue>
#define MAX 1001

using namespace std;

vector<int> graph[MAX];
vector<bool> visited(MAX, false);

void BFS(int vertex);

int main(void) {
    int v, e, start;
    scanf("%d %d %d", &v, &e, &start);

    visited.resize(e + 1);
    
    for(int i = 0; i < e; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    BFS(start);
    return 0;
}

void BFS(int start) {
    queue<int> q;
    q.push(start);
    visited[start] = true;

    while(!q.empty()) {
        int vertex = q.front();
        q.pop();
        printf("%d ", vertex);
        for(int i = 0; i < graph[vertex].size(); i++) {
            int next = graph[vertex][i];
            if(!visited[next]) {
                q.push(next);
                visited[next] = true;
            }
        }
    }
}
Comments