Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))

leoyang0406 4 views 67 slides Mar 11, 2025
Slide 1
Slide 1 of 67
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67

About This Presentation

Course slides from Graduate class


Slide Content

CS6515: Lecture 1
Dynamic Programming
(Fibonacci, LIS, LCS)
Dr. GerandyBrito
Dr. Mengmeng (Helen) Liu
Email: [email protected]
Phone: (+86)-15117982925
Office: Room 418

•Data analysis
•Clustering
•Social Network Analysis
•Centrality Measures, Recommendation, Information Diffusion
•Databases
•Queries
•Circuit Design
•Every electronic device you use
•Scheduling & Resource Allocation
•Timetabling (your classes and exams)
•Transportation
•Routing, network design
•Computational biology
•Genome Sequencing, Protein Folding
•Computational Physics & Chemistry
Wide Range of Applications of Algorithm

•Dynamic Programming
•Divide and Conquer
•Randomized Algorithms
•Graph Algorithms
•Max-Flow
•Linear Programming
•NP-Completeness
Algorithms Covered in CS6500

Algorithms
Proof of Correctness: (Review)
On every valid input, the algorithm produces the output
that satisfies the required input/output relationshi
Definition:
a sequence of computationalstepsthat transformthe
input into a desired output.
Analysis of Time and Space: (Review)
Given the ‘size’ of the input, how many computational
steps does the algorithm take?

•Model of computation: Random-access Machine (RAM)
•Instructions are executed one after the other (non concurrency)
•Each “simple” operation takes 1 step: (+, -, =, if, call, memory
access)
•Loops and subroutine calls are not simple operations. They depend
upon the size of the data and the contents of a subroutine.
•“Sort” is not a single step operation
•Input size: depends on the problem being studied
•E.g. Number of items in the input
•E.g. number of nodes and number of edges in a graph
•Running time
•Number of steps taken by the algorithm, given input size
Running Time

•Worst-Case: the maximumnumber of steps taken on
any instance of size n
•Best-Case: the minimumnumber of steps taken on any
instance of size n.
•Average-Case: the averagenumber of steps taken on
any instance of size n.
Running Time
•Need an
understanding of the
distributionof
possible inputs

•Best, worst, and average are difficult to deal with
precisely because the details are very complicated
Exact Analysis is Hard
•It easier to talk about upperand lowerboundsof the
function
•Asymptotic notation (??????,??????,??????) are the way we practically
deal with analysis of time complexity.

•Proofs by Counterexample
Review of Proofs
•Proofs by Contradiction
•E.g., ‘All primes are odd’ is False
→Exhibit the even prime 2
•Q: Can you give other examples ?
•Assume the opposite of hypothesis, show thesis cannot follow
•e.g., There are infinite number of primes
→Assume there are a finite number of primes: �
1,�
2,...,�
�
→Consider the number p=�
1∗�
2∗…∗�
�+1
→p is composite: p is not divisible by any of our primes, hence it
must be prime .
→p is larger than any of the primes in our finite set →contradiction

Review of Proofs
•InductiveProofs
•e.g., Proof: for any positive n, show that: 1+2+3+…+�=
��+1
2
•Step 1:�=1→1=1∗2/2
1)A Statementinvolving a natural number n, andwe will show that
it holds for all values of n
2)Basis: show it holds for small inputs, usually for n=0 or n=1
3)Induction Hypothesis: assume it holds for n (or all values <= n)
4)Inductive Step: show it holds for n+1
•Step 2: Assume it holds for n
•Step 3: show it holds for n+1:
1+2+3+…+&#3627408475;+&#3627408475;+1=
&#3627408475;&#3627408475;+1
2
+&#3627408475;+1=
&#3627408475;+1&#3627408475;+2
2

