Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Weighted Graphs and Dijkstra’s

Algorithm
Weighted Graphs
 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. Some
intercity distances are larger than others.

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


equally.
Definition
 A weighted 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 word cost is used instead of weight

UNWEIGHTED GRAPH WEIGHTED GRAPH


Dijkstra’s Algorithm
 Dijksta’s 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 v∈V, such that all edge
weights are nonnegative

 Output: Lengths of shortest paths (or the shortest paths themselves) from
a given source vertex v∈V to all other vertices
How it works..
 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 a
shortest distance.
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Solve:
 Using Dijkstra’s algorithm find the shortest
path length between the vertices a and z in
this weighted graph.(Qn 37)
b

8
2
z
a

2 4

c 6 d
Solve:
 Using Dijkstra’s algorithm find the shortest
path length between the vertices a and z in
this weighted graph.(Qn 39)
b d
6

3 8

a 2 1 2 z

7
5

c 6
e
Conclusion
 As mentioned, Dijkstra’s algorithm calculates the shortest path to every
vertex.

 However, it is about as computationally expensive to calculate the


shortest path from vertex u to every vertex using Dijkstra’s as it is to
calculate 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 Dijkstra’s algorithm.

You might also like