-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadth_first_search.cpp
74 lines (66 loc) · 2.02 KB
/
breadth_first_search.cpp
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
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
map < int, vector<int> > graph {
{0,{1,3}},
{1,{0,2,3}},
{2,{1,3,4}},
{3,{0,1,2,4}},
{4,{2,3}}
};
map<int,bool> discovered;//keeps track if a node has been reached
map<int,int> parent;//stores the nodes ancestor that discovered it for the first time
map<int,int> distances;//stores the distance to reach each node from the source
vector <int> visited;
void clearGraph()
{
for(map<int,int>::iterator iter = distances.begin(); iter != distances.end(); iter++)
distances[iter->first] = -1;
for(map<int,int>::iterator iter = parent.begin(); iter != parent.end(); iter++)
parent[iter->first] = -1;
for(map<int,bool>::iterator iter = discovered.begin(); iter != discovered.end(); iter++)
discovered[iter->first] = false;
}
void bfs(int start)
{
int current;//current node being processed
int next;//reached node by current
unsigned int i;
queue<int> vertices;//vertices to process
distances[start] = 0;
discovered[start] = true;
vertices.push(start);
while(!vertices.empty())
{
current = vertices.front();
visited.push_back(current);
vertices.pop();
for(i = 0; i < graph[current].size(); i++)//for each node connected to current
{
next = graph[current][i];
if(!discovered[next])//if it hasn't been reached
{
discovered[next] = true;//mark it as reached
parent[next] = current;//store its parent
distances[next] = distances[current]+1;//save the distance
vertices.push(next);//push it to the queue for further analysis
}
}
}
}
int main()
{
int start=2;
clearGraph();
bfs(2);
vector<int> ::iterator vii;
for(vii=visited.begin();vii!=visited.end();vii++)
cout<<*vii<<" ";
cout<<endl;
map<int,int>::iterator mii;
for(mii=distances.begin();mii!=distances.end();mii++)
cout<<"distance of "<<(*mii).first<<":"<<(*mii).second<<endl;
return 0;
}