Professional Documents
Culture Documents
Chapter 4: Course Graph Algorithms
Chapter 4: Course Graph Algorithms
SHORTEST PATH
Introduction
In the shortest path:
we think of weights (as representing any measure:
time, cost, penalties, loss) that accumulates
linearly along a path,
we want to minimize the sum of the
weights
It is a generalization of breadth-first search
to weighted graphs.
Introduction
s x y z t s x y z t
Example
s x y z t s x y z t
- - - - - 0 Inf Inf Inf Inf
s s 5 10
y y y 14 7 8
z 13
t 9
Solution
Negative-weight edges
Dijkstra’s algorithm relies on the fact that a shortest
path from s to t goes only through vertices that are
closer to s. This is no longer the case for graphs with
negative edges.
Dijkstra’s will not provide the correct result in case
of a graph with negative-weight
Negative-weight edges
Bellman-Ford algorithm allows negative-weight edges in
the input graph and produces a correct answer as long as
no negative-weight cycles are reachable from the source.
Typically, if there is such a negative-weight cycle, the
algorithm can detect and report its existence.
NB: Negative weight cycle is obtained when the sum of
the edge-weights in cycle is negative.
Bellman-Ford ‘s algorithm
Allows negative-weight edges.
Computes d[v] and π[v] for all v ∈ V.
It returns TRUE if no negative-weight cycles reachable
from the source s, and it returns FALSE otherwise.
Bellman-Ford ‘s algorithm
s r x y z s r x y z
- - - - - 0 Inf Inf Inf inf
s r x y z s r x y z
- - - - - 0 Inf Inf Inf inf
s r r s,r -1 1 1 4,2
x -2
Example
Find the shortest paths
from A to all vertices.
Cases with negative edges and
negative weight cycle
Cases with negative edges and
negative weight cycle
RUR to
USD Using a convenient algorithm
RUR to
USD Using a convenient algorithm
RUR to
USD Using a convenient algorithm
Shortest paths
Single-source:
Dijkstra algorithm (only positive weight-
edges)
Bellman-Ford algorithm (non-negative cycle)
Single-destination ?
Single-pair
Dijkstra algorithm (only positive weight-
edges)
Bellman-Ford algorithm (non-negative cycle)
All-pairs ?
Single Destination
Single destination Problem:
Find a shortest path to a given destination
vertex t from each vertex v.
Solution: ?
3 A
5
S 2
1
4 1 B
1
2
C 3
Single Destination
Solution:
Find G of the graph G (Reverse the direction of each edge
in the graph)
Apply Dijkstra’ /Bellman-Ford algorithm (pb of single
source)
Deduce the required shortest paths for the single
destination in G
Single Destination
Example: Find all the shortest
paths to the vertex x of the
graph G
Solution:
x s y z
0 Inf Inf inf
x s y z
- - - -
Solution:
x s y z
0 Inf Inf inf
10 4 2
3
8
x s y z
- - - -
x x x
z
y
Shortest paths
Single-source: Dijkstra algorithm &
Bellman-Ford algorithm
Single-destination: Dijkstra algorithm &
Bellman-Ford algorithm (applied on G)
Single-pair: result already provided by
Dijkstra algorithm & Bellman-Ford
algorithm
All-pairs ?
All-pairs
All-pairs Problem:
Find shortest path from u to v for all u,v ∈ V.
Solution :
Floyd-Warshall algorithm
Floyd-Warshall algorithm
Example : 8
1 3
2
We shall find the
2
shortest paths from: 7 2 5
4 3
1
Floyd-Warshall algorithm
Dynamic algorithm, the problem
should be solved by taking sequence of
decisions. 8
In each stage we will take a decision 1 3
2
5 2
7 2
4 3
1
Floyd-Warshall algorithm
[ ] [ ]
0 3 ∞ 7
( 0) 8 0 2 ∞
− 1 − 1
𝐷 = (0) 2 − 2 −
5 ∞ 0 1 Π =
2 ∞ ∞ 0 3 − − 3
8
4 − − −
[ [ ] ]
1 3
0 3 ∞ 7 Π (1)= − 1 − 1 − ¿4 ¿ − 2
¿ 0 ¿ ¿2 ¿ ¿ ¿ 0 ¿ ¿ ¿ ¿ ¿ ¿2 2
8 0 ¿ ¿ 2 − ¿ ¿ 7
5
4 3
1
Floyd-Warshall algorithm
[ ][ ]
0 3 ∞ 7 − 1 − 1
( 0) 8 0 Π2(0) =
∞ 2 − 2 −
𝐷 = 3 − − 3
5 ∞ 0 1
2 ∞ ∞ 0 4 − − −
8
[ ][ ]
0 3 ∞ 7 1 3
0 1 − 1 2
) 8 0 2 15
=
5 8 0 Π (1)
1 = 2 0 2 1 2
5
2 5 ∞ 0 3 1 0 3 7 2
4 1 − 0
4 3
1
Floyd-Warshall algorithm
[ ]
0 1 − 1
(1) 2 0 2 1
Π =
3 1 0 3 8
4 1 − 0
[ ]
1 3
[ ]
2
03¿
15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 ( 𝑚− 1)
𝑘𝑗 𝑖𝑓 𝐷(𝑚−1) ( 𝑖 , 𝑗 ) >𝐷(𝑚−1) ( 𝑖 ,𝑘 ) +𝐷(𝑚−1) ( 𝑘, 𝑗 )
Floyd-Warshall algorithm
1. Initialize the matrices and Π (0) =( 𝜋 𝑖𝑗 )
3. after selecting the last intermediate vertex n, the matrix contains the length
of the shortest paths between each pair and determine these shortest paths. (n is
the number of vertices of the graph)
Floyd-Warshall algorithm
Example
( )
0 3 8 ∞ −4
∞ 0 ∞ 1 7
( 0)
𝐷 = ∞ 4 0 ∞ ∞
2 ∞ −5 0 ∞
∞ ∞ ∞ 6 0
( )
− 1 1 − 1
− − − 2 2
(0 )
Π = − 3 − − −
4 − 4 − −
− − − 5 −
( )
0 3 8 ∞ −4
∞ 0 ∞ 1 7
( 0)
𝐷 = ∞ 4 0 ∞ ∞
2 ∞ −5 0 ∞
∞ ∞ ∞ 6 0
( )
− 1 1 − 1
− − − 2 2
(0 )
Π = − 3 − − −
4 − 4 − −
− − − 5 −
Result of the algorithm
Fro All pairs Shortest Paths Fro All pairs Shortest Paths
m m
Ori Orig
gin in
1
4
5
3
Result of the algorithm
From All pairs Shortest Paths From All pairs Shortest Paths
Origin Origin
1 1 4 4
1 4
1 4
1 4
2 5 5
2 5
2 5
2 5
3 3
3
3
3
End of Chapter
Single-source shortest paths
in directed acyclic graphs
Matrix multiplication
Input :
The adjacency matrix of weights of the graph G
(V,E)
Matrix multiplication
Input :
The adjacency matrix of weights of the graph G
(V,E)
Matrix multiplication
Input :
The adjacency matrix of weights of the graph G (V,E)
Output :
Matrix
where = ( is the shortest path from vertex i to vertex j)
Matrix multiplication
All sub-paths of a shortest path are shortest paths.
Suppose that we represent the graph by an adjacency matrix
W=(.
Create L an nxn matrix Example
For to n do
for to n do
for to n do
Return L’
Return L’
In this example, the number of
vertices are 5, then the shortest
paths cannot have more than 4
edges, that's‘ why we need to
calculate. Remember that (i.e.
is the minimum weight of any
path between all pairs with at
most 1 edge)
Remarks
…. No change in
◦ ….
for all k
We can compute using the similarity with the matrix multiplication