Single pair shortest path problem

Embed Size (px)

Citation preview

  • 8/13/2019 Single pair shortest path problem

    1/19

    Single pair shortest path

    problemMD Tanvir Anwoar

  • 8/13/2019 Single pair shortest path problem

    2/19

    Introduction:In graph theory, the shortest path problem is the problem of finding a path betweentwo vertices (or nodes) in a graph such that the sum of the weights of its constituentedges is minimized.This is analogous to the problem of finding the shortest path between two intersectionson a road map: the graph's vertices correspond to intersections and the edges

    correspond to road segments, each weighted by the length of its road segment.

  • 8/13/2019 Single pair shortest path problem

    3/19

    Application:

    * The traffic and length of the highways are path weights.* Vehicle routing problem solving* Solving network design problem* In video games, these algorithms are frequently used to find the shortest

    path between two points on a map

    Purpose of the problem solving:

    * Improve Quality of the service, Reducing time and cost

  • 8/13/2019 Single pair shortest path problem

    4/19

    Problem solving methods :

    Dijkstra Algorithm :

    a. All edge weights should be non-negativeb. Each iteration of Dijkstra takes O(n^2) for array-based or O(m log n)

    for heap-basedc. Total complexity is either O(n^3) or O(mn log n)

    d. This is a case where just repeatedly using a solution to a simplerproblem works out fine.

  • 8/13/2019 Single pair shortest path problem

    5/19

    Problem solving methods :

    Bellman-Ford Algorithm:

    a. If some edge weights are negativeb. It has complexity O(nm) for a single sourcec. Total sources solution is O(n^2 m), which is O(n4) for dense graphs

  • 8/13/2019 Single pair shortest path problem

    6/19

    Problem solving methods :

    Floyd-Warshall Algorithm :

    a. If some edge weights are negativeb. Dynamic programming solution to compute all sources shortest pathsc. Works with negative weights (or without) we assume no negative cyclesd. Complexity O(n^3)

  • 8/13/2019 Single pair shortest path problem

    7/19

    Floyd-Warshall Algorithm

    1. Graph should be directed

    2. It may contain negative edges but no negative cycle3. Find the shortest path between all the pairs of nodes

    A B

  • 8/13/2019 Single pair shortest path problem

    8/19

    Floyd-Warshall Algorithm

    1. Graph should be directed2. It may contain negative edges but no negative cycle

    3. Find the shortest path between all the pairs of nodes

    A B

    - 1

    - 2

  • 8/13/2019 Single pair shortest path problem

    9/19

    Floyd-Warshall Algorithm

    Example :

    1 2

    34

  • 8/13/2019 Single pair shortest path problem

    10/19

    Floyd-Warshall Algorithm

    Example :

    1 2

    34

    D1

    1,4 =

  • 8/13/2019 Single pair shortest path problem

    11/19

    Floyd-Warshall Algorithm

    Example :

    1 2

    34

    D1

    1,4 = 1 ,2,4

  • 8/13/2019 Single pair shortest path problem

    12/19

    Floyd-Warshall Algorithm

    Example :

    1 2

    34

    D1

    1,4 = 1 ,2,4 ; 1,4

  • 8/13/2019 Single pair shortest path problem

    13/19

    Floyd-Warshall Algorithm

    matrix formation :

    D(k)

    ( I , j ) = { min (D(k-1)

    ( I , j ) ) ,D(k-1)

    ( I , k) + D(k-1)

    ( k , j )}

    if k>= 1

  • 8/13/2019 Single pair shortest path problem

    14/19

    Floyd-Warshall Algorithm

    matrix formation :

    D(0)0 3 8 X -4

    X 0 X 1 7X 4 0 X -42 X -5 1 7X X X 0 0

    1 2

    35

    4

    3

    -4

    -56

    8

    1

    47

    2

  • 8/13/2019 Single pair shortest path problem

    15/19

    Floyd-Warshall Algorithm

    matrix formation :

    1 2

    35

    4

    D(0)0 3 8 X -4

    X 0 X 1 7X 4 0 X -42 X -5 1 7X X X 0 0

    3

    -4

    -56

    8

    1

    47

    D(0)

    ( 2 , 4 ) = 1

    1

    2

  • 8/13/2019 Single pair shortest path problem

    16/19

    Floyd-Warshall Algorithm

    matrix formation :

    D(1)0 3 8 X -4

    X 0 X 1 7X 4 0 X X2 5 -5 0 -2X X X 6 0

    Adjacency Matrix

    D(1)

    ( 4 , 5 ) = -2

    1 2

    35

    4

    3

    -4

    -56

    8

    1

    47

    2

    -2

  • 8/13/2019 Single pair shortest path problem

    17/19

    Floyd-Warshall Algorithm

    matrix formation :

    D(5)0 1 -3 2 -4

    3 0 -4 1 -17 4 0 5 32 -1 -5 0 -28 5 1 6 0

    Adjacency Matrix

    D(5)

    ( 2 , 3 ) = -4

    1 2

    35

    4

    3

    -4

    -56

    8

    1

    47

    2

    -4

  • 8/13/2019 Single pair shortest path problem

    18/19

    Floyd-Warshall Algorithm

    Pseudocode:

    1. For i=1 to |V| do2. For j=1 to |V| do3. S[i,j,0] = w(i,j)

    4. For k=1 to |V| do5. For i=1 to |V| do6. For j=1 to |V| do7. S[i,j,k] = min {8. S[i,j,k-1],9. S[i,k,k-1]+S[k,j,k-1]

    }

    10. Return S[:,:,n]# return 2d array with n = |V|

  • 8/13/2019 Single pair shortest path problem

    19/19

    Floyd-Warshall Algorithm