0% found this document useful (0 votes)
7 views26 pages

DynamicProgramming Part3 FEUP

Uploaded by

GoDaTa replays
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views26 pages

DynamicProgramming Part3 FEUP

Uploaded by

GoDaTa replays
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Analysis and Synthesis of Algorithms

Design of Algorithms

Dynamic Programming
All-Pairs Shortest Paths Problem
Recursive Formulation and Optimality Principle
Algorithms of Floyd-Warshall & Johnson
Copyright 2024, Pedro C. Diniz, all rights reserved.
Students enrolled in the DA class at Faculdade de Engenharia da Universidade do Porto (FEUP)
have explicit permission to make copies of these materials for their personal use.
All-Pairs Shortest Paths: Observations
• Goal: Find All-Pairs Shortest Paths
• If non negative weights
– Use Dijkstra’s Algorithm, assuming each vertex as source
• O(V E log V) (i.e., O(V3 logV) if graph is dense)

• If negative weights
– Use Bellman-Ford-Moore Algorithm, assuming each vertex as source:
• O(V2E) (i.e., O(V4) if graph is dense)

• These Approaches Work…


– Need to find better performing Algorithms

Analysis and Synthesis of Algorithms 2


APSPs - Definitions
• Data Structure: Adjacency Matrix
• Edge Weights Matrix (! × !) W = (wij)

ì0 if i = j
ï
wij = íweight of edge (i, j) if i ¹ j and (i, j ) Î E
ï¥ if i ¹ j and (i, j ) Ï E
î
• Distance Matrix (! × !) D = (dij)
– dij is the added weight of the shortest path
between i and , i.e., dij = d(vi,vj)

Analysis and Synthesis of Algorithms 3


APSPs - Definitions
• Shortest Paths representations
– Predecessors Matrix Õ = (pij)
– pij:
• nil: if i = j or there is no path from i to j
• otherwise: pred. of j is in the shortest path from i to j

– Sub-graph of Predecessors of G to i, Gp, i = (Vp,i, Ep,i)

Vp ,i = {j Î V : p ij ¹ NIL}È {}
i
!",$ = { '$( : * ∈ ,",$ − {.}}

• Sub-graph induced by path i in Õ

Analysis and Synthesis of Algorithms 4


APSPs - Floyd-Warshall Algorithm
• Idea: Augment the Shortest Paths
– For All the Paths from any two nodes áv1,…,vkñ
– Consider Paths going through intermediate nodes from the set {1,…,k}
– Start with direct paths (i.e,. not going through any other nodes)
– Increase the index of the nodes a path can go through and determine
which is the shortest path p1
p2
k
i j

Nodes from 1 to k-1


• Dynamic Programming Formulation
(k ) ìwij if k = 0

d ij
î min d ij(
( k -1)
, d ( k -1)
ik + d ( k -1)
kj ) if k ³ 1

Nodes from 1 to k
Analysis and Synthesis of Algorithms 5
APSPs - Floyd-Warshall Algorithm
Floyd-Warshall(W)
n = rows[W];
D(0) = W;
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
(
d ij(k ) = min d ij(k -1) , d ik(k -1) + d kj(k -1) )
return D(n)

• Complexidade: Q(n3)
p1
p2
k
i j
Nodes from 1 to k-1

Analysis and Synthesis of Algorithms 6


Flyod-Warshall Algorithm: Example

3 2
7
-4
1 5
1
8 2 6
4

3 4
-5

Analysis and Synthesis of Algorithms 7


Flyod-Warshall Algorithm: Example
æ 0 3 8 ¥ - 4ö
ç ÷
ç ¥ 0 ¥ 1 7 ÷
3 2 D (0) = ç¥ 4 0 ¥ ¥ ÷
7 ç ÷
-4 ç 2 ¥ -5 0 ¥ ÷
1 5 ç¥ ¥ ¥ 6 0 ÷
è ø
1
8 2 6
4
æ nil 1 1 nil 1 ö
ç ÷
3 4 ç nil nil nil 2 2÷
-5 P (0) = ç nil 3 nil nil nil ÷
ç ÷
ç 4 nil 4 nil nil ÷
ç nil nil nil 5 nil ÷ø
è

