Given a graph and a source vertex src in a weighted undirected graph, find the shortest paths from src to all vertices in the given graph. The graph may contain negative weight edges.
For this problem, we have already discussed Dijkstra’s algorithm and Bellman-Ford Algorithm. But D’Esopo-Pape Algorithm performs quite well in most of the cases. However there are some cases in which it takes exponential time.
Input: Adjacency List of the graph and source vertex src.
Output: Shortest distance to all vertices from src.
This Algorithm uses a Bi-directional queue that stores the vertices to be operated.
Following are the detailed steps of the algorithm.
- Intialize distance of the vertices from source to infinite in an array.
- Maintain a Queue which will store the vertices to be operated and also maintain a Boolean array for vertices which will be used to decide that the vertex is already present in the queue or not.
- Append source vertex in the queue.
- Start popping vertices from the queue until queue is empty, and perform following steps for each popped vertex(let U be the popped vertex) :
- Set the vertex U as not present in the queue.
- For each adjacent vertex V of U, check if its current minimum Distance[V] is greater than the distance through U,
i.e. Distance[U] + weight of edge connecting U and V.
- If yes, update Distance[V] = Distance[U] + weight of edge connecting U and V.
Check If V is not present in the queue with the help of Boolean Array:
- If V is entering the queue for the first time, append V at the back of the queue and set vertex V as present in the queue with the help of Boolean Array.
- Else append in the front of the queue and set V vertex as present in the queue.
- Return list Distance which will have shortest distance of every vertex from the source vertex.
Intially the Distance from source to itself will be 0 and for other vertices it will be infinite.
Now for each Adjacent vertex of source that is 0 in this case are [1, 4] update the distance and mark the vertices as present in the with weight of 4 and 8 respectively.
Now Deque the vertex 4 from the queue and following are the adjacent vertices are connected to the vertex 4 –
- Vertex 1 – As Vertex 1 have already visited and the weight to reach the vertex 1 is 4, where as when move to vertex 1 via the edge 4 — 1 from source the total weight will be 11 which is greater than weight stored in the distance array.
- Vertex 3 – As Vertex 3 is not visited and also not present in the queue, So the distance is updated to 9 for vertex 3 and also enqueued into the queue at the front.
- Vertex 4 – As vertex 4 is already visited and the weight is already minimum So Distance is not updated.
- Vertex 2 – As Vertex 2 is not visited and also not present in the queue, So the distance is updated to 11 for vertex 3 and also enqueued into the queue at the front.
Below is the implementation of the above approach.
[0, 4, 11, 9, 8]
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Multi Source Shortest Path in Unweighted Graph
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Shortest path to reach one prime to other by changing single digit at a time
- Dijkstra’s shortest path algorithm using set in STL
- Shortest Path Faster Algorithm
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Dijkstra's shortest path algorithm in Java using PriorityQueue
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Probabilistic shortest path routing algorithm for optical networks
- Find if there is a path of more than k length from a source
- Minimum edges to reverse to make path from a source to a destination
- Some interesting shortest path questions | Set 1
- Multistage Graph (Shortest Path)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.