-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
136 lines (100 loc) · 2.53 KB
/
bfs.cpp
File metadata and controls
136 lines (100 loc) · 2.53 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This file contains an implementation of breadth first search of an undirected graph
* Code by: Humayun Kabir, humayun.k1@gmail.com */
#include <iostream>
#include "Graph.cpp"
#include "queue.cpp"
using namespace std;
class BFS {
private:
//number of vertices
int n;
//the source vertex
int s;
//an array to keep track of visited vertices
bool *marked;
//gives parent vertex of vistied vertices
int *edgeTo;
public:
//Constructor
BFS(Graph & g, int s) : n(g.N()), s(s) {
marked = new bool[n];
//Initially all the vertices are unmakred or not visited
for(int v = 0; v < n; v++)
marked[v] = false;
edgeTo = new int[n];
//does a bfs from the source vertex s
bfs(g, s);
}
//bfs on a graph g from source vertex s
void bfs(Graph & g, int t) {
//Declare a queue of integers
Queue<int> que;
//insert the source t into the queue
que.Enqueue(t);
//mark t as visited
marked[t] = true;
//continue until the queue is empty
while( !que.isEmpty() ) {
//dequeue an element from the queue
int v = que.Dequeue();
//check all the neighbors of v
for(LinkedList<int>::iterator it = g.adjList(v).begin(); it != g.adjList(v).end(); it ++) {
//neighbor of v
int w = *it;
//if w is not visited
if( !marked[w] ) {
//insert w into the queue
que.Enqueue( w );
//mark w as visited
marked[w] = true;
//update edgeTo array
//w is reached from v --in other words edgeTo array gives parent of a vertex
edgeTo[w] = v;
}
}
}
}
//tests where there is a path to v from the source vertex s
bool hasPathTo(int v) {
return marked[v];
}
//Returns a path from s to v
//If there is no path - an empty linkedList is returned
void pathTo(int v, LinkedList<int> & path) {
if( !hasPathTo(v) )
return;
for(int x = v; x != s; x = edgeTo[x])
path.add(x);
path.add(s);
return;
}
//Destructor
~BFS() {
delete [] marked;
delete [] edgeTo;
}
};
//Test the bfs search implementation of a graph
int main() {
Graph g("graph2.txt");
int source = 5;
BFS bfs(g, source);
cout<<"Number of vertices in g: "<<g.N()<<endl;
int v = 1;
if(bfs.hasPathTo(v) ) {
cout<<"There is path from: "<< source<<" to: "<< v<<endl;
cout<<"The path is: "<<endl;
LinkedList<int> path;
bfs.pathTo(v, path);
for( LinkedList<int>::iterator it = path.begin(); it != path.end(); it ++) {
if( *it == source )
cout<<*it;
else
cout<<"->"<<*it;
}
cout<<endl;
}
else
cout<<"There is no path from: "<< source<<" to: "<< v<<endl;
return 0;
}