Analysis and Synthesis of Algorithms 8


Flyod-Warshall Algorithm: Example
æ 0 3 8 ¥ - 4ö æ nil 1 1 nil 1 ö
ç ÷ ç ÷
ç¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D (0)
= ç¥ 4 0 ¥ ¥ ÷ P (0)
= ç nil 3 nil nil nil ÷
ç ÷ ç ÷
ç 2 ¥ -5 0 ¥ ÷ ç 4 nil 4 nil nil ÷
ç¥ ¥ ¥ 6 0 ÷ ç nil
3 2 è ø è nil nil 5 nil ÷ø
7
-4
1 5
1
8 2 6
4

3 4
-5

Analysis and Synthesis of Algorithms 9


Flyod-Warshall Algorithm: Example
æ nil 1 1 nil 1 ö
æ 0 3 8 ¥ - 4ö ç ÷
ç ÷ ç nil nil nil 2 2÷
ç¥ 0 ¥ 1 7 ÷
P (0) = ç nil 3 nil nil nil ÷
D (0) = ç¥ 4 0 ¥ ¥ ÷ ç ÷
ç ÷ ç 4 nil 4 nil nil ÷
ç 2 ¥ -5 0 ¥ ÷ ç nil
ç¥ ¥ ¥ 6 0 ÷ è nil nil 5 nil ÷ø
è ø
3 2
7
-4 æ 0 3 8 ¥ - 4ö
ç ÷
æ nil
ç
1 1 nil 1 ö
÷
1 5
ç¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D (1) = ç¥ 4 0 ¥ ¥ ÷ P (1)
= ç nil 3 nil nil nil ÷
ç ÷ ç ÷
1 ç 2 5 - 5 0 - 2÷
ç¥ ¥ ¥ 6 0 ÷ ç 4 1 4 nil 1 ÷
8 2 6 è ø ç nil
è nil nil 5 nil ÷ø
4

3 4
-5

Analysis and Synthesis of Algorithms 10


Flyod-Warshall Algorithm: Example
æ nil 1 1 nil 1 ö
æ 0 3 8 ¥ - 4ö ç ÷
ç ÷ ç nil nil nil 2 2÷
ç¥ 0 ¥ 1 7 ÷
P (0) = ç nil 3 nil nil nil ÷
D (0) = ç¥ 4 0 ¥ ¥ ÷ ç ÷
ç ÷ ç 4 nil 4 nil nil ÷
ç 2 ¥ -5 0 ¥ ÷ ç nil
ç¥ ¥ ¥ 6 0 ÷ è nil nil 5 nil ÷ø
è ø
3 2
7 2
-4 æ 0 3 8 ¥ - 4ö
ç ÷
æ nil
ç
1 1 nil 1 ö
÷
1 5
ç¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D (1) = ç ¥ 4 0 ¥ ¥ ÷ P (1)
= ç nil 3 nil nil nil ÷
ç ÷ ç ÷
4 ç 2 5 - 5 0 - 2÷
1 ç¥ ¥ ¥ 6 0 ÷ ç 4 1 4 nil 1 ÷
8 2 6 è ø ç nil
è nil nil 5 nil ÷ø
4

3 4
-5

Analysis and Synthesis of Algorithms 11