Review of Proofs
•StructuralInduction: proofstatements about recursively
defined structures, e.g. lists and trees.
•e.g.Binary tree is defined as a node with no children (a leaf), or as a node with two
children, both of which are binary trees.
•The root of the tree has two binary tree children T1 and T2.
Proof: Any binary tree with &#3627408475;leaves, has &#3627408475;−1non-leaf nodes.
•Step 1: &#3627408475;=1, a tree consisting of a leaf has &#3627408475;−1=0non-leaf nodes.
•Step 2: Assume it holds for all trees with ≤&#3627408475;leaves.
•Step 3: Consider a tree T with (&#3627408475;+1) leaves.
•Both T1 and T2 consist of at least one node, hence both have leaves<&#3627408475;−1
•leaf(T) = leaf(T1) + leaf(T2)
•Inter(T) = inter(T1) + inter(T2) + 1
= (leaf(T1) –1) + (leaf(T2) –1) + 1 = leaf(T1) + leaf(T2) –1 = leaf(T) -1

Review of Proofs
•e.g.All horses are the same color.
•Label horses: 1,2,3,…,&#3627408475;,&#3627408475;+1
•Step 1: in any set of &#3627408475;horses, all horses have the same color
•Step 2: &#3627408475;=1, clearly holds.
•Step 3: assume it holds for any set with ≤&#3627408475;horses
•Step 4: Consider a set of (&#3627408475;+1) horses.
•Apply hypothesis since size is ≤&#3627408475;, each set must be of one color
•Consider sets ,1,2,3,…,nand2,3,…,n+1
•The two sets intersect: have at least one horse in common
•Hence, all (&#3627408475;+1) horses are the same color.
Q: Is my proof right ?
→“The two sets intersect” are not true for n = 2

Time Complexity
•The time complexity of an algorithm associates a
number T(n),
•the worst-case time the algorithm takes, with each
problem size n
•Mathematically, T: N → R
•T is a function mapping non-negativeintegers
(problem sizes) to realnumbers (number of steps)
•A key question is “Scaling up” of the algorithm
•What happens to my runtime if I have twice as many items,
or twice as large graph?
•(E.g. today: &#3627408464;&#3627408475;
2
, next year: &#3627408464;(2&#3627408475;)
2
=4&#3627408464;&#3627408475;
2
: 4x longer)
•Big-Oh notation answers this kind of questions.

Big O (DPV Chapter 0)
Upper bounds: T(n) is O(f(n)) if there exist constants c > 0 and
&#3627408475;
00 such that for all n &#3627408475;
0we have T(n) c · f(n)
Lower bounds: T(n) is (f(n)) if there exist constants c > 0 and
&#3627408475;
00 such that for all n &#3627408475;
0we have T(n) c · f(n).
Tight bounds: T(n) is (f(n)) if T(n) is both O(f(n)) and (f(n)).

Big O (DPV Chapter 0)
e.g., &#3627408455;&#3627408475;=32&#3627408475;
2
+17&#3627408475;+32
→T(n) is O(&#3627408475;
2
), O(&#3627408475;
3
), (&#3627408475;
2
), (n), and (&#3627408475;
2
)
→T(n) is not O(n), (&#3627408475;
3
), (n), or (&#3627408475;
3
).

Big O (DPV Chapter 0)
•Not Transitive:
&#3627408455;
1&#3627408475;=5&#3627408475;
3
,&#3627408455;
2&#3627408475;=3&#3627408475;
2
&#3627408455;
1&#3627408475;=&#3627408450;&#3627408475;
3
,&#3627408455;
2&#3627408475;=&#3627408450;&#3627408475;
3
,but&#3627408455;
1&#3627408475;≠&#3627408455;
2&#3627408475;
Better notation: &#3627408455;&#3627408475;∈&#3627408450;(&#3627408467;(&#3627408475;))
•T(n) is in the setof functions bounded from above by f(n)
•Transitivity:
•If f O(g) and g O(h) then f O(h).
•If f = (g) and g = (h) then f (h).
•If f (g) and g (h) then f (h)
•Additivity:
If f O(h) and g O(h) then f + g O(h).
If f (h) and g (h) then f + g (h).
If f (h) and g O(h) then f + g (h)

•Some Common Functions
•Polynomials:&#3627408462;
0+&#3627408462;
1&#3627408475;+…+&#3627408462;
??????&#3627408475;
??????
&#3627408470;&#3627408480;(&#3627408475;
??????
)if &#3627408462;
??????> 0
•Logarithms: the base does not matter
•&#3627408450;log
&#3627408462;&#3627408475;=&#3627408450;(log
&#3627408463;&#3627408475;)for any constant &#3627408462;,&#3627408463;>0
•log
&#3627408463;&#3627408475;=log
&#3627408462;&#3627408475;/log
&#3627408462;&#3627408463;
•Polynomial time. Running time is O(&#3627408475;
??????
) for some constant d
independent of the input size n.
•Exponential time. Running time is O(&#3627408479;
&#3627408475;
) for some constant r
•Logarithms grow slower than every polynomial: &#3627408473;&#3627408476;&#3627408468;&#3627408475;∈&#3627408450;(&#3627408475;
??????
)
•Exponentials grow faster than every
polynomial(r > 1, d > 0)

Small O Notation
•Let &#3627408467;and &#3627408468;be two functions from N to R+ (the set of positive real
numbers).
•If &#3627408473;&#3627408470;&#3627408474;
n→
(f(n)/g(n))=0,then&#3627408467;&#3627408475;∈&#3627408528;(&#3627408468;(&#3627408475;))
Upper bounds: &#3627408507;(&#3627408527;)is ??????(??????(&#3627408527;))if there existconstants &#3627408464;>0and
&#3627408475;
0≥0such that for all&#3627408475;≥&#3627408475;
0we have &#3627408507;&#3627408527;≤??????·??????(&#3627408527;).
•If &#3627408467;&#3627408475;∈&#3627408528;(&#3627408468;(&#3627408475;))then &#3627408467;&#3627408475;∈??????(&#3627408468;(&#3627408475;)), but the reverse is not always true
•Inequality of big-Oneeds to hold for somepositive constant c,
•Inequality of small-omust hold for everypositive constant c
Small-o: For anypositive real number &#3627408464;, a number &#3627408475;
0exists such that
for every &#3627408475;≥&#3627408475;
0,&#3627408467;(&#3627408475;)<&#3627408464;&#3627408468;(&#3627408475;).

