3

I'm currently studying shortest path problems in graphs with non-negative edge weight.

I know that Dijkstra algorithm can give me the solution to the single-source shortest path problem ie one can find the shortest path from one node to all other nodes but I haven't found algorithm that can give me the solution to a a priori simpler problem : find the shortest path between two nodes.

Intuitively, I think that one can find examples that show that the "simpler" problem isn't simpler than the single-source shortest path problem but I'm looking for references that show this contradiction (a priori) on simple (ie with a few number of nodes) graphs.

5
  • I am not sure I understand your question. Dijkstra easily tells you the shortest path between a single source and a single destination - that is kinda the "base case". And with simple extensions (and graph reversal support) you get all other combinations as well. Set of sources to single destination, single source to set of destination, set of sources to set of destinations, single source to all, all to single destination. Commented May 11, 2022 at 8:46
  • If it helps, here is a library that provides all query combinations with Dijkstra: github.com/Zabuzard/Maglev (shameless self promotion) Commented May 11, 2022 at 8:47
  • In my understanding of Dijkstra algorithm you get at the end shortest path from a single source to all the other destinations. This is what you call single source to all. Commented May 11, 2022 at 8:48
  • 1
    Well, depends. You are supposed to stop the algorithm once it found the shortest path to your destination - and not continue it all the way to the end. You can then just ignore the "by-products" and only focus on the single destination you are interested in. Due to the nature of Dijkstra, once it found the shortest path to your destination, it also found shortest paths to all other nodes that are reachable within the same (or less) distance - you can not prevent that. You can provide more goal-steering using heuristics, the algorithm is then called A* (A-star) instead. Commented May 11, 2022 at 8:50
  • 1
    Thanks! I think you made me understand one thing about this algorithm that clarifies my problem. Commented May 11, 2022 at 8:57

1 Answer 1

0

Some (but not all) single-source shortest paths algorithms can be easily modified to return the shortest path between two nodes by stopping the algorithm early. A simple example of this is breadth-first search in unweighted graphs. For example, to find the shortest path from a node u to a node v, start a BFS from u. As soon as v is found, the path from u to v discovered that way is the shortest path. Dijkstra’s algorithm can also do this: if you run a Dijkstra’s algorithm starting at node u, you can stop the algorithm as soon as you dequeue v from the priority queue to get the shortest path there.

These approaches are usually faster than running the whole algorithm to completion. But if you’re interested specifically in finding a path from one node to another, you might want to look at the A* search algorithm. This is a modification of Dijkstra’s algorithm that’s specifically optimized for the problem of getting from one node to another. It uses a heuristic to guide the search toward the target, deprioritizing searching for other nodes, and thus is much faster than Dijkstra’s algorithm in general.

As a note, not all shortest paths algorithms can be cut off early this way. For example, when there are negative edge weights but no negative cycles, Bellman-Ford can still compute shortest paths. However, it may continually revise node distances as it runs, up to the last round of the algorithm. Cutting the search off early can give back the wrong answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.