BFS
Breadth-First Search (BFS) is a graph traversal algorithm that explores all the vertices of a graph level by level.
It starts at a designated vertex (often called the "root" or "source") and systematically explores all the vertices reachable from that vertex.
BFS operates by visiting all the neighbors of a vertex before moving on to the next level of vertices.
Here's how BFS typically works:
Start at the source vertex.
Visit all the neighbors of the source vertex.
Move to the neighbors of these neighbors that have not yet been visited.
Continue this process until all vertices reachable from the source vertex have been visited.
BFS is often implemented using a queue data structure.
Here's a high-level outline of the BFS algorithm:
Enqueue the source vertex into a queue and mark it as visited.
While the queue is not empty: a. Dequeue a vertex from the queue. b. Visit and process the dequeued vertex. c. Enqueue all unvisited neighbors of the dequeued vertex into the queue and mark them as visited.
BFS guarantees that it will visit all vertices in the graph, and it does so in a systematic manner by exploring vertices level by level.
This makes it useful for tasks such as finding the shortest path between two vertices in an unweighted graph, checking if a graph is bipartite, and exploring connected components in a graph.
One thing to note is that BFS requires additional memory to store the visited vertices and the queue.
The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges in the graph.
This is because BFS traverses each vertex and each edge exactly once.
Following this new example image, the values would be traversed in the following order: [10, 6, 15, 3, 8, 20]
.
Typically, the steps followed by BFS algorithms are the following:
Create a queue and a variable to store the nodes that have been "visited"
Place the root node inside the queue
Keep looping as long as there's anything in the queue
Dequeue a node from the queue and push the value of the node into the variable that stores the visited nodes
If there's a left property on the dequeued node, add it to the queue
If there's a right property on the dequeued node, add it to the queue
pseudocode:
A possible implementation could be the following:
Last updated