8a shortest paths more

Upload: meena-murugesan

Post on 03-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 8a Shortest Paths More

    1/22

    18a-ShortestPathsMore

    Shortest Paths

    in a Graph (contd)Fundamental Algorithms

  • 7/28/2019 8a Shortest Paths More

    2/22

    28a-ShortestPathsMore

    All-Pairs Shortest Paths

    We now want to compute a table giving thelength of the shortest path between any two

    vertices. (We also would like to get the shortest

    paths themselves.)

    We could just call Dijkstra or Bellman-Ford |V|

    times, passing a different source vertex each

    time.

    It can be done in (V3), which seems to be asgood as you can do on dense graphs.

  • 7/28/2019 8a Shortest Paths More

    3/22

    38a-ShortestPathsMore

    Doing APSP with SSSP

    Dijkstra would take time(VV2) = (V3) (standard version)

    (V (VlgV+ E)) = (V2lgV+ VE))

    (modified, Fibonacci heaps),

    but doesnt work with negative-weight edges.

    Bellman-Ford would take (VVE) = (V2E).

  • 7/28/2019 8a Shortest Paths More

    4/22

    48a-ShortestPathsMore

    The Floyd-Warshall AlgorithmRepresent the directed, edge-weighted graph in

    adjacency-matrix form.

    W= matrix of weights =

    w w w

    w w w

    w w w

    11 12 13

    21 22 23

    31 32 33

    wijis the weight of edge (i, j), or if there is nosuch edge.

    Return a matrix D, where each entry dij is d(i,j).

    Could also return a predecessor matrix, P, whereeach entry pij is the predecessor of j on theshortest path from i.

  • 7/28/2019 8a Shortest Paths More

    5/22

    58a-ShortestPathsMore

    Floyd-Warshall: Idea

    Considerintermediate vertices of a path:

    i j

    Say we know the length of the shortest path

    from i to j whose intermediate vertices are only

    those with numbers 1, 2, ..., k-1. Call this

    lengthNow how can we extend this from k-1 to k? In

    other words, we want to compute . Can we

    use , and if so how?

    dijk( )1

    dijk( )

    dijk( )1

  • 7/28/2019 8a Shortest Paths More

    6/22

    68a-ShortestPathsMore

    Floyd-Warshall Idea, 2

    Two possibilities:

    1. Going through the vertex kdoesnt help thepath through vertices 1...k-1 is still the shortest.

    2. There is a shorter path consisting of twosubpaths, one from ito kand one from ktoj.Each subpath passes only through vertices

    numbered 1 to k-1.

    j

    k

    i

  • 7/28/2019 8a Shortest Paths More

    7/22

    78a-ShortestPathsMore

    Floyd-Warshall Idea,3

    Thus,

    (since there are no intermediate vertices.)

    When k= |V|, were done.

    ijk

    ijk

    ikk

    kjk

    ij ij

    d d d d

    d w

    ( ) ( ) ( ) ( )

    ( )

    min( , )

    1 1 1

    0Also,

  • 7/28/2019 8a Shortest Paths More

    8/22

    88a-ShortestPathsMore

    Dynamic Programming

    Floyd-Warshall is a dynamic programming

    algorithm:

    Compute and store solutions to sub-

    problems. Combine those solutions to

    solve larger sub-problems.

    Here, the sub-problems involve finding the

    shortest paths through a subset of the vertices.

  • 7/28/2019 8a Shortest Paths More

    9/22

    98a-ShortestPathsMore

    Code for Floyd-WarshallFloyd-Warshall(W)

    simple.)areoperationsbecauseality,proportion

    ofconstant(Small(Vzippya:timeRunning

    return

    tofordo5to1ifordo

    tofor3

    verticesofnumber//

    3

    )(

    )(

    )(

    ).

    7

    ),min(6

    14

    1

    2

    ][1

    )1()1()1(

    0

    n

    k

    kj

    k

    ik

    k

    ij

    k

    ij

    D

    njn

    nk

    WD

    Wrowsn

    dddd

  • 7/28/2019 8a Shortest Paths More

    10/22

    108a-ShortestPathsMore

    Example of Floyd-Warshall

  • 7/28/2019 8a Shortest Paths More

    11/22

    118a-ShortestPathsMore

    Johnsons Algorithm

    Makes clever use of Bellman-Ford and Dijkstrato do All-Pairs-Shortest-Paths efficiently on

    sparse graphs.

    Motivation:By running Dijkstra |V| times, wecould do APSP in time (V2lgV+VElgV)(Modified Dijkstra), or(V2lgV+VE) (Fibonacci

    Dijkstra). This beats (V3) (Floyd-Warshall)

    when the graph is sparse.

    Problem: negative edge weights.

  • 7/28/2019 8a Shortest Paths More

    12/22

    128a-ShortestPathsMore

    The Basic Idea

    Reweight the edges so that:

    1. No edge weight is negative.

    2. Shortest paths are preserved. (Ashortest path in the original graph is still

    one in the new, reweighted graph.)An obvious attempt: subtract the minimumweight from all the edge weights. E.g. if theminimum weight is -2:

    -2 - -2 = 03 - -2 = 5

    etc.

  • 7/28/2019 8a Shortest Paths More

    13/22

    138a-ShortestPathsMore

    Counterexample

    Subtracting the minimum weight from every

    weight doesnt work.

    Consider:

    Paths with more edges are unfairly penalized.

    -2 -1

    -2

    0 1

    0

  • 7/28/2019 8a Shortest Paths More

    14/22

    148a-ShortestPathsMore

    Johnsons Insight

    Add a vertex s to the original graph G, with

    edges of weight 0 to each vertex in G:

    Assign new weights to each edge as follows:

    (u, v) = w(u, v) +d

    (s, u) -d

    (s, v)

    s0

    0

    0

  • 7/28/2019 8a Shortest Paths More

    15/22

    158a-ShortestPathsMore

    Question 1

    Are all the s non-negative? Yes:

    Otherwise, s u v would beshorter than the shortest path

    from s to v.

    s

    u

    v

    w(u, v)

    0),(),(),(

    ),(),(),(

    vsusvuw

    vsvuwus

    dd

    dd

    :Rewriting

    ),( vuw

    ),(),(),( vsvuwus dd bemust

  • 7/28/2019 8a Shortest Paths More

    16/22

    168a-ShortestPathsMore

    Question 2Does the reweighting preserve shortest paths? Yes: Consider any

    path kvvvp ,,, 21

    ),(),()(

    ),(),(),(

    ),(),(),(

    ),(),(),(

    ),()(

    1

    11

    3232

    2121

    1

    1

    1

    k

    kkkk

    k

    i

    ii

    vsvspw

    vsvsvvw

    vsvsvvw

    vsvsvvw

    vvwpw

    dd

    dd

    dd

    dd

    the sum

    telescopes

    A value that depends only on

    the endpoints, not on the path.

    In other words, we have adjusted the lengths of all paths by the same

    amount. So this will not affect the relative ordering of the paths

    shortest paths will be preserved.

  • 7/28/2019 8a Shortest Paths More

    17/22

    178a-ShortestPathsMore

    Question 3

    How do we compute the d(s, v)s?

    Use Bellman-Ford.

    This also tells us if we have a negative-weight

    cycle.

  • 7/28/2019 8a Shortest Paths More

    18/22

    188a-ShortestPathsMore

    Johnsons: Algorithm

    1. Compute G, which consists of G augmented

    with s and a zero-weight edge from s to

    every vertex in G.

    2. Run Bellman-Ford(G, w, s) to obtain the

    d(s,v)s

    3. Reweight by computing for each edge

    4. Run Dijkstra on each vertex to compute

    5. Undo reweighting factors to compute d

    d

  • 7/28/2019 8a Shortest Paths More

    19/22

    198a-ShortestPathsMore

    Johnsons: CLRS

  • 7/28/2019 8a Shortest Paths More

    20/22

    208a-ShortestPathsMore

    Johnson: reweighting

    (u, v) = w(u, v) + d(s, u) - d(s, v)

  • 7/28/2019 8a Shortest Paths More

    21/22

    218a-ShortestPathsMore

    Johnson using Dijkstra

  • 7/28/2019 8a Shortest Paths More

    22/22

    228a-ShortestPathsMore

    Johnsons: Running Time

    1. Computing G: (V)

    2. Bellman-Ford: (VE)

    3. Reweighting: (E)

    4. Running (Modified) Dijkstra: (V2lgV+VElgV)

    5. Adjusting distances: (V2)

    Total is dominated by Dijkstra: (V2lgV+VElgV)