Flyod-Warshall Algorithm: Example
æ 0 3 8 ¥ - 4ö æ nil 1 1 nil 1 ö
ç ÷ ç ÷
ç¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D (0) = ç¥ 4 0 ¥ ¥ ÷ P (0) = ç nil 3 nil nil nil ÷
ç ÷ ç ÷
ç 2 ¥ -5 0 ¥ ÷ ç 4 nil 4 nil nil ÷
ç¥ ¥ ¥ 6 0 ÷ ç nil nil nil 5 nil ÷ø
è
è ø
3 2
7
æ0 3 8 ¥ - 4ö æ nil 1 1 nil 1 ö
-4 ç
ç¥ 0 ¥ 1 7 ÷
÷ ç
ç nil nil nil 2 2÷
÷
1 5 D (1) = ç¥ 4 0 ¥ ¥÷ P (1) = ç nil 3 nil nil nil ÷
ç ÷ ç ÷
ç2 5 -5 0 - 2÷ ç 4 1 4 nil 1 ÷
ç¥ ç nil nil ÷ø
0 ÷ø
1 è ¥ ¥ 6 è nil nil 5

8 2 6
4 æ0 3 8
ç
4 - 4ö
÷
æ nil 1
ç
1 2 1 ö
÷
ç¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D ( 2) = ç¥ 4 0 5 11 ÷ P ( 2) = ç nil 3 nil 2 2÷
ç ÷ ç ÷
3 4 ç 2 5 -5 0 - 2÷ ç 4 1 4 nil 1 ÷
ç nil nil nil 5 nil ÷
-5 ç¥ ¥ ¥
è 6 0 ÷ø è ø

Analysis and Synthesis of Algorithms 12


Flyod-Warshall Algorithm: Example
æ 0 3 8 ¥ - 4ö æ nil 1 1 nil 1 ö
ç ÷ ç ÷
ç¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D (0) = ç¥ 4 0 ¥ ¥ ÷ P (0) = ç nil 3 nil nil nil ÷
ç ÷ ç ÷
ç 2 ¥ -5 0 ¥ ÷ ç 4 nil 4 nil nil ÷
ç¥ ¥ ¥ 6 0 ÷ ç nil nil nil 5 nil ÷ø
è
3 2 è ø
7
æ 0 3 8 ¥ - 4ö æ nil 1 1 nil 1 ö
-4 ç ÷ ç ÷
1 5
ç¥ 0 ¥ 1 7 ÷ ç nil
= ç nil
nil nil 2 2÷
nil ÷
D (1) = ç¥ 4 0 ¥ ¥ ÷ P (1)
ç
3 nil nil
÷
ç ÷
ç 2 5 - 5 0 - 2÷ ç 4 1 4 nil 1 ÷
1 ç¥ ¥ ¥ 6 0 ÷
è ø
ç nil
è nil nil 5 nil ÷ø

8 2 6
4 æ0 3 8 4 - 4ö æ nil 1
ç
1 2 1 ö
÷
ç ÷ ç nil nil nil 2 2÷
ç¥ 0 ¥ 1 7 ÷
= ç¥ 4 0 P ( 2) = ç nil 3 nil 2 2÷
D ( 2) 5 11 ÷ ç ÷
3 4 ç
ç 2 5 -5
÷
0 - 2÷ ç 4 1 4 nil 1 ÷
-5 ç¥ ¥ ¥
è 6 0 ÷ø
ç nil nil nil 5 nil ÷
è ø

æ0 3 8 4 - 4ö æ nil 1 1 2 1 ö
ç ÷ ç ÷
ç ¥ 0 ¥ 1 7 ÷ ç nil nil nil 2 2÷
D ( 3) = ç¥ 4 0 5 11 ÷ P ( 3) = ç nil 3 nil 2 2÷
ç ÷ ç ÷
ç 2 -1 - 5 0 - 2÷ ç 4 3 4 nil 1 ÷
ç¥ ¥ ¥ 6 0 ÷ø ç nil nil nil 5 nil ÷ø
è è

Analysis and Synthesis of Algorithms 13


