Professional Documents
Culture Documents
Home Exercise 3: Dynamic Programming and Randomized Algorithms
Home Exercise 3: Dynamic Programming and Randomized Algorithms
Team member: Yifan WANG, Xingshan HE, Michele Natacha Ela Essola
item 1 2 3 4 5
profit 4 3 5 6 2
weight 4 3 4 2 3
Follow the dynamic programming algorithm from the lecture and fill out the table below with the
corresponding profit values of the subproblems to pack the first 𝑖 items into a knapsack of weight 𝑗:
What is the optimal packing?
𝑖/𝑗 0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 4 4 4 4 4 4 4
2 0 0 0 3 4 4 4 7 7 7 7
3 0 0 0 3 5 5 5 8 9 9 9
4 0 0 6 6 6 9 11 11 11 14 15
5 0 0 6 6 6 9 11 11 11 14 15
The weight of 𝑖3 is 4 and the value of 𝑖3 is 5, 𝑉[3−1,8−4] = 𝑉[2,4] = 4 = 𝑉[3,8] − 5, move to 𝑉[2,4];
As 𝑉[2,4] = 𝑉[1,4] and 𝑉[1,4] ≠ 𝑉[0,4] = 0, the introduction of item 2 has no effect on the optimal packing,
and item 1 is in the optimal packing.
So the optimal packing is:
𝑖1 𝑖2 𝑖3 𝑖4 𝑖5
1 0 1 1 0
2-Matrix Chain Multiplications (5 points)
Consider the multiplication of 𝑛 matrices 𝐴1 · A2 ··· An where matrix 𝐴𝑖 is an 𝑎𝑖 -by-𝑏𝑖 matrix. The
number of all possible orders of multiplications is exponential in 𝑛 and, thus, an enumeration/brute force
approach will not be feasible. We consider dynamic programming instead here. Let 𝐶(𝑖, 𝑗) be the optimal
cost (in number of basic multiplications) to compute 𝐴𝑖 · Ai+1 ··· Aj .
1. Which values of 𝐶(𝑖, 𝑗) are easy to compute (“initialization of the dynamic programming”) and which
value of 𝐶(𝑖, 𝑗) corresponds to the optimal solution (the cost of the entire matrix chain multiplication)?
According to the Lecture Slides, dynamic programming is a method for solving a complex problem by breaking it
down into a collection of simpler subproblems. Two of the Typical Algorithm Designs are (1) bottom-up solving of
the subproblems (i.e. computing their optimal value), starting from the smallest by using a table structure to store
the optimal values and the Bellman equality; and (2) eventually construct the final solution (can be omitted if only
the value of an optimal solution is sought).
In this case, the ‘bottom’ or ‘base’ (simplest subproblems) are the cost (numbers of multiplications) of
getting 𝐶𝑥 of 𝐴𝑥 where 𝑖 ≤ 𝑥 ≤ 𝑗, which are 0 and are easy to compute (have no cost).
An upper tier of simple subproblems is to get 𝐶(𝑥, 𝑥 + 1) which is the numbers of multiplications to get
the product of 𝐴𝑥 · Ax+1 where 𝑖 ≤ 𝑥 ≤ 𝑗 − 1. This is also easy to compute (but more complicated
compared with getting 𝐶𝑥 of 𝐴𝑥 , these processes already need the values of the base, which is 𝐶𝑥 ).
Obtaining the numbers of multiplications to get the products of three adjacent matrix like 𝐴𝑥−1 · Ax ·
Ax+1 is much more complicated and will need the Bellman equation. There are ways like (𝐴𝑥−1 · Ax ) ·
Ax+1 and 𝐴𝑥−1 · (Ax · Ax+1 ), so it is not a easy computation.
And in this case, the number of multiplications we get on the very top (constructed final solution)
corresponds to the optimal solution. It is the value of the minimum number of multiplications to compute
𝐴𝑖 · Ai+1 ··· Aj . This can only be obtained after all subproblems are done in dynamic programming.
2.Consider that the corresponding solution for the subproblem 𝐶(𝑖, 𝑗) calculates first 𝐴𝑖 ··· Ak , then 𝐴𝑘+1 ·
·· Aj and finally multiplies the corresponding matrices 𝐴𝑖 · Ai+1 ··· Aj is computed as (𝐴𝑖 ··· Ak ) · (Ak+1 ···
Aj ). Note that the splitting point 𝑘 is unknown in advance.
For 𝑘 in a fixed position, the total number of multiplications are 𝐶(𝑖, 𝑘) + 𝐶(𝑘 + 1, 𝑗) + 𝑝𝑖−1 𝑝𝑘 𝑝𝑗
To find 𝐶(𝑖, 𝑗), we need to compare all the values for 𝑘 in all 𝑗 − 𝑖 positions and pick the smallest one.
So, the Bellman equation to compute 𝐶(𝑖, 𝑗) is:
𝐶(𝑖, 𝑗) = 0, 𝑖𝑓 𝑖 = 𝑗
𝐶(𝑖, 𝑗) = min {𝐶(𝑖, 𝑗) = 𝐶(𝑖, 𝑘) + 𝐶(𝑘 + 1, 𝑗) + 𝑝𝑖−1 𝑝𝑘 𝑝𝑗 } , 𝑖𝑓 𝑖 < 𝑗
𝑖≤𝑘<𝑗
3.Consider the example of five matrices 𝐴1 (5-by-2), 𝐴2 (2-by-10), 𝐴3 (10-by-1), 𝐴4 (1-by-10), and 𝐴5
(10-by-2) and complete a table like the following one with the values of C(i, j) as the dynamic
programming approach would do. What are the actual minimum number of basic multiplications needed?
𝑖/𝑗 1 2 3 4 5
1 0 100 30 80 60
2 - 0 20 40 44
3 - - 0 100 40
4 - - - 0 20
5 - - - - 0
𝑘 1 2 3 4 5
1 - - 1 3 3
2 - - - 3 3
3 - - - - 3
4 - - - - -
5 - - - - -
a) Given the fitness function 𝑓(𝑥) = √𝑥 on a single variable x ∈ R, calculate the probability of selecting
the individuals 𝑎 with 𝑥 = 1, 𝑏 with 𝑥 = 4, and 𝑐 with 𝑥 = 9, using the roulette wheel selection from the
lecture.
individual a b c
fitness 1 2 3
𝑄𝑖 1 2 3
6 6 6
Cumulative Probability 1 3 6
6 6 6
b) Calculate the probability of selecting the same individuals when the fitness function is 𝑔(𝑥) =
2
(𝑓(𝑥)) .
2 2
𝑔(𝑥) = (𝑓(𝑥)) = (√𝑥) = 𝑥
individual a b c
fitness 1 4 9
𝑄𝑖 1 4 9
14 14 14
Cumulative Probability 1 5 14
14 14 14
c) Calculate the probabilities of selecting each solution from the population {𝑎, 𝑏, 𝑐} (with the same above
solutions 𝑥 = 1 (a), 𝑥 = 4 (b), and 𝑥 = 9 (c) and for both fitness functions 𝑓(𝑥) and 𝑔(𝑥) )in a single
binary tournament. A single binary tournament means, we draw two solutions uniformly at random from
the population (with replacement) and pick the better one.
The table for all possible results is as shown:
Pairs 𝑓(𝑥) 𝑔(𝑥) Selection
𝑎, 𝑎 1 1 1 1 𝑎
𝑎, 𝑏 1 2 1 4 𝑏
𝑎, 𝑐 1 3 1 9 𝑐
𝑏, 𝑎 2 1 4 1 𝑏
𝑏, 𝑏 2 2 4 4 𝑏
𝑏, 𝑐 2 3 4 9 𝑐
𝑐, 𝑎 3 1 9 1 𝑐
𝑐, 𝑏 3 2 9 4 𝑐
𝑐, 𝑐 3 3 9 9 𝑐
The selection of fitness function (𝑓 𝑎𝑛𝑑 𝑔) does not change the results in this case.
𝑓𝑂𝑀 (𝑥) = ∑ 𝑥𝑖
𝑛=1
Or the 𝑓𝑇𝑍 (𝑥) + 1𝑡ℎ element is the first 𝑥𝑖 that 𝑥𝑓𝑇𝑍 (𝑥)+1 = 1
b) What are the maxima of the two functions? What are the values of 𝑓𝑂𝑀 and of 𝑓𝑇𝑍 at those optima?
When 𝑓𝑂𝑀 is maximized, all the elements of the bitstring equals to 1; 𝑥𝑖 = 1 𝑓𝑜𝑟 1 ≤ 𝑖 ≤ 𝑛
𝑀𝑎𝑥(𝑓𝑂𝑀 ) = ∑𝑥𝑙 = 𝑛
When 𝑓𝑇𝑍 is maximized, all the elements of the bitstring equals to 0; 𝑥𝑖 = 0 𝑓𝑜𝑟 1 ≤ 𝑖 ≤ 𝑛
𝑀𝑎𝑥(𝑓𝑇𝑍 ) = 1 + 1 + ⋯ + 1 = 𝑛
c) Compute the expected time (in number of function evaluations) to reach the optimum as a function of
the search space dimension n.
Hint: show that the time to reach the optimum follows a geometric distribution with a parameter to
determine.
1
For the search space dimension n, the chance for a search to find the optimum is 𝑝 = 2𝑛
𝑘−1
1 𝑘−1 1
Pr(𝑋 = 𝑘) = (1 − 𝑝) 𝑝 = (1 − 𝑛 ) ∗ 𝑛
2 2
Which follows a geometric distribution.
1
𝐸(𝑥) = = 2𝑛
𝑝