0% found this document useful (0 votes)
5 views

10 - APS - Dynamic Programming

The document discusses dynamic programming as a method for solving optimization problems characterized by overlapping subproblems and optimal substructure. It details the Floyd-Warshall algorithm for finding all-pairs shortest paths in a weighted directed graph, explaining its efficiency and the construction of optimal solutions through a matrix. The algorithm's time complexity is Θ(n³), and it utilizes a bottom-up approach to compute the shortest paths.

Uploaded by

kgztjmqsss
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)
5 views

10 - APS - Dynamic Programming

The document discusses dynamic programming as a method for solving optimization problems characterized by overlapping subproblems and optimal substructure. It details the Floyd-Warshall algorithm for finding all-pairs shortest paths in a weighted directed graph, explaining its efficiency and the construction of optimal solutions through a matrix. The algorithm's time complexity is Θ(n³), and it utilizes a bottom-up approach to compute the shortest paths.

Uploaded by

kgztjmqsss
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/ 55

Algorithms & data structures

Dynamic programming
Damjan Strnad
2

Dynamic programming

dynamic programming is a method for solving
optimization problems that:
– consist of similar subproblems that overlap (i.e., have
common subsubproblems)
– have optimal substructure property (i.e., optimal solution is
built from optimal solutions of subproblems)

dynamic programming is most often used for solving
optimization problems in four steps:
1)characterize the structure of an optimal solution
2)recursively define the value of an optimal solution
3)compute the value of an optimal solution in a bottom-up
fashion
4)construct an optimal solution from computed information
3

Dynamic programming

DP is similar to divide-and-conquer strategy, but the latter
solves problems with independent subproblems:
– with overlapping subproblems, divide-and-conquer
redundantly solves the same subproblems multiple times
– dynamic programming solves each subproblem once and
remembers its solution
– the term „programming“ in the name of the strategy does
not refer to writing code but to tabular storage of already
calculated results (tabulation)

DP is similar to greedy strategy:
– both are based on optimal substructure property
– greedy method does not construct the problem solution
from subproblem solutions but in each step adds a „locally“
optimal solution element to the complete solution
4

All-pairs shortest paths problem



we are searching for shortest paths between all pairs of
nodes in a weighted directed graph G=(V,E) where the cost
function is w: E  ℝ

the problem can be solved using an algorithm for single-
shortest path problem, where a different starting node is
used each time (i.e., the search is repeated |V|-times):
– the time complexity of Dijkstra's algorithm would be
O(|V|3+|V|·|E|) = O(|V|3), if an array is used for the priority
queue, and O(|V|·|E|·log2|V|), if a binary heap is used
– for graphs with negative costs the Bellman-Ford algorithm
can be used with time complexity O(|V|2·|E|), which for dense
graphs means O(|V|4)
5

Floyd-Warshall algorithm

solves the all-pairs shortest paths problem for a weighted
directed graph G=(V,E) with cost function w: E  ℝ

it is based on dynamic programming and is more efficient
than a sequential search of shortest paths from single
source

the graph is described by adjacency (weight) matrix W:

