13 ShortestPath II

Embed Size (px)

Citation preview

  • 8/2/2019 13 ShortestPath II

    1/41

    Huo Hongwei 1

    Design and Analysis of Algorithms

    Shortest Paths II

    Reference:CLRS Chapter 24

    Topics:

    Bellman-Ford algorithm

    DAG shortest paths

    Linear programming and difference constraints

    VLSI layout compaction

  • 8/2/2019 13 ShortestPath II

    2/41

    Huo Hongwei 2

    Negative-weight cycles

    Recall: If a graph G = (V,E) contains a negative-weightcycle, then some shortest paths may not exist.

    Example:

    u vxx

    < 0 xx

  • 8/2/2019 13 ShortestPath II

    3/41

    Huo Hongwei 3

    Negative-weight cycles

    Recall: If a graph G = (V,E) contains a negative-weightcycle, then some shortest paths may not exist.

    Example:

    Bellman-Ford algorithm: Finds all shortest-path lengths

    from a source s Vto all v Vor determines thatnegative-weight cycle exists.

    u vxx

    < 0 xx

  • 8/2/2019 13 ShortestPath II

    4/41

    Huo Hongwei 4

    Bellman-Ford algorithm

    Time = O(VE).

    BELLMAN-FORD(G,w,s)

    1 d[s] 02 for each v V {s}3 do d[v] 4 for i 1 to |V|-15 do for each edge (u,v) E6 do if d[v] > d[u] + w(u, v)7 then d[v] d[u] + w(u, v)8 for each edge (u,v) E9 do if d[v] > d[u] + w(u,v)

    10 then report that a negative-weight cycle exists.

    11 return d[v] // = (s, u), if no negative-weight cycles

    BELLMAN-FORD's ALGORITHM

    Relaxationstep

    initialization

  • 8/2/2019 13 ShortestPath II

    5/41

    Huo Hongwei 5

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

  • 8/2/2019 13 ShortestPath II

    6/41

    Huo Hongwei 6

    Example of Bellman-Ford

    Initialization

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

  • 8/2/2019 13 ShortestPath II

    7/41

    Huo Hongwei 7

    Example of Bellman-Ford

    Order of edge relaxation

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    8/41

    Huo Hongwei 8

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    9/41

    Huo Hongwei 9

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    10/41

    Huo Hongwei 10

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    11/41

    Huo Hongwei 11

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    -1

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    12/41

    Huo Hongwei 12

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    4

    -1

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    13/41

    Huo Hongwei 13

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    4

    -1

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    14/41

    Huo Hongwei 14

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2

    -1

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    15/41

    Huo Hongwei 15

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2

    -1

    4 71

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    16/41

    Huo Hongwei 16

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2

    -1

    4 71

    52

    6

    8

    3

    End of pass 1

  • 8/2/2019 13 ShortestPath II

    17/41

    Huo Hongwei 17

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    18/41

    Huo Hongwei 18

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    19/41

    Huo Hongwei 19

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 1

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    20/41

    Huo Hongwei 20

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 1

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    21/41

    Huo Hongwei 21

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 1

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    22/41

    Huo Hongwei 22

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 1

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    23/41

    Huo Hongwei 23

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 1

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    24/41

    Huo Hongwei 24

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 -2

    -1

    147

    1

    52

    6

    8

    3

  • 8/2/2019 13 ShortestPath II

    25/41

    Huo Hongwei 25

    Example of Bellman-Ford

    A

    C

    B

    E

    D

    1

    -12

    2

    5

    3

    4 -3

    0

    2 -2

    -1

    4 71

    52

    6

    8

    3

    End of pass 2 (and 3 and 4)

  • 8/2/2019 13 ShortestPath II

    26/41

    Huo Hongwei 26

    Correctness

    Theorem. If G = (V,E) contains no negative-weightcycles, then after the Bellman-Ford algorithm executes,

    d[v] = (s, v) for all v V.Proof. Let v Vbe any vertex, and consider a shortestpathp froms to v with the minimum number of edges.

    Sincep is a shortest path, we have(s, vi) = (s, vi-1) + w(vi-1, vi)

    v

    v0v1

    v2v3 vk

    p:s

  • 8/2/2019 13 ShortestPath II

    27/41

    Huo Hongwei 27

    Correctness (continued)

    Initially,d[v0] = 0 = (s, v0) andd[v0] is unchanged by subsequentrelaxations (because of the the lemma from previous Lecture that

    d[v] (s, v)).

    After 1 pass throughE, we haved[v1] = (s, v1). After 2 pass throughE, we haved[v2] = (s, v2).

    Afterk pass throughE, we haved[vk] = (s, vk).Since G contains no negative-weight cycles,p is simple.

    Longest simple path has |V| -1 edges.

    v

    v0v1

    v2v3 vk

    p:

    s

  • 8/2/2019 13 ShortestPath II

    28/41

    Huo Hongwei 28

    Detection of negative-weight cycles

    Corollary. If a valued[v] fails to converge after |V| -1passes, there exists a negative-weight cycle in G

    reachable froms.

  • 8/2/2019 13 ShortestPath II

    29/41

    Huo Hongwei 29

    Linear programming

    LetA be anm n matrix,b be anm-vector, andc be ann-vector. Find ann-vectorx that maximizescTx subject to

    Ax b, or determine that no such solution exists.n

    m

    A x b

    .

    cT x

    .maximizing

  • 8/2/2019 13 ShortestPath II

    30/41

    Huo Hongwei 30

    Linear programming algorithm

    Algorithms for the general problem

    Simplex methods practical, but worst-case

    exponential time.

    Interior-point methods polynomial time andcompetes with simplex.

    Feasibility problem: No optimization criterion. Just findx such thatAx b. In general, just as hard as ordinary LP.

  • 8/2/2019 13 ShortestPath II

    31/41

    Huo Hongwei 31

    Solving a system of difference constrains

    Linear programming where each row ofA containsexactly one 1, one 1, and the rest 0s.

    Example:

    x1 x2 3x2 x3 -2x1 x3 2

    xj xi wij

  • 8/2/2019 13 ShortestPath II

    32/41

    Huo Hongwei 32

    Solving a system of difference constrains

    Linear programming where each row ofA containsexactly one 1, one 1, and the rest 0s.

    Example:

    x1 x2 3x2 x3 -2x1 x3 2

    xj xi wijx1 = 3

    x2 = 0

    x3 = 2

    Solution:

  • 8/2/2019 13 ShortestPath II

    33/41

    Huo Hongwei 33

    Solving a system of difference constrains

    Linear programming where each row ofA containsexactly one 1, one 1, and the rest 0s.

    Example:

    Constraint graph:

    x1 = 3

    x2 = 0

    x3 = 2

    Solution:

    xj xi wij vi wij vj(The A

    matrix hasdimensions

    |E| |V|.)

    x1 x2 3x2 x3 -2x1 x3 2

    xj xi wij

  • 8/2/2019 13 ShortestPath II

    34/41

    Huo Hongwei 34

    Unsatisfiable constraints

    Theorem. If the constraint graph contains a negative-weight cycle, then the system of differences is

    unsatisfiable.Proof. Suppose that the negative-weight cycle is v1

    v2 vkv1. Then, we havex2 x1 w12x3 x2 w23

    xk xk-1 wk-1,kx1 xk wk1

  • 8/2/2019 13 ShortestPath II

    35/41

    Huo Hongwei 35

    Unsatisfiable constraints

    Theorem. If the constraint graph contains a negative-weight cycle, then the system of differences is

    unsatisfiable.Proof. Suppose that the negative-weight cycle is v1

    v2 vkv1. Then, we havex2 x1 w12x3 x2 w23

    xk xk-1 wk-1,kx1 xk wk1

    0 weight of cycle< 0

    Therefore, novalues for thexican satisfy the

    constraints.

  • 8/2/2019 13 ShortestPath II

    36/41

    Huo Hongwei 36

    Satisfying the constraints

    Theorem. Suppose no negative-weight cycle exists inthe constraint graph. Then, the constraint is satisfiable.

    Proof. Add a new vertexs to Vwith a 0-weight edge toeach vertex vi V.

    v1

    v4

    v9

    v7

    v3

  • 8/2/2019 13 ShortestPath II

    37/41

    Huo Hongwei 37

    Satisfying the constraints

    Theorem. Suppose no negative-weight cycle exists inthe constraint graph. Then, the constraint is satisfiable.

    Proof. Add a new vertexs to Vwith a 0-weight edge toeach vertex vi V.

    s

    v1

    v4

    v9

    v7

    v3

    Note: no negative-weight introduced shortest paths exist.

  • 8/2/2019 13 ShortestPath II

    38/41

    Huo Hongwei 38

    Proof (continued)

    Claim: The assignmentxi = (s, vi) solves the constraint.Consider any constraintxj xi wij, and consider theshortest paths froms to vj and vi:

    The triangle inequality gives us (s, vj) (s, vi) + wij.Sincexi = (s, vi) andxj = (s, vj), the constraintxj xi wijis satisfied.

    s vi

    vj

    (s, vi)

    wij(s, vj)

    B ll F d d li i

  • 8/2/2019 13 ShortestPath II

    39/41

    Huo Hongwei 39

    Bellman-Ford and linear programming

    Corollary. The Bellman-Ford algorithm can solve asystem ofm difference constraints onn variable in

    O(mn) time. Single-source shortest paths is a simple LP problem.

    In fact, Bellman-Ford maximizesx1 +x2 + + xn subject

    to the constraints xj xi

    wij andxi

    0. Bellman-Ford also minimizes maxi{xi} mini{xi}.

    A li i VLSI l i

  • 8/2/2019 13 ShortestPath II

    40/41

    Huo Hongwei 40

    Application to VLSI layout compaction

    Problem: Compact (in one dimension) the spacebetween the features of a VLSI layout without bringingany features too close together.

    minimum separation

    Integrated-circuit features

    A li ti t VLSI l t ti

  • 8/2/2019 13 ShortestPath II

    41/41

    Huo Hongwei 41

    Application to VLSI layout compaction

    Constraint: x2 x1 d1 + Bellman-Ford minimizes maxi{xi} mini{xi}, which

    compacts the layout in thex-dimension.

    1

    d1

    x1 x2

    2