Given a directed graph where weight of every edge is same, we can effi...
With BFS, we first find explore vertices at one edge distance, then all vertices at 2 edge distance, and so on.
View all questions of this test
Given a directed graph where weight of every edge is same, we can effi...
Breadth First Traversal is the correct option.
Explanation:
Breadth First Traversal (BFS) can be used to find the shortest path in a directed graph where the weight of every edge is the same. BFS explores all the vertices of a graph in a level-by-level manner, starting from the source vertex. It visits all the vertices that are at a distance of 1 from the source, then visits all the vertices that are at a distance of 2, and so on.
Steps to find the shortest path using BFS:
1. Start BFS traversal from the source vertex.
2. Maintain a queue to store the vertices to be visited. Enqueue the source vertex.
3. Maintain a visited array to keep track of the visited vertices.
4. Initialize the visited array with false for all vertices except the source vertex.
5. While the queue is not empty, do the following steps:
a. Dequeue a vertex from the queue.
b. If the dequeued vertex is the destination vertex, then the shortest path is found.
c. Otherwise, enqueue all the adjacent vertices of the dequeued vertex that are not visited yet.
d. Mark the dequeued vertex as visited.
6. If the destination vertex is not reached after the BFS traversal, then there is no path from the source to the destination.
Why BFS is efficient in this case?
In a directed graph with the same weight on every edge, BFS guarantees finding the shortest path from the source to the destination vertex. This is because BFS visits the vertices in a level-by-level manner, exploring all the vertices at distance 1 before moving on to vertices at distance 2, and so on. Since the weight of every edge is the same, the path with fewer edges will always be the shortest path.
Example:
Consider a graph with vertices A, B, C, D, and E, where edges are directed and have a weight of 1. Let the source vertex be A and the destination vertex be D. The graph can be represented as:
```
A -> B
A -> C
B -> C
B -> D
C -> D
C -> E
```
Using BFS, the shortest path from A to D can be found as: A -> B -> D.
Hence, Breadth First Traversal is an efficient way to find the shortest path in a directed graph with the same weight on every edge.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).