{
0, if i= j
w ij = weight (i , j), if i≠ j and (i , j)∈E
∞, if i≠ j and (i , j)∉E

the weights can be negative but we assume there are no
cycles with negative weight
6

Floyd-Warshall algorithm

optimal solution structure:
– let V={1,2,…,n} be the set of nodes of graph G
– let p be the shortest path from node i to node j that goes through
a subset of nodes Q={1,2,…,k}:

all intermediate nodes on path p are in Q (have index ≤k)

the reverse does not hold necessarily (some nodes from Q are not
in p)
– we can define a relation between p and shortest paths whose
intermediate nodes are in Q'={1,2,…,k-1}:

if node k is not in p, then p is
equal to the shortest path from
i to j with intermediate nodes in Q'

if node k is in p, then p can be
split into two subpaths p1 and p2
whose intermediate nodes are in Q'
7

Floyd-Warshall algorithm
● the optimal solution is built in matrix D=(dij), where dij is
the weight of the shortest path from i to j

the algorithm iterations are labelled using the superscript
index: D(k)=(dij(k)), where dij(k) is the weight of the shortest
path from i to j through the set of nodes {1,2,3,...,k}

recursive solution definition:
– if k=0, then d (k)=w (direct link between nodes)
ij ij
– if k≥1, then the weight of the shortest path from i to j is the
shorter of paths going through and paths going past node
k; their lengths are already computed in D(k-1)
(k )
d =
ij
{
w ij ,
min ( d (k−1)
ij ,d (k−1)
ik +d (k−1)
kj ),
if k=0
if k≥1
8

Floyd-Warshall algorithm
● the optimal solution is built in matrix D=(dij), where dij is
the weight of the shortest path from i to j

the algorithm iterations are labelled using the superscript
index: D(k)=(dij(k)), where dij(k) is the weight of the shortest
path from i to j through the set of nodes {1,2,3,...,k}

recursive solution definition:
– if k=0, then d (k)=w (direct link between nodes)
ij ij
– if k≥1, then the weight of the shortest path from i to j is the
shorter of paths going through and paths going past node
k; their lengths are already computed in D(k-1)

{
w ijthe, length of shortest path from i to j past
if node k (already known)
(k ) k=0
d =
ij
min ( d (k−1)
ij ,d (k−1)
ik +d (k−1)
kj ), if k≥1
9

Floyd-Warshall algorithm
● the optimal solution is built in matrix D=(dij), where dij is
the weight of the shortest path from i to j

the algorithm iterations are labelled using the superscript
index: D(k)=(dij(k)), where dij(k) is the weight of the shortest
path from i to j through the set of nodes {1,2,3,...,k}

recursive solution definition:
– if k=0, then d (k)=w (direct link between nodes)
ij ij
– if k≥1, then the weight of the shortest path from i to j is the
shorter of paths going through and paths going past node
k; their lengths arethealready computed in D (k-1)
length of shortest path from i to j through node k (the

{
lengths of both parts from i to k and from k to j already known)
(k ) w ij , if k=0
d =
ij
min ( d (k−1)
ij ,d (k−1)
ik +d (k−1)
kj ), if k≥1
10

Floyd-Warshall algorithm

bottom-up calculation of optimal solution value:
FLOYD-WARSHALL(W)
n ← number of rows in W
D(0) ← W
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
dij(k) ← min(dij(k-1),dik(k-1) + dkj(k-1))
return D(n)

● matrix D(n)=(dij(n)) contains the lengths of shortest paths,


i.e., dij(n) = δ(i,j) for all i, j∈V

algorithm time complexity is Θ(n3)
11

Floyd-Warshall algorithm

construction of optimal solutions:
– based on matrix D(n) we can construct the predecessor
matrix Π=(πij), where πij is the predecessor of node j on the
shortest path from node i
– Π can also be calculated by constructing the sequence Π(0),
Π(1), …, Π(n), where π(k)ij denotes the predecessor of node j
on the shortest path from node i through subset {1,2,…,k}
(0)
{
π ij = NIL , if i= j or w ij =∞
i, if i≠ j and w ij <∞

{
(k−1) (k−1) (k−1) (k−1)
(k ) π
π = (k−1)
ij , if d ij ≤d ik +d kj
ij
π kj , if d (k−1)
ij >d (k−1)
ik +d (k−1)
kj
– the shortest paths can be reconstructed from Π=Π(n)
12

Floyd-Warshallov algorithm – example



let’s find shortest paths between all 5

pairs of nodes in the given graph 7 2 6

3
8 1
-3 -4
9 5 -2
4 2
7
13

Floyd-Warshallov algorithm – example



initialization (n=5) 5

7 2 6

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

[ ]
0 5 8 −4 ∞
−2 0 ∞ ∞ ∞
(0)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 ∞ 7 ∞ 0

[ ]
NIL 1 1 1 NIL
2 NIL NIL NIL NIL
(0)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 NIL 5 NIL NIL
14

Floyd-Warshallov algorithm – example



1. iteration of the first for loop, k=1: path 5
can go through intermediate node 1 7 2 6

changes occur for the following shortest 8
paths*: 3 1
-3 -4
– 2→3: as d(0)23 > d(0)21 + d(0)13 (∞>-2+8) 5 -2
9
– 2→4: as d(0)24 > d(0)21 + d(0)14 (∞>-2+(-4))
4 2
– 5→2: as d(0)52 > d(0)51 + d(0)12 (∞>6+5) 7

[ ]
– 5→4: as d(0)54 > d(0)51 + d(0)14 (∞>6+(-4)) 0 5 8 −4 ∞
−2 0 ∞ ∞ ∞
(0)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 ∞ 7 ∞ 0

[ ]
NIL 1 1 1 NIL
2 NIL NIL NIL NIL
(0)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 NIL 5 NIL NIL
15

Floyd-Warshallov algorithm – example



1. iteration of the first for loop, k=1: path 5
can go through intermediate node 1 7 2 6

changes occur for the following shortest 8
paths*: 3 1
-3 -4
– 2→3: as d(0)23 > d(0)21 + d(0)13 (∞>-2+8) 5 -2
9
– 2→4: as d(0)24 > d(0)21 + d(0)14 (∞>-2+(-4))
4 2
– 5→2: as d(0)52 > d(0)51 + d(0)12 (∞>6+5) 7

[ ]
– 5→4: as d(0)54 > d(0)51 + d(0)14 (∞>6+(-4)) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(1)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 11 7 2 0

[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(1)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 1 5 1 NIL
16

Floyd-Warshallov algorithm – example



2. iteration of the first for loop, k=2: path 5
can go through intermediate nodes 1 and 2 7 2 6

changes occur for the following shortest 8
paths*: 3 1
-3 -4
– 3→1: as d(1)31 > d(1)32 + d(1)21 (∞>-3+(-2)) 5 -2
9
– 3→4: as d(1)34 > d(1)32 + d(1)24 (9>-3+6)
4 2
– 4→1: as d(1)41 > d(1)42 + d(1)21 (∞>7+(-2)) 7

[ ]
– 4→3: as d(1)43 > d(1)42 + d(1)23 (∞>7+6) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(1)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 11 7 2 0

[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(1)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 1 5 1 NIL
17

Floyd-Warshallov algorithm – example



2. iteration of the first for loop, k=2: path 5
can go through intermediate nodes 1 and 2 7 2 6

changes occur for the following shortest 8
paths*: 3 1
-3 -4
– 3→1: as d(1)31 > d(1)32 + d(1)21 (∞>-3+(-2)) 5 -2
9
– 3→4: as d(1)34 > d(1)32 + d(1)24 (9>-3+(-6))
4 2
– 4→1: as d(1)41 > d(1)42 + d(1)21 (∞>7+(-2)) 7

[ ]
– 4→3: as d(1)43 > d(1)42 + d(1)23 (∞>7+6) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(2)
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
6 11 7 2 0

[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(2)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
5 1 5 1 NIL
18

Floyd-Warshallov algorithm – example



3. iteration of the first for loop, k=3: path 5
can go through intermediate nodes 1, 2 and 7 2 6
3
3
8 1

changes occur for the following shortest
paths*: -3 -4
9 5 -2
– 5→1: as d 51 > d
(2) (2)
53+d (2)
31 (6>7+(-5))
4 2
– 5→2: as d(2)52 > d(2)53 + d(2)32 (11>7+(-3)) 7

[ ]
– 5→4: as d(2)54 > d(2)53 + d(2)34 (2>7+(-9)) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(2)
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
6 11 7 2 0

[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(2)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
5 1 5 1 NIL
19

Floyd-Warshallov algorithm – example



3. iteration of the first for loop, k=3: path 5
can go through intermediate nodes 1, 2 and 7 2 6
3
3
8 1

changes occur for the following shortest
paths*: -3 -4
9 5 -2
– 5→1: as d 51 > d
(2) (2)
53+d (2)
31 (6>7+(-5))
4 2
– 5→2: as d(2)52 > d(2)53 + d(2)32 (11>7+(-3)) 7

[ ]
– 5→4: as d(2)54 > d(2)53 + d(2)34 (2>7+(-9)) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(3)
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
2 4 7 −2 0

[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(3)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
2 3 5 1 NIL
20

Floyd-Warshallov algorithm – example



4. iteration of the first for loop, k=4: path 5
can go through intermediate nodes 1, 2, 3 7 2 6
and 4
3
8 1

changes occur for the following shortest
paths*: -3 -4
9 5 -2
– 1→2: as d 12 > d
(3) (3)
14+d (3)
42 (5>-4+7)
4 2
– 1→5: as d(3)15 > d(3)14 + d(3)45 (∞>-4+2) 7

[ ]
– 2→5: as d(3)25 > d(3)24 + d(3)45 (∞>-6+2) 0 5 8 −4 ∞
– 3→5: as d(3)35 > d(3)34 + d(3)45 (∞>-9+2) (3)
−2 0 6 −6 ∞
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
2 4 7 −2 0

[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(3)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
2 3 5 1 NIL
21

Floyd-Warshallov algorithm – example



4. iteration of the first for loop, k=4: path 5
can go through intermediate nodes 1, 2, 3 7 2 6
and 4
3
8 1

changes occur for the following shortest
paths*: -3 -4
9 5 -2
– 1→2: as d 12 > d
(3) (3)
14+d (3)
42 (5>-4+7)
4 2
– 1→5: as d(3)15 > d(3)14 + d(3)45 (∞>-4+2) 7

[ ]
– 2→5: as d(3)25 > d(3)24 + d(3)45 (∞>-6+2) 0 3 8 −4 −2
– 3→5: as d(3)35 > d(3)34 + d(3)45 (∞>-9+2) (4)
−2 0 6 −6 −4
D =W= −5 −3 0 −9 −7
5 7 13 0 2
2 4 7 −2 0

[ ]
NIL 4 1 1 4
2 NIL 1 1 4
(4)
Π = 2 3 NIL 1 4
2 4 1 NIL 4
2 3 5 1 NIL
22

Floyd-Warshallov algorithm – example



5. iteration of the first for loop, k=5: path 5
can go through all intermediate nodes 7 2 6

changes occur for the following shortest 8
paths*: 3 1
-3 -4
– 1→2: as d(3)12 > d(3)15 + d(3)52 (3>-2+4) 5 -2
9
– 1→3: as d(3)13 > d(3)15 + d(3)53 (8>-2+7)
4 2
– 2→3: as d(3)23 > d(3)25 + d(3)53 (6>-4+7) 7

[ ]
– 4→1: as d(3)41 > d(3)45 + d(3)51 (5>2+2) 0 3 8 −4 −2
– 4→2: as d(3)42 > d(3)45 + d(3)52 (7>2+4) −2 0 6 −6 −4
(4)
D =W= −5 −3 0 −9 −7
– 4→3: as d(3)43 > d(3)45 + d(3)53 (13>2+7) 5 7 13 0 2
2 4 7 −2 0

[ ]
NIL 4 1 1 4
2 NIL 1 1 4
(4)
Π = 2 3 NIL 1 4
2 4 1 NIL 4
2 3 5 1 NIL
23

Floyd-Warshallov algorithm – example



5. iteration of the first for loop, k=5: path 5
can go through all intermediate nodes 7 2 6

changes occur for the following shortest 8
paths*: 3 1
-3 -4
– 1→2: as d(3)12 > d(3)15 + d(3)52 (3>-2+4) 5 -2
9
– 1→3: as d(3)13 > d(3)15 + d(3)53 (8>-2+7)
4 2
– 2→3: as d(3)23 > d(3)25 + d(3)53 (6>-4+7) 7

[ ]
– 4→1: as d(3)41 > d(3)45 + d(3)51 (5>2+2) 0 2 5 −4 −2
– 4→2: as d(3)42 > d(3)45 + d(3)52 (7>2+4) −2 0 3 −6 −4
(5)
D =W= −5 −3 0 −9 −7
– 4→3: as d(3)43 > d(3)45 + d(3)53 (13>2+7) 4 6 9 0 2
2 4 7 −2 0

[ ]
NIL 3 5 1 4
2 NIL 5 1 4
(5)
Π = 2 3 NIL 1 4
2 3 5 NIL 4
2 3 5 1 NIL
24

Floyd-Warshallov algorithm – example



reconstruction of shortest paths – 5

example: 7 2 6
– shortest path 1→2: 8
3 1
● the parent of 2 on path from 1 is π(5)12=3
-3 -4
● the parent of 3 on path from 1 is π(5)13=5 9 5 -2
● the parent of 5 on path from 1 is π(5)15=4 4 2
7
the parent of 4 on path from 1 is π(5)14=1

[ ]

0 2 5 −4 −2

shortest path is 1-4-5-3-2 −2 0 3 −6 −4
(5)
D =W= −5 −3 0 −9 −7
– shortest path 4→3: 4 6 9 0 2
● the parent of 3 on path from 4 is π(5)43=5 2 4 7 −2 0

[ ]
● the parent of 5 on path from 4 is π(5)45=4 NIL 3 5 1 4

shortest path is 4-5-3 2 NIL 5 1 4
(5)
Π = 2 3 NIL 1 4
2 3 5 NIL 4
2 3 5 1 NIL
25

Traveling salesman problem



we are searching for the shortest cycle (Hamiltonian
path) in a weighted graph G=(V,E) with cost (distance)
matrix C=(cij), which visits all graph nodes exactly once
and returns to the starting node

in a full graph with n nodes there are (n-1)! possible
circular paths, which means that the upper bound of time
complexity is O(nn)

the start node is arbitrary, but the problem has optimal
substructure (each part of optimal circular path is optimal)

the solution using dynamic programming is the Bellman-
Held-Karp algorithm
26

Bellman-Held-Karp algorithm

optimal solution structure: suppose the start node is node 1
from which we continue to node k: k
– the circular path consists of edge (1,k) and path
from node k back to node 1 through all other 1
nodes V – {1,k} V-{1,k}
– let g(i,S) denote the length of shortest path that
starts in i, goes through all nodes in set S exactly once and
finishes in node 1
– S can contain all graph nodes except node 1, so its size can be |
S|=1, 2,…, n-1
– the length of the shortest circular path is given by g(1,S) for
S=V – {1}, |S|=n-1
– according to the optimality principle the following holds:
g(1 , V −{1})= min {c [1 , k ]+ g(k , V −{1 , k })}
2≤k≤n
27

Bellman-Held-Karp algorithm

recursive solution definition: assume more generally that
we are in node i and still have to visit the nodes in S:
– for the continuation of the circular path, we will select the
node j∈S for which the sum of edge (i,j) cost and the cost of
the rest of circular path is minimal (Bellman equation):
g(i , S)=min {c [i , j]+ g( j , S−{ j})}, for 1<i≤n
j∈S

– if the set S is empty, we have visited all nodes and only need
to return to the start node:
g(i , ∅)=c [i , 1] , for 1<i≤n

Bellman equation represents the recursive description of a
solution, which is constructed bottom-up (from |S|=0
towards |S|=n-1)
28

Bellman-Held-Karp algorithm

reconstruction of optimal circular path:
– let J(i,S) denote the value of j that minimizes the right
side of the Bellman equation:
g(i , S)=min {c [i , j]+ g( j , S−{ j})}, for 1<i≤n
j∈S

– the optimal path is built in reverse order:



in the last step we find the first intermediate node to
which we can go from the start node, i.e., J(1,V-{1})

the second intermediate node is found from the result of
penultimate step, i.e., J(J(1,V-{1}), V-{1}-J(1,V-{1})

similarly we reconstruct the complete optimal circular
path
29

Bellman-Held-Karp algorithm – example



given is an adjacency matrix of a graph with 4 nodes

[ ]
– for |S|=0 we get: 0 5 11 4
g(2 , ∅)=c [2,1]=3 3 0 6 2
C=
1 8 0 7
g(3 , ∅)=c [3,1]=1
6 4 9 0
g(4 , ∅)=c [ 4,1]=6
30

Bellman-Held-Karp algorithm – example



given is an adjacency matrix of a graph with 4 nodes

[ ]
– for |S|=0 we get: 0 5 11 4
g(2 , ∅)=c [2,1]=3 3 0 6 2
C=
1 8 0 7
g(3 , ∅)=c [3,1]=1
6 4 9 0
g(4 , ∅)=c [ 4,1]=6
– for |S|=1 we get:
g(2 , {3})=c [2,3]+ g(3 , ∅)=6+1=7 J (2 , {3})=3
g(2 , {4 })=c [2,4 ]+ g(4 , ∅)=2+6=8 J (2 , {4 })=4
g(3 , {2})=c [3,2]+ g(2 , ∅)=8+3=11 J (3 , {2})=2
g(3 , {4 })=c [3,4 ]+ g(4 , ∅)=7+6=13 J (3 , {4 })=4
g(4 , {2})=c [ 4,2]+ g(2 , ∅)=4+3=7 J (4 , {2})=2
g(4 , {3})=c [ 4,3]+ g(3 , ∅)=9+1=10 J (4 , {3})=3
31

Bellman-Held-Karp algorithm – example



given is an adjacency matrix of a graph with 4 nodes

[ ]
– for |S|=2 we get: 0 5 11 4
g(2 , {3,4 })= min {c [2,3]+ g(3 , {4 }), c [2,4 ]+ g(4 , {3})}= 3 0 6 2
j∈{3,4} C=
1 8 0 7
=min {6+13,2+10}=12
6 4 9 0
g(3 , {2,4 })= min {c [3,2]+ g(2 , {4 }), c [3,4 ]+ g(4 , {2})}=
j∈{2,4}
J (2 , {3,4 })=4
=min {8+8,7+7}=14
J (3 , {2,4 })=4
g(4 , {2,3})= min {c [ 4,2]+ g(2 , {3}), c [ 4,3]+ g(3 , {2})}=
j∈{2,3} J (4 , {2,3})=2
=min {4+7,9+11}=11
32

Bellman-Held-Karp algorithm – example



given is an adjacency matrix of a graph with 4 nodes

[ ]
– for |S|=2 we get: 0 5 11 4
g(2–, {3,4 })= min {c [2,3]+ g(3 , {4 }), c [2,4 ]+ g(4 , {3})}= 3 0 6 2
j∈{3,4} C=
1 8 0 7
=min {6+13,2+10}=12
6 4 9 0
g(3 , {2,4 })= min {c [3,2]+ g(2 , {4 }), c [3,4 ]+ g(4 , {2})}=
j∈{2,4}
J (2 , {3,4 })=4
=min {8+8,7+7}=14
J (3 , {2,4 })=4
g(4 , {2,3})= min {c [ 4,2]+ g(2 , {3}), c [ 4,3]+ g(3 , {2})}=
j∈{2,3} J (4 , {2,3})=2
=min {4+7,9+11}=11
– for |S|=3 we get:
g(1 , {2 , 3,4 })= min {c [1,2]+ g(2 , {3,4 }), c [1,3]+ g(3 , {2,4 }), c [1,4 ]+ g(4 , {2,3})}=
j∈{2,3,4}
=min {5+12,11+14,4+11}=15
J (1 , {2,3,4 })=4 the length of shortest tour
33

Bellman-Held-Karp algorithm – example


reconstruction of optimal path:
– the first intermediate node is J(1,{2,3,4})=4
– the next intermediate node is J(4,{2,3})=2
– the last intermediate node is J(2,{3})=3
– optimal circular path is 1-4-2-3-1

J (2 , {3})=3 J (2 , {3,4 })=4 J (1 , {2,3,4 })=4


J (2 , {4 })=4 J (3 , {2,4 })=4
J (3 , {2})=2 J (4 , {2,3})=2
J (3 , {4 })=4
J (4 , {2})=2
J (4 , {3})=3
34

Bellman-Held-Karp algorithm

algorithm time complexity:
– the size of set S is increased from 1 to n-1 (k=1,...,n-1)

– the set V-{1} has ( nk- 1) subsets of size k


– for each subset S of size k, we perform n-k-1
calculations of g (as many as there are elements in
V-{1} that are not in S)
– in each calculation of g we compute k costs (one for
each element of S)
– total time complexity is T(n)=O(n2·2n), which is more
efficient than O(nn) but still exponential

space complexity is also exponential S(n)=O(n·2n)
35

Optimal binary search trees



optimal binary search tree minimizes the average
search time for keys given their frequency
● let a1, a2, ..., an be the symbols (keys) that are stored in a
binary search tree, such that a1 < a2 < ... < an
● let pi denote the search probability for key ai
● let z0, z1, ..., zn denote the sets of symbols which are not
stored in the tree, such that z0 < a1 < z1 < a2 < ... < an < zn
● let qi denote the search probability for set zi (i.e., the
probability that we will be unsuccessfully searching a
symbol between ai and ai+1)
n n

it holds that ∑ pi + ∑ qi =1
i=1 i= 0
36

Optimal binary search trees


● for given keys ai, probabilities pi and probabilities qi we want to
construct a binary search tree szch that the average search
time is minimal
● keys ai are represented by inner tree nodes and unsuccessful
searches end in the leaves which represent key sets zi
● the search time for key ai is depth
depth(ai)+1 and the search time a3 0
for a key from zi is depth(zi)
a2 a5 1

the cost of binary search tree
with n keys ai is:
a1 z2 a4 z5 2
n n
c=∑ pi⋅(depth(ai )+1)+ ∑ q i⋅(depth( z i ))
i=1 i=0 z0 z1 z3 z4 3
37

Optimal binary search trees


● example: given is a set of keys a = [a1, a2, a3] = [for, if,
while] with corresponding probabilities p = [0.5, 0.1, 0.05]
and q = [0.15, 0.1, 0.05, 0.05]

examples of binary search trees and their costs:
c=2.65 c=1.5 c=2.05
while for while

if z3 z0 if for z3

for z2 z1 while z0 for

z0 z1 z2 z3 z1 z2
38

Optimal binary search trees


● example: given is a set of keys a = [a1, a2, a3] = [for, if,
while] with corresponding probabilities p = [0.5, 0.1, 0.05]
and q = [0.15, 0.1, 0.05, 0.05]

examples of binary search trees and their costs:
c=1.6 c=1.9
for if

z0 while for while

if z3 z0 z1 z2 z3

z1 z2
39

Optimal binary search trees



optimal solution structure:
– every subtree of optimal binary search tree is an optimal
binary search tree for the subset of keys it contains (every
subtree contains consecutive keys)
– let Tij (0 ≤ i, j ≤ n) denote a subtree with minimal cost for a
subset of keys ai+1, ai+2, ..., aj; Tij also „covers“ nodes
zi, zi+1, ..., zj
– let cij denote the cost of tree Tij and rij its root node
– let wij denote the sum of probabilities of nodes in Tij:
j j
w ij = ∑ pl + ∑ q l
l=i+1 l=i
40

Optimal binary search trees



optimal solution structure: ak
– optimal tree Tij consists of root ak
(i < k ≤ j) and two optimal subtrees, the
left subtree Ti,k−1 and the right subtree Tkj*
Ti,k-1 Tk,j
– the left optimal subtree Ti,k−1 contains
keys ai+1, ai+2, ..., ak−1, the right optimal
subtree Tkj contains keys ak+1, ak+2, ..., aj
– if i=k−1 (or k=j), the subtree is „empty“ and contains only
node zi (or zj)
– the probability of tree Tii is wii=qi, its cost is cii=0
41

Optimal binary search trees



recursive solution definition:
– the cost of optimal (sub)tree Tij with root ak (i < k ≤ j) equals:
c ij = p k +(w i , k−1 +c i , k−1 )+(w kj +c kj )=
=c i , k−1 +c kj +( p k +w i , k−1 +w kj )=
=c i , k−1 +c kj +w ij
– optimal binary search tree can be built from two optimal
binary subtrees by choosing the root ak for which the
following expression takes minimal value:
c ij = min {c i , k−1 +c kj }+w ij
i<k≤ j
42

Optimal binary search trees



recursive solution definition:
– the cost of optimal (sub)tree Tij with
cost of searching the root
root atadepth
k
(i <0 k ≤ j) equals:
c ij = p k +(w i , k−1 +c i , k−1 )+(w kj +c kj )=
=c i , k−1 +c kj +( p k +w i , k−1 +w kj )=
=c i , k−1 +c kj +w ij
– optimal binary search tree can be built from two optimal
binary subtrees by choosing the root ak for which the
following expression takes minimal value:
c ij = min {c i , k−1 +c kj }+w ij
i<k≤ j
43

Optimal binary search trees



recursive solution definition:
cost of deepening the search
– the cost of optimal (sub)tree Tij with
(moving)
root ak (i < k ≤ j) equals:
into left subtree
c ij = p k +(w i , k−1 +c i , k−1 )+(w kj +c kj )=
=c i , k−1 +c kj +( p k +w i , k−1 +w kj )=
=c i , k−1 +c kj +w ij
– optimal binary search tree can be built from two optimal
binary subtrees by choosing the root ak for which the
following expression takes minimal value:
c ij = min {c i , k−1 +c kj }+w ij
i<k≤ j
44

Optimal binary search trees



recursive solution definition:
– costroot
the cost of optimal (sub)tree Tij with of searching
ak (i < k ≤ j) equals:
the left subtree
c ij = p k +(w i , k−1 +c i , k−1 )+(w kj +c kj )=
=c i , k−1 +c kj +( p k +w i , k−1 +w kj )=
=c i , k−1 +c kj +w ij
– optimal binary search tree can be built from two optimal
binary subtrees by choosing the root ak for which the
following expression takes minimal value:
c ij = min {c i , k−1 +c kj }+w ij
i<k≤ j
45

Optimal binary search trees



recursive solution definition:
– cost of deepening
the cost of optimal (sub)tree the search
Tij with root ak (i < k ≤ j) equals:
(moving) into right subtree
c ij = p k +(w i , k−1 +c i , k−1 )+(w kj +c kj )=
=c i , k−1 +c kj +( p k +w i , k−1 +w kj )=
=c i , k−1 +c kj +w ij
– optimal binary search tree can be built from two optimal
binary subtrees by choosing the root ak for which the
following expression takes minimal value:
c ij = min {c i , k−1 +c kj }+w ij
i<k≤ j
46

Optimal binary search trees



recursive solution definition:
– costT
the cost of optimal (sub)tree of searching
with root ak (i < k ≤ j) equals:
ij subtree
the right
c ij = p k +(w i , k−1 +c i , k−1 )+(w kj +c kj )=
=c i , k−1 +c kj +( p k +w i , k−1 +w kj )=
=c i , k−1 +c kj +w ij
– optimal binary search tree can be built from two optimal
binary subtrees by choosing the root ak for which the
following expression takes minimal value:
c ij = min {c i , k−1 +c kj }+w ij
i<k≤ j
47

Optimal binary search trees



bottom-up calculation of optimal solution value:
– construction of optimal binary search w ii =q i
tree starts from „empty“ trees which c ii =0
contain only individual nodes zi r ii =0 , za i=0,1,2 ,…, n
– in the next step we combine w i ,i+1=q i + pi+1 +q i+1
optimal binary trees with a c i ,i+1=q i + pi+1 +q i+1=w i ,i+1
single key ai+1
r i ,i+1=i+1 , za i=0,1,2 ,…, n−1
– in all subsequent steps we w ij = p j +q j +w i , j−1
combine ever larger optimal c ij = min {c i , k−1 +c kj }+w ij
trees based on already known i<k≤ j

costs of smaller optimal r ij =k , za i=0,1,2 ,…, n− j+i


subtrees
48

Optimalna dvojiška drevesa



tvorba optimalne rešitve:
– the cost of complete optimal binary search tree is obtained in
c0n, its root is r0n
– optimal binary search tree T0n with root k=r0n has subtrees T0,k-1
with root r0,k-1 and Tkn with root rkn
– by following the subtrees and their recursive subdivision we
can reconstruct the complete optimal binary search tree

k=r0,n

a=r0,k-1 b=rk,n

r0,a-1 ra,k-1 rk,b-1 rb,n


49

Optimal binary search trees

OPTIMAL-BINARY-SEARCH-TREE(n,a,p,q,T)
% inicialization of empty trees and trees with one node
for i ← 0 to n-1 do
[wii,cii,rii] ← [qi,0,0]
[wi,i+1,ci,i+1,ri,i+1] ← [qi+pi+1+qi+1,qi+pi+1+qi+1,i+1]
% inicialization of empty tree Tnn
[wnn,cnn,rnn] ← [qn,0,0]
for m ← 2 to n do % find optimal trees with m nodes
for i ← 0 to n-m do
j ← i + m
wij ← wi,j-1 + pj + qj
k ← value [t] that minimizes {ci,t-1 + ct,j} given
ri,j-1 ≤ t ≤ ri+1,j
cij ← wij + ci,k-1 + ckj
rik ← k
50

Optimal binary search trees



algorithm time complexity:
– tree size m goes from 2 to n (line 5)
– for each m we need to compute n−m+1 different values
cij (line 6)
– computation of each cij requires finding the minimum of
m values (line 9)
– each of above nested steps requires O(n), in total O(n3)
– it has been proven that the time complexity can be
reduced to O(n2) if the number of root candidates is
limited by requiring:

r i , j−1≤k≤r i+1 , j
51

Optimal binary search trees – example


a=[for, if, while], p=[4/15, 2/15, 1/15], q=[2/15, 3/15, 2/15, 1/15]
● we can safely eliminate fractions: p=[p1,p2,p3]=[4,2,1],
q=[q0,q1,q2,q3]=[2,3,2,1] and summarize the procedure in a table:

m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
– m is the number of keys included in a tree and i is the index of the
first key (i.e., the included keys are ai+1,...,ai+m)
– for each tree Ti,i+m three values are reported: wi,i+m, ci,i+m, ri,i+m
52

Optimal binary search trees – example



reconstruction of optimal binary search tree:
– the root of tree T is a , the left subtree
03 1
a1
T00 is „empty“ (contains only node z0)
and the right subtree is T13

T00 T13

m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
53

Optimal binary search trees – example



reconstruction of optimal binary search tree:
– the root of tree T is a , the left subtree
03 1
a1
T00 is „empty“ (contains only node z0)
and the right subtree is T13 z0 a2
– the root of subtree T is a , the left
13 2
subtree T11 is „empty“ (contains only T11 T23
node z1) and the right subtree is T23

m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
54

Optimal binary search trees – example



reconstruction of optimal binary search tree:
– the root of tree T is a , the left subtree
03 1
a1
T00 is „empty“ (contains only node z0)
and the right subtree is T13 z0 a2
– the root of subtree T is a , the left
13 2 z1 a3
subtree T11 is „empty“ (contains only
node z1) and the right subtree is T23 z2 z3
– the root of subtree T is a , the left
23 3
subtree T22 is „empty“ (contains only node z2) and the right
subtree T33 is „empty“ (contains only node z3)
m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
55

Optimal binary search trees – example



reconstruction of optimal binary search tree:
– the root of tree T is a , the left subtree
03 1
for
T00 is „empty“ (contains only node z0)
and the right subtree is T13 z0 if
– the root of subtree T is a , the left
13 2 z1 while
subtree T11 is „empty“ (contains only
node z1) and the right subtree is T23 z2 z3
– the root of subtree T is a , the left
23 3
subtree T22 is „empty“ (contains only node z2) and the right
subtree T33 is „empty“ (contains only node z3)
m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)

You might also like