Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Design and Analysis of Algorithms

Dynamic Programming
Dr. Lê Nguyên Khôi
The VNU University of Engineering and Technology
Contents

 Bottom-up design technique


 Typical problems

Design and Analysis of Algorithms 1


Divide & Conquer – Re-visit

 Algorithm design technique


 Top-down design
 Divide the problem into sub-problems
 Solve sub-problems recursively
 Combine solution to sub-problems to construct the
solution to the original problem
 Example
 Merge sort
 Quick sort
 Compute Fibonacci number

Design and Analysis of Algorithms 2


Divide & Conquer – Fibonacci Numbers

0 1 1 2 3 5 8 13 21 …
Fibonacci
1 if return
2 if return
3 return Fibonacci
+ Fibonacci
Complexity: (exponential growth)
for – golden ratio

Design and Analysis of Algorithms 3


Divide & Conquer – Fibonacci Numbers

 Use binary tree representation to illustrate the


computational process
 What is the height of the tree
 How many leaves are there
 What is the bound for the number of leaves
 Number of additions
 Complexity
 Top-down design technique
 Solve similar sub-problems

Design and Analysis of Algorithms 4


Fibonacci Numbers – Other Technique

 Work backward from leaves of the left-most


sub-tree
 Compute the value (key) of the corresponding
parent (internal node)
 Work upward to the root of the tree
 Bottom-up design technique

Design and Analysis of Algorithms 5


Dynamic Programming

 Algorithm design technique


 Bottom-up design
 Solve sub-problems from the smallest size ( ) to
the biggest size (the original problem)
 Construct solutions to the sub-problem based on
solutions of the smaller size sub-problems
 Example
 Compute Fibonacci number
 Insertion sort

Design and Analysis of Algorithms 6


Dynamic Programming – Usage

Properties of the problem:


 Overlapping sub-problems
 A recursive solution contains a “small” number of
distinct sub-problems repeated many times
 Sub-problems share sub-subproblems
 Optimal structure
 An optimal solution to a problem contains optimal
solutions to sub-problems
 The optimal solutions to the sub-problem could be
used to construct the optimal solutions to the original
problem

Design and Analysis of Algorithms 7


Dynamic Programming – Usage

 Apply to optimization problems


 Have many possible solutions
 Each solution has a objective value
 Find a solution with the optimal value
 There could be more than 1 optimal solution

Design and Analysis of Algorithms 8


Design Steps
1. Characterize the structure of a solution
2. The method to combine solutions to sub-problems to
construct solutions to the original problem
3. Design tables to store solutions to sub-problems for
latter use
4. Compute solutions to sub-problems (from small sub-
problems to bigger sub-problems)
5. Determine the solution to the original problem based
on solutions in the table

Dynamic programming is a tabular method

Design and Analysis of Algorithms 9


Divide & Conquer – Dynamic Programming

 Determine the correct design technique


 Apply the divide & conquer technique for
problems with overlapping subproblems
 Re-compute solutions to sub-problems
 Inefficient
 Example: compute Fibonacci number

Design and Analysis of Algorithms 10


Problems

 Compute Fibonacci number


 Binomial coefficients
 Cutting rod
 Longest increasing subsequence
 Knapsack
 Longest common subsequence

Design and Analysis of Algorithms 11


Compute Fibonacci number

 Divide & conquer


 Overlapping sub-problems
 Dynamic programming
 Table to store values of

Design and Analysis of Algorithms 12


Binomial Coefficients

 Divide & conquer


 Overlapping sub-problems
 Dynamic programming
 Table to store values of

Design and Analysis of Algorithms 13


Cutting Rod

 Read section 15.1 p.360

Design and Analysis of Algorithms 14


Longest Increasing Subsequence
 S = { 14, 1, 17, 2, 16, 17, 3, 15, 4, 1, 5,
18, 13, 6, 7, 19, 8, 12, 1, 9, 10, 8 }
 Increasing subsequence
 S’ = { 14, 1, 17, 2, 16, 17, 3, 15, 4, 1, 5,
18, 13, 6, 7, 19, 8, 12, 1, 9, 10, 8 }
 Longest increasing subsequence
 S = { 14, 1, 17, 2, 16, 17, 3, 15, 4, 1, 5,
18, 13, 6, 7, 19, 8, 12, 1, 9, 10, 8 }

Design and Analysis of Algorithms 15


Longest Increasing Subsequence

 Bài toán con: Tìm dãy con tăng dài nhất


kết thúc tại phần tử thứ k
 Thuật toán
 Nếu tất cả các phần tử trước k đều lớn hơn
hoặc bằng S[k], trả về dãy con chỉ chứa S[k]
 Nếu có t phần tử đứng trước k nhỏ hơn S[k],
gọi W là dãy dài nhất trong các dãy con tăng
kết thúc tại các phần tử này. Trả về W S[k]

Design and Analysis of Algorithms 16


