Solution: Breadth First Graph Traversal
Explore how to implement breadth first graph traversal in C++ using both iterative and recursive approaches. Understand BFS concepts, avoid revisiting nodes with visited tracking, and analyze time complexity to master graph traversal techniques.
Solution #1: Iterative
In this algorithm, we begin from a selected node (it can be a root node) and traverse the graph layerwise (one level at a time). All neighbor nodes (those connected to the source node) are explored, then we move to the next level of neighbor nodes.
Simply, as the name Breadth First suggests, we traverse the graph by first moving horizontally and visiting all the nodes of the current layer, then moving to the next layer.
Psuedocode
Let’s have a look at the pseudocode of breadth-first traversal:
Input: root is a node in the graph
Output: all nodes visited in breadth-first order
breadthFirstTraversal(source)
Let q = queue
while source != ø
print source
for all adjacent vertices v of source
q.enqueue(v)
if !q.isEmpty()
source = q.dequeue()
else
source = ø
Avoid Visiting the Same Nodes Again!
A graph may contain cycles, which will lead to visiting the same node again and again while we traverse the graph. To avoid processing the same node again, we can use a boolean array that marks visited array.
To make this process easy, use a queue to store the node and mark it as visited until all its neighbors (vertices that are directly connected to it) are marked.
The queue follows the First In First Out (FIFO) queuing method. Therefore, neighbors of the node will be visited in the order in which they were inserted in the queue; i.e. the node that was inserted first will be visited first, and so on.
Remember to mark all vertices as not-visited before running the algorithm.
Also, have a look at how built in C++ list is iterated. You can find the documentation here.
list <int> ::iterator i;
for (i = adjacencyList[source].begin(); i != adjacencyList[source].end(); ++i) {
}
Time Complexity
The time complexity of BFS can be computed as the total number of iterations performed by the for loop in the code above
Let be the set of all edges in the connected component visited by the algorithm. For each edge in the algorithm makes two for loop iteration steps: one time when the algorithm visits the neighbors of , and one time when it visits the neighbors of .
Hence, the time complexity is ( ).
Solution #2: Recursive
In the recursive solution, we write a utilityFunction() that helps us perform recursion.
As shown by the highlighted text, we call the utility function starting from source vertex and then recursively call this function again for all the nodes in the queue. Since the queue is a first in first out (FIFO) data structure, the vertex v that is first pushed in the queue is processed first.
Pseudocode
breadthFirstTraversal(source):
Queue q;
q.push(source);
utilityFunction(q)
utilityFunction(Queue q):
if q.empty() return;
Node u = q.pop()
print u
for all adjacent vertices v of u
if q.push(v)
utilityFunction(q) //recursion here.
Time Complexity
The time complexity of Breadth First Iterative Traversal and Breadth First Recursive Traversal remains the same; because we are traversing the entire graph once.