Dijkstra’s, Kruskals and Floyd-Warshall Algorithms

download Dijkstra’s, Kruskals and Floyd-Warshall Algorithms

of 38

Transcript of Dijkstra’s, Kruskals and Floyd-Warshall Algorithms

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    1/38

    Dijkstras, Kruskals and Floyd-

    Warshall Algorithms

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    2/38

    Single-Source Shortest Path Problem

    Single-Source Shortest Path Problem - Theproblem of finding shortest paths from a sourcevertex vto all other vertices in the graph.

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    3/38

    Dijkstra's algorithm

    Dijkstra's algorithm -is a solution to the single-source

    shortest path problem in graph theory.

    Works on both directed and undirected graphs.However, all edges must have nonnegative weights.

    Approach:Greedy

    Input:Weighted graph G={E,V} and source vertex vV,such that all edge weights are nonnegative

    Output:Lengths of shortest paths (or the shortestpaths themselves) from a given source vertexvV toall other vertices

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    4/38

    Dijkstra's algorithm - Pseudocode

    dist[s]0 (distance to source vertex is zero)for allv V{s}

    do dist[v] (set all other distances to infinity)S (S, the set of visited vertices is initially empty)QV (Q, the queue initially contains all

    vertices)while Q (while the queue is not empty)do umindistance(Q,dist) (select the element of Q with the min. distance)

    SS{u} (add u to list of visited vertices)for allv neighbors[u]

    do if dist[v] > dist[u] + w(u, v) (if new shortest path found)

    then d[v]

    d[u] + w(u, v) (set new value of shortest path)(if desired, add traceback code)return dist

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    5/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    6/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    7/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    8/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    9/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    10/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    11/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    12/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    13/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    14/38

    Dijkstra Animated Example

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    15/38

    Kruskal Algorithm

    Minimum Spanning Tree

    Disjoint Sets

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    16/38

    4

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    Complete Graph

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    17/38

    1

    4

    2

    5

    2

    5

    4

    3

    4

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    A AB D

    B B

    B

    C D

    J C

    C

    E

    F

    D

    D H

    J E G

    F FG I

    G GI J

    H J JI

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    18/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Sort Edges

    (in reality they are placed in a priority

    queue - not sorted - but sorting them

    makes the algorithm easier to visualize)

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    19/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    20/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    21/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    22/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    23/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    24/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Cycle

    Dont Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    25/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    26/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    27/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    28/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Cycle

    Dont Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    29/38

    2

    5

    2

    5

    4

    3

    4

    4

    10

    1

    6

    3

    3

    2

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    B

    B

    D

    J

    C

    C

    E

    F

    D

    D H

    J

    E G

    F

    F

    G

    I

    G

    G

    I

    J

    H J

    JI

    1A D

    4B C

    4A B

    Add Edge

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    30/38

    4

    1

    2

    2 1

    3

    32

    4

    A

    B C

    D

    E F

    G

    H

    I

    J

    4

    1

    2 3

    2 1

    3

    5

    3

    4

    2

    5 6

    4

    4

    10

    A

    B C

    D

    E F

    G

    H

    I

    J

    Minimum Spanning Tree Complete Graph

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    31/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    32/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    33/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    34/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    35/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    36/38

    FloydWarshall algorithm

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    37/38

  • 8/12/2019 Dijkstras, Kruskals and Floyd-Warshall Algorithms

    38/38

    Thank You