dijkstra's

Upload: pravin2m

Post on 03-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Dijkstra's

    1/28

    Lecture 9: Dijkstras Shortest PathAlgorithm

    CLRS 24.3

    Outline of this Lecture

    Recalling the BFS solution of the shortest path

    problem for unweighted (di)graphs.

    The shortest path problem for weighted digraphs.

    Dijkstras algorithm.

    Given for digraphs but easily modified to work on

    undirected graphs.

    1

  • 7/28/2019 Dijkstra's

    2/28

    Recall: Shortest Path Problem for Graphs

    Let

    be a (di)graph.

    The shortest path between two vertices is a path

    with the shortest length (least number of edges).

    Call this the link-distance.

    Breadth-first-search is an algorithm for finding short-

    est (link-distance) paths from a single source ver-tex to all other vertices.

    BFS processes vertices in increasing order of their

    distance from the root vertex.

    BFS has running time

    .

    2

  • 7/28/2019 Dijkstra's

    3/28

    Shortest Path Problem for Weighted Graphs

    Let

    be a weighted digraph, with weight

    function

    mapping edges to real-valued

    weights. If

    , we write

    for

    .

    The length of a path $ &

    is the

    sum of the weights of its constituent edges:

    length

    $

    ( )

    ( 0

    (

    The distance from

    to

    , denoted 3

    , is the

    length of the minimum length path if there is a

    path from

    to

    ; and is 4 otherwise.

    5 6

    7 8

    5 6

    7 8

    5 6

    7 8

    5 6

    7 8

    5 6

    7 8

    9

    @

    A

    B

    C D

    E

    F

    G

    H

    I P

    P

    P

    P R

    T

    U

    U

    U

    U V

    I

    X

    X

    X

    X Y

    length( ` a c d c e c f h i) q

    distance from a to f isq

    3

  • 7/28/2019 Dijkstra's

    4/28

    Single-Source Shortest-Paths Problem

    The Problem: Given a digraph with positive edge

    weights

    and a distinguished source vertex,

    ,

    determine the distance and a shortest path from the

    source vertex to every vertex in the digraph.

    Question: How do you design an efficient algorithm

    for this problem?

    4

  • 7/28/2019 Dijkstra's

    5/28

    Single-Source Shortest-Paths Problem

    Important Observation: Any subpath of a shortest

    path must also be a shortest path. Why?

    Example: In the following digraph,

    9

    @

    A

    &

    is a short-

    est path. The subpath

    9

    @

    A

    &

    is also a shortest path.

    5 6

    7 8

    5 6

    7 8

    5 6

    7 8

    5 6

    7 8

    5 6

    7 8

    9

    @

    A

    B

    C D

    E

    F

    G

    H

    I P

    P

    P

    P R

    T

    U

    U

    U

    U V

    I

    X

    X

    X

    X Y

    length( ` a c d c e c f h i) q

    distance froma

    tof

    is

    q

    Observation Extending this idea we observe the ex-

    istence of a shortest path tree in which distance from

    source to vertex

    is length of shortest path from source

    to vertex in original tree.

    5

  • 7/28/2019 Dijkstra's

    6/28

    Intuition behind Dijkstras Algorithm

    Report the vertices in increasing order of their dis-

    tance from the source vertex.

    Construct the shortest path tree edge by edge; ateach step adding one new edge, corresponding

    to construction of shortest path to the current new

    vertex.

    6

  • 7/28/2019 Dijkstra's

    7/28

    The Rough Idea of Dijkstras Algorithm

    Maintain an estimateB

    of the length 3

    of

    the shortest path for each vertex

    .

    AlwaysB

    3

    andB

    equals the length

    of a known path(

    B

    4if we have no paths so far).

    InitiallyB

    and all the otherB

    values are

    set to 4 . The algorithm will then process the ver-

    tices one by one in some order.The processed vertexs estimate will be validated

    as being real shortest distance, i.e.B

    3

    Here processing a vertex

    means finding new

    paths and updatingB

    for all

    B

    if nec-

    essary. The process by which an estimate is up-

    dated is called relaxation.

    When all vertices have been processed,B

    3

    for all

    .

    7

  • 7/28/2019 Dijkstra's

    8/28

    The Rough Idea of Dijkstras Algorithm

    Question 1: How does the algorithm find new paths

    and do the relaxation?

    Question 2: In which order does the algorithm pro-

    cess the vertices one by one?

    8

  • 7/28/2019 Dijkstra's

    9/28

    Answer to Question 1

    Finding new paths. When processing a vertex

    ,

    the algorithm will examine all vertices

    B

    .

    For each vertex

    B

    , a new path from to

    is found (path from to

    + new edge).

    Relaxation. If the new path from to

    is shorter

    than

    B

    , then update

    B

    to the length of thisnew path.

    Remark: Whenever we setB

    to a finite value, there

    exists a path of that length. ThereforeB

    3

    .

    (Note: If

    )

    c

    i, then further relaxations cannot change

    its value.)

    9

  • 7/28/2019 Dijkstra's

    10/28

    Implementing the Idea of Relaxation

    Consider an edge from a vertex

    to

    whose weight is

    c

    i.

    Suppose that we have already processed

    so that we know

    )

    c iand also computed a current estimate for

    .Then

    There is a (shortest) path from

    to with length

    .

    There is a path from

    to

    with length

    .

    Combining this path from

    to

    with the edge

    c

    i

    , we obtainanother path from

    to

    with length

    c

    i.

    If

    c

    i

    , then we replace the old path`

    c c c

    h

    with the new shorter path`

    c c c

    h. Hence we update

    )

    c

    i

    f

    )

    (originally,

    f

    ) )

    ).

    s

    u

    v

    d[v]

    d[u]

    w

    10

  • 7/28/2019 Dijkstra's

    11/28

    The Algorithm for Relaxing an Edge

    Relax(u,v)

    if (B

    B

    )B

    B

    ;

    B

    ;

    Remark: The predecessor pointer B

    is for deter-

    mining the shortest paths.

    11

  • 7/28/2019 Dijkstra's

    12/28

    Idea of Dijkstras Algorithm: Repeated Relaxation

    Dijkstras algorithm operates by maintaining a sub-set of vertices,

    , for which we know the true

    distance, that isB

    3

    .

    Initially

    , the empty set, and we setB

    andB

    4 for all others vertices

    . One byone we select vertices from

    to add to .

    The set can be implemented using an array of

    vertex colors. Initially all vertices are white, and

    we set A

    black to indicate that

    .

    12

  • 7/28/2019 Dijkstra's

    13/28

    The Selection in Dijkstras Algorithm

    Recall Question 2: What is the best order in which

    to process vertices, so that the estimates are guaran-

    teed to converge to the true distances.That is, how does the algorithm select which vertex

    among the vertices of

    to process next?

    Answer: We use a greedy algorithm. For each ver-

    tex in

    , we have computed a distance es-

    timateB

    . The next vertex processed is always a

    vertex

    for whichB

    is minimum, that is,

    we take the unprocessed vertex that is closest (by our

    estimate) to .

    Question: How do we implement this selection of ver-

    tices efficiently?

    13

  • 7/28/2019 Dijkstra's

    14/28

    The Selection in Dijkstras Algorithm

    Question: How do we perform this selection efficiently?

    Answer: We store the vertices of

    in a priority

    queue, where the key value of each vertex

    isB

    .

    [Note: if we implement the priority queue using a heap,

    we can perform the operations Insert(), Extract Min(),

    and Decrease Key(), each in

    time.]

    14

  • 7/28/2019 Dijkstra's

    15/28

    Review of Priority Queues

    A Priority Queue is a data structure (can be imple-

    mented as a heap) which supports the following oper-

    ations:

    insert(

    ): Insert

    with the key value

    in .

    u = extractMin(): Extract the item with the minimum

    key value in .

    decreaseKey(

    -

    ): Decrease

    s key value to

    -

    .

    Remark: Priority Queues can be implementad such

    that each operation takes time

    . See CLRS!

    15

  • 7/28/2019 Dijkstra's

    16/28

    Description of Dijkstras Algorithm

    Dijkstra(G,w,s)

    % Initializefor (each

    )

    )

    ;e

    )

    white;

    )

    ;

    f

    )

    NIL;

    )

    (queue with all vertices);

    while (Non-Empty(

    )) % Process all vertices

    )

    Extract-Min

    i

    ; % Find new vertexfor (each

    )if (

    c

    i

    ) % If estimate improves

    )

    c

    i

    ; relaxDecrease-Key

    c

    c

    i

    ;

    f

    )

    ;

    e

    )

    black;

    16

  • 7/28/2019 Dijkstra's

    17/28

    Dijkstras Algorithm

    Example:

    s

    a

    b c

    d

    7

    2

    3 2

    1

    8

    5

    4 50

    inf

    inf

    inf

    inf

    Step 0: Initialization.

    s a b c dB

    0 4 4 4 4

    B

    nil nil nil nil nilA

    W W W W W

    Priority Queue:

    s a b c dB

    0 4 4 4 4

    17

  • 7/28/2019 Dijkstra's

    18/28

    Dijkstras Algorithm

    Example:

    s

    a

    b c

    d

    7

    2

    3 2

    1

    8

    5

    4 5

    inf

    inf

    7

    2

    0

    Step 1: As B

    9

    @

    , work on9

    and@

    andupdate information.

    s a b c dB

    0C

    4 4

    B

    nil s s nil nilA

    B W W W W

    Priority Queue:

    a b c dB

    C

    4 4

    18

  • 7/28/2019 Dijkstra's

    19/28

    Dijkstras Algorithm

    Example:

    s

    a

    b c

    d

    7

    2

    3 2

    1

    8

    5

    4 50

    2 7

    5 10

    Step 2: After Step 1, 9 has the minimum key in the

    priority queue. As B

    9

    @

    A

    B

    , work on@

    , A ,B

    and update information.

    s a b c dB

    0C

    E

    F

    B

    nil s a a aA

    B B W W W

    Priority Queue:

    b c dB

    E

    F

    19

  • 7/28/2019 Dijkstra's

    20/28

    Dijkstras Algorithm

    Example:

    s

    a

    b c

    d

    7

    2

    3 2

    1

    8

    5

    4 50

    2 7

    65

    Step 3: After Step 2,@

    has the minimum key in the

    priority queue. As B @

    9

    A

    , work on 9 , A and

    update information.

    s a b c dB

    0C

    E

    B

    nil s a b aA

    B B B W W

    Priority Queue:

    c dB

    20

  • 7/28/2019 Dijkstra's

    21/28

    Dijkstras Algorithm

    Example:

    s

    a

    b c

    d

    7

    2

    3 2

    1

    8

    5

    4 50

    2 7

    5 6

    Step 4: After Step 3,

    A

    has the minimum key in the pri-ority queue. As B

    A

    B

    , work onB

    and update

    information.

    s a b c dB

    0C

    E

    B

    nil s a b aA

    B B B B W

    Priority Queue:

    dB

    21

  • 7/28/2019 Dijkstra's

    22/28

    Dijkstras Algorithm

    Example:

    s

    a

    b c

    d

    7

    2

    3 2

    1

    8

    5

    4 50

    2

    5 6

    7

    Step 5: After Step 4,B

    has the minimum key in the pri-

    ority queue. As B B

    A

    , work onA

    and updateinformation.

    s a b c dB

    0C

    E

    B

    nil s a b aA

    B B B B B

    Priority Queue:

    .

    We are done.

    22

  • 7/28/2019 Dijkstra's

    23/28

    Dijkstras Algorithm

    Shortest Path Tree:

    )

    c i

    , where

    )

    f

    c

    i

    The array

    f

    is used to build the tree.

    s

    a

    b c

    d

    2

    3

    1

    5

    0

    2

    5 6

    7

    Example:

    s a b c d

    0

    q

    f

    nil s a b a

    23

  • 7/28/2019 Dijkstra's

    24/28

    Correctness of Dijkstras Algorithm

    Lemma: When a vertex

    is added to

    (i.e., dequeued from the

    queue),

    )

    c i

    .Proof: Suppose to the contrary that at some point Dijkstras al-gorithm first attempts to add a vertex to for which

    )

    c i

    . By our observations about relaxation,

    c i

    .

    Consider the situation just prior to the insertion of

    . Considerthe true shortest path from

    to

    . Because

    and

    ,at some point this path must fi rst take a jump out of

    . Let

    c i

    be the edge taken by the path, where

    and (itmay be that

    )

    and/or

    )

    ).

    s

    xy

    uS

    24

  • 7/28/2019 Dijkstra's

    25/28

    Correctness of Dijkstras Algorithm Continued

    We now prove that

    )

    c i. We have done relaxation

    when processing

    , so

    c i

    (1)

    Since

    is added to

    earlier, by hypothesis,

    )

    c

    i (2)

    Since`

    c c

    c h

    is subpath of a shortest path, by (2)

    c i

    )

    c

    i

    c i

    )

    c i (3)

    By (1) and (3),

    c i

    Hence

    )

    c i

    So

    )

    (because we suppose

    c i

    ).

    Now observe that since

    appears midway on the path from

    to

    , and all subsequent edges are positive, we have

    c i

    c i, and thus

    )

    c i

    c i

    Thus

    would have been added to before , in contradiction to

    our assumption that

    is the next vertex to be added to

    .

    25

  • 7/28/2019 Dijkstra's

    26/28

    Proof of the Correctness of Dijkstras Algorithm

    By the lemma,B

    3

    when

    is added

    into , that is when we set A

    black.

    At the end of the algorithm, all vertices are in ,

    then all distance estimates are correct.

    26

  • 7/28/2019 Dijkstra's

    27/28

    Analysis of Dijkstras Algorithm:

    The initialization uses only

    time.

    Each vertex is processed exactly once soNon-Empty()

    and Extract-Min() are called exactly once, e.g.,

    times in total.

    The inner loop for (each

    B

    ) is called once

    for each edge in the graph. Each call of the inner loop

    does

    F

    work plus, possibly, one Decrease-Keyoperation.

    Recalling that all of the priority queue operations re-

    quire

    time we have that the

    algorithm uses

    F

    time.

    27

  • 7/28/2019 Dijkstra's

    28/28

    Prove: Dijkstras algorithm processes vertices in non-

    decreasing order of their actual distance from the source

    vertex.

    28