Presentation
On
Single Source Shortest Path.
Presenter:
Kazi Emad
ID no. 191902025
01
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
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 ,vi1)
i1
 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.
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
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
Initialization:
INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v  V
2. do d[v] ← 
3. [v] ← NIL
4. d[s] ← 0
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)
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
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6







0
distance label
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6

 




0
distance label
delmin
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
distance label
decrease key
X

X
X
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
distance label
X

X
X
delmin
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
X

X
X
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
X

X
X
decrease key
X 33
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
X

X
X
X 33
delmin
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9



14

0
X

X
X
X 33
44
X
X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
delmin
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 X
24
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 X
delmin
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
delmin
X 33X
32
24
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
24
X50
X45
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
24
X50
X45
delmin
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
24
X50
X45
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
X50
X45
delmin
X 33X
32
24
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
X50
X45
X 33X
32
Implementation of Dijkstra's Algorithm:
s
3
t
2
6
7
4
5
24
18
2
9
14
15
5
30
20
44
16
11
6
19
6
15
9


14

0
X

X
X
44
X
35X
59 XX51
X 34
X50
X45
X 33X
32
Implementation of Dijkstra's Algorithm:
S 2 3 4 5 6 7 t
S 0 9 inf inf inf 14 15 inf
2 0 9 33 inf inf 14 15 inf
6 0 9 32 inf 44 14 15 inf
7 0 9 32 inf 35 14 15 59
3 0 9 32 inf 34 14 15 51
5 0 9 32 45 34 14 15 50
4 0 9 32 45 34 14 15 50
t 0 9 32 45 34 14 15 50
Tabular Solution of the Graph for Shortest Path
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.
Implementation of Bellman-Ford Algorithm:
Initialize all the
distances
Implementation of Bellman-Ford Algorithm:
iterate over all
edges/vertices
and apply update
rule
Implementation of Bellman-Ford Algorithm:
check for negative
cycles
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is
the shortest path
from s to:
A:
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is
the shortest path
from s to:
A: 3
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is
the shortest path
from s to:
A: 3
B:
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is
the shortest path
from s to:
A: 3
B: 5
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is
the shortest path
from s to:
A: 3
B: 5
D:
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
How many edges is
the shortest path
from s to:
A: 3
B: 5
D: 7
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 





Iteration: 0
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 10




8
Iteration: 1
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 10


12
9
8
Iteration: 2
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
10

8
9
8
Iteration: 3
A has the correct
distance and path
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
6
11
7
9
8
Iteration: 4
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
5
7
147
9
8
Iteration: 5
B has the correct
distance and path
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
5
6
107
9
8
Iteration: 6
Implementation of Bellman-Ford Algorithm:
G
S
F
E
A
D
B
C
10
8
1
-1
-1
3
1
1
2
-2
-4
0 5
5
6
97
9
8
Iteration: 7
D (and all other
nodes) have the
correct distance
and path
Implementation of Bellman-Ford Algorithm:
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.
Thank You

Single sourceshortestpath by emad