Single source shortest path problem is the task
of finding the shortest possible paths from a single
starting point typically source to all other vertices
in a weighted graph.
For this problem there are typically two most
useful algorithm:
Dijkstra’s and Bellman-ford Algorithm
I am gonna discuss about Bellman-ford algorithm which has some unique feature like:
Handles Negative Weights: Unlike Dijkstra’s algorithm, Bellman-Ford can process
graphs with negative edge weights.
Detects Negative Cycles: It can identify if there’s a cycle in the graph with a
negative total weight, which means there’s no solution for shortest paths since
distances can become infinitely negative.
Bellman-Ford algorithm
0
10
S A Iteration: 0
8 1
G -4 B
2
1 1
-2
F C
-1 3
E D
-1
Bellman-Ford algorithm
0 10
10
S A Iteration: 1
8 1
8 G -4 B
2
1 1
-2
F C
-1 3
E D
-1
Bellman-Ford algorithm
0 10
10
S A Iteration: 2
8 1
8 G -4 B
2
1 1
-2
9 F C
-1 3
E D
-1
12
Bellman-Ford algorithm
0 5
10
S A Iteration: 3
8 1
10
8 G -4 B A has the correct
2 distance and path
1 1
-2
9 F C
-1 3
E D
-1
8
Bellman-Ford algorithm
0 5
S 10 A Iteration: 4
8 1
6
8 G -4 B
2
1 1
F -2 C 11
9
-1 3
E D
-1
7
Bellman-Ford algorithm
0 5
S 10 A Iteration: 5
8 1
5
8 G -4 B B has the correct
2 distance and path
1 1
F -2 C 7
9
-1 3
E D
-1
7 14
Bellman-Ford algorithm
0 5
S 10 A Iteration: 6
8 1
5
8 G -4 B
2
1 1
F -2 C 6
9
-1 3
E D
-1
7 10
Bellman-Ford algorithm
0 5
S 10 A Iteration: 7
8 1
5
8 G -4 B D (and all other
2 nodes) have the
1 1
correct distance
-2 C 6 and path
F
9
-1 3
E D
-1
7 9
●
Time complexity:
O( V . E ), here V represents number of vertices
and E represents number of edges