Top Banner

of 18

Weighted Graphs and Dijkstra's Algorithm

Apr 10, 2018

Download

Documents

rakeshjainrulz
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/8/2019 Weighted Graphs and Dijkstra's Algorithm

    1/18

    Andrew Pereira

    1st Year M.Sc Computer Science

    Roll-1025001

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    2/18

    We may need to find the fastest way to route a data packet between 2

    computers. We note that some computers in a computer network are

    faster than others.

    We may want to find the fastest way to travel cross country. Someintercity distances are larger than others.

    Thus it is natural to consider graphs whose edges are not weighted

    equally.

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    3/18

    Aweighted graph is a graph that has a numeric label w(e) associated with

    each edge e called the weight of the edge e.

    The weight of a path or the weight of a tree in a weighted graph is the

    sum of the weights of the selected edges.

    Sometimes the wordc

    ost is used instead of weight

    UNWEIGHTEDGRAPH WEIGHTEDGRAPH

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    4/18

    Dijkstas Algorithm is used to find the shortest path between any pair of

    vertices in a connected weighted graph.

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

    have nonnegative weights.

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

    weights are nonnegative

    Output: Lengths of shortest paths (or the shortest paths themselves) froma given source vertex vV to all other vertices

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    5/18

    The algorithms begins by assigning a permanent label 0 to the starting

    vertex and a temporary label to all other n-1 vertices. At every

    iteration another vertex gets a permanent label according to :

    Each vertex j that is not permanently labelled gets a new temporary

    label whose value is given by label j={min of j, old label of i+dij

    } where

    i is the latest vertex permanently labelled in the previous iteration and

    dij is the distance between i and j. If i and j are not joined by an edge

    then dij = .

    The smallest value between all the temporary labels is found and the

    this becomes the permanent label of the corresponding vertex.

    Repeat the above two steps until we reach the terminal vertex with ashortest distance.

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    6/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    7/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    8/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    9/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    10/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    11/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    12/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    13/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    14/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    15/18

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    16/18

    ` Using Dijkstras algorithm find the shortest path

    length between the vertices a and z in this

    weighted graph.(Qn 37)b

    a

    z

    c d

    8

    4

    6

    2

    2

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    17/18

    ` Using Dijkstras algorithm find the shortest path

    length between the vertices a and z in this

    weighted graph.(Qn 39)

    a

    b

    c e

    d

    z

    3

    7

    6

    6

    8

    5

    2 21

  • 8/8/2019 Weighted Graphs and Dijkstra's Algorithm

    18/18

    As mentioned, Dijkstras algorithm calculates the shortest path to every

    vertex.

    However, it is about as computationally expensive to calculate the

    shortest path from vertexu

    to every vertex usingD

    ijkstras as it is tocalculate the shortest path to some particular vertex v.

    Therefore, anytime we want to know the optimal path to some other

    vertex from a determined origin, we can use Dijkstras algorithm.