Flyod-Warshall Algorithm: Example
æ 0 3 -1 4 - 4ö æ nil 1 4 2 1 ö
ç ÷ ç ÷
ç 3 0 - 4 1 -1 ÷ ç 4 nil 4 2 1 ÷
D ( 4) = ç7 4 0 5 3 ÷ P ( 4) =ç 4 3 nil 2 1 ÷
ç ÷ ç ÷
ç 2 -1 - 5 0 - 2÷ ç 4 3 4 nil 1 ÷
ç8 5 1 6 0 ÷ø ç 4 3 4 5 nil ÷ø
è è
3 2
7
æ 0 1 - 3 2 - 4ö æ nil 3 4 5 1 ö
-4 ç ÷ ç ÷
1 5
ç 3 0 - 4 1 -1 ÷ ç 4 nil 4 2 1 ÷
D (5) = ç7 4 0 5 3 ÷ P (5) =ç 4 3 nil 2 1 ÷
ç ÷ ç ÷
ç 2 -1 - 5 0 - 2÷ ç 4 3 4 nil 1 ÷
ç8 5 ç 4 nil ÷ø
1 1 6 0 ÷ø
8 2 6 è è 3 4 5

3 4
-5

Analysis and Synthesis of Algorithms 14


Flyod-Warshall Algorithm: Example
æ 0 1 - 3 2 - 4ö
ç ÷
ç 3 0 - 4 1 - 1 ÷
D (5) ç
= 7 4 0 5 3 ÷
ç ÷
3 2 ç 2 - 1 - 5 0 - 2 ÷
7
ç8 5 1 6 0 ÷ø
-4 è
1 5
æ nil 3 4 5 1 ö
ç ÷
1 6 ç 4 nil 4 2 1 ÷
8 2
4 P (5) =ç 4 3 nil 2 1 ÷
ç ÷
ç 4 3 4 nil 1 ÷
3 4 ç 4 3 4 5 nil ÷ø
-5 è

Analysis and Synthesis of Algorithms 15


Flyod-Warshall Algorithm: Example
æ 0 1 - 3 2 - 4ö
ç ÷
ç 3 0 - 4 1 - 1 ÷
D (5) ç
= 7 4 0 5 3 ÷
ç ÷
3 2 ç 2 - 1 - 5 0 - 2 ÷
7
ç8 5 1 6 0 ÷ø
-4 è
1 5
æ nil 3 4 5 1 ö
-2 ç ÷
1 6 ç 4 nil 4 2 1 ÷
8 2
4 P (5) =ç 4 3 nil 2 1 ÷
ç ÷
-1 ç 4 3 4 nil 1 ÷
ç 4 nil ÷ø
3 4 è 3 4 5
-5

Analysis and Synthesis of Algorithms 16


Transitive Closure of a Directed Graph
• Given a directed graph G = (V, E), the transitive
closure graph G* = (V, E*) is defined over the same
set of nodes with edges given by:
E*={(i, j): exists path from i to j in G}

• Algorithm:
– Assign each Edge Weight 1 and use Floyd-Warshall
algorithm
• If dij ¹ ¥, then (i, j) Î E*
• Complexity: Q(n3)

Analysis and Synthesis of Algorithms 17


Transitive Closure of a Directed Graph
• A Simple Implementation:
– Replace min and + by Ú (OR) and Ù (AND), respectively
– If there is a path from i to j with intermediate nodes in
{1,2,…,k},
tij(k ) = 1
– Otherwise,
tij(k ) = 0
– Formulation:
(0 ) ì0 if i ¹ j and (i, j ) Ï E (
tij(k ) = tij(k -1) Ú tik(k -1) Ù t kj(k -1) ) if k ³ 1
tij =í
î1 if i = j or (i, j ) Î E
– Complexity: Q(n3)

Analysis and Synthesis of Algorithms 18


Transitive Closure of a Directed Graph
Transitive-Closure(G)
n = |V[G]|
for i = 1 to n do
for j = 1 to n do
if i = j or (i, j) Î E then
(0 )
tij = 1
else
tij(0 ) = 0
for k = 1 to n
for i = 1 to n
for j = 1 to n
(
tij(k ) = tij(k -1) Ú tik(k -1) Ù t kj(k -1) )
return T(n)

Analysis and Synthesis of Algorithms 19


Transitive Closure : Example

1 2

3 4

æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
T =ç
1 0 1 1÷
ç ÷
ç0 1 0 1 ÷ø
è

