-
Notifications
You must be signed in to change notification settings - Fork 3
/
Breath-first-search.cpp
123 lines (109 loc) · 2.8 KB
/
Breath-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
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
// BFS(G,s)
// for each vertex u ∈ V [G] − {s} do
// state[u] = “undiscovered”
// p[u] = nil, i.e. no parent is in the BFS tree
// state[s] = “discovered”
// p[s] = nil
// Q = {s}
// while Q = ∅ do
// u = dequeue[Q]
// process vertex u as desired
// for each v ∈ Adj[u] do
// process edge (u,v) as desired
// if state[v] = “undiscovered” then
// state[v] = “discovered”
// p[v] = u
// enqueue[Q,v]
// state[u] = “processed”
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Edge_Node {
int src, desc;
};
class Graph {
public:
vector<vector<int>> adjList;
int V, nedges;
vector<int> parent;
Graph(int V, vector<Edge_Node> edges) {
this->V = V;
parent.resize(V + 1);
adjList.resize(V);
for (auto edge : edges) {
adjList[edge.src].push_back(edge.desc);
adjList[edge.desc].push_back(edge.src);
}
}
void insert_edge(int x, int y) { adjList[x].push_back(y); }
void BFS(int source) {
bool proceesed[V + 1];
bool discovered[V + 1];
for (int i = 0; i < V; i++) {
proceesed[i] = discovered[i] = false;
parent[i] = -1;
}
/*BFS starts here*/
queue<int> q; /* queue of vertices to visit */
int u; /*Current vertex */
int v; /*successor vertex*/
Edge_Node *p; /* temporary pointer */
q.push(source);
discovered[source] = true;
while (!q.empty()) {
u = q.front();
q.pop();
process_vertex_early(u);
proceesed[u] = true;
for (auto v : adjList[u]) {
if (!proceesed[v]) {
// process v
process_edge(u, v);
}
if (!discovered[v]) {
q.push(v);
discovered[v] = true;
parent[v] = u;
}
}
process_vertex_late(u);
}
std::cout << std::endl;
}
/*
run it only after bfs so that parent get filled
*/
void find_paths(int u, int v) {
std::cout << "Path from " << u << " to " << v << " : " << std::endl;
find_pathUtil(u, v);
}
void find_pathUtil(int u, int v) {
if (u == v || v == -1) {
std::cout << u << std::endl;
} else {
find_pathUtil(u, parent[v]);
std::cout << v << std::endl;
}
}
void process_vertex_early(int vertex) {
std::cout << "Early Vertex Process : " << vertex << std::endl;
}
void process_edge(int u, int v) {
nedges++;
std::cout << "Process Edge : " << u << " " << v << std::endl;
}
void process_vertex_late(int vertex) {
// std::cout << "late Vertex Process : "<< vertex << std::endl;
}
};
int main(int argc, const char **argv) {
vector<Edge_Node> edges = {{0, 1}, {1, 2}, {2, 3}, {2, 4},
{3, 5}, {4, 5}, {5, 4}};
int V = 6;
Graph graph = Graph(V, edges);
graph.BFS(0);
graph.find_paths(0, 4);
graph.find_paths(5, 1);
return 0;
}