0

As I understand one can modify BFS for unweighted graph or Dijakstra for weighted graph to find all possible shortest paths from $s$ to $t$ in linear time. But how can this be, when there are $O(2^n)$ such paths? What am I missing here?

David Richerby
  • 82,470
  • 26
  • 145
  • 239

1 Answers1

2

As you (almost) observe, there can be $2^{\Theta(n)}$ shortest paths between a pair of vertices:

       *   *           *
      / \ / \ /     \ / \
    s*   *   *  ...  *   *t
      \ / \ /       / \ /
       *   *           *

With $k$ cycles, this graph has $3k+1$ vertices and there are $2^k = 2^{n/3-1}$ shortest $s$–$t$ paths: you can go clockwise or anticlockwise around each cycle, independently of the others.

The simple answer to your question is that you are mistaken. It is impossible for any algorithm to output all shortest paths in linear time (time $O(n)$), just because it is impossible to output an exponential amount of data in a polynomial number of steps: a Turing machine can only write one character of output per step.

Outputting all shortest paths is an example of an enumeration problem. Typically, we use different measures for these problems because it seems "unfair" to say that an algorithm is slow just because it has a lot of work to do. The usual standard for enumeration algorithms is to look at the number of steps between each successive output. Using modified Dijkstra will (I assume) have polynomial delay: there's a constant $c$ such that, for any large enough graph on $n$ vertices, the algorithm will output some shortest path after at most $n^c$ steps and, after each output, there will be at most $n^c$ more steps before the algorithm outputs another shortest path or terminates.

David Richerby
  • 82,470
  • 26
  • 145
  • 239