BFS (Breadth First Search) 너비 우선 탐색

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
 
// BFS (Breadth First Search) 너비 우선 탐색
 
struct Vertex
{
    // int data;
};
 
vector<Vertex> vertices;
vector<vector<int>> adjacent;
vector<bool> discovered;
 
void CreateGraph()
{
    vertices.resize(6);
    adjacent = vector<vector<int>>(6);
 
    // 인접 리스트
    adjacent[0].push_back(1);
    adjacent[0].push_back(3);
    adjacent[1].push_back(0);
    adjacent[1].push_back(2);
    adjacent[1].push_back(3);
    adjacent[3].push_back(4);
    adjacent[5].push_back(4);
}
 
// 0 q[ 1 3 ]
// 1 q[ 3 2 ]
// 3 q[ 4 ]
 
void Bfs(int here)
{
    // 누구에 의해 발견 되었는지?
    vector<int> parent(6-1);
    // 시작점에서 얼만큼 떨어져 있는지?
    vector<int> distance(6-1);
    
    queue<int> q;
    q.push(here);
    discovered[here] = true;
 
    parent[here] = here; // 첫점은 내 자신의 의해 발견되었다
    distance[here] = 0;  // 초기값 
 
    while (q.empty() == false)
    {
        here = q.front();
        q.pop();
 
        cout << "Visited : " << here << endl;
 
        for (int there : adjacent[here])
        {
            if (discovered[there])
                continue;
 
            q.push(there);
            discovered[there] = true;
 
            parent[there] = here;
            distance[there] = distance[here] + 1;
        }
    }
 
}
 
void BfsAll()
{
    for (int i = 0; i < 6; i++)
        if (discovered[i] == false)
            Bfs(i);
}
 
int main()
{
    CreateGraph();
 
    discovered = vector<bool>(6false);
 
    BfsAll();
}
 
cs

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
 
// BFS (Breadth First Search) 너비 우선 탐색
 
struct Vertex
{
    // int data;
};
 
vector<Vertex> vertices;
vector<vector<int>> adjacent;
vector<bool> discovered;
 
void CreateGraph()
{
    vertices.resize(6);
    adjacent = vector<vector<int>>(6);
 
    //// 인접 리스트
    //adjacent[0].push_back(1);
    //adjacent[0].push_back(3);
    //adjacent[1].push_back(0);
    //adjacent[1].push_back(2);
    //adjacent[1].push_back(3);
    //adjacent[3].push_back(4);
    //adjacent[5].push_back(4);
 
    // 인접 행렬 version
    adjacent = vector<vector<int>>
    {
        { 010100},
        { 101100},
        { 000000},
        { 000010},
        { 000000},
        { 000010},
    };
}
 
// 0 q[ 1 3 ]
// 1 q[ 3 2 ]
// 3 q[ 4 ]
 
void Bfs(int here)
{
    // 누구에 의해 발견 되었는지?
    vector<int> parent(6-1);
    // 시작점에서 얼만큼 떨어져 있는지?
    vector<int> distance(6-1);
    
    queue<int> q;
    q.push(here);
    discovered[here] = true;
 
    parent[here] = here; // 첫점은 내 자신의 의해 발견되었다
    distance[here] = 0;  // 초기값 
 
    while (q.empty() == false)
    {
        here = q.front();
        q.pop();
 
        cout << "Visited : " << here << endl;
 
        for (int there = 0; there < 6; there++)
        {
            if (adjacent[here][there] == 0)
                continue;
            if (discovered[there])
                continue;
 
            q.push(there);
            discovered[there] = true;
 
            parent[there] = here;
            distance[there] = distance[here] + 1;
        }
    }
 
}
 
void BfsAll()
{
    for (int i = 0; i < 6; i++)
        if (discovered[i] == false)
            Bfs(i);
}
 
int main()
{
    CreateGraph();
 
    discovered = vector<bool>(6false);
 
    BfsAll();
}
 
cs