0% found this document useful (0 votes)
3 views6 pages

week06_DynamicProgramming

Dynamic programming, developed by Bellman during World War II, is an optimization technique that breaks problems into smaller, overlapping subproblems to solve them efficiently. It shares similarities with backtracking and divide-and-conquer algorithms but focuses on storing solutions to avoid redundant calculations. The document provides examples, including finding the subsequence with the largest sum and the longest common subsequence, illustrating the application of dynamic programming principles.
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)
3 views6 pages

week06_DynamicProgramming

Dynamic programming, developed by Bellman during World War II, is an optimization technique that breaks problems into smaller, overlapping subproblems to solve them efficiently. It shares similarities with backtracking and divide-and-conquer algorithms but focuses on storing solutions to avoid redundant calculations. The document provides examples, including finding the subsequence with the largest sum and the longest common subsequence, illustrating the application of dynamic programming principles.
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/ 6

DATA

STRUCTURES AND
ALGORITHMS

INTRODUCTION

 Dynamic programming was invented by Bellman during


World War II. The first name of this algorithm was multi-
stage decision process (decision making through many
DATA STRUCTURES AND stages).

ALGORITHMS  The dynamic programming algorithm is a powerful


technique for solving optimization problems by dividing
Week 6: Dynamic programming them into smaller problems and solving the sub-problems
only once.

 Dynamic programming algorithms have many similarities


with backtracking and divide and conquer algorithms.

3 4
ALGORITHM DIAGRAM ALGORITHM DIAGRAM

Memory, mapping of programs and


 DIVIDE the starting problem into subproblems that are not solutions
necessarily independent of each other
The smallest subproblem, corresponding to
 SOLVE sub-problems from small to large, the solutions are the basic step of the recursive algorithm
stored in memory (to ensure each problem is only solved
correctly once) Always check the memory to see if the sub-
 The smallest subproblem must be solved in a direct, problem has been solved. Once it have been
solved it, don't solve it again. If it haven’t been
simple way solved it, try solving it.
 COMBINE the solution of the larger problem from the
existing solutions of the smaller sub-problems (need to use Solve a subproblem
a recurrence formula)
Solve all subproblems and combine the
 The number of subproblems needed is bounded by a solutions found to form the solution to the
polynomial function of the input data size larger problem

Save the subproblem results in memory


6
5 6

ALGORITHM DIAGRAM AGORITHM DIAGRAM

Memory, mapping of programs and


solutions

The smallest subproblem, corresponding to


the basic step of the recursive algorithm
Always check the memory to see if the sub-
problem has been solved. Once it have
been solved it, don't solve it again. If it
haven’t been solved it, try solving it.
Solve a subproblem

Solve all subproblems and combine the


solutions found to form the solution to
the larger problem

Save the subproblem results in memory

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.

subproblems are independent; In dynamic programming, subproblems overlap or overlap.


• Example: Given a sequence of 7 intergers:

• The biggest difficulty in proposing dynamic programming algorithms is the Recursive


-16 7 -3 0 -1 5 -4
Formula (other names are Dynamic Programming Formula, Recursive Formula)

• 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

• Dynamic programming formula (formula combining solutions to sub-problems to


obtain solution to parent problem): Let 𝑆𝑖 be the sum of elements of the solutions
• Example: of 𝑃𝑖 ,  𝑖 = 1, … , 𝑛.

We have: 𝑆 = 𝑎 ,
𝑠 + 𝑎 𝑖𝑓 𝑠 >0
𝑺𝟏 = −𝟏𝟔, 𝑺𝟐 = 𝒂𝟐 = 𝟕, 𝑺𝟑 = 𝑺𝟐 + 𝒂𝟑 = 𝟒, 𝑺𝟒 = 𝑺𝟑 + 𝟎 = 𝟒, 𝑆 =
𝑎 𝑖𝑓 𝑠 ≤0

𝑺𝟓 = 𝑺𝟒 + −𝟏 = 𝟑, 𝑺𝟔 = 𝑺𝟓 + 𝟓 = 𝟖, 𝑺𝟕 = 𝑺𝟔 + −𝟒 = 𝟒 • Solution: The sum of the elements of the sub-segment including consecutive


elements of the sequence with the largest sum of selected elements is:
𝒎𝒂𝒙(𝑺𝟏 , 𝑺𝟐 , … , 𝑺𝒏 )

• 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.

• Homework: Submit code to the online system


Solution: 𝒎𝒂𝒙 𝑺𝟏 , 𝑺𝟐 , … , 𝑺𝟕 = 𝟖

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

EXAMPLE: LONGEST COMMON SUBSEQUENCE EXAMPLE: LONGEST COMMON SUBSEQUENCE

• 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

You might also like