bfs in C++
C++
// A Quick implementation of BFS using
// vectors and queue
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
vector<bool> v;
vector<vector<int> > g;
void edge(int a, int b)
{
g[a].pb(b);
// for undirected graph add this line
// g[b].pb(a);
}
void bfs(int u)
{
queue<int> q;
q.push(u);
v[u] = true;
while (!q.empty()) {
int f = q.front();
q.pop();
cout << f << " ";
// Enqueue all adjacent of f and mark them visited
for (auto i = g[f].begin(); i != g[f].end(); i++) {
if (!v[*i]) {
q.push(*i);
v[*i] = true;
}
}
}
}
// Driver code
int main()
{
int n, e;
cin >> n >> e;
v.assign(n, false);
g.assign(n, vector<int>());
int a, b;
for (int i = 0; i < e; i++) {
cin >> a >> b;
edge(a, b);
}
for (int i = 0; i < n; i++) {
if (!v[i])
bfs(i);
}
return 0;
}
Also in C++:
- Title
- how to use wasd c++
- Category
- C++
- Title
- traverse map c++
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- compare string c++
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- pass ss tream as parameter c++
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- timer in c++
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- c++ find prime numbers
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- max heap c++ stl;
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- rick astley - never gonna give you up
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- how to iterate through a map in c++
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- uepic games github
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- how to initialize an struct object in c++
- Category
- C++
- Title
- c++ excel blank cells
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++