Overview of Single Source Shortest Path
Types of Single Source Shortest Path Algorithm
Representation of Single Source Shortest Path
Initialization
Relaxation
Implementation of Dijkstra's Algorithm
Does Dijkstra’s Algorithm Always Work?
Implementation of Bellman-Ford Algorithm
Negative Weight Cycles in Bellman-Ford Algorithm
3. Agenda
1. Overview of Single Source Shortest
Path
2. Types of Single Source Shortest Path
Algorithm
3. Representation of Single Source
Shortest Path
4. Initialization
5. Relaxation
6. Implementation of Dijkstra's Algorithm
7. Does Dijkstra’s Algorithm Always Work?
8. Implementation of Bellman-Ford
Algorithm
9. Negative Weight Cycles in Bellman-
Ford Algorithm
4. Overview of Single Source Shortest
Path:
In a shortest-paths problem, we are given a weighted, directed graph G = (V, E), with weight
function w : E -> R mapping edges to real-valued weights. The weight w(p) of path p = (V0,
V1….., Vk) ki is the sum of the weights of its constituent edges:
w(p) w(vi ,vi1)
i1
The single-source shortest path problem, in which we have to find shortest paths from a source
vertex v to all other vertices in the graph.
5. Types of Single Source Shortest Path Algorithm:
There are two types of single source shortest path algorithm. And they are:
1. Dijkstra’s Algorithm.
2. Bellman-Ford Algorithm
6. Representation of Single Source Shortest Path:
For each vertex v V:
d[v] = δ(s, v): a shortest-path estimate
Initially, d[v]=∞
Reduces as algorithms progress
[v] = predecessor of v on a shortest path from s
If no predecessor, [v] = NIL
induces a tree—shortest-path tree
8. Relaxation:
Relaxing an edge (u, v) = testing whether we can improve the shortest path to v found so far by
going through u.
Example:
If d[v] > d[u] + w(u, v)
update d[v] and [v]
S 8
2
4
u
v
2 6
u v
4
RELAX(u,v,w)
9. Implementation of Dijkstra's Algorithm:
Given a graph and a source vertex in the graph, find shortest paths from
source to all vertices in the given graph.
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
30. Does Dijkstra’s Algorithm Always Work?
No. Dijkstra’s Algorithm doesn’t always work in case of negative weight
edges.
So, we have a new algorithm named “Bellman-Ford Algorithm” which can
also perform in negative weight edges.
49. Negative Weight Cycles in Bellman-Ford
Algorithm:
• Negative edges are OK, as long as there are no negative weight cycles (otherwise paths with
arbitrary small “lengths” would be possible)
• Shortest-paths can have no cycles (otherwise we could improve them by removing cycles)
– Any shortest-path in graph G can be no longer than n – 1 edges, where n is the number of
vertices.