Longest Increasing Subsequence
s1 14 {14}
S = { 14, 1, 17, 2, 16, s2 1 {1}
s3 17 {14|1, 17}
17, 3, 15, 4, 1, 5, 18, s4 2 {1, 2}
s5 16
13, 6, 7, 19, 8, 12, 1, s6 17
9, 10, 8 } s7
s8
3
15
s9 4
s10 1
s11 5
Use all previously s12 18
constructed Si to s13
s14
13
6
construct Sj (j>i) s15
s16
7
19
s17 8
s18 12
s19 1
s20 9
s21 10
s22 8
Design and Analysis of Algorithms 17
Longest Increasing Subsequence
s1 14 {14}
S = { 14, 1, 17, 2, 16, s2 1 {1}
s3 17 {14|1, 17}
17, 3, 15, 4, 1, 5, 18, s4 2 {1, 2}
s5 16 {1, 2, 16}
13, 6, 7, 19, 8, 12, 1, s6 17 {1, 2, 16, 17}
9, 10, 8 } s7
s8
3
15
{1, 2, 3}
{1, 2, 3, 15}
s9 4 {1, 2, 3, 4}
s10 1 {1}
s11 5 {1, 2, 3, 4, 5}
Use all previously s12 18 {1, 2, 3, 4, 5, 18}
constructed Si to s13
s14
13
6
{1, 2, 3, 4, 5, 13}
{1, 2, 3, 4, 5, 6}
construct Sj (j>i) s15
s16
7
19
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7, 19}
s17 8 {1, 2, 3, 4, 5, 6, 7, 8}
s18 12 {1, 2, 3, 4, 5, 6, 7, 8, 12}
s19 1 {1}
s20 9 {1, 2, 3, 4, 5, 6, 7, 8, 9}
s21 10 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
s22 8 {1, 2, 3, 4, 5, 6, 7, 8}
Design and Analysis of Algorithms 18
Knapsack
There are N items, item i has weight wi and profit ti
A knapsack has the weight limit of M kg
Find a subset of items such that their total weight
does not exceed the weight limit and the profit of
the knapsack is maximized

Example
N = 5, M = 10
i A B C D E
wi 1 3 5 7 9
ti $100 $200 $301 $400 $500

Design and Analysis of Algorithms 19


Knapsack
for all cell[x, y]
if x = 0 & y = 0 then
cell[x, y] = 0kg $0 {}
Item Weight Profit
else
A 1kg $100
B 3kg $200 Let m be the additional item at column y
C 5kg $301 Let w be the weight of item m
D 7kg $400 if w > maximum weight of the row
E 9kg $500 cell[x, y] = cell[x, y-1]
else
cell[x, y] = max {cell[x, y-1], (cell[x-w, y-1] U {m})}

empty A A/B A/B/C A/B/C/D A/B/C/D/E


kg ≤ 0 0kg $0 {} 0kg $0 {} 0kg $0 {} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 1 0kg $0 {} 1kg $100 {A} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 2 0kg $0 {} 1kg $100 {A} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 3 0kg $0 {} 1kg $100 {A} 3kg $200 {B} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 4 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 5 0kg $0 {} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 6 0kg $0 {} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 7 0kg $0 {} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 8 0kg $0 {} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 9 0kg $0 {} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {} ?kg $? {}
kg ≤ 10 0kg $0 {} 1kg $100 {A} ?kg $? {} ?kg $? {} ?kg $? {} ?kg $? {}

Design and Analysis of Algorithms 20


Knapsack
for all cell[x, y]
if x = 0 & y = 0 then
cell[x, y] = 0kg $0 {}
Item Weight Profit
else
A 1kg $100
B 3kg $200 Let m be the additional item at column y
C 5kg $301 Let w be the weight of item m
D 7kg $400 if w > maximum weight of the row
E 9kg $500 cell[x, y] = cell[x, y-1]
else
cell[x, y] = max {cell[x, y-1], (cell[x-w, y-1] U {m})}

empty A A/B A/B/C A/B/C/D A/B/C/D/E


kg ≤ 0 0kg $0 {} 0kg $0 {} 0kg $0 {} 0kg $0 {} 0kg $0 {} 0kg $0 {}
kg ≤ 1 0kg $0 {} 1kg $100 {A} 1kg $100 {A} 1kg $100 {A} 1kg $100 {A} 1kg $100 {A}
kg ≤ 2 0kg $0 {} 1kg $100 {A} 1kg $100 {A} 1kg $100 {A} 1kg $100 {A} 1kg $100 {A}
kg ≤ 3 0kg $0 {} 1kg $100 {A} 3kg $200 {B} 3kg $200 {B} 3kg $200 {B} 3kg $200 {B}
kg ≤ 4 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 4kg $300 {A, B} 4kg $300 {A, B} 4kg $300 {A, B}
kg ≤ 5 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 5kg $301 {C} 5kg $301 {C} 5kg $301 {C}
kg ≤ 6 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 6kg $401 {A,C} 6kg $401 {A,C} 6kg $401 {A,C}
kg ≤ 7 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 6kg $401 {A,C} 6kg $401 {A,C} 6kg $401 {A,C}
kg ≤ 8 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 8kg $501 {B,C} 8kg $501 {B,C} 8kg $501 {B,C}
kg ≤ 9 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 9kg $601 {A, B, C} 9kg $601 {A, B, C} 9kg $601 {A, B, C}
kg ≤ 10 0kg $0 {} 1kg $100 {A} 4kg $300 {A, B} 9kg $601 {A, B, C} 9kg $601 {A, B, C} 9kg $601 {A, B, C}

Design and Analysis of Algorithms 21


Longest Common Subsequence

Given 2 sequence and


Find the longest common subsequence

: A B C B D A B
: B D C A B A

Common subsequence: AB / BCDB / BCBA


LCS( , ) = BCBA

Design and Analysis of Algorithms 22


Longest Common Subsequence

 Check if all subsequences of is


the subsequence of
 Analysis
 Check subsequence for each
subsequence
 There are subsequence of .
 Complexity

Design and Analysis of Algorithms 23

You might also like