Last Updated: 06 October, 2024 5 Mins
Graph traversal is a technique for searching a vertex in a graph. It can also be used to determine the order in which vertices are visited during the search process. Graph traversal determines which edges to use during the search process so as to avoid loops.
There are the following two possible way of graph traversal:
Breadth First Search (BFS)
In the graph tutorial, we are going learn all about Breadth First Search (BFS).
Breadth First Search (BFS) is a graph traversal algorithm. BFS first traverse all the neighboring nodes of the starting node once all neighboring nodes are visited, then it will move on to the next level as given in the image below.
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops.
BFS is the foundation of numerous well-known graph algorithms, including Prim's algorithm, Dijkstra's shortest path, and Kahn's algorithm.
Starting Node: Traversing begings from the staring node of the graph.
Queue: Create a queue to store nodes to be visited.
Visit: Mark the starting node as visited and enqueue it.
Dequeue and Explore:
Repeat: Continue steps 4 and 5 until the queue is empty or the target node is found.
Output
BFS starting from node 0 :
0 1 2 3 4
Time Complexity: O(V+E), where V is the number of nodes and E is the number of edges.
Auxiliary Space: O(V)
That's all, guys. I hope this article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com