Given an adjacency list representation of a directed graph, the task is to find the path from source to every other node in the graph using BFS.
Input: Output: 0 <- 2 1 <- 0 <- 2 2 3 <- 1 <- 0 <- 2 4 <- 5 <- 2 5 <- 2 6 <- 2
Approach: In the images shown below:
- que array stores the vertices reached and we will enqueue a vertex only if it has not been visited and dequeue it once all its child node have been considered.
- In order to distinguish whether the node has been visited or not we will put 1 in visited array at the respective index to signify it has been visited and if at given index 0 is present it will signify that it has not been visited.
- Parent array is to store the parent node of each vertex. For ex. In case of 0 connected to 2, 2 will be the parent node of 0 and we will put 2 at the index 0 in the parent array.
Below is the implementation of the above approach:
# Python3 implementation of the approach
# Function to print the path from
# src (s) to destination (d)
def printfunc(parent, s, d):
# The while loop will stop only when
# the destination and the src node
# become equal
while s != d:
# Print the destination and store
# the parent of the node in the
# destination since parent stores
# the node through which the current
# node has been reached
print(str(d) + ” <-", end = " ") d = parent[d] print(d) # Finding Path using BFS ALgorithm def bfs(adjList, src, n): parent =  * (n) que =  * (n) front, rear = -1, -1 visited =  * (n) visited[src] = 1 parent[src] = src # To add any non visited node we will # increment the rear and add that vertex # to the end of the array (enqueuing) rear += 1 que[rear] = src # The loop will continue till the rear # and front are equal while front != rear: # Here Dequeuing is nothing but to # increment the front int front += 1 k = que[front] List = adjList[k] for i in range(0, len(List)): j = List[i] if visited[j] == 0: rear += 1 que[rear] = j visited[j] = 1 parent[j] = k # Print the path from src to every # other node for k in range(0, n): printfunc(parent, src, k) # Driver code if __name__ == "__main__": # Adjacency list representation # of the graph adjList =  # Vertices 1 and 2 have an incoming edge # from vertex 0 adjList.append([1, 2]) # Vertex 3 has an incoming edge # from vertex 1 adjList.append() # Vertices 0, 5 and 6 have an incoming # edge from vertex 2 adjList.append([0, 5, 6]) # Vertices 1 and 4 have an incoming edge # from vertex 3 adjList.append([1, 4]) # Vertices 2 and 3 have an incoming edge # from vertex 4 adjList.append([2, 3]) # Vertices 4 and 6 have an incoming edge # from vertex 5 adjList.append([4, 6]) # Vertex 5 has an incoming edge # from vertex 6 adjList.append() n = len(adjList) src = 2 bfs(adjList, src, n) # This code is contributed by Rituraj Jain [tabbyending]
0 <- 2 1 <- 0 <- 2 2 3 <- 1 <- 0 <- 2 4 <- 5 <- 2 5 <- 2 6 <- 2
- Java Program for Finding the vertex, focus and directrix of a parabola
- Finding minimum vertex cover size of a graph using binary search
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Find a Mother Vertex in a Graph
- Find the Degree of a Particular vertex in a Graph
- All vertex pairs connected with exactly k edges in a graph
- Topological Sort of a graph using departure time of vertex
- k'th heaviest adjacent node in a graph where each vertex has weight
- Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)
- Finding in and out degrees of all vertices in a graph
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Path in a Rectangle with Circles
- Some interesting shortest path questions | Set 1
- Shortest path in a Binary Maze
- Check if two nodes are on same path in a tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain