Top Banner

of 15

1 Chandler 18.304lecture1

Jun 03, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/12/2019 1 Chandler 18.304lecture1

    1/15

    Floyd-Warshall Algorithm

    Chandler Burfield

    February 20, 2013

    Chandler Burfield Floyd-Warshall February 20, 2013 1 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    2/15

    All-Pairs Shortest Paths

    Problem

    To find the shortest pathbetween all vertices vVfor a weighted graphG = (V,E).

    Chandler Burfield Floyd-Warshall February 20, 2013 2 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    3/15

    Dynamic-Programming

    How to Develop a Dynamic-Programming Algorithm

    1. Characterize the structure of an optimal solution.

    2. Recursively define the value of an optimal solution.

    3. Compute the value of an optimal solution in a bottom-up manner.

    Chandler Burfield Floyd-Warshall February 20, 2013 3 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    4/15

  • 8/12/2019 1 Chandler 18.304lecture1

    5/15

    Weight Matrix

    wij

    The weight of an edge between vertex iand vertex j in graph G = (V,E),where

    wij=

    0 if i=j

    the weight of a directed edge (i,j) if i=jand (i,j) E if i=jand (i,j) /E

    W

    An n x n matrix representing the edge weights of an n-vertex graph,where W = (wij).

    Chandler Burfield Floyd-Warshall February 20, 2013 5 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    6/15

    Path Matrix

    d(k)ij

    The weight of the shortest path from vertex i to vertex jfor which allintermediate vertices are in the set {1, 2, ...,k}.

    D(k)

    An n x n matrix representing the path distances between vertices in a

    directed n-vertex graph, where D(k) = (d(k)ij ).

    Chandler Burfield Floyd-Warshall February 20, 2013 6 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    7/15

    Observations

    A shortest path does not contain the same vertex more than once.

    For a shortest path from i to jsuch that any intermediate vertices onthe path are chosen from the set {1, 2, ..., k}, there are twopossibilities:

    1. k is not a vertex on the path,so the shortest such path has length dk1ij

    2. k is a vertex on the path,so the shortest such path is dk1ik +d

    k1kj

    So we see that we can recursively define d(k)ij as

    d(k)ij =

    wij ifk= 0

    min(d(k1)ij , d

    (k1)ik +d

    (k1)kj ) ifk1

    Chandler Burfield Floyd-Warshall February 20, 2013 7 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    8/15

    Predecessor Matrix

    (k)

    ijThe predecessor of vertex jon a shortest path from vertex iwith allintermediate vertices in the set {1, 2, ...,k}. Where

    (0)

    ij

    = NIL if i=j orwij=i if i=j and wijd

    (k1)ik +d

    (k1)kj

    (k)

    The predecessor matrix, where (k) = ((k)ij ).

    Chandler Burfield Floyd-Warshall February 20, 2013 8 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    9/15

    Algorithm

    Floyd-Warshall(W)n=W.rowsD(0) =W

    fork

    = 1 ton

    let D(k) = (d(k)ij ) be a new matrixfor i= 1 to n

    for j= 1 to n

    d(k)ij = min(d

    (k1)ij , d

    (k1)ik +d

    (k1)kj )

    return D(n)

    Chandler Burfield Floyd-Warshall February 20, 2013 9 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    10/15

    Analysis of Algorithm

    Floyd-Warshall(W)n=W.rowsD(0) =Wfork= 1 to n

    let D(k) = (d(k)ij ) be a new matrixfor i= 1 to n

    for j= 1 to n

    d(k)ij = min(d

    (k1)ij , d

    (k1)ik +d

    (k1)kj )

    return D(n)

    Runtime

    (n3)

    Space

    (n3) here,but if we reuse spacethis can be done in(n2).

    Chandler Burfield Floyd-Warshall February 20, 2013 10 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    11/15

    Analysis of Improved Algorithm

    Floyd-Warshall(W)n=W.rowsD=W initialization

    fork= 1 tonfor i= 1 to n

    for j= 1 to nifdij>dik+dkjthen dij=dik+dkj

    ij=kjreturn D

    Analysis

    The shortest pathcan be constructed,not just the lengthsof the paths.

    Runtime: (n3).

    Space: (n2).

    Chandler Burfield Floyd-Warshall February 20, 2013 11 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    12/15

    Example

    Floyd-Warshall(W)n=W.rowsD=W initialization

    fork= 1 to nfor i= 1 to n

    for j= 1 to nifdij>dik+dkjthen dij=dik+dkj

    ij=kjreturn D

    Chandler Burfield Floyd-Warshall February 20, 2013 12 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    13/15

    Proof of Correctness

    Inductive Hypothesis

    Suppose that prior to the kth iteration it holds that for i,jV, dijcontains the length of the shortest path Q from i to j in Gcontaining onlyvertices in the set {1, 2, ..., k 1}, and ijcontains the immediatepredecessor of jon path Q.

    Chandler Burfield Floyd-Warshall February 20, 2013 13 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    14/15

    Applications

    Detecting the Presence of a Negative Cycle

    Transitive Closure of a Directed Graph

    Chandler Burfield Floyd-Warshall February 20, 2013 14 / 15

  • 8/12/2019 1 Chandler 18.304lecture1

    15/15

    Other All-Pairs Shortest Paths Algorithms

    Dynamic Programming Approach Based on Matrix Multiplication

    Johnsons Algorithm for Sparse Graphs

    Chandler Burfield Floyd-Warshall February 20, 2013 15 / 15