week06_DynamicProgramming
week06_DynamicProgramming
STRUCTURES AND
ALGORITHMS
INTRODUCTION
3 4
ALGORITHM DIAGRAM ALGORITHM DIAGRAM
7 8
CHARACTERISTICS EXAMPLE: SUBSEQUENCE WITH THE LARGEST SUM
• Compare to the divide and conquer algorithm, the dynamic programming algorithm also • Problem: Given a sequence of 𝑛 integers (𝑎 , 𝑎 , … , 𝑎 ), find a subsequence consisting of
has 3 steps: Divide, Solve subproblems and Combine. However, in divide and conquer, the consecutive elements of the sequence such that the sum of the selected elements is maximized.
• There are 2 approaches: Top-Down and Bottom-Up, in which Top-Down is natural and
Subsequence with sum of -12 Subsequence with sum of 0
easy to understand and easy to install.
• Optimal solution: (subsequence with the largest sum of 8): 7, -3, 0, -1, 5
• Memory design greatly affects the speed of the algorithm
• The exhaustive algorithm has complexity 𝑂(𝑛2), can we do better with the divide and conquer
• Memory is still used to trace and explicitly find the optimal solution algorithm?
9 10
EXAMPLE: SUBSEQUENCE WITH THE LARGEST SUM EXAMPLE: SUBSEQUENCE WITH THE LARGEST SUM
• Problem: Given a sequence of 𝑛 integers (𝑎 , 𝑎 , … , 𝑎 ), find a subsequence consisting of • Problem: Given a sequence of 𝑛 integers (𝑎 , 𝑎 , … , 𝑎 ), find a subsequence consisting of
consecutive elements of the sequence such that the sum of the selected elements is maximized. consecutive elements of the sequence such that the sum of the selected elements is maximized.
• Determine the subproblems: 𝑃𝑖 is the problem of finding a sub-segment consisting of • Determine the subproblems: 𝑃𝑖 is the problem of finding a sub-segment consisting of
consecutive elements with the largest sum whose last element is 𝑎𝑖, for all 𝑖 = 1,…, 𝑛. consecutive elements with the largest sum whose last element is 𝑎𝑖, for all 𝑖 = 1,…, 𝑛.
Solution of 𝑷𝟐 S𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒐𝒇 𝑷𝟔 • Dynamic programming formula (formula combining solutions to sub-problems to obtain
solution to parent problem): Let 𝑆𝑖 be the sum of elements of the solutions of 𝑃𝑖 , 𝑖 =
1, … , 𝑛.
-16 7 -3 0 -1 5 -4
We have: 𝑆 =𝑎 ,
𝑠 + 𝑎 𝑖𝑓 𝑠 >0
𝑆 =
S𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒐𝒇 𝑷𝟕 𝑎 𝑖𝑓 𝑠 ≤0
S𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒐𝒇 𝑷𝟒
11 12
EXAMPLE: SUBSEQUENCE WITH THE LARGEST SUM EXAMPLE: SUBSEQUENCE WITH THE LARGEST SUM
We have: 𝑆 = 𝑎 ,
𝑠 + 𝑎 𝑖𝑓 𝑠 >0
𝑺𝟏 = −𝟏𝟔, 𝑺𝟐 = 𝒂𝟐 = 𝟕, 𝑺𝟑 = 𝑺𝟐 + 𝒂𝟑 = 𝟒, 𝑺𝟒 = 𝑺𝟑 + 𝟎 = 𝟒, 𝑆 =
𝑎 𝑖𝑓 𝑠 ≤0
• Complexity: O(n)
13 14
EXAMPLE: SUBSEQUENCE WITH THE LARGEST SUM EXERCISE: LONGEST INCREASING SUBSEQUENCE
• The sum of the elements of the sub-segment including consecutive elements of • Problem: Given a list of integers 𝐴 = (𝑎1, 𝑎2, … , 𝑎 ) satisfy the
the sequence with the largest sum of selected elements is:
condition that the elements are pairwise different (𝑎 ≠ 𝑎 , ∀𝑖 ≠ 𝑗). A
𝒎𝒂𝒙(𝑺𝟏 , 𝑺𝟐 , … , 𝑺𝒏 )
subsequence of 𝐴 is a sequence obtained by deleting some elements
Example:
in 𝐴. A subsequence 𝐵 = (𝑏 , … , 𝑏 ) is called tight increase when 𝑏 <
𝑏 , ∀𝑖 ∈ 1, … , 𝑘 − 1 . Find the length of the strongly increasing
𝑺𝟏 = −𝟏𝟔, 𝑺𝟐 = 𝒂𝟐 = 𝟕, 𝑺𝟑 = 𝑺𝟐 + 𝒂𝟑 = 𝟒, 𝑺𝟒 = 𝑺𝟑 + 𝟎 = 𝟒,
𝑺𝟓 = 𝑺𝟒 + −𝟏 = 𝟑, 𝑺𝟔 = 𝑺𝟓 + 𝟓 = 𝟖, 𝑺𝟕 = 𝑺𝟔 + −𝟒 = 𝟒 subsequence of 𝐴 that has the greatest length.
15 16
EXAMPLE: LONGEST COMMON SUBSEQUENCE EXAMPLE: LONGEST COMMON SUBSEQUENCE
• Problem: Given 2 lists of characters X = (𝑥 , 𝑥 , … , 𝑥 ) and 𝑌 = • Problem: Given 2 lists of characters X = (𝑥 , 𝑥 , … , 𝑥 ) and 𝑌 = (𝑦 , … , 𝑦 } . A
(𝑦 , … , 𝑦 }. A subsequence of A is a sequence obtained by deleting subsequence of A is a sequence obtained by deleting some elements from A. Find
the length of the longest common subsequence of X and Y.
some elements in A. Find the length of the longest common
subsequence of X and Y. • Determine the subproblem: Let S(i,j) be the length of the longest common
subsequence of the two sequences, subsequences of X are Xi = (x , … , x )
• Example: with i ∈ {1, … , n} and subsequences of Y are Y = y , … , y with j ∈
• 𝑋 = "𝑎𝑏𝑐𝑏“ and 𝑌 = "𝑏𝑑𝑐𝑎𝑏“ {1, … , m}.
• The longest common subsequence is "𝑏𝑐𝑏“ with length of 3 • Base problems (smallest subproblems):
• Comment: The exhaustive algorithm, comparing all subsequences of X 𝑆 𝑖, 0 = 0, ∀𝑖 ∈ {1, … , 𝑛}
and Y will have complexity O(2^n×2^m×max(m,n)). Can we solve this 𝑆 0, 𝑗 = 0, ∀𝑗 ∈ {1, … , 𝑚}
problem faster with a dynamic programming algorithm?
17 18
• Determine the subproblem: Let S(i,j) be the length of the longest common • Dynamic programming formula
subsequence of the two sequences, subsequences of X are Xi = (x , … , x )
with i ∈ {1, … , n} and subsequences of Y are Y = y , … , y with j ∈ 𝑿 3 7 2 5 1 4 9
{1, … , m}.
𝑆 𝑖 − 1, 𝑗 − 1 𝑖𝑓 𝑥 = 𝑦
𝑆 𝑖, 0 = 0, ∀𝑖 ∈ {1, … , 𝑛} 𝒀 4 3 2 3 6 1 5 4 9 7
𝑆(𝑖, 𝑗) = 𝑚𝑎𝑥 𝑆(𝑖 − 1, 𝑗)
• Base problems (smallest subproblems): 𝑆 0, 𝑗 = 0, ∀𝑗 ∈ {1, … , 𝑚} 𝑆(𝑖, 𝑗 − 1) 1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 1 1 1 1 1 1
2 0 1 1 1 1 1 1 1 1 2
Dynamic programming formula: 3 0 1 2 2 2 2 2 2 2 2
𝑆 𝑖 − 1, 𝑗 − 1 𝑖𝑓 𝑥 = 𝑦
4 0 1 2 2 2 2 3 3 3 3
𝑆(𝑖, 𝑗) = 𝑚𝑎𝑥 𝑆(𝑖 − 1, 𝑗)
5 0 1 2 2 2 3 3 3 3 3
𝑆(𝑖, 𝑗 − 1) 6 1 1 2 2 2 3 3 4 4 4
7 1 1 2 2 2 3 3 4 5 5
19 20
EXAMPLE: LONGEST COMMON SUBSEQUENCE EXERCISE: LONGEST INCREASING SUBSEQUENCE
• Determine the subproblem: Let S(i,j) be the length of the longest common
subsequence of the two sequences, subsequences of X are Xi = (x , … , x ) • Problem: Given a sequence 𝐴 = (𝑎1, 𝑎2, . . . , 𝑎𝑛). A subsequence of the
with i ∈ {1, … , n} and subsequences of Y are Y = y , … , y with j ∈ sequence 𝐴 is a sequence obtained by deleting some elements from
{1, … , m}.
𝑆 𝑖, 0 = 0, ∀𝑖 ∈ {1, … , 𝑛} 𝐴. Find the length of the subsequence of 𝐴 that is a progression with a
• Base problems (smallest subproblems):
𝑆 0, 𝑗 = 0, ∀𝑗 ∈ {1, … , 𝑚} step of 1 and has the largest length.
𝑆 𝑖 − 1, 𝑗 − 1 𝑖𝑓 𝑥 = 𝑦
• Dynamic programming formula: 𝑆(𝑖 − 1, 𝑗) • Homework: Submit code to the online system
𝑆(𝑖, 𝑗) = 𝑚𝑎𝑥
𝑆(𝑖, 𝑗 − 1)
• Complexity: 𝑂(𝑛 × 𝑚)
21 22
THANK YOU !
23