All-Pairs Shortest Paths (26.0/25) - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

All-Pairs Shortest Paths (26.0/25)

Description:

Directed graph G = (V,E), weight E. Goal: Create n n matrix of s-p distances (u,v) ... dij(m) = mink{dik(m-1) wkj} Runtime = O( n4) because n-1 passes ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 8
Provided by: ALE7150
Learn more at: http://www.cs.gsu.edu
Category:
Tags: pairs | paths | shortest

less

Transcript and Presenter's Notes

Title: All-Pairs Shortest Paths (26.0/25)


1
All-Pairs Shortest Paths (26.0/25)
  • HW problem 26-1, p. 576/25-1, p. 641
  • Directed graph G (V,E), weight E ? ?
  • Goal Create n ? n matrix of s-p distances ?(u,v)
  • Running Bellman-Ford once from each vertex
  • O( ) O( ) on dense
    graphs
  • Adjacency-matrix representation of graph
  • n ? n matrix W (wij) of edge weights
  • assume wii 0 ?i,
  • s-p to self has no edges in absence of negative
    cycles

2
Dynamic Programming (26.1/25.1)
  • dij(m) weight of s-p from i to j with ? m edges
  • dij(0) 0 if i j and dij(0) ? if i ?
    j
  • dij(m) minkdik(m-1) wkj
  • Runtime O( n4)
  • because n-1 passes
  • each computing n2 ds in O(n) time

? m-1
j
i
? m-1
3
Matrix Multiplication (26.1/25.1)
  • Similar C A ? B, two n ? n matrices
  • cij ?k aik ? bkj O(n3) operations
  • replacing ? min
  • ? ?
  • gives cij mink aik bkj
  • D(m) D(m-1) ? W
  • identity matrix is D(0)
  • Cannot use Strassens because no subtraction
  • Time is still O(n ? n3 ) O(n4 )
  • Repeated squaring W2n Wn ? Wn
  • Compute W, W2 , W4 ,..., W2k , k log n,
    O(n3 log n)

4
Floyd-Warshall Algorithm (26.2/25.2)
  • Also dynamic programming but faster (by log n)
  • cij(m) weight of s-p from i to j with
    intermediate vertices in the set 1, 2, ..., m
    ? ?(i, j) cij(n)
  • DP compute cij(n) in terms of smaller cij(n-1)
  • cij(0) wij
  • cij(m) min cij(m) , cim(m-1) cmj(m-1)
  • intermediate nodes in 1,
    2, ..., m

cmj(m-1)
m
cim(m-1)
j
i
cij(m-1)
5
Floyd-Warshall Algorithm (26.2/25.2)
  • Difference from previous we do not check all
    possible intermediate vertices.
  • Code for m1..n do for i1..n do for j 1..n do
  • cij(m) min cij(m-1) , cim(m-1)
    cmj(m-1)
  • Runtime O(n3 )
  • Transitive Closure G of graph G
  • (i,j) ? G iff ? path from i to j in G
  • Adjacency matrix, elements on 0,1
  • Floyd-Warshall with min ? OR ,
    ? AND
  • Runtime O(n3 )
  • Useful in many problems

6
Johnsons Algorithm (26.3/25.3)
  • Johnsons Bellman-Ford Dijkstras (or
    Thorups)
  • Re-weighting of the graph G(V,E,w)
  • assign weights to all vertices h V ? ?
  • change weight of edges w(u,v) w(u,v) h(u) -
    h(v)
  • then for any u,v ?(u, v) ?(u,v) h(u) -
    h(v)
  • Addition of auxiliary vertex s
  • V ? V s
  • E ? V (s,v), v ? V , w(s,v) 0
  • Run Bellman-Ford find h(v) ?(s,v) (or negat.
    cycle)
  • w(u,v) ? 0 since h(v) ? h(u) w(u,v) and
  • w(u,v) w(u,v) h(u) - h(v) ? 0

7
Johnsons Algorithm (26.3/25.3)
  • Since all weights are nonnegative,
  • apply Dijkstras for each source and
  • find all modified distances ?(u, v)
  • Restore actual distances from modified using
  • ?(u, v) ?(u,v) - h(u) h(v)
  • Runtime
  • O(VE) V times O(E) - M.Thorups algorithm
  • O(V(EV log V)) V times Dijkstras with
    Fibonacci heaps
  • O(VE log V) V times Dijkstras with binary
    heaps (faster for sparse graphs)
  • To find s-p for a single pair s-p from single
    source all pairs shortest paths.
Write a Comment
User Comments (0)
About PowerShow.com