-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.cpp
73 lines (61 loc) · 1.87 KB
/
bfs.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
#include <iostream>
#include <list>
#include <queue>
using namespace std;
//make an adjacency list from a list of edges
//number of vertices is given and the graph is simple
int main(){
int num_verteces;
cin >> num_verteces;
list<int> *adjl = new list<int>[num_verteces];
int edge1,edge2;
//two because the graph is undirected
while((cin >> edge1 >> edge2)&&(edge1 != (-1))){
adjl[edge1-1].push_back(edge2-1);
adjl[edge2-1].push_back(edge1-1);
}
int index = 0;
while(index < num_verteces){
list<int>::iterator it;
cout << index+1 << ": ";
for(it = adjl[index].begin(); it != adjl[index].end(); ++it)
cout << (*it) + 1 << " ";
cout << endl;
index++;
}
cout << endl << endl;
//---------------------------------------------------------
//BREATH-FIRST-SEARCH
//the adjacency list is in adjl and the number of verteces
//is num_verteces
//0: not visited, 1: passed by, 2: completely checked, -1 no parent
queue<int> to_visit;
int start_vertex;
cin >> start_vertex;
int *visited = new int[num_verteces];
int *parent = new int[num_verteces];
for(int i = 0; i < num_verteces; ++i){
visited[i] = 0;
parent[i] = -1;
}
to_visit.push(start_vertex-1);
cout << start_vertex-1 << endl;
while(!(to_visit.empty())){
int current = to_visit.front();
to_visit.pop();
visited[current]=2;
list<int>::iterator itr;
for(itr=adjl[current].begin(); itr != adjl[current].end(); ++itr){
if(visited[*itr]==0){
to_visit.push(*itr);
visited[*itr]=1;
parent[*itr]=current;
}
}
}
for(int i = 0; i < num_verteces; ++i){
cout << visited[i] << " ";
cout << parent[i]+1 << endl;
}
return 0;
}