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
60namespace graph {
61/* Class Graph definition */
62template <typename T>
63class Graph {
68 std::map<T, std::list<T> > adjacency_list;
69
70 public:
71 Graph() = default;
72 ;
73 void add_edge(T u, T v, bool bidir = true) {
83 adjacency_list[u].push_back(v); // u-->v edge added
84 if (bidir == true) {
85 // if graph is bidirectional
86 adjacency_list[v].push_back(u); // v-->u edge added
87 }
88 }
89
95 std::map<T, bool> breadth_first_search(T src) {
97 std::map<T, bool> visited;
100 for (auto const &adjlist : adjacency_list) {
101 visited[adjlist.first] = false;
102 for (auto const &node : adjacency_list[adjlist.first]) {
103 visited[node] = false;
104 }
105 }
106
108 std::queue<T> tracker;
109
111 tracker.push(src);
113 visited[src] = true;
114 while (!tracker.empty()) {
117 T node = tracker.front();
119 tracker.pop();
120 for (T const &neighbour : adjacency_list[node]) {
123 if (!visited[neighbour]) {
125 tracker.push(neighbour);
127 visited[neighbour] = true;
128 }
129 }
130 }
131 return visited;
132 }
133};
134/* Class definition ends */
135} // namespace graph
136
138static void tests() {
141 std::map<int, bool> correct_result;
142 g.add_edge(0, 1);
143 g.add_edge(1, 2);
144 g.add_edge(2, 3);
145 correct_result[0] = true;
146 correct_result[1] = true;
147 correct_result[2] = true;
148 correct_result[3] = true;
149
150 std::map<int, bool> returned_result = g.breadth_first_search(2);
151
152 assert(returned_result == correct_result);
153 std::cout << "Test 1 Passed..." << std::endl;
154
156 returned_result = g.breadth_first_search(0);
157
158 assert(returned_result == correct_result);
159 std::cout << "Test 2 Passed..." << std::endl;
160
163
164 g2.add_edge("Gorakhpur", "Lucknow", false);
165 g2.add_edge("Gorakhpur", "Kanpur", false);
166 g2.add_edge("Lucknow", "Agra", false);
167 g2.add_edge("Kanpur", "Agra", false);
168 g2.add_edge("Lucknow", "Prayagraj", false);
169 g2.add_edge("Agra", "Noida", false);
170
171 std::map<std::string, bool> correct_res;
172 std::map<std::string, bool> returned_res =
173 g2.breadth_first_search("Kanpur");
174 correct_res["Gorakhpur"] = false;
175 correct_res["Lucknow"] = false;
176 correct_res["Kanpur"] = true;
177 correct_res["Agra"] = true;
178 correct_res["Prayagraj"] = false;
179 correct_res["Noida"] = true;
180 assert(correct_res == returned_res);
181 std::cout << "Test 3 Passed..." << std::endl;
182}
183
185int main() {
186 tests();
187 size_t edges = 0;
188 std::cout << "Enter the number of edges: ";
189 std::cin >> edges;
190
192
193 std::cout << "Enter space-separated pairs of vertices that form edges: "
194 << std::endl;
195 while (edges--) {
196 int u = 0, v = 0;
197 std::cin >> u >> v;
198 g.add_edge(u, v);
199 }
200
201 g.breadth_first_search(0);
202 return 0;
203}
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.