Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single destination shortest-path in a graph

Given a graph and a destination node, how do you find all the shortest paths from all other vertices to the destination vertex.

like image 592
shreyasva Avatar asked Apr 25 '11 21:04

shreyasva


People also ask

How do you find the shortest path between source and destination?

Approach: The is to do a Breadth First Traversal (BFS) for a graph. Below are the steps: Start BFS traversal from source vertex. While doing BFS, store the shortest distance to each of the other nodes and also maintain a parent vector for each of the nodes.

Is Dijkstra single source shortest path?

The Dijkstra Shortest Path algorithm computes the shortest path between nodes. The algorithm supports weighted graphs with positive relationship weights. The Dijkstra Single-Source algorithm computes the shortest paths between a source node and all nodes reachable from that node.


1 Answers

Dijkstra's algorithm. You can work it backwards as if your destination is your starting vertex. This will give you the distance and path to any other node.

*PS: Just one thing to remember. You need to reverse the edges BEFORE applying Dijkstra with your destination as your starting vertex in order for that to work.

like image 192
Nik Avatar answered Nov 08 '22 09:11

Nik