Breadth First Search |
A graph traversal algorithm that starts traversing the graph from a specified source vertex and explores all the vertices at the current level before moving on to the next level - BFS Visualizer
Time Complexity: O(m+n), where m is the number of edges and n is the number of vertices
void BFS(vector<vector<int>> &graph, int vertix)
{
if (visited.empty())
{
visited.resize(graph.size(), false);
}
queue<int> queue;
queue.push(vertix);
visited.at(vertix) = true;
while (!queue.empty())
{
int current_vertix = queue.front();
queue.pop();
for (int i = 0; i < graph.at(current_vertix).size(); i++)
{
if (!visited.at(graph.at(current_vertix).at(i)))
{
queue.push(graph.at(current_vertix).at(i));
visited.at(graph.at(current_vertix).at(i)) = true;
}
}
cout < "Finished the Discovery of Vertix (" < current_vertix < ")" < endl;
}
}
// Example Run
int main()
{
// Sample Adjecency List
vector<vector<int>> graph = {
{1, 2},
{0, 2, 3},
{0, 1},
{1}};
// Sample Run
BFS(graph, 0);
}