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 (BFS) is the correct answer for finding the shortest path in a directed graph where the weight of every edge is the same. Here's why:
Breadth First Traversal (BFS)
BFS is a graph traversal algorithm that explores all the vertices of a graph in breadth-first order, i.e., it explores all the vertices at the same level before moving to the next level. BFS can be used to find the shortest path in an unweighted graph or in a graph where the weight of every edge is the same.
Algorithm:
1. Create a queue and enqueue the source vertex.
2. Create a visited array to keep track of visited vertices and initialize it with false values.
3. Mark the source vertex as visited.
4. While the queue is not empty, do the following:
- Dequeue a vertex from the queue.
- If the dequeued vertex is the destination vertex, the path is found. Return the path.
- Otherwise, enqueue all the adjacent vertices of the dequeued vertex (which are not visited yet) and mark them as visited.
5. If the queue becomes empty and the destination vertex is not reached, it means there is no path from the source to the destination.
Explanation:
When the weight of every edge is the same, BFS guarantees that the first time a vertex is discovered during the traversal, that distance would give the shortest path to reach that vertex. This is because BFS explores the graph level by level, and as the weight of every edge is the same, the path length will be the minimum number of edges required to reach the destination vertex.
Comparison with Dijkstra's Algorithm:
Dijkstra's algorithm is used to find the shortest path in a weighted graph where the weight of edges can be different. It uses a priority queue to select the vertex with the smallest distance. In the case of a graph with the same weight for every edge, Dijkstra's algorithm will still work correctly, but it is less efficient compared to BFS as it involves unnecessary calculations and operations associated with edge weights.
Therefore, when the weight of every edge is the same, BFS is the most efficient choice for finding the shortest path in a directed graph.
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).