All-Pairs Shortest Paths Problem

Dynamic Programming Solution

  • Structure of a shortest path: decomposing path to
    • is still a shortest path

Let be the minimum weight of any containing at most edges, we have

The actual weight is given by

EXTEND-SHORTEST-PATHS( L(r-1), W, L(r), n )
    for i = 1 to n
        for j = 1 to n
            for k = 1 to n
                L(r)[i][j] = min( L(r)[i][j], L(r-1)[i][k] + W[k][j] )
  • Invoking EXTEND-SHORTEST-PATHS times =
  • Using repeated squaring =

Other Solutions