TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
breadth_first_search.cpp
Go to the documentation of this file.
1
48#include <algorithm>
49#include <cassert>
50#include <iostream>
51#include <list>
52#include <map>
53#include <queue>
54#include <string>
55#include <vector>
56
61namespace graph {
62/* Class Graph definition */
63template <typename T>
64class Graph {
69 std::map<T, std::list<T> > adjacency_list;
70
71 public:
72 Graph() = default;
73 ;
74 void add_edge(T u, T v, bool bidir = true) {
84 adjacency_list[u].push_back(v); // u-->v edge added
85 if (bidir == true) {
86 // if graph is bidirectional
87 adjacency_list[v].push_back(u); // v-->u edge added
88 }
89 }
90
96 std::map<T, bool> breadth_first_search(T src) {
98 std::map<T, bool> visited;
101 for (auto const &adjlist : adjacency_list) {
102 visited[adjlist.first] = false;
103 for (auto const &node : adjacency_list[adjlist.first]) {
104 visited[node] = false;
105 }
106 }
107
109 std::queue<T> tracker;
110
112 tracker.push(src);
114 visited[src] = true;
115 while (!tracker.empty()) {
118 T node = tracker.front();
120 tracker.pop();
121 for (T const &neighbour : adjacency_list[node]) {
124 if (!visited[neighbour]) {
126 tracker.push(neighbour);
128 visited[neighbour] = true;
129 }
130 }
131 }
132 return visited;
133 }
134};
135/* Class definition ends */
136} // namespace graph
137
139static void tests() {
142 std::map<int, bool> correct_result;
143 g.add_edge(0, 1);
144 g.add_edge(1, 2);
145 g.add_edge(2, 3);
146 correct_result[0] = true;
147 correct_result[1] = true;
148 correct_result[2] = true;
149 correct_result[3] = true;
150
151 std::map<int, bool> returned_result = g.breadth_first_search(2);
152
153 assert(returned_result == correct_result);
154 std::cout << "Test 1 Passed..." << std::endl;
155
157 returned_result = g.breadth_first_search(0);
158
159 assert(returned_result == correct_result);
160 std::cout << "Test 2 Passed..." << std::endl;
161
164
165 g2.add_edge("Gorakhpur", "Lucknow", false);
166 g2.add_edge("Gorakhpur", "Kanpur", false);
167 g2.add_edge("Lucknow", "Agra", false);
168 g2.add_edge("Kanpur", "Agra", false);
169 g2.add_edge("Lucknow", "Prayagraj", false);
170 g2.add_edge("Agra", "Noida", false);
171
172 std::map<std::string, bool> correct_res;
173 std::map<std::string, bool> returned_res =
174 g2.breadth_first_search("Kanpur");
175 correct_res["Gorakhpur"] = false;
176 correct_res["Lucknow"] = false;
177 correct_res["Kanpur"] = true;
178 correct_res["Agra"] = true;
179 correct_res["Prayagraj"] = false;
180 correct_res["Noida"] = true;
181 assert(correct_res == returned_res);
182 std::cout << "Test 3 Passed..." << std::endl;
183}
184
186int main() {
187 tests();
188 size_t edges = 0;
189 std::cout << "Enter the number of edges: ";
190 std::cin >> edges;
191
193
194 std::cout << "Enter space-separated pairs of vertices that form edges: "
195 << std::endl;
196 while (edges--) {
197 int u = 0, v = 0;
198 std::cin >> u >> v;
199 g.add_edge(u, v);
200 }
201
202 g.breadth_first_search(0);
203 return 0;
204}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
static void tests()
std::map< T, bool > breadth_first_search(T src)
void add_edge(T u, T v, bool bidir=true)
std::map< T, std::list< T > > adjacency_list
Graph Algorithms.