2

Given an undirected weighted graph, a start, and an end point. You need to find the shortest path to that end point, but throw an error if multiple shortest paths are found. How would we do the error part? Is there a way to check if multiple shortest paths to a destination node exist?

My idea is that when we pop out of the priority queue in dijkstra's algorithm, at that time if the node is the destination node, then we check if in this priority queue, another element exists for the same destination node. During the relaxation, instead of only pushing to the queue if the distance is less than, we can push if the distance is less than or equal to. But I am not sure about it.

EDIT - Its a weighted undirected graph

subspring
  • 690
  • 2
  • 7
  • 23
  • As I recall, upon solving with Dijkstra at each node `n` a record exists of both the length of the shortest path from the origin to `n` and a node immediately preceding `n` on a shortest path from the origin to `n` (needed to to reconstruct a shortest path from the origin to the destination). I would think that when ties occurred during calculations--two or more nodes immediately preceding `n` were on paths from the origin to `n` that had the same shortest known length--one could record all such immediately preceding notes, not just any one of them, the latter being what Dijkstra does... – Cary Swoveland Feb 27 '22 at 06:36
  • ...Then when back-tracking from the destination (or from any other node) one could determine whether there were multiple shortest paths by simply observing whether any of the arrays of immediately preceding nodes contained more than one node. – Cary Swoveland Feb 27 '22 at 06:38
  • that does sound like a good idea. Basically while mapping the path from source to destination, if at the same level in the path, more than two nodes exist while traversing, we have multiple routes. Thanks! – Abhimanyue Singh Tanwar Feb 27 '22 at 07:44
  • An undirected acyclic graph is a tree (or forest); in a tree, there is exactly one path between any two nodes, no checking required and Dijkstra's isn't necessary. Did you mean 'directed graph'? – kcsquared Feb 27 '22 at 09:29
  • Apologies for the confusion here, it's a weighted undirected acyclic graph – Abhimanyue Singh Tanwar Feb 27 '22 at 12:05
  • That’s still a tree regardless of weights; you don’t need to check for multiple paths. – kcsquared Feb 27 '22 at 12:11

1 Answers1

1

One way to do this is by creating a shortest path DAG. Whenever you relax some edge from node A to node B with cost C (assuming the current shortest distance from source to each node is stored in array dist), if dist[A] + C is greater than dist[B] then do nothing, if dist[A] + C is equal to dist[B], then we can reach B in a shortest path using a different route than before, so we add A to the list of nodes that can reach B in its shortest path (let's call this array pars), so we add A to pars of B, and finally if dist[A] + C is less than dist[B], then we update dist[B] and clear the previous values from pars[B], and add A to pars[B].

The resulting graph is guaranteed to be a DAG if all edge weights are strictly greater than 0. Then you can count the number of paths to the destination node using some easy dynamic programming methods, process node in a topological order, then the number of paths of each node is the sum of number of paths of nodes that reach it (nodes in pars[node]).

Hopefully this was useful and clear.

subspring
  • 690
  • 2
  • 7
  • 23