Shotest Path Algorithm Dijikstra’s Algorithm

download Shotest Path Algorithm Dijikstra’s Algorithm

of 21

Transcript of Shotest Path Algorithm Dijikstra’s Algorithm

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    1/21

    Shortest Path Algorithm

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    2/21

     AVL Trees / Slide 2

    2

    Shortest Path Algorithm

    Definition:Shortest path between two vertices u

    and v of a graph G is a path between u and v

    with minimum number of edges in the path for

    an unweighted graph.

    For a weighted graph, it is the path

     between u and v for which the sum of the

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    3/21

     AVL Trees / Slide 3

    3

    Shortest Path Algorithm

    Versions of Shortest Path Algorithm:

    .!nwighted shortest path problem.

    ".#eighted shortest problem with no

    negative edges.

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    4/21

     AVL Trees / Slide 4

    !nwighted shortest path problem.

    Example:

    Find the shortest path from S to ever$ other

    Verte% in the graph G.

    v1 v2

    v3 v4 v5

    v6 v7

     V K dv pv

     v1 0 0

     v2 0 0

     v3 0 0 0

     v4 0 0

     v5 0 0

     v6 0 0

     v7 0 0&nitiali'e a table with following values:

    (nown Field for all vertices are set to 'ero)*+.Distance vector dv for all vertices are set to infinit$.

    &nitial onfiguration -able

    S

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    5/21

     AVL Trees / Slide 5

    !nwighted shortest path problem.

    Step:/ar0 the start node as reachable in 'ero edges

    v1 v2

    v3 v4 v5

    v6 v7

     V K dv pv

     v1 0 0

     v2 0 0

     v3 1 0 0

     v4 0 0

     v5 0 0

     v6 0 0

     v7 0 0

    0

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    6/21

     AVL Trees / Slide 6

    !nwighted shortest path problem.

    Step":

    Find all the vertices that are at a distance awa$ from s./ar0 them reachable with distance

    i.e Vertices V and V1 )onl$ v and v1 are ad2acent to v3+

    v1 v2

    v3 v4 v5

    v6 v7

     V K Dv pv

     v1 1 1 v3

     v2 0 0

     v3 1 0 0

     v4 0 0

     v5 0 0

     v6 1 1 v3

     v7 0 0

    0

    1

    1

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    7/21

     AVL Trees / Slide 7

    !nwighted shortest path problem.

    v1 v2

    v3 v4 v5

    v6 v7

    0

    1

    1

    2

    2

     V K Dv pv

     v1 1 1 v3

     v2 1 2 v1

     v3 1 0 0

     v4 1 2 v1

     v5 0 0

     v6 1 1 v3

     v7 0 0

    Step3:

    Find all the vertices that are at a distance " awa$ from s./ar0 them reachable with distance ".

    i.e Vertices V" and V4 )onl$ v" and v" are ad2acent to v3+

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    8/21

     AVL Trees / Slide 8

    !nwighted shortest path problem.

    v1 v2

    v3 v4 v5

    v6 v7

    0

    1

    1

    2

    2 V K Dv pv

     v1 1 1 v3

     v2 1 2 v1

     v3 1 0 0

     v4 1 2 v1

     v5 1 3 v2

     v6 1 1 v3

     v7 1 3 v4

    3

    3

    Step4:

    Find all the vertices that are at a distance 3 awa$ from s./ar0 them reachable with distance 3.

    i.e Vertices V5 and V6 are ad2acent to v" 7 v4 so update

    the dv and pv in the table and mar0 it as 0nown.

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    9/21

     AVL Trees / Slide 9

    Di2i0stra8s Algorithm

    Definition:

    -he general method to solve the single

    source shortest path problem is 0nown as Di2i0stra8s

    algorithm.

    -he algorithm is an e%ample of greed$ algorithm.

    Greed$ algorithms generall$ solve a problem in stages

     b$ doing what appears to be the best thing at each

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    10/21

     AVL Trees / Slide 10

    Di2i0stra8s Algorithm

    At each stage, Di2i0stra8s algorithm selects a verte% 9v8, which has

    the smallest distance dv among all the un0nown vertices, and

    declares that the shortst path from s to v is 0nown.

    -he remainder of a stage consists of updating the values of dw.

    &n unweighted graph, we set dwdv ; if dw infinit$.

    &n weighted case, we set dw dv ; v,w if this new value of dw

    would be an improvement.

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    11/21

     AVL Trees / Slide 11

    Di2i0stra8s Algorithm

    Find the shortest path from v to all the other vertices

     V K dv pv

     v1 0 0 0

     v2 0 0

     v3 0 0 0

     v4 0 0

     v5 0 0

     v6 0 0

     v7 0 0

    &nitial configuration -able

    v1 v2

    v3 v4 v5

    v6 v7

    4

    2

    1 3 10

    22

    5

    1

    6

    48

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    12/21

     AVL Trees / Slide 12

    Di2i0stra8s Algorithm

     

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    13/21

     AVL Trees / Slide 13

    Di2i0stra8s Algorithm

     

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    14/21

     AVL Trees / Slide 14

    Di2i0stra8s Algorithm

    Find the ad2acent vertices of v". v4 7 v5 are ad2acent

    vertices of v". Since the verte% v4 has alread$ set to

    0nown to , no need to consider v4.

    =ut v58s 0nown field is still *. So find dw for v5.V K dv pv

     v1 1 0 0

     v2 1 2 V1

     v3 0 3 v4

     v4 1 1 v1

     v5 0 3 v4

     v6 0 9 v4

     v7 0 5 v4

    So, dw dv ; v,w

    " ; *

      "

    =ut in the table to v5 there is alread$ an entr$

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    15/21

     AVL Trees / Slide 15

    Di2i0stra8s Algorithm

     ence mar0 it as

    0nown.

     V K dv pv

     v1 1 0 0

     v2 1 2 V1

     v3 1 3 v4

     v4 1 1 v1 v5 1 3 v4

     v6 0 9 v4

     v7 0 5 v4

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    16/21

     AVL Trees / Slide 16

    Di2i0stra8s Algorithm

    Find the ad2acent vertices of v3 7 v5. v 7 v1 are

    ad2acent to v3. Since v is alread$ mar0ed 0nown no

    need to consider v. alculate dw for v1.

    So, dw dv ; v,w

    3 ; 5

      ?

    =ut in the table to v1 there is alread$ an entr$

    V K dv pv

     v1 1 0 0

     v2 1 2 V1

     v3 1 3 v4

     v4 1 1 v1

     v5 1 3 v4

     v6 0 9, 8 v4, v3

     v7 0 5 v4

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    17/21

     AVL Trees / Slide 17

    Di2i0stra8s Algorithm

    For V5 the ad2acent verte% is v6.

    alculate dw for v6.

    So, dw dv ; v,w

    3 ; 1

      @=ut in the table to v6 there is alread$ an entr$

    from v3 with dv 5 which is shortest when

    compared to the calculated dw, >ence no

    need to alter the table entries.

     V K dv pv

     v1 1 0 0

     v2 1 2 V1

     v3 1 3 v4

     v4 1 1 v1

     v5 1 3 v4

     v6 0 8 v3

     v7 0 5 v4

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    18/21

     AVL Trees / Slide 18

    Di2i0stra8s Algorithm

     

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    19/21

     AVL Trees / Slide 19

    Di2i0stra8s Algorithm

     

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    20/21

     AVL Trees / Slide 20

    Di2i0stra8s Algorithm

    -he left out entr$ is v1. v1 is finall$ mar0ed 0nown.

     V K dv pv

     v1 1 0 0

     v2 1 2 V1

     v3 1 3 v4

     v4 1 1 v1

     v5 1 3 v4

     v6 1 6 v7

     v7 1 5 v4

  • 8/18/2019 Shotest Path Algorithm Dijikstra’s Algorithm

    21/21

     AVL Trees / Slide 21Di2i0stra8s Algorithm

    -he left out entr$ is v1. v1 is finall$ mar0ed 0nown. V K dv pv

     v1 1 0 0

     v2 1 2 V1

     v3 1 3 v4

     v4 1 1 v1

     v5 1 3 v4

     v6 1 6 v7

     v7 1 5 v4

    v1 v2

    v3 v4 v5

    v6 v7

    42

    1 3 10

    22

    5

    1

    648

    0

    1

    3

    65

    2

    3

    Distance and path from v1 to all the other vertices are:

    v1 to v2 = v1 -> v2 = 2

    v1 to v3 = v1 -> v4 -> v3 = 31 t 4 1 4 3