BFS Simplest C++ ๐ช
Hello!
Letโs learn one of the best algorithm named as BFS ๐ผ๐ป.
It uses a queue to traverse the graphโs nodes. The source node is added to queue and then we start with condition until queue is empty ๐.
So we start popping out the node from the queue until its empty and we also push the non-visited adjacent nodes to the queue of the current nodeโ๏ธ.
To maintain the state of nodes whether they are visited or not we use an array named visited [โ๏ธ].
Now letโs have a look at the source code in c++ for the same ๐ป.
The above code is very easy to understand and implement ๐. I have added the picture over here, so you would love to code instead of copy ๐ ๐.
Itโs too easy and you can code it in one go, so the above function takes three arguments as soucre vertex named as start, number of vertices and the adjacency list of the all graph nodes. ๐ค
To understant the input see the below screenshot-
Cheers!! ๐
Thank you for reading!๐
Clap if you like it ๐๐