-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBFS
More file actions
82 lines (62 loc) · 1.63 KB
/
Copy pathBFS
File metadata and controls
82 lines (62 loc) · 1.63 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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Graph{
public:
int vertex;
list<int>* adjacent;
bool *vertices;
Graph(int t)
{
vertex=t;
adjacent=new list<int>[t];
vertices=new bool[t];
}
void addEdge(int source,int destination)
{
adjacent[source].push_back(destination);
adjacent[destination].push_back(source);
}
void Breadth_first_search(int start)
{
int i;
for(i=0;i<vertex;i++)
vertices[i]=false;
queue<int> q;
vertices[start]=true;
q.push(start);
list<int>::iterator j;
// cout<<start<<"->";
while(!q.empty())
{
int curr_vertex=q.front();
cout<<q.front()<<" ";
vertices[curr_vertex]=true;
//cout<<curr_vertex<<"->";
q.pop();
/*if(q.empty())
cout<<"yes"<<" ";
else
cout<<"no"<<" ";*/
for(j=adjacent[curr_vertex].begin();j!=adjacent[curr_vertex].end();j++)
{
int present_vertex=*j;
if(!vertices[present_vertex])
{
vertices[present_vertex]=true;
q.push(present_vertex);
}
}
}
}
};
int main()
{
Graph g(4);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,2);
g.addEdge(2,3);
//g.addEdge(3,3);
g.Breadth_first_search(2);
}