lecture-37 all pairs shortest path problem

Upload: taqi-shah

Post on 06-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    1/16

    All Pairs Shortest

    Path Problem

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    2/16

    All pairs shortest path The problem: find the shortest path between

    every pair ofvertices ofa graph

    The graph: may contain negative edges butno negative cycles

    Arepresentation

    :a weight matrix whereW(i,j)=0 if i=j.

    W(i,j)=g if there is no edge between i and j.W(i,j)=weight ofedge

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    3/16

    The weight matrix and the

    graph1 2 3 4 5

    1 0 1 1 5

    2 9 0 3 2

    3 0 4

    4 2 0 3

    5 3 0

    g

    gg g g

    g g

    g g g

    v1

    v2

    v3

    v4

    v5

    32

    2

    4

    1

    3

    1

    93

    5

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    4/16

    Solution Dijkstras Algorithm can be used.

    How?

    Set each vertex as source and call

    Dijkstras algo

    But we may solve the problem using direct

    approach (Algorithm By Floyd)

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    5/16

    Floyds AlgoAssume vertices are numbered as

    1,2,3n for simplicity

    Uses nn matrix D (n is the number of

    vertices in the graph G)

    Shortest paths are computed in matrix D

    After running the algorithm D[i,j] contains

    the shortest distance (cost) between

    vertices i and j

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    6/16

    Floyds Algo

    Initially we set D[i,j]=W[i,j] Remember

    W[i,j]=0 if i==j

    W(i,j)= if there is no edge between i andj

    W(i,j)=weight of edge

    We make n iteration over matrix D

    After kth iteration D[i,j] will store the valueof minimum weight path from vertex i tovertex j that does not pass through avertex numbered higher than k

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    7/16

    Floyds Algo In kth iteration we use following formula tocompute D

    !

    ],[],[

    ],[min],[

    11

    1

    jkDkiD

    jiDjiD

    kk

    k

    k

    Subscript k denotes the value of matrix D after

    the kth iteration (It should not be assumed thereare n different matrices of size nn)

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    8/16

    ViVj

    Vk

    Shortest Path using intermediate vertices { V1, . . . Vk-1 }

    Shortest path using intermediate vertices

    {V1, . . . Vk}

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    9/16

    Floyeds AlgorithmFloyd1. DnW // initialize D array to W[ ]

    2.

    3. forkn 1 to n

    4. do forin 1 to n

    5. do forjn 1 to n

    6. if (D[ i,j] > D[ i, k ] + D[ k,j] ) then7. D[ i,j]n D[ i, k ] + D[ k,j]

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    10/16

    Recovering Paths Use another matrix P

    P[i,j] hold the vertex k that led Floyd to find

    the smallest value of D[i,j]

    If P[i,j]=0 there is no intermediate edge

    involved, shortest path is direct edge

    between i and j

    So modified version of Floyd is given

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    11/16

    Recovering PathsFloyd1. DnW // initialize D array to W[ ]

    2. Pn 0

    3. forkn 1 to n4. do forin 1 to n5. do forjn 1 to n6

    . if

    (D

    [ i,j] >D

    [ i,k

    ] +D

    [k,j] ) then7. D[ i,j]n D[ i, k ] + D[ k,j]

    8. P[ i, j] n k

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    12/16

    Example

    W = D0 =40 5

    2 0 w

    w -3 0

    1 2 3

    1

    2

    3

    00 0

    0 0 0

    0 0 0

    1 2 3

    1

    2

    3

    P =

    1

    2

    3

    5

    -3

    24

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    13/16

    D1=40 5

    2 0 7

    w -3 0

    1 2 3

    1

    2

    3

    00 0

    0 0 1

    0 0 0

    1 2 3

    1

    2

    3

    P =

    1

    2

    3

    5

    -32

    4

    k = 1

    Vertex 1 can be

    intermediate node

    D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] )

    = min (w

    , 7)= 7

    D1

    [3,2] = min( D0

    [3,2], D0

    [3,1]+D0

    [1,2] )= min (-3,w)

    = -3

    40 5

    2 0 w

    w -3 0

    1 2 3

    1

    2

    3

    D0 =

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    14/16

    D2=40 5

    2 0 7

    -1 -3 0

    1 2 3

    1

    2

    3

    00 0

    0 0 1

    2 0 0

    1 2 3

    1

    2

    3

    P =

    D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )

    = min (5, 4+7)= 5

    D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )

    = min (w, -3+2)

    = -1

    1

    2

    3

    5

    -32

    4

    D1=40 5

    2 0 7

    w -3 0

    1 2 3

    1

    2

    3

    k = 2

    Vertices 1, 2can be

    intermediate

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    15/16

    D3=20 5

    2 0 7

    -1 -3 0

    1 2 3

    1

    2

    3

    30 0

    0 0 1

    2 0 0

    1 2 3

    1

    2

    3

    P =

    D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] )

    = min (4, 5+(-3))

    = 2

    D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] )

    = min (2, 7+ (-1))

    = 2

    D2=

    40 5

    2 0 7

    -1 -3 0

    1 2 3

    1

    2

    3

    1

    2

    3

    5

    -32

    4

    k = 3

    Vertices 1, 2, 3can be

    intermediate

  • 8/3/2019 Lecture-37 All Pairs Shortest Path Problem

    16/16

    Printing intermediate nodes on

    shortest path from i to jPath (i, j)

    k= P[ i, j ];

    if (k== 0)return;

    Path (i , k);

    print( k);Path (k, j);

    30 0

    0 0 1

    2 0 0

    1 2 3

    1

    2

    3

    P =

    1

    2

    3

    5

    -3

    24