Weighted Graphs and Dijkstra's Algorithm
Weighted Graphs and Dijkstra's Algorithm
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.
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.