Analysis and Synthesis of Algorithms 20


Transitive Closure : Example

1 2 æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
T (0) =ç
0 1 1 0÷
ç ÷
ç1 0 1 1 ÷ø
è
3 4

æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
T =ç
0 1 1 0÷
ç ÷
ç1 0 1 1 ÷ø
è

Analysis and Synthesis of Algorithms 21


Transitive Closure : Example

æ1 0 0 0ö æ1 0 0 0ö
ç ÷ ç ÷
1 2 ç0 1 1 1÷ ç0 1 1 1÷
T (0) =ç T (1) =ç
0 1 1 0÷ 0 1 1 0÷
ç ÷ ç ÷
ç1 0 1 1 ÷ø ç1 0 1 1 ÷ø
è è

3 4

æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
T =ç
0 1 1 0÷
ç ÷
ç1 0 1 1 ÷ø
è

Analysis and Synthesis of Algorithms 22


Transitive Closure : Example
æ1 0 0 0ö æ1 0 0 0ö
ç ÷ ç ÷
ç0 1 1 1÷ ç0 1 1 1÷
1 2 T (0) =ç T (1) =ç
0 1 1 0÷ 0 1 1 0÷
ç ÷ ç ÷
ç1 0 1 1 ÷ø ç1 0 1 1 ÷ø
è è

æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
3 4 T ( 2) =ç
0 1 1 1÷
ç ÷
ç1 0 1 1 ÷ø
è

æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
T =ç
0 1 1 0÷
ç ÷
ç1 0 1 1 ÷ø
è

Analysis and Synthesis of Algorithms 23


Transitive Closure : Example
æ1 0 0 0ö æ1 0 0 0ö
ç ÷ ç ÷
1 2 ç0 1 1 1÷ ç0 1 1 1÷
T (0) =ç T (1) =ç
0 1 1 0÷ 0 1 1 0÷
ç ÷ ç ÷
ç1 0 1 1 ÷ø ç1 0 1 1 ÷ø
è è

æ1 0 0 0ö æ1 0 0 0ö
ç ÷ ç ÷
ç0 1 1 1÷ ç0 1 1 1÷
T ( 2) =ç T ( 3) =ç
3 4 0 1 1 1÷ 0 1 1 1÷
ç ÷ ç ÷
ç1 0 1 1 ÷ø ç1 1 1 1 ÷ø
è è

æ1 0 0 0ö
ç ÷
ç0 1 1 1÷
T =ç
0 1 1 0÷
ç ÷
ç1 0 1 1 ÷ø
è

Analysis and Synthesis of Algorithms 24


Transitive Closure : Example

æ1 0 0 0ö æ1 0 0 0ö
ç ÷ ç ÷
ç0 1 1 1÷ ç0 1 1 1÷
1 2 T (0) =ç T (1) =ç
0 1 1 0÷ 0 1 1 0÷
ç ÷ ç ÷
ç1 0 1 1 ÷ø ç1 0 1 1 ÷ø
è è

æ1 0 0 0ö æ1 0 0 0ö
ç ÷ ç ÷
ç0 1 1 1÷ ç0 1 1 1÷
T ( 2) =ç T ( 3) =ç
3 4 0 1 1 1÷ 0 1 1 1÷
ç ÷ ç ÷
ç1 0 1 1 ÷ø ç1 1 1 1 ÷ø
è è

æ1 0 0 0ö
æ1 0 0 0ö ç ÷
ç ÷ ç1 1 1 1÷
ç0 1 1 1÷ T ( 4) =ç
T =ç 1 1 1 1÷
ç ÷
0 1 1 0÷ ç1 1 1 1 ÷ø
ç ÷ è
ç1 0 1 1 ÷ø
è

Analysis and Synthesis of Algorithms 25


Summary
• All-Pairs Shortest Paths (APSPs)
– Definitions
– Recursive Solutions
• Floyd-Warshall Algorithm
– Transitive Closure

Analysis and Synthesis of Algorithms 26

You might also like