Some Examples of Running Times
•??????(log&#3627408527;): Binary search on sorted list
•??????(&#3627408527;): Find max element in a list
•??????(&#3627408527;log&#3627408527;): Sorta set of elements
•??????(&#3627408423;
&#3627409360;
): Find the pair of points that is closest to each other
•??????(&#3627408423;
&#3627409361;
): Given &#3627408475;sets &#3627408454;
1,…,&#3627408454;
&#3627408475;, each of which is a subsetof
1,2,…,&#3627408475;, is there some pair of these which are disjoint?
•??????(&#3627408423;
&#3627408420;
): Given a graph of &#3627408475;nodes, are there &#3627408472;nodes such that no
two are joined by an edge?
•??????(&#3627409360;
&#3627408423;
): Enumerate all subsets of a set of &#3627408475;elements
•??????(&#3627408527;!): Given a set of cities and distances, find a route that visits
each city exactly once and returns to the start city?
(naïve approach)

Why Algorithm Design Matters?

Big O : Practical Problem

Big O : Practical Problem

Big O : Practical Problem

Big O : Practical Problem
Exponentials grow faster than every polynomial(&#3627408479;>1,&#3627408465;>0)

Big O : Practical Problem
Inductive Proofs
1)A Statementinvolving a natural number &#3627408475;, andwe will show that
it holds for all values of &#3627408475;
2)Basis: show it holds for small inputs, usually for &#3627408475;=0or &#3627408475;=1
3)Induction Hypothesis: assume it holds for &#3627408475;(or all values ≤&#3627408475;)
4)Inductive Step: show it holds for (&#3627408475;+1)

Dynamic Programming

•Dynamic programming
•Break up a probleminto a series of overlapping sub-problems
•build upsolutions to larger and larger sub-problems,
(reusingsolutions of encountered subproblems as much as
possible)
•Dynamic programming algorithms
•systematically search allpossibilities (thus guaranteeing
correctness)
•storing results to avoid recomputing (thus providing efficiency)
•Some famous dynamic programming algorithms.
•Comparing two files -Unix diff
•Hidden Markov models –Viterbi
•Genetic sequence alignment -Smith-Waterman
•Shortest paths -Bellman-Ford
•Parsing context free grammars -Cocke-Kasami-Younger
Dynamic Programming

•Top-down DP =Memoization
•Design a recursivealgorithm
•Store result for each subproblem when you first compute it
•Check for existing result for a subproblem, before doing any
extra work
Dynamic Programming
•Bottom-upDP =IterativeDP
•Determine dependency between a problem and its subproblems
•Determine an order in which to compute subproblems so that you
always have what you need already available
•Fill in the table of results in the determined order (using FORloops)

Dynamic Programming: Outline
•Fibonacci Numbers
•Longest Increasing Subsequence (LIS)
•Longest Common Subsequence (LCS)
•Knapsack
•Chain Matrix Multiply
•Shortest Path Algorithms

Fibonacci Numbers
•How to solveit ?
•RecursiveAlgorithm
•DynamicProgramming
•Goal: Given number &#3627408475;generate the &#3627408475;-thFibonacci numbers
What is Fibonaccinumbers?
0,1,1,2,3,5,8,13,21,34,….
??????
0=0,??????
1=1, ??????
&#3627408475;=??????
&#3627408475;−1+??????
&#3627408475;−2for&#3627408475;>1
•Define Problem
•Input: integer n≥0
•Output: &#3627408475;-thFibonacci number

Fibonacci Numbers: RecursiveAlgorithm
Fib1(n):
Input: integer &#3627408475;≥0
Output: ??????
&#3627408475;
If n = 0, return 0
If n = 1, return 1
return Fib1(n-1) + Fib1(n-2)
Shortages:
repeat computing
(recursive from up to down)

Fibonacci Numbers: DP Pseudocode
Fib2(n):
F[0] = 0
F[1] = 1
For i= 2 to n do
F[i] = F[i-1] + F[i-2]
End for
Return F[n]
•No repeat computing, time complexity: &#3627408450;(&#3627408475;)
•Fasterthan recursive
•Simpler to analyze time complexity
Dynamic Programming (bottom-up)
Efficient
Computing

DP Solution
•1
st
: Define Subproblem In Words
•??????[&#3627408470;]=&#3627408470;-thFibonacci Number
•2
nd
: State Recursive Relations
•??????&#3627408470;=??????&#3627408470;−1+??????&#3627408470;−2for∀2≤&#3627408470;≤&#3627408475;
•Define Initial State
•??????[0]=0,??????[1]=1

Longest Increasing Subsequences (LIS)
•LIS problem:
•Find the lengthof the longest subsequence of a given
sequence s.t.all elements of the subsequenceare
sorted in increasing order.
•Subsequences: set of elements in order (can skip,
NO need to be consecutive)
E.g, Given sequence {5,7,4,−3,9,1,10,4,5,8,9,3}
•Subsequences :
−3,9,10 5,7,9,10 {4,4,5,8,9}
•LIS={−3,1,4,5,8,9}→len=&#3627409364;

LIS: DP Definition
•1
st
: Define Subproblem In Words
•Let ??????&#3627408470;=length of LIS on &#3627408462;
1,&#3627408462;
2,&#3627408462;
3,…..,&#3627408462;
&#3627408470;andincludes ??????
&#3627408522;
•2
nd
: State Recursive Relations
•Express ??????&#3627408470;in terms of ??????1,??????2,…,??????&#3627408470;−1
•??????&#3627408470;=1+max
1≤&#3627408471;≤&#3627408470;−1,&&#3627408462;
&#3627408471;<&#3627408462;
&#3627408470;
{??????[&#3627408471;]}for∀2≤&#3627408470;≤&#3627408475;
•Initial State
•??????1=1
•Input: &#3627408475;numbers &#3627408462;
1,&#3627408462;
2,&#3627408462;
3,…..,&#3627408462;
&#3627408475;
•Goal: find length of LIS in a list of numbers

LIS: DP Algorithm
LIS(&#3627408462;
1,&#3627408462;
2,&#3627408462;
3,…..,&#3627408462;
&#3627408475;):
L[1] = 1
for i= 2 to n do
for j = 1 to i-1do
if &#3627408462;
&#3627408471;< &#3627408462;
&#3627408470;then L[i] = 1 + L[j]
end if
end for
end for
#### find the maximum length of subsequences
max = 1
for i= 2 to n do
if L[i] > L[max] then max = i
end if
end for
return L[max]

Q: Find the length of LIS for {5, 7, 4, -3, 9, 1, 10, 4, 5, 8, 9, 3}
→len=6and LIS is {10, 22, 33, 50, 60, 80}.
index12345 6 7 8 9 10 11 12
value574-39 1 104 5 8 9 3
LIS_Len12113 2 4 3 4 5 6 3
LIS 55,
7
4-35, 7,
9
-3, 15, 7,
9, 10
-3, 1, 4-3, 1, 4,
5
-3, 1,
4, 5, 8
-3, 1, 4,
5, 8, 9
-3, 1,3
??????&#3627408470;=1+max
1≤&#3627408471;<&#3627408470;,&&#3627408462;
&#3627408471;<&#3627408462;
&#3627408470;
??????&#3627408471;for∀2≤&#3627408470;≤&#3627408475;

Q: Find the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80}
→len= 6 and LIS is {10, 22, 33, 50, 60, 80}.
index12 34 5 6 7 8 9
value1022 933 21 50 41 60 80
LIS_Len12 13 2 4 4 5 6
LIS 1010, 22910, 22,
33
10, 21
or
9, 21
10, 22,
33, 50
10, 22,
33, 41
10, 22, 33, 41, 60
or
10, 22, 33, 50, 60
10, 22, 33,
41, 60, 80
or
10, 22, 33,
50, 60, 80
Ref: https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/
Other examples: https://afteracademy.com/blog/longest-increasing-subsequence
??????&#3627408470;=1+max
1≤&#3627408471;<&#3627408470;,&&#3627408462;
&#3627408471;<&#3627408462;
&#3627408470;
??????&#3627408471;for∀2≤&#3627408470;≤&#3627408475;

Longest Common Subsequence (LCS)
•Input: 2 strings X=x
1,&#3627408485;
2,…,&#3627408537;
&#3627408527;,
??????=&#3627408486;
1,&#3627408486;
2,…,&#3627408538;
&#3627408527;
•Goal: find the lengthof longest strings which
is a subsequenceof both XandY.
e.g, GivenX=BCDBCDA,Y=ABECBAB,
what is their longest common subsequence?
→LCS =BCBA, len =4

•Can be used to:
•compare files, e.g., Unix diff command (diff helps you to find differences
between files and directories.)
•Different ways of comparing files in Unix:
https://www.softwaretestinghelp.com/compare-two-files-unix/
•Biological applications:
•compare DNA of 2 or more different organisms to check similarity.
•minimize insert/delete mutations that can transform one DNA string
into the other
&#3627408454;
1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA(len= 29)
&#3627408454;
2=GTCGTTCGGAATGCCGTTGCTCTGTAAA(len = 28)
Longest Common Subsequence (LCS)

LCS: DP Definition 1
•1
st
: Design Subproblems in Words
•For &#3627408470;in[0,&#3627408475;], let ??????&#3627408470;=length of LCS in x
1,&#3627408485;
2,…,&#3627408537;
&#3627408522;and
y
1,&#3627408486;
2,…,&#3627408538;
&#3627408522;
•2
nd
: State Recursive Relations
•Express ??????&#3627408470;intermsof??????1, ??????2, …., ??????&#3627408470;−1for∀1≤&#3627408470;≤&#3627408475;
•Initial State
•??????0=0
e.g., X=BCDBCDAC,
Y=ABECBABC,
if x
i=y
i,thenLi=1+L[i−1]
if x
i≠y
i, LCS does not include x
ior/andy
i.
e.g, if exclude y
i, then subproblem is: LCS(&#3627408485;
1,&#3627408485;
&#3627408522;;;&#3627408486;
1,…,&#3627408486;
&#3627408522;−&#3627409359;)
→not recursive definition

LCS: DP Definition 2
•1
st
: Design Subproblems in Words
•For i,jin[0,&#3627408475;], let ??????&#3627408522;,&#3627408523;= length of LCS in x
1,&#3627408485;
2,…,&#3627408537;
&#3627408522;and
y
1,&#3627408486;
2,…,&#3627408538;
&#3627408523;
•2
nd
: State Recursive Relations
•Express ??????&#3627408470;,&#3627408471;intermsof??????0,0,…,??????1,1, ??????1,2, ….,
??????&#3627408470;−1,&#3627408471;−1,Li−1,j,….,Li,j−1
•2 cases: &#3627408433;
&#3627408418;≠&#3627408434;
&#3627408418;or&#3627408537;
&#3627408522;=&#3627408538;
&#3627408522;
•Initial State
•??????&#3627408470;,0=0
•??????0,&#3627408471;=0

LCS: DP Definition 2
e.g., X=BCDBCDAC,
Y=ABECBABC, D
(1) if x
i≠y
j, LCS does not include x
ior/andy
i.
•if exclude &#3627408486;
&#3627408471;, then subproblem is: ????????????&#3627408454;(&#3627408485;
1,&#3627408485;
&#3627408470;;;&#3627408486;
1,…,&#3627408486;
&#3627408471;−1)
→??????[&#3627408470;,&#3627408471;]=??????[&#3627408470;,&#3627408471;−1]
•if exclude &#3627408485;
&#3627408470;, then subproblem is: ????????????&#3627408454;(x
1,x
i−1;;&#3627408486;
1,…,&#3627408486;
&#3627408471;)
→??????[&#3627408470;,&#3627408471;]=??????[&#3627408470;−1,&#3627408471;]
•if exclude both, then subproblem is: ????????????&#3627408454;(x
1,x
i−1;;&#3627408486;
1,…,&#3627408486;
&#3627408471;−1)
→??????[&#3627408470;,&#3627408471;]=??????[&#3627408470;−1,&#3627408471;−1]
→??????[&#3627408522;,&#3627408523;]=max{??????[&#3627408522;,&#3627408523;−&#3627409359;],??????[&#3627408522;−&#3627409359;,&#3627408523;],??????[&#3627408522;−&#3627409359;,&#3627408523;−&#3627409359;]}
=max{??????[&#3627408522;,&#3627408523;−&#3627409359;],??????[&#3627408522;−&#3627409359;,&#3627408523;]}

e.g., X=BCDBCDC,
Y=ABECBABC
(2) if x
i=y
&#3627408471;, LCS does not include x
ior/andy
i.
•if exclude y
&#3627408471;, then subproblem is: ????????????&#3627408454;(&#3627408485;
1,&#3627408485;
&#3627408470;;;&#3627408486;
1,…,&#3627408486;
&#3627408471;−1)
→??????[&#3627408470;,&#3627408471;]=??????[&#3627408470;,&#3627408471;−1]
•if exclude x
i, then subproblem is: ????????????&#3627408454;(&#3627408485;
1,&#3627408485;
&#3627408470;−1;;&#3627408486;
1,…,&#3627408486;
&#3627408471;)
→??????[&#3627408470;,&#3627408471;]=??????[&#3627408470;−1,&#3627408471;]
•if exclude both, then subproblem is: ????????????&#3627408454;(&#3627408485;
1,&#3627408485;
&#3627408470;−1;;&#3627408486;
1,…,&#3627408486;
&#3627408471;−1)
→??????[&#3627408470;,&#3627408471;]=??????[&#3627408470;−1,&#3627408471;−1]+1
→??????&#3627408418;,&#3627408419;=max{Li,j−1, Li−1,j, Li−1,j−1+1}
=??????&#3627408418;−&#3627409359;,&#3627408419;−&#3627409359;+&#3627409359;
LCS: DP Definition 2

LCS: DP Definition 2
•1
st
: Design Subproblems in Words
•For i,jin[0,&#3627408475;], let ??????&#3627408522;,&#3627408523;= length of LCS in x
1,&#3627408485;
2,…,&#3627408537;
&#3627408522;
and y
1,&#3627408486;
2,…,&#3627408538;
&#3627408523;
•2
nd
: State Recursive Relations
•Express ??????&#3627408470;,&#3627408471;intermsof??????0,0,…,??????1,1, ??????1,2, ….,
??????&#3627408470;−1,&#3627408471;−1,Li−1,j,….,Li,j−1
•for1≤&#3627408470;≤&#3627408475;,1≤&#3627408471;≤&#3627408475;:
•??????&#3627408470;,&#3627408471;=൝
&#3627408526;??????&#3627408537;??????&#3627408522;,&#3627408523;−&#3627409359;,??????&#3627408522;−&#3627409359;,&#3627408523;&#3627408522;??????&#3627408537;
&#3627408522;≠&#3627408538;
&#3627408523;
??????&#3627408522;−&#3627409359;,&#3627408523;−&#3627409359;+&#3627409359;&#3627408522;??????&#3627408537;
&#3627408522;=&#3627408538;
&#3627408523;
•????????????????????????????????????????????????????????????????????????
•??????&#3627408470;,0=0 ??????0,&#3627408471;=0

LCS: DP Algorithm Pseudocode
LCS(X, Y):
#### initial states
for &#3627408470;=0&#3627408481;&#3627408476;&#3627408527;&#3627408465;&#3627408476;
??????&#3627408470;,0=0
end for
for&#3627408471;=0&#3627408481;&#3627408476;&#3627408475;&#3627408465;&#3627408476;:
??????0,&#3627408471;=0
end for
####recursiverelations
for&#3627408470;=1&#3627408481;&#3627408476;&#3627408527;&#3627408465;&#3627408476;
for&#3627408471;=1&#3627408481;&#3627408476;&#3627408475;&#3627408465;&#3627408476;
if &#3627408485;
&#3627408470;=&#3627408486;
&#3627408471;then ??????&#3627408470;,&#3627408471;=??????&#3627408470;−1,&#3627408471;−1+1
else: ??????&#3627408470;,&#3627408471;=&#3627408474;&#3627408462;&#3627408485;??????&#3627408470;,&#3627408471;−1,??????&#3627408470;−1,&#3627408471;
end if
end for
end for
return ??????&#3627408527;,&#3627408475;
Time
complexity:
&#3627408450;(&#3627408475;
2
)

LCS: DP Algorithm Pseudocode
LCS(X, Y):
#### initial states
for &#3627408470;=0&#3627408481;&#3627408476;&#3627408526;&#3627408465;&#3627408476;
??????&#3627408470;,0=0
end for
for&#3627408471;=0&#3627408481;&#3627408476;&#3627408475;&#3627408465;&#3627408476;:
??????0,&#3627408471;=0
end for
####recursiverelations
for&#3627408470;=1&#3627408481;&#3627408476;&#3627408526;&#3627408465;&#3627408476;
for&#3627408471;=1&#3627408481;&#3627408476;&#3627408475;&#3627408465;&#3627408476;
if &#3627408485;
&#3627408470;=&#3627408486;
&#3627408471;then ??????&#3627408470;,&#3627408471;=??????&#3627408470;−1,&#3627408471;−1+1
else: ??????&#3627408470;,&#3627408471;=&#3627408474;&#3627408462;&#3627408485;??????&#3627408470;,&#3627408471;−1,??????&#3627408470;−1,&#3627408471;
end if
end for
end for
return ??????&#3627408526;,&#3627408475;
Time
complexity:
&#3627408450;(&#3627408526;&#3627408475;)

LCS(X, Y):
#### initial states
for &#3627408470;=0&#3627408481;&#3627408476;&#3627408526;&#3627408465;&#3627408476;
??????&#3627408470;,0=0
end for
for&#3627408471;=0&#3627408481;&#3627408476;&#3627408475;&#3627408465;&#3627408476;:
??????0,&#3627408471;=0
end for
####recursiverelations
for&#3627408470;=1&#3627408481;&#3627408476;&#3627408526;&#3627408465;&#3627408476;
for&#3627408471;=1&#3627408481;&#3627408476;&#3627408475;&#3627408465;&#3627408476;
if &#3627408485;
&#3627408470;=&#3627408486;
&#3627408471;then ??????&#3627408470;,&#3627408471;=??????&#3627408470;−1,&#3627408471;−1+1
else: ??????&#3627408470;,&#3627408471;=&#3627408474;&#3627408462;&#3627408485;??????&#3627408470;,&#3627408471;−1,??????&#3627408470;−1,&#3627408471;
end if
end for
end for
return ??????&#3627408526;,&#3627408475;
&#3627408400;:FindLCSofX=ABCBDAB,Y=BDCABA
&#3627408523;0 1 2 3 4 5 6
&#3627408522; &#3627408538;
&#3627408523;B D C A B A
0 &#3627408537;
&#3627408522;
1 A
2 B
3 C
4 B
5 D
6 A
7 B
0 0 0 0 0 00
0
0
0
0
0
0
0
0 0 0 1 1 1
1 1 1 1 2
2
1 1 2 2 2 2
1 1 2 2 3
3
1 2 2 2 3 3
1 2 2 3 3 4
1 2 2 3 4 4
Q: How do we find the actual
optimal subsequence?
Q: Do we need the whole matrix
L (for finding longest length)?

DP Algorithm Definition Summary
•1
st
: Define Subproblemsin Words
a)Start with Prefixof current problem
b)Add constraintsif needed, e.g, include last
elements in LIS
•2
nd
: Express RecursiveRelations
•&#3627408455;[&#3627408470;]in terms of &#3627408455;1,…,&#3627408455;&#3627408470;−1for∀valid&#3627408470;
•Define the InitialState
•T[1] or T[0]

Practical Problems
DPV 6.1: Maximum Subarray Problem
•Input: A list of numbers a
1,a
2,…,a
n
•Goal: Find Subarray (contiguous subsequence) of maximum sum
→Cannot determine if can append
a
i(do not know what it ends), so
need to revise subproblems
And the substring includes ??????
&#3627408418;
→&#3627408402;&#3627408418;=??????
&#3627408418;+&#3627408422;??????&#3627408433;{&#3627409358;,&#3627408402;[&#3627408418;−&#3627409359;]}
&#3627408402;&#3627409358;=&#3627409358;,&#3627408402;&#3627409359;=??????
&#3627409359;
•1
st
: Define Subproblemsin Words
Siin terms of S[0], S[1], …, S[i-1]
•2
nd
: Express RecursiveRelations
•Define the InitialState
For iin0,n,letSi=maxsumofsubarryofa
1,a
2,….,a
i
Q: What is the output?
Q: Anyone can write the
pseudocode?
Q: [5, 15, -30, 10, -5, 40, 10] output?

DPV 6.1: Maximum Subarray Pseudocode
X= a
1,a
2,…,a
n
MaximumSubarray(X):
S[1] = &#3627408462;
1
For&#3627408470;&#3627408470;&#3627408475;2,&#3627408527;:
&#3627408506;&#3627408522;=??????
&#3627408522;+&#3627408526;??????&#3627408537;{&#3627409358;,&#3627408506;[&#3627408522;−&#3627409359;]}
Return &#3627408474;&#3627408462;&#3627408485;(&#3627408506;&#3627408522;)
LeetCode: https://leetcode.com/problems/maximum-subarray/
Q: [5, 15, -30, 10, -5, 40, 10] output?
→Subarray: [10, -5, 40, 10]
→minimum sum =10+−5+40+10=55

Practical Problems
DPV 6.2: Hotel Stops
•You are going on a long trip. You start on the road at mile post 0.
Along the way there are n hotels, at mile posts &#3627408462;
1<&#3627408462;
2<…<&#3627408462;
&#3627408475;,
where each &#3627408462;
&#3627408470;is measured from the starting point.
•The only places you are allowed to stop are at these hotels, but
you can choose which of the hotels you stop at.
•You must stop at the final hotel (at distance &#3627408462;
&#3627408475;), which is your
destination.
•You’d ideally like to travel 200 miles a day, but this may not be
possible (depending on the spacing of the hotels). If you travel &#3627408485;
miles during a day, the penalty for that day is &#3627409360;&#3627409358;&#3627409358;−&#3627408537;
&#3627409360;
.
•You want to plan your trip so as to minimize the total penalty—that
is, the sum, over all travel days, of the daily penalties.
•Give an efficient algorithm that determines the optimal sequence
of hotels at which to stop.

Practical Problems
DPV 6.2: Hotel Stops
•Input: sequence of increasing positive numbers: &#3627408462;
1<&#3627408462;
2<…<&#3627408462;
&#3627408475;
•Output: subsequence of numbers with minimum sum of penalties&
must include &#3627408462;
&#3627408475;
•If you travel &#3627408485;miles during a day, the penalty for that day is
200−&#3627408485;
2
•1
st
: Define Subproblemsin Words
•2
nd
: Express RecursiveRelations
•3
rd
: Define the InitialState
&#3627408451;[&#3627408470;]=minimumsumofpenalitieswhenstopat&#3627408462;
&#3627408470;
&#3627408451;[&#3627408470;]=Min
0≤&#3627408471;≤&#3627408470;−1
{&#3627408451;&#3627408471;+(200−(&#3627408462;
&#3627408470;−&#3627408462;
&#3627408471;))
2
}
&#3627408451;[0]=0
Q: What is the output?
Q: Anyone can write the
pseudocode?
Q: [60, 150, 210, 300, 520, 700]
output?

DPV 6.2: Hotel Stops Pseudocode
X= a
1,a
2,…,a
n
HotelStops(X):
&#3627408477;[0]=0
For&#3627408470;&#3627408470;&#3627408475;1,&#3627408527;:
&#3627408451;[&#3627408470;]=Min
0≤&#3627408471;≤&#3627408470;−1
{&#3627408451;&#3627408471;+(200−(&#3627408462;
&#3627408470;−&#3627408462;
&#3627408471;))
2
}
Return &#3627408451;[&#3627408527;]
Q: [60, 150, 210, 300, 520, 700] output?
→Stop at: 150, 300, 520, 700
→minimum penalty =50
2
+50
2
+20
2
+20
2

Practical Problems
DPV 6.3: Yuck Donald’s Restaurant
•Yuckdonald’s is considering opening a series of restaurants
along Quaint Valley Highway (QVH).
The &#3627408527;possible locations are along a straight line, and the
distances of these locations from the
start of QVH are, in miles and in increasing order, &#3627408474;
1,&#3627408474;
2,…,&#3627408474;
&#3627408475;.
•The constraints are as follows:
•At each location, Yuckdonald’smay open at most one
restaurant.
•The expected profit from opening a restaurant at location &#3627408470;is
&#3627408477;
&#3627408470;, where &#3627408477;
&#3627408470;>0,&#3627408462;&#3627408475;&#3627408465;&#3627408470;=1,2,3,…,&#3627408475;
•Any two restaurants should be at least &#3627408524;miles apart, where &#3627408524;
is a positive integer.
•Give an efficient algorithm to compute the maximum expected
total profit subject to the given constraints.

Practical Problems
•1
st
: Define Subproblemsin Words
•2
nd
: Express RecursiveRelations
•3
rd
: Define the InitialState
&#3627408467;&#3627408476;&#3627408479;&#3627408470;&#3627408470;&#3627408475;1,&#3627408475;,&#3627408451;&#3627408470;
=maximumsumofprofitwhenopeningthelastrestaurantat&#3627408474;
&#3627408470;
&#3627408451;&#3627408470;= Max
1≤&#3627408471;≤&#3627408470;−1
and&#3627408474;
&#3627408470;−&#3627408474;
&#3627408471;≥&#3627408472;
&#3627408451;&#3627408471;+&#3627408477;
&#3627408470;
(&#3627408451;1=&#3627408477;
1)
DPV 6.3: Yuck Donald’s Restaurant
•Input: nlocations’ milestone: &#3627408474;
1<&#3627408474;
2<⋯<m
n,with profit of each
location is : &#3627408477;
1,&#3627408477;
2,…,&#3627408477;
&#3627408475;.
•Goal: subsequence of numbers with maximum expected total profit
•Constraints :
•May open at most one restaurant at each location.
•The expected profit from opening a restaurant at location &#3627408470;is &#3627408477;
&#3627408470;, where
&#3627408477;
&#3627408470;>0,&#3627408462;&#3627408475;&#3627408465;&#3627408470;=1,2,3,…,&#3627408475;
•Any 2 restaurants should be at least &#3627408524;miles apart (&#3627408524;is a positive integer).
Q: What is the output?
Q: Anyone can write the
pseudocode?

DPV 6.3: Yuck Donald’s Restaurant Pseudocode
m=&#3627408474;
1,&#3627408474;
2,…,&#3627408474;
n, p=p
1,&#3627408477;
2,…,&#3627408477;
n
ChooseResaurant(m,p):
&#3627408451;[0]=0
For&#3627408470;&#3627408470;&#3627408475;1,&#3627408527;:
&#3627408451;&#3627408470;= Max
1≤&#3627408471;≤&#3627408470;−1
and&#3627408474;
&#3627408470;−&#3627408474;
&#3627408471;≥&#3627408472;
&#3627408451;&#3627408471;+&#3627408477;
&#3627408470;
Return max{&#3627408451;&#3627408470;}
Q: m=[60,150,210,300,520,700], &#3627408477;=[50,30,60,80,100,80],
&#3627408472;=200, output?
→Open restaurant at: 60,300,520
→minimum penalty =50+80+100=230

Practical Problems
DPV 6.4: String of Words
•You are given a string of &#3627408475;characters &#3627408475;&#3627408480;[1…&#3627408475;], which you believe to be a
corrupted text document, in which all punctuation has vanished (so that it
looks something like “itwasthebestoftimes...”).
You wish to reconstruct the document using a dictionary, which is available in
the form of a Boolean function dict∙for any string &#3627408484;,
dict&#3627408484;=ቊ
&#3627408481;&#3627408479;&#3627408482;&#3627408466;&#3627408470;&#3627408467;&#3627408484;&#3627408470;&#3627408480;&#3627408483;&#3627408462;&#3627408473;&#3627408470;&#3627408465;&#3627408484;&#3627408476;&#3627408479;&#3627408465;
&#3627408467;&#3627408462;&#3627408473;&#3627408480;&#3627408466;&#3627408476;&#3627408481;ℎ&#3627408466;&#3627408479;&#3627408484;&#3627408470;&#3627408480;&#3627408466;
•(a) Give a dynamic programming algorithm that determines whether the
string s[·] can be reconstituted as a sequence of valid words. The running
time should be at most O(&#3627408475;
2
), assuming calls to dict∙take unit time.
•(b) In the event that the string is valid, make your algorithm output the
corresponding sequence of words.

Practical Problems
DPV 6.4: String of Words
•Input: A string of &#3627408475;characters &#3627408475;&#3627408480;1…&#3627408475;
•Goal: Determines whether the string s[·] can be reconstituted as a
sequence of valid words.
•1
st
: Define Subproblemsin Words
•2
nd
: Express RecursiveRelations
•3
rd
: Define the InitialState
for&#3627408470;in1,&#3627408475;,&#3627408483;&#3627408470;=the first ??????characters can be reconstituted as a
sequence of valid words
&#3627408483;&#3627408470;=??????&#3627408401;
0≤&#3627408471;<&#3627408470;(v
j⋀dict(substring(j,i)))
&#3627408483;0=&#3627408481;&#3627408479;&#3627408482;&#3627408466;
•??????&#3627408401;
0≤&#3627408471;<&#3627408470;&#3627408485;
&#3627408471;=&#3627408485;
0⋁&#3627408485;
1⋁&#3627408485;
2⋁…⋁&#3627408485;
&#3627408470;−1
•substringj,iis the substring of characters located in the place (&#3627408471;,&#3627408470;]
Q: What is the output?
Q: Anyone can write
the pseudocode?

Practical Problems
DPV 6.4: String of Words Pseudocode
&#3627408480;&#3627408481;&#3627408479;&#3627408470;&#3627408475;&#3627408468;&#3627408476;&#3627408467;&#3627408475;&#3627408464;ℎ&#3627408462;&#3627408479;&#3627408462;&#3627408464;&#3627408481;&#3627408466;&#3627408479;&#3627408480;=&#3627408480;1…&#3627408475;
String_of_Words(&#3627408480;):
??????[0]=1
For&#3627408470;&#3627408470;&#3627408475;1,&#3627408527;:
&#3627408451;&#3627408470;=OR
0≤&#3627408471;<&#3627408470;(v
j⋀dict(substring(j,i)))
Return ??????[&#3627408475;]

Practical Problems
DPV 6.11: Longest Common SubString(LCSS)
•Input: Given two strings &#3627408485;=&#3627408485;
1&#3627408485;
2….&#3627408485;
&#3627408475;and &#3627408486;=&#3627408486;
1&#3627408486;
2….&#3627408486;
&#3627408474;,
•Goal: Find the lengthof their Longest Common SubString(LCSS)
•Show how to do this in time O(mn).
•1
st
: Define Subproblemsin Words
•2
nd
: Express RecursiveRelations •3
rd
: Define the
InitialState
For i,jin[0,&#3627408475;], let ??????&#3627408522;,&#3627408523;= length of LCSS in x
1,&#3627408485;
2,…,&#3627408537;
&#3627408522;
and y
1,&#3627408486;
2,…,&#3627408538;
&#3627408523;, and LCCS include &#3627408537;
&#3627408522;and &#3627408538;
&#3627408523;
Express ??????&#3627408470;,&#3627408471;intermsof??????0,0,…,??????1,1, ??????1,2, ….,
??????&#3627408470;−1,&#3627408471;−1,Li−1,j,….,Li,j−1
2 cases: &#3627408433;
&#3627408418;≠&#3627408434;
&#3627408523;or&#3627408537;
&#3627408522;=&#3627408538;
&#3627408523;
fori≥1,j≥1:
??????&#3627408470;,&#3627408471;=൝
&#3627409358; &#3627408522;??????&#3627408537;
&#3627408522;≠&#3627408434;
&#3627408523;
??????&#3627408418;−&#3627409359;,&#3627408419;−&#3627409359;+&#3627409359;&#3627408522;??????&#3627408433;
&#3627408418;=&#3627408434;
&#3627408419;
??????&#3627408470;,0=0
??????0,&#3627408471;=0
Q: What is the output?
Q: Anyone can write
the pseudocode?

LCSS(X, Y):
For &#3627408470;&#3627408470;&#3627408475;0,&#3627408526;:
??????&#3627408470;,0=0
For&#3627408471;&#3627408470;&#3627408475;0,&#3627408475;:
??????0,&#3627408471;=0
For&#3627408470;&#3627408470;&#3627408475;1,&#3627408526;:
For&#3627408471;&#3627408470;&#3627408475;1,&#3627408475;:
if &#3627408537;
&#3627408522;=&#3627408538;
&#3627408523;then ??????&#3627408470;,&#3627408471;=??????&#3627408522;−&#3627409359;,&#3627408523;−&#3627409359;+&#3627409359;
else:??????&#3627408470;,&#3627408471;=&#3627409358;
Return &#3627408474;&#3627408462;&#3627408485;{??????&#3627408526;,&#3627408475;}
DPV 6.11: Longest Common SubString(LCSS)

Practical Problems
Rod Cutting
•Serling Enterprises buys long steel rods and cuts them
into shorter rods, which it then sells. Each cut is free.
•The management of Serling Enterprises wants to know
the best way to cut up the rods.
•We assume that we know, for &#3627408470;=1,2,3,…,&#3627408475;, the price &#3627408529;
&#3627408522;
in dollars that Serling Enterprises charges for a rod of
length &#3627408522;inches. Rod lengths are always an integral
numberof inches.
Ref: T.Cormen, C. Leiserson, R. Rivest, & C. Stein. Introduction to Algorithm 3
rd
Edition. ISBN 978-0-262-03384-8

Rod-Cutting Problem:
•Input: Given a rod of length &#3627408475;inches and a table of prices &#3627408477;
&#3627408470;for&#3627408470;=
1,2,3,…,&#3627408475;,
•Goal: Determine the maximum revenue &#3627408479;
&#3627408475;obtainable by cutting up
the rod and selling the pieces.
•Note: if the price &#3627408477;
&#3627408475;for a rod of length &#3627408475;is large enough, an
optimal solution may require no cutting at all.
•1
st
: Define Subproblemsin Words
•2
nd
: Express RecursiveRelations
•3
rd
: Define the InitialState
For &#3627408470;in[1,&#3627408475;], let &#3627408453;&#3627408522;=maximum revenue obtainable by cutting up a rod
of length &#3627408470;and selling the pieces.
&#3627408453;&#3627408522;=&#3627408526;??????&#3627408537;&#3627408477;
&#3627408470;,&#3627408453;&#3627408522;−&#3627409359;+&#3627408505;&#3627409359;,&#3627408453;&#3627408522;−&#3627409360;+&#3627408505;&#3627409360;,…,&#3627408453;&#3627408522;+&#3627408505;&#3627408522;−&#3627409359;
=&#3627408526;??????&#3627408537;
&#3627409359;≤&#3627408523;<&#3627408522;{&#3627408529;
&#3627408522;,&#3627408453;&#3627408522;−&#3627408523;+&#3627408505;&#3627408523;}
&#3627408453;1=&#3627408477;
1
Rewrite &#3627408453;&#3627408522;in terms of &#3627408453;&#3627409358;,&#3627408453;&#3627409359;,…,&#3627408453;&#3627408522;−&#3627409359;

Rod_Cut(p, n):
&#3627408453;1=&#3627408477;
1
for&#3627408470;in2,&#3627408475;:
&#3627408478;=&#3627408529;
&#3627408522;## price of i-inch rod
for j in &#3627409359;,&#3627408522;−&#3627409359;:
&#3627408478;=&#3627408526;??????&#3627408537;{&#3627408478;,R&#3627408522;−&#3627408523;+&#3627408505;&#3627408523;}
&#3627408453;&#3627408470;=&#3627408478;
Return &#3627408453;&#3627408475;
Rod-Cutting Problem: DP Pseudocode
Q: What is the best way to cut a 10-inch long rod, with profit from
each inch listed below?
Q: What is the time
complexity?
Q: Is there any way to
optimize this code?
Q: What is the time
complexity after
optimization?

&#3627408453;&#3627408522;=maximum revenue obtainable by cutting up a rod of length &#3627408470;.
Rod_Cut(p, n):
&#3627408453;1=&#3627408477;
1
for&#3627408470;&#3627408470;&#3627408475;2,&#3627408475;:
&#3627408478;=&#3627408529;
&#3627408522;## price of i-inch rod
for j in &#3627409359;,&#3627408522;/&#3627409360;:##floor(i/2)
&#3627408478;=&#3627408526;??????&#3627408537;{&#3627408478;,R&#3627408522;−&#3627408523;+&#3627408505;&#3627408523;}
&#3627408453;&#3627408470;=&#3627408478;
Return &#3627408453;&#3627408475;
•&#3627408453;2=max&#3627408477;
2,R1+R1=
max5,1+1=&#3627409363;→&#3627409360;
•R3=&#3627408474;&#3627408462;&#3627408485;&#3627408477;
3,&#3627408453;1+&#3627408453;2=
max8,1+5=&#3627409366;→&#3627409361;
•R4=&#3627408474;&#3627408462;&#3627408485;{
}
&#3627408477;
4,&#3627408453;1+&#3627408453;3,&#3627408453;2+
&#3627408453;[2]=max9,1+8,5+5=
&#3627409359;&#3627409358;→&#3627409360;,&#3627409360;
•&#3627408453;5=max{
}
&#3627408477;
5,R1+R4,R2+
R3=max10,1+10,5+8=
&#3627409359;&#3627409361;→&#3627409360;,&#3627409361;
•R6=&#3627408474;&#3627408462;&#3627408485;{
}
&#3627408477;
6,&#3627408453;1+&#3627408453;5,&#3627408453;2+&#3627408453;4,
&#3627408453;3+&#3627408453;3=max{
}
17,1+13,5+10,
8+8=&#3627409359;&#3627409365;→&#3627409364;
•R7=&#3627408474;&#3627408462;&#3627408485;{
}
&#3627408477;
7,&#3627408453;1+&#3627408453;6,&#3627408453;2+
&#3627408453;[5],&#3627408453;3+&#3627408453;4=max{
}
17,1+17,
5+13,8+10=&#3627409359;&#3627409366;→
&#3627409359;,&#3627409364;&#3627408528;??????&#3627409360;,&#3627409360;,&#3627409361;&#3627408528;??????&#3627409361;,&#3627409360;,&#3627409360;
•R8=&#3627408474;&#3627408462;&#3627408485;{
}
&#3627408477;
8,&#3627408453;1+&#3627408453;7,&#3627408453;2+
&#3627408453;6,&#3627408453;3+&#3627408453;5,&#3627408453;4+&#3627408453;4=
max{
}
20,1+18,5+17,8+13,10+
10=&#3627409360;&#3627409360;→&#3627409360;,&#3627409364;
•R9=&#3627408474;&#3627408462;&#3627408485;{
}
&#3627408477;
9,&#3627408453;1+&#3627408453;8,&#3627408453;2+
&#3627408453;7,&#3627408453;3+&#3627408453;6,&#3627408453;4+&#3627408453;5=
max{
}
24,1+22,5+18,8+17,10+
13=&#3627409360;&#3627409363;→&#3627409361;,&#3627409364;
•R10=&#3627408474;&#3627408462;&#3627408485;{
}
&#3627408477;
10,&#3627408453;1+&#3627408453;9,&#3627408453;2+
&#3627408453;8,&#3627408453;3+&#3627408453;7,&#3627408453;4+&#3627408453;6,&#3627408453;5+
&#3627408453;5=max{
}
30,1+25,5+22,8+
18,10+17,13+13=&#3627409361;&#3627409358;→&#3627409359;&#3627409358;

THANK YOU!
67