-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadth_First_Search_iterative_Graph.cpp
More file actions
111 lines (95 loc) · 2.46 KB
/
Copy pathBreadth_First_Search_iterative_Graph.cpp
File metadata and controls
111 lines (95 loc) · 2.46 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
#include <queue>
#include "iostream"
#include "vector"
#include "set"
using namespace std;
struct Edge {
int source,destination;
};
class Graph{
int V;
vector<vector<int>> adjList;
public:
Graph(vector<Edge> edges,int V){
this->V = V;
adjList.resize(V);
for(auto i : edges){
adjList[i.source].push_back(i.destination);
// adjList[i.destination].push_back(i.source);
}
}
void BFSIterativelyJointGraph(int s);
void BFSIterativelydisJointGraph();
void printGraph();
void BFSutility(vector <bool> &discovered ,int i);
};
void Graph :: printGraph()
{
for (int i = 0; i < V; i++)
{
cout << i << " -- ";
for (int v : adjList[i])
cout <<"->"<< v << " ";
cout << endl;
}
}
void Graph :: BFSIterativelyJointGraph(int s){
vector<bool> discovered(V, false);
for(int i = 0; i < V; i++)
discovered[i] = false;
queue <int> q;
discovered[s] = true;
q.push(s);
while(!q.empty())
{
s = q.front(); cout << s << " "; q.pop();
for (auto j : adjList[s]) {
if(!discovered[j]){
discovered[j] = true;
q.push(j);
}
}
}
}
void Graph :: BFSutility(vector <bool> &discovered ,int s){
queue <int> q;
q.push(s);
discovered[s] = true;
while(!q.empty())
{
s = q.front(); cout << s << " "; q.pop();
for (auto j : adjList[s]) {
if(!discovered[j]){
discovered[j] = true;
q.push(j);
}
}
}
}
void Graph :: BFSIterativelydisJointGraph(){
vector <bool> discovered(V, false);
for(int i = 0; i < V ; i++) if(discovered[i] == false) BFSutility(discovered,i);
}
int main()
{
vector<Edge> edges =
{
{0, 1}, {0, 2}, {1, 2}, {2, 0}, {2,3},{3,3}
};
set <int > setsize;
for(auto i : edges){ setsize.insert(i.source);setsize.insert(i.destination);}
int V = setsize.size();
Graph graph(edges, V);
//graph.printGraph();
graph.BFSIterativelyJointGraph(2);
cout << "\n";
edges = {
{0,4},{1,2},{1,3},{1,4},{2,3},{3,4}
}; // this is aa disjoint graph
set <int > setsize1;
for(auto i : edges){ setsize1.insert(i.source);setsize1.insert(i.destination);}
int V1 = setsize1.size();
Graph graph2(edges,V1);
graph2.BFSIterativelydisJointGraph();
return 0;
}