5 Graph Theory Routing Problem
5 Graph Theory Routing Problem
Routing problem
[Link]
Chapter 4: Shortest Path Problem
• Introduction to the shortest path problem
• Dijkstra
• Bellman-Ford Algorithm
• Floyd- Warshall algorithm
Problem Statement
• The routing problem is a central challenge in many graph theory applications.
• Representing networks such as routes, Internet connections, or social
interactions.
• The goals is to minimize some metric, such as distance, time, or cost.
• Routing problems apply to both directed and undirected graphs.
• This is crucial in a variety of areas:
• Transportation networks: Minimize the distance or travel time between two points.
• Telecommunications and computer networks: Optimizing bandwidth usage and
reducing transmission delays.
• Logistics and distribution: Finding optimal routes for transporting goods while
minimizing costs.
The Shortest Path Problem 3
Problem Statement
• Let G=(V,E) be a graph where:
• Each edge 𝒆 ∈ 𝑬 may have an associated weight w(e), representing a cost
metric such as distance, time, or expense.
• The weights may be non-negative or, in some cases, include negative values,
depending on the application.
• The routing problem involves finding a path or a set of paths 𝑃 =
𝑣1 , 𝑣2 , … , 𝑣𝑘 ∈ 𝐺 that connects a source node 𝑠 ∈ 𝑉 to a destination node
𝑡 ∈ 𝑉, such that a specified objective function is minimized.
• The objective function typically minimizes the path cost: The sum of edge
weights along the path, σ 𝑒∈𝑃 𝑤(𝑒).
5 2
a d
g
4 7
c f
3
8
b e
3 5
5 2
a d
g
a b c d e f g
4 7
c f
3
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ 4 7
c f
3
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3) a(4) a(5) ∞ ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞ ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
B(3+8) ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) b(11) ∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) b(11) C(3+4)
∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11) C(7) ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
d(7) ∞
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
d(7) ∞
f(14)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
d(7) ∞
f(14) e(7)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
d(7) ∞
F(7+7)
f(14) e(7)
f(14)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
d(7) ∞
F(7+7)
f(14) e(7)
f(14)
e(12)
8
b e
3 5
5 2
a d
g
a b c d e f g
0 ∞ ∞ ∞ ∞ ∞ ∞ a(0) 4 7
c f
3
a(3)
∞ a(4)
∞ a(5)
∞ ∞ ∞ ∞ b(3)
a(4) a(5) ∞
b(11)
B(3+8) ∞ ∞ c(4)
a(5) C(7)
b(11) C(3+4)
∞ ∞ d(5)
b(11)
d(7) C(7) ∞ f(7)
d(7) ∞
F(7+7)
f(14) e(7)
f(14)
e(12) g(12)
Example
b
3 1
a d
5
-2
c
Example
a b c d
0 ∞ ∞ ∞ a(0) b
3 1
a d
5
-2
c
Example
a b c d
0 ∞ ∞ ∞ a(0) b
1
a (3) a(5) ∞ b(3) 3
a d
5
-2
c
Example
a b c d
0 ∞ ∞ ∞ a(0) b
1
a (3) a(5) ∞ b(3) 3
2
A E
2
-4
2 D
-2
5 vertices 4 iterations for calculating the shortest path B
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B ∞ -
C ∞ -
D ∞ -
E ∞ -
iteration 1 of 4 Arc(A, C) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B ∞ -
C ∞ -
D ∞ -
E ∞ -
iteration 1 of 4 Arc(A, C) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B ∞ -
Dist (C) = min( Dist (C), Dist (A) + L(A, C))
C ∞ -
D ∞ - Dist (C) = min(∞, 0+ (-1) )
E ∞ -
iteration 1 of 4 Arc(A, C) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B ∞ -
Dist (C) = min( Dist (C), Dist (A) + L(A, C))
C -1 A
D ∞ - Dist (C) = -1
E ∞ -
iteration 1 of 4 Arc(A, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B ∞ -
C -1 A
D ∞ -
E ∞ -
iteration 1 of 4 Arc(A, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B ∞ -
Dist (B) = min( Dist (B), Dist (A) + L(A, B))
C -1 A
D ∞ - Dist (B) = min(∞, 0+ 2 )
E ∞ -
iteration 1 of 4 Arc(A, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 2 A
Dist (B) = min( Dist (B), Dist (A) + L(A, B))
C -1 A
D ∞ - Dist (B) = 2
E ∞ -
iteration 1 of 4 Arc(C, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 2 A
Dist (B) = min( Dist (B), Dist (C) + L(C, B))
C -1 A
D ∞ - Dist (B) = min(2, 2 + (-1) ) = 1
E ∞ -
iteration 1 of 4 Arc(C, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (B) = min( Dist (B), Dist (C) + L(C, B))
C -1 A
D ∞ - Dist (B) = 1
E ∞ -
iteration 1 of 4 Arc(B, D) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (D) = min( Dist (D), Dist (B) + L(B, D))
C -1 A
D ∞ - Dist (D) = min(∞, 1+ (-2) )
E ∞ -
iteration 1 of 4 Arc(B, D) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (D) = min( Dist (D), Dist (B) + L(B, D))
C -1 A
D -1 B Dist (D) = -1
E ∞ -
iteration 1 of 4 Arc(C, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (E) = min( Dist (E), Dist (C) + L(C, E))
C -1 A
D -1 B Dist (E) = min(∞, -1+ (2) ) = 1
E ∞ -
iteration 1 of 4 Arc(C, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (E) = min( Dist (E), Dist (C) + L(C, E))
C -1 A
D -1 B Dist (E) = min(∞, -1+ (2) ) = 1
E ∞ -
iteration 1 of 4 Arc(C, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (E) = min( Dist (E), Dist (C) + L(C, E))
C -1 A
D -1 B Dist (E) = min(∞, -1+ (2) ) = 1
E 1 C
iteration 1 of 4 Arc(D, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (E) = min( Dist (E), Dist (D) + L(D, E))
C -1 A
D -1 B Dist (E) = min(-1, -1+ (-4) ) = -5
E 1 C
iteration 1 of 4 Arc(D, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (E) = min( Dist (E), Dist (D) + L(D, E))
C -1 A
D -1 B Dist (E) = -5
E -5 D
iteration 1 of 4 Arc(C, D) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
Dist (D) = min( Dist (D), Dist (C) + L(C, D))
C -1 A
D -1 B Dist (D) = min(-1, 1 + 2) = -1
E -5 D
iteration 2 of 4 C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A
D -1 B
E -5 D
iteration 2 of 4 Arc(A, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (B) = min(1, 0 + 2) = 2
D -1 B
E -5 D
iteration 2 of 4 Arc(A, C) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (C) = min(-1, 0 + (-1)) = -1
D -1 B
E -5 D
iteration 2 of 4 Arc(B, D) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (D) = min(-1, 1 + (-2)) = -1
D -1 B
E -5 D
iteration 2 of 4 Arc(C, B) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (B) = min(1, -1 + (2)) = 1
D -1 B
E -5 D
iteration 2 of 4 Arc(C, D) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (D) = min(-1, 1 + (2)) = -1
D -1 B
E -5 D
iteration 2 of 4 Arc(C, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (E) = min(-1, 1 + (2)) = -1
D -1 B
E -5 D
iteration 2 of 4 Arc(D, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (D) = min(-1, -1 + (-4)) = -5
D -1 B
E -5 D
iteration 2 of 4 Arc(D, E) C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A Dist (D) = min(-1, -1 + (-4)) = -5
D -1 B
E -5 D
iteration 2 of 4 C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A
D -1 B
E -5 D
iteration 3 of 4 C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A
D -1 B
E -5 D
iteration 4 of 4 C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A
D -1 B
E -5 D
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
B 1
C -1 If Dist (U) > min( Dist (U), Dist (U) + L(U, V) ) Then
the graph contains a negative circuit
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(A, B)
B 1
C -1 Dist (U) > min( Dist (U), Dist (U) + L(U, V) )
D -1
Dist (B) = 1 < min(1, 0 + 2) = 2
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(A, C)
B 1
C -1 Dist (C) = -1 <= min( -1, 0 + (-1) ) = -1
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(B, D)
B 1
C -1 Dist (D) =-1 <= min(-1, 1 + (-2)) = -1
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(C, B)
B 1
C -1 Dist (B) = 1 <= min(1, -1 + (2)) = 1
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(C, D)
B 1
C -1 Dist (D) = -1 <= min( -1, 1 + (2) ) = -1
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(C, E)
B 1
C -1 Dist (E) = -5 <= min( -1, 1 + (2) ) = -1
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(D, E)
B 1
C -1 Dist (D) = -1 <= min(-1, -1 + (-4)) = -5
D -1
E -5
Detection of negative circuits C
2
-1
2
A E
2
Dist -4
2 D
A 0 -2
B
Arc(D, E)
B 1
C -1 Dist (D) = -1 <= min(-1, -1 + (-4)) = -5
D -1
E -5
END C
2
-1
2
A E
2
Dist Prev -4
2 D
A 0 - -2
B
B 1 C
C -1 A • Absence of negative weight circuit accessible from
vertex “A” .
D -1 B • The result represents the shortest paths from "A" to
all other vertices of the graph as well as their weights.
E -5 D
Exercise 1
• Calculate the shortest path from A to the other points of the graph using
the Bellman-Ford algorithm.
b
3 1
a d
5
-2
c
Improving cycle
• Improving cycles (or negative cycles in English) are infinite loops that
continuously reduce the total path distance.
• They pose a real problem because each pass through the cycle can
further reduce the distance, always yielding a “better” path to take.
4 -6 3
C D
2
5
A B
Dist Prev 4 -6 3
A 0 - C D
B ∞ - 2
C ∞ -
D ∞ -
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B ∞ - 2
C ∞ -
D ∞ -
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B 5 A 2
C ∞ -
D ∞ -
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B 5 A 2
C ∞ -
D ∞ -
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B 5 A 2
C 4 A
D ∞ -
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B 5 A 2
C 4 A
D ∞ -
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B 5 A 2
C 4 A
D 8 B
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B 5 A 2
C 4 A
D 8 B
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 8 B
iteration 1 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 8 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 8 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 8 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 8 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 1 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 1 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 4 A
D 1 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 3 D
D 1 B
iteration 2 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 3 D
D 1 B
iteration 3 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 3 D
D 1 B
iteration 3 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 3 D
D 1 B
iteration 3 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 3 D
D 1 B
iteration 3 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -2 C 2
C 3 D
D 1 B
iteration 3 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -3 C 2
C 3 D
D 1 B
iteration 3 of 3
5
A B
Dist Prev 4 -6 3
A 0 - C D
B -3 C 2
C 3 D
D 1 B
Detection of negative circuits
5
A B
Dist 4 -6 3
A 0 C D
B -3 2
Dist 4 -6 3
A 0 C D
B -3 2
Dist 4 -6 3
A 0 C D
B -3 2
Dist 4 -6 3
A 0 C D
B -3 2
• Dijkstra Shortest path from one vertex to all other vertices, positive
arc weight > 0
• Bellman-Ford Shortest path from one vertex to all other vertices,
negative weight arc < 0
• Floyd- Warshall Shortest path between any pair of vertices
Floyd- Warshall algorithm
• Allows to calculate the shortest path for any pair (X,Y) of vertices of the graph,
• the labels are no longer an array (one label per vertex), but a matrix M of size n*n
where the entry M ij corresponds to the shortest path between vertices i and j .
• This algorithm is valid regardless of the weights of the arcs, including if it involves
negative circuits (the algorithm allows us to prove the existence or non-existence of
such circuits).
• The algorithm consists of N main iterations; for each iteration K, we compute the
shortest paths between any pair of vertices with intermediate vertices belonging only
to the set {1,2,…K}.
• At initialization, we calculate the shortest path between any pair of vertices that have
no intermediate vertices, so we just take the length of the arcs that exist and put an
infinite weight if the arc does not exist.
𝐾
• Subsequently, if we note 𝑀𝑖𝑗 the value of the shortest path from i to j whose only
intermediate vertices are in the set {1,2,…K}, then we have the following equality:
𝑘 𝑘−1 𝑘−1 𝑘−1
𝑀𝑖𝑗 = min(𝑀𝑖𝑗 , 𝑀𝑖𝑘 +𝑀𝑘𝑗 )
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 ∞ ∞ ∞ A A 0 0 0
B ∞ 0 ∞ ∞ B 0 B 0 0
C ∞ ∞ 0 ∞ C 0 0 C 0
D ∞ ∞ ∞ 0 D 0 0 0 D
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 ∞ ∞ ∞ A A 0 0 0
B ∞ 0 ∞ ∞ B 0 B 0 0
C ∞ ∞ 0 ∞ C 0 0 C 0
D ∞ ∞ ∞ 0 D 0 0 0 D
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ ∞ B 0 B 0 0
C ∞ ∞ 0 ∞ C 0 0 C 0
D ∞ ∞ ∞ 0 D 0 0 0 D
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ ∞ B 0 B 0 0
C ∞ ∞ 0 ∞ C 0 0 C 0
D ∞ ∞ ∞ 0 D 0 0 0 D
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ ∞ 0 ∞ C 0 0 C 0
D ∞ ∞ ∞ 0 D 0 0 0 D
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ ∞ 0 D 0 0 0 D
5
A B
initialization
4 -6 3
C D
2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=1 5
A B
i=1
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0 > 0 + 0 2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=1
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0 > 5 + ∞ 2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=1
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 5 > 5 + ∞ 2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=1
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 4>5+∞ 2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=1
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>5+3 2
M P
A B C D A B C D
A 0 5 4 ∞ A A A A 0
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=1
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>8 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=2
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>0+∞ 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=2
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0>0+0 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=2
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>0+∞ 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=2
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 3>0+3 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=3
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞ > -6 + ∞ 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=3
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] -6 > -6 + 0 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=3
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0 > -6 + ∞ 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=3
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞ > -6 + 3 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 ∞ C 0 C C 0
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=3
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>-3 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=4
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞ > ∞ + ∞ 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=4
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>∞+0 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=2 5
A B
i=4
j=3 4 -6 3
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0 > ∞ + 3 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=1
j=1 4 -6 3
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 5 > 4 + -6 2
M P
A B C D A B C D
A 0 5 4 8 A A A A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=1
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 5 > -2 2
M P
A B C D A B C D
A 0 -2 4 8 A A C A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=1
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 4>4+∞ 2
M P
A B C D A B C D
A 0 -2 4 8 A A C A B
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=1
j=4 4 -6 3
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 8>1 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=1
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 8 > 1 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=1
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 8>1 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=2
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>∞+∞ 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=2
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0>∞-6 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=2
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞ > ∞ - 6 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=2
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 3>∞-6 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=3
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>∞+0 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=3
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] -6 > -6 + 0 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=3
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0>0+0 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=3
j=4 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] -3 > 0 + -3 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=4
j=1 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞ > 2 + ∞ 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=4
j=2 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] ∞>2+∞ 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=4
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 2>2+0 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=4
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0 > 2 + -3 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 0 D 0 0 D D
K=3 5
A B
i=4
j=3 4 -6 3
C D
M[ i , j] > M[ i , k]+ M[k, j] 0 > -1 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 -1 D 0 0 D C
K=3 5
A B
i=4
j=3 4 -6 3
C D
M[D, D] < 0 A negative cycle exists 2
M P
A B C D A B C D
A 0 -2 4 1 A A C A C
B ∞ 0 ∞ 3 B 0 B 0 B
C ∞ -6 0 -3 C 0 C C B
D ∞ ∞ 2 -1 D 0 0 D C
Exercise 2
• Compute the shortest path between each pair of Vertex,
5
A B
3
4 -6
C D
2