Shortest Path
Dr. Yasir
Weighted Graphs
• In many applications,
each edge of a graph has
an associated numerical
value, called a weight.
• Usually, the edge weights
are nonnegative integers.
• Weighted graphs may be
either
directed
or
undirected.
The weight of an edge is often referred to as the "cost" of the edge.
In applications, the weight may be a measure of the length of a route, the
capacity of a line, the energy required to move between locations along a
route, etc.
Weighted Graphs
Weight of edges can represent everything in real world, e.g
amount of money to be transferred from one account to an
other account can be positive or negative,
Think of a driver, who gets paid to drive his employer
from s to t but he pays between a and b (say travelling between
his home and his workplace).
Shortest Path
• Given a weighted graph, and a designated node S, we would
like to find a path of least total weight from S to each of the
other vertices in the graph.
• The total weight of a path is the sum of the weights of its
edges.
Single-Source Shortest Path Problem
The problem of finding shortest paths from a source vertex
v to all other vertices in the graph.
Applications
Traffic Information Systems are most prominent use
- Mapping (Map Quest, Google Maps)
- Routing Systems
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 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.
Complexity: |E| log |V|
A greedy algorithm is a
mathematical
process
that looks for simple,
easy-to-implement
solutions to complex,
multi-step problems by
deciding which next step
will provide the most
obvious benefit.
Edsger Wybe Dijkstra
May 11, 1930 – August 6, 2002
- Received the 1972 A. M. Turing
Award, widely considered the most
prestigious award in computer
science.
- The Schlumberger Centennial Chair
of Computer Sciences at The
University of Texas at Austin from
1984 until 2000
- Made a strong case against use of
the GOTO statement in programming
languages and helped lead to its
deprecation.
- Known for his many essays on
programming.
Dijkstra's Algorithm – Main Idea
The idea is to visit the nodes in order of their closeness to
source vertex s.
The closest node to s, say x, must be adjacent to s and the next
closest be either adjacent to s or x. The third closest node to s
must be either adjacent to s or x or y, and so on.
Predecessor π[𝑣] & Shortest Path
Estimate 𝑑[𝑣]
We set the 𝜋 attribute so that the chain of predecessors
originating at a vertex 𝑣 runs backwards along the shortest path
form vertex 𝑠 to 𝑣.
• Given a graph 𝐺(𝑉; 𝐸), we maintain for each vertex 𝑣 ∈ 𝑉 a
predecessor π[𝑣] that is either a vertex or NIL.
For each vertex 𝑣 ∈ 𝑉 , we maintain an attribute 𝑑[𝑣], which is
an upper bound on the weight of a shortest path from source 𝑠
to vertex 𝑣.
• We call 𝑑[𝑣] a Shortest Path Estimate.
Relaxation
The process of relaxing an edge (𝑢; 𝑣) consist of testing
whether we can improve the shortest path to 𝑣 found so far by
going through 𝑢 and, if so, update 𝑑[𝑣] and π[𝑣].
A relaxation step may decrease the shortest path estimate 𝑑[𝑣]
and updates 𝑣’s predecessor field π[𝑣].
Dijkstra’s algorithm also maintains a set S of those vertices for
which the shortest path weights from the source s have already
been determined.
Dijkstra's Algorithm – Working
Consider the following graph with source vertex 𝑎.
𝑎
5
𝑏
2
𝑒
7
6
3
6
𝑐
2
𝑑
5
2
3
9
𝑓
1
𝑔
Dijkstra's Algorithm – Working
Initialize:
5
𝑎
𝑏
𝑆={}
𝜋 𝑎 = 𝑁𝐼𝐿
𝑒
7
6
3
6
𝑐
2
𝑑
2
5
2
3
9
𝑓
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
0
∞
∞
∞
∞
∞
∞
Dijkstra's Algorithm – Working
Relax 𝑎
𝑎
5
𝑏
6
𝑐
𝑒
7
6
3
𝑆 = {𝑎 }
𝜋 𝑎 = 𝑁𝐼𝐿
Blue shows the relaxation
process
2
d
2
5
2
3
9
𝑓
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
Dijkstra's Algorithm – Working
Relax 𝑎
Yellow
highlight
means the
node 𝒃 is
selected as
the nearest
from 𝒂
𝑎
𝑏
6
c
𝑒
7
6
3
𝑆 = {𝑎, 𝑏 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
5
2
𝑑
2
5
3
9
𝑓
5𝑎
means the
distance of 𝑐 from
𝒂 is 5 and the
immediate
predecessor of 𝒄 is 𝒂
2
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
Dijkstra's Algorithm – Working
Relax 𝑏
𝑎
Red shows
that these
vertices are
visited and
decision has
been made
𝑏
6
𝑐
𝑒
7
6
3
𝑆 = {𝑎, 𝑏 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
5
2
2
3
𝑑
2
5
9
𝑓
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
Min(𝟔𝒂 , 3+2)=𝟓𝒃
Dijkstra's Algorithm – Working
Relax 𝑏
𝑎
5
𝑏
𝑆 = {𝑎, 𝑏, 𝑐 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
𝑒
7
6
3
6
𝑐
2
2
3
𝑑
2
5
9
𝑓
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
Dijkstra's Algorithm – Working
Relax 𝑏
𝑎
5
𝑏
𝑆 = {𝑎, 𝑏, 𝑐 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
𝑒
7
6
3
6
𝑐
2
2
3
𝑑
2
5
9
𝑓
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
Dijkstra's Algorithm – Working
Relax 𝑐
𝑎
5
b
𝑆 = {𝑎, 𝑏, 𝑐 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
e
7
6
3
6
c
2
2
3
d
2
5
9
f
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
Min(𝟓𝒃 , 5+2)=𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑐
𝑎
b
6
c
e
7
6
3
𝑆=
{𝑎, 𝑏, 𝑐, 𝑑 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
𝜋 𝑑 =𝑏
5
2
2
3
d
2
5
9
f
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑑
5
𝑎
b
𝑆 = {𝑎, 𝑏, 𝑐, 𝑑 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
𝜋 𝑑 =𝑏
e
7
6
3
6
c
2
d
2
5
2
3
f
9
Yellow
line
represent that
this edge is of
no more use.
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
Min(𝟖𝒄 , 5+9)=𝟖𝒄
Dijkstra's Algorithm – Working
Relax 𝑑
5
𝑎
b
𝑆=
{𝑎, 𝑏, 𝑐, 𝑑, 𝑓 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
𝜋 𝑑 =𝑏
𝜋 𝑓 =𝑐
e
7
6
3
6
c
2
d
2
5
2
3
f
9
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑓
5
𝑎
3
b
6
c
7
6
2
5
3
d
2
𝑆 = {𝑎, 𝑏, 𝑐, 𝑑, 𝑓 }
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
e
𝜋 𝑐 =𝑎
𝜋 𝑑 =𝑏
2
𝜋 𝑓 =𝑐
9
f
𝑔
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
Min(𝟏𝟐𝒄 , 8+1)=𝟗𝒇
Min(𝟏𝟏𝒄 , 8+5)=𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑓
5
𝑎
3
b
6
c
7
6
2
5
3
d
2
𝑆=
{𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔 }
𝜋 𝑎 = 𝑁𝐼𝐿
e
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
𝜋 𝑑 =𝑏
2
𝜋 𝑓 =𝑐
𝜋 𝑔 =𝑓
𝑔
9
f
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟗𝒇
Min(𝟏𝟏𝒄 , 8+5)=𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑓
5
𝑎
3
b
6
c
7
6
2
d
2
𝑆=
{𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔, 𝑒 }
𝜋 𝑎 = 𝑁𝐼𝐿
e
𝜋 𝑏 =𝑎
𝜋𝑐 =𝑎
𝜋𝑑 =𝑏
2
𝜋 𝑓 =𝑐
𝜋 𝑔 =𝑓
𝑔
𝜋 𝑒 =𝑐
5
3
9
f
1
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟗𝒇
Min(𝟏𝟏𝒄 , 8+5)=𝟏𝟏𝒄
Min(𝟏𝟏𝒄 , 9+2=𝟏𝟏𝒈)=𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑓
𝑎
𝑏
𝑐
𝑑
𝑓
𝑆=
{𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔, 𝑒 }
e
𝜋 𝑎 = 𝑁𝐼𝐿
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
2
𝜋 𝑑 =𝑏
𝜋 𝑓 =𝑐
𝜋 𝑔 =𝑓
𝑔
𝜋𝑒 =𝑐
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟗𝒇
Min(𝟏𝟏𝒄 , 8+5)=𝟏𝟏𝒄
5
𝑎
7
6
3
b
6
c
2
3
d
2
5
9
f
1
𝟏𝟏𝒄
Dijkstra's Algorithm – Working
Relax 𝑓
5
𝑎
7
6
3
b
Shortest paths
from the source 𝑎
𝑎𝑏
𝑎𝑐
𝑎𝑏𝑐
𝑎𝑐𝑓
𝑎𝑐𝑒
𝑎𝑐𝑓𝑔
6
c
2
d
2
5
3
f
9
1
𝑆=
{𝑎, 𝑏, 𝑐, 𝑑, 𝑓, 𝑔, 𝑒 }
𝜋 𝑎 = 𝑁𝐼𝐿
e
𝜋 𝑏 =𝑎
𝜋 𝑐 =𝑎
2
𝜋 𝑑 =𝑏
𝜋 𝑓 =𝑐
𝜋 𝑔 =𝑓
g
𝜋 𝑒 =𝑐
𝑎
𝑏
𝑐
𝑑
𝑓
𝑔
𝑒
𝟎𝒂
∞
∞
∞
∞
∞
∞
𝟑𝒂
𝟓𝒂
𝟔𝒂
∞
∞
∞
𝟓𝒂
𝟓𝒃
∞
∞
∞
𝟓𝒃
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟖𝒄
𝟏𝟐𝒄
𝟏𝟏𝒄
𝟗𝒇
𝟏𝟏𝒄
𝟗𝒇
𝟏𝟏𝒄
𝟎𝒂
𝟑𝒂
𝟓𝒂
𝟓𝒃
𝟖𝒄
Find the shortest path using Dijkstra algorithm from the source
vertex 𝑎. Show every step of algorithm.
© Copyright 2026 Paperzz