Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
1.
CS6515: Lecture 1
DynamicProgramming
(Fibonacci, LIS, LCS)
Dr. Gerandy Brito
Dr. Mengmeng (Helen) Liu
Email: liumengmeng@gtsi.edu.cn
Phone: (+86)-15117982925
Office: Room 418
3.
• 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
4.
• Dynamic Programming
•Divide and Conquer
• Randomized Algorithms
• Graph Algorithms
• Max-Flow
• Linear Programming
• NP-Completeness
Algorithms Covered in CS6500
5.
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 computational steps that transform the
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?
6.
• Model ofcomputation: 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
7.
• Worst-Case: themaximum number of steps taken on
any instance of size n
• Best-Case: the minimum number of steps taken on any
instance of size n.
• Average-Case: the average number of steps taken on
any instance of size n.
Running Time
• Need an
understanding of the
distribution of
possible inputs
8.
• 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 upper and lower bounds of the
function
• Asymptotic notation (𝜪, 𝜴, 𝜣) are the way we practically
deal with analysis of time complexity.
9.
• Proofs byCounterexample
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
10.
Review of Proofs
•Inductive Proofs
• e.g., Proof: for any positive n, show that: 1 + 2 + 3 + … + 𝑛 =
𝑛 𝑛+1
2
• Step 1: 𝑛 = 1 → 1 = 1 ∗ 2/2
1) A Statement involving a natural number n, and we 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 + … + 𝑛 + 𝑛 + 1 =
𝑛 𝑛+1
2
+ 𝑛 + 1 =
𝑛+1 𝑛+2
2
11.
Review of Proofs
•Structural Induction: proof statements 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 𝑛 leaves, has 𝑛 − 1 non-leaf nodes.
• Step 1: 𝑛 = 1 , a tree consisting of a leaf has 𝑛 − 1 = 0 non-leaf nodes.
• Step 2: Assume it holds for all trees with ≤ 𝑛 leaves.
• Step 3: Consider a tree T with (𝑛 + 1) leaves.
• Both T1 and T2 consist of at least one node, hence both have leaves < 𝑛 − 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
12.
Review of Proofs
•e.g. All horses are the same color.
• Label horses: 1, 2, 3, … , 𝑛, 𝑛 + 1
• Step 1: in any set of 𝑛 horses, all horses have the same color
• Step 2: 𝑛 = 1 , clearly holds.
• Step 3: assume it holds for any set with ≤ 𝑛 horses
• Step 4: Consider a set of (𝑛 + 1) horses.
• Apply hypothesis since size is ≤ 𝑛, each set must be of one color
• Consider sets , 1, 2, 3, … , n and 2, 3, … , n + 1
• The two sets intersect: have at least one horse in common
• Hence, all (𝑛 + 1) horses are the same color.
Q: Is my proof right ?
→ “The two sets intersect” are not true for n = 2
13.
Time Complexity
• Thetime 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-negative integers
(problem sizes) to real numbers (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: 𝑐𝑛2, next year: 𝑐(2𝑛)2= 4𝑐𝑛2: 4x longer)
• Big-Oh notation answers this kind of questions.
14.
Big O (DPVChapter 0)
Upper bounds: T(n) is O(f(n)) if there exist constants c > 0 and
𝑛0 0 such that for all n 𝑛0 we have T(n) c ·f(n)
Lower bounds: T(n) is (f(n)) if there exist constants c > 0 and
𝑛0 0 such that for all n 𝑛0 we have T(n) c ·f(n).
Tight bounds: T(n) is (f(n)) if T(n) is both O(f(n)) and (f(n)).
15.
Big O (DPVChapter 0)
e.g., 𝑇 𝑛 = 32𝑛2
+ 17𝑛 + 32
→ T(n) is O(𝑛2
), O(𝑛3
), (𝑛2
), (n), and (𝑛2
)
→ T(n) is not O(n), (𝑛3), (n), or (𝑛3).
16.
Big O (DPVChapter 0)
• Not Transitive:
𝑇1 𝑛 = 5𝑛3
, 𝑇2 𝑛 = 3𝑛2
𝑇1 𝑛 = 𝑂 𝑛3 , 𝑇2 𝑛 = 𝑂 𝑛3 , but 𝑇1 𝑛 ≠ 𝑇2 𝑛
Better notation: 𝑇 𝑛 ∈ 𝑂(𝑓(𝑛))
• T(n) is in the set of 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)
17.
• Some CommonFunctions
• Polynomials: 𝑎0 + 𝑎1𝑛 + … + 𝑎𝑑𝑛𝑑 𝑖𝑠 (𝑛𝑑) if 𝑎𝑑 > 0
• Logarithms: the base does not matter
• 𝑂 log𝑎 𝑛 = 𝑂(log𝑏 𝑛) for any constant 𝑎, 𝑏 > 0
• log𝑏 𝑛 = log𝑎 𝑛 / log𝑎 𝑏
• Polynomial time. Running time is O(𝑛𝑑 ) for some constant d
independent of the input size n.
• Exponential time. Running time is O(𝑟𝑛
) for some constant r
• Logarithms grow slower than every polynomial: 𝑙𝑜𝑔 𝑛 ∈ 𝑂(𝑛𝑑)
• Exponentials grow faster than every
polynomial (r > 1, d > 0)
18.
Small O Notation
•Let 𝑓 and 𝑔 be two functions from N to R+ (the set of positive real
numbers).
• If 𝑙𝑖𝑚n→(f(n)/g(n)) = 0, then 𝑓 𝑛 ∈ 𝒐(𝑔(𝑛))
Upper bounds: 𝑻(𝒏) is 𝑶(𝒇(𝒏)) if there exist constants 𝑐 > 0 and
𝑛0 ≥ 0 such that for all 𝑛 ≥ 𝑛0 we have 𝑻 𝒏 ≤ 𝒄 · 𝒇(𝒏) .
• If 𝑓 𝑛 ∈ 𝒐(𝑔(𝑛)) then 𝑓 𝑛 ∈ 𝑶(𝑔(𝑛)), but the reverse is not always true
• Inequality of big-O needs to hold for some positive constant c,
• Inequality of small-o must hold for every positive constant c
Small-o: For any positive real number 𝑐, a number 𝑛0 exists such that
for every 𝑛 ≥ 𝑛0, 𝑓(𝑛) < 𝑐 𝑔(𝑛) .
19.
Some Examples ofRunning Times
• 𝑶(log 𝒏): Binary search on sorted list
• 𝑶(𝒏): Find max element in a list
• 𝑶(𝒏 log 𝒏): Sort a set of elements
• 𝑶(𝐧𝟐
): Find the pair of points that is closest to each other
• 𝑶(𝐧𝟑): Given 𝑛 sets 𝑆1, … , 𝑆𝑛 , each of which is a subset of
1, 2, … , 𝑛, is there some pair of these which are disjoint?
• 𝑶(𝐧𝐤): Given a graph of 𝑛 nodes, are there 𝑘 nodes such that no
two are joined by an edge?
• 𝑶(𝟐𝐧
): Enumerate all subsets of a set of 𝑛 elements
• 𝑶(𝒏!): 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)
Big O :Practical Problem
Exponentials grow faster than every polynomial (𝑟 > 1, 𝑑 > 0)
25.
Big O :Practical Problem
Inductive Proofs
1) A Statement involving a natural number 𝑛, and we will show that
it holds for all values of 𝑛
2) Basis: show it holds for small inputs, usually for 𝑛 = 0 or 𝑛 = 1
3) Induction Hypothesis: assume it holds for 𝑛 (or all values ≤ 𝑛)
4) Inductive Step: show it holds for (𝑛 + 1)
• Dynamic programming
•Break up a problem into a series of overlapping sub-problems
• build up solutions to larger and larger sub-problems,
(reusing solutions of encountered subproblems as much as
possible)
• Dynamic programming algorithms
• systematically search all possibilities (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
28.
• Top-down DP= Memoization
• Design a recursive algorithm
• 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-up DP = Iterative DP
• 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 FOR loops)
Fibonacci Numbers
• Howto solve it ?
• Recursive Algorithm
• Dynamic Programming
• Goal: Given number 𝑛 generate the 𝑛-th Fibonacci numbers
What is Fibonacci numbers?
0, 1, 1 , 2, 3, 5, 8, 13, 21, 34, … .
𝐹0 = 0, 𝐹1 = 1, 𝐹𝑛= 𝐹𝑛−1 + 𝐹𝑛−2 for 𝑛 > 1
• Define Problem
• Input: integer n ≥ 0
• Output: 𝑛-th Fibonacci number
31.
Fibonacci Numbers: RecursiveAlgorithm
Fib1(n):
Input: integer 𝑛 ≥ 0
Output: 𝐹𝑛
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)
32.
Fibonacci Numbers: DPPseudocode
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: 𝑂(𝑛)
• Faster than recursive
• Simpler to analyze time complexity
Dynamic Programming (bottom-up)
Efficient
Computing
33.
DP Solution
• 1st:Define Subproblem In Words
• 𝐹[𝑖] = 𝑖-th Fibonacci Number
• 2nd : State Recursive Relations
• 𝐹 𝑖 = 𝐹 𝑖 − 1 + 𝐹 𝑖 − 2 for ∀ 2 ≤ 𝑖 ≤ 𝑛
• Define Initial State
• 𝐹[0] = 0, 𝐹[1] = 1
34.
Longest Increasing Subsequences(LIS)
• LIS problem:
• Find the length of the longest subsequence of a given
sequence s.t. all elements of the subsequence are
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 = 𝟔
35.
LIS: DP Definition
•1st: Define Subproblem In Words
• Let 𝐿 𝑖 = length of LIS on 𝑎1, 𝑎2, 𝑎3, … . . , 𝑎𝑖 and includes 𝒂𝒊
• 2nd : State Recursive Relations
• Express 𝐿 𝑖 in terms of 𝐿 1 , 𝐿 2 ,…, 𝐿 𝑖 − 1
• 𝐿 𝑖 = 1 + max1≤ 𝑗≤𝑖−1,& 𝑎𝑗<𝑎𝑖
{𝐿[𝑗]} for ∀ 2 ≤ 𝑖 ≤ 𝑛
• Initial State
• 𝐿 1 = 1
• Input: 𝑛 numbers 𝑎1, 𝑎2, 𝑎3, … . . , 𝑎𝑛
• Goal: find length of LIS in a list of numbers
36.
LIS: DP Algorithm
LIS(𝑎1,𝑎2, 𝑎3, … . . , 𝑎𝑛):
L[1] = 1
for i = 2 to n do
for j = 1 to i-1 do
if 𝑎𝑗 < 𝑎𝑖 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]
Longest Common Subsequence(LCS)
• Input: 2 strings X = x1, 𝑥2, … , 𝒙𝒏,
𝑌 = 𝑦1, 𝑦2, … , 𝒚𝒏
• Goal: find the length of longest strings which
is a subsequence of both X and Y.
e.g, Given X = BCDBCDA, Y = ABECBAB,
what is their longest common subsequence?
→ LCS = BCBA, len = 4
40.
• Can beused 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
𝑆1 = ACC GGT CGA GTG CGC GGA AGC CGG CCG AA (len = 29)
𝑆2 = GTC GTT CGG AAT GCC GTT GCT CTG TAA A (len = 28)
Longest Common Subsequence (LCS)
41.
LCS: DP Definition1
• 1st : Design Subproblems in Words
• For 𝑖 in [0, 𝑛], let 𝐿 𝑖 = length of LCS in x1, 𝑥2, … , 𝒙𝒊 and
y1, 𝑦2, … , 𝒚𝒊
• 2nd : State Recursive Relations
• Express 𝐿 𝑖 in terms of 𝐿 1 , 𝐿 2 , …., 𝐿 𝑖 − 1 for ∀ 1 ≤ 𝑖 ≤ 𝑛
• Initial State
• 𝐿 0 = 0
e.g., X = B C D B C D A C,
Y = A B E C B A B C,
if xi = yi, then L i = 1 + L[i − 1]
if xi ≠ yi, LCS does not include xi or / and yi .
e.g, if exclude yi, then subproblem is: LCS(𝑥1, 𝑥𝒊;; 𝑦1, … , 𝑦𝒊−𝟏)
→ not recursive definition
42.
LCS: DP Definition2
• 1st : Design Subproblems in Words
• For i, j in [0, 𝑛], let 𝑳 𝒊, 𝒋 = length of LCS in x1, 𝑥2, … , 𝒙𝒊 and
y1, 𝑦2, … , 𝒚𝒋
• 2nd : State Recursive Relations
• Express 𝐿 𝑖, 𝑗 in terms of 𝐿 0,0 , … , 𝐿 1, 1 , 𝐿 1, 2 , ….,
𝐿 𝑖 − 1, 𝑗 − 1 , L i − 1, j , … . , L i, j − 1
• 2 cases: 𝐱𝐢 ≠ 𝐲𝐢 or 𝒙𝒊 = 𝒚𝒊
• Initial State
• 𝐿 𝑖, 0 = 0
• 𝐿 0, 𝑗 = 0
43.
LCS: DP Definition2
e.g., X = B C D B C D A C,
Y = A B E C B A B C, D
(1) if xi ≠ yj, LCS does not include xi or / and yi .
• if exclude 𝑦𝑗, then subproblem is: 𝐿𝐶𝑆(𝑥1, 𝑥𝑖;; 𝑦1, … , 𝑦𝑗−1)
→ 𝐿[𝑖, 𝑗] = 𝐿[𝑖, 𝑗 − 1]
• if exclude 𝑥𝑖 , then subproblem is: 𝐿𝐶𝑆(x1, xi−1;; 𝑦1, … , 𝑦𝑗)
→ 𝐿[𝑖, 𝑗] = 𝐿[𝑖 − 1, 𝑗]
• if exclude both, then subproblem is: 𝐿𝐶𝑆(x1, xi−1;; 𝑦1, … , 𝑦𝑗−1)
→ 𝐿[𝑖, 𝑗] = 𝐿[𝑖 − 1, 𝑗 − 1]
→ 𝑳[𝒊, 𝒋] = max{𝑳[𝒊, 𝒋 − 𝟏], 𝑳[𝒊 − 𝟏, 𝒋], 𝑳[𝒊 − 𝟏, 𝒋 − 𝟏]}
= max{𝑳[𝒊, 𝒋 − 𝟏], 𝑳[𝒊 − 𝟏, 𝒋]}
44.
e.g., X =B C D B C D C,
Y = A B E C B A B C
(2) if xi = y𝑗, LCS does not include xi or / and yi .
• if exclude y𝑗, then subproblem is: 𝐿𝐶𝑆(𝑥1, 𝑥𝑖;; 𝑦1, … , 𝑦𝑗−1)
→ 𝐿[𝑖, 𝑗] = 𝐿[𝑖, 𝑗 − 1]
• if exclude xi, then subproblem is: 𝐿𝐶𝑆(𝑥1, 𝑥𝑖−1;; 𝑦1, … , 𝑦𝑗)
→ 𝐿[𝑖, 𝑗] = 𝐿[𝑖 − 1, 𝑗]
• if exclude both, then subproblem is: 𝐿𝐶𝑆(𝑥1, 𝑥𝑖−1;; 𝑦1, … , 𝑦𝑗−1)
→ 𝐿[𝑖, 𝑗] = 𝐿[𝑖 − 1, 𝑗 − 1] + 1
→ 𝐋 𝐢, 𝐣 = max{L i, j − 1 , L i − 1, j , L i − 1, j − 1 + 1}
= 𝐋 𝐢 − 𝟏, 𝐣 − 𝟏 + 𝟏
LCS: DP Definition 2
45.
LCS: DP Definition2
• 1st : Design Subproblems in Words
• For i, j in [0, 𝑛], let 𝑳 𝒊, 𝒋 = length of LCS in x1, 𝑥2, … , 𝒙𝒊
and y1, 𝑦2, … , 𝒚𝒋
• 2nd : State Recursive Relations
• Express 𝐿 𝑖, 𝑗 in terms of 𝐿 0,0 , … , 𝐿 1, 1 , 𝐿 1, 2 , ….,
𝐿 𝑖 − 1, 𝑗 − 1 , L i − 1, j , … . , L i, j − 1
• for 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛:
• 𝐿 𝑖, 𝑗 = ൝
𝒎𝒂𝒙 𝑳 𝒊, 𝒋 − 𝟏 , 𝑳 𝒊 − 𝟏, 𝒋 𝒊𝒇 𝒙𝒊 ≠ 𝒚𝒋
𝑳 𝒊 − 𝟏, 𝒋 − 𝟏 + 𝟏 𝒊𝒇 𝒙𝒊 = 𝒚𝒋
• 𝐈𝐧𝐢𝐭𝐢𝐚𝐥 𝐒𝐭𝐚𝐭𝐞
• 𝐿 𝑖, 0 = 0 𝐿 0, 𝑗 = 0
46.
LCS: DP AlgorithmPseudocode
LCS(X, Y):
#### initial states
for 𝑖 = 0 𝑡𝑜 𝒏 𝑑𝑜
𝐿 𝑖, 0 = 0
end for
for 𝑗 = 0 𝑡𝑜 𝑛 𝑑𝑜:
𝐿 0, 𝑗 = 0
end for
#### recursive relations
for 𝑖 = 1 𝑡𝑜 𝒏 𝑑𝑜
for 𝑗 = 1 𝑡𝑜 𝑛 𝑑𝑜
if 𝑥𝑖 = 𝑦𝑗 then 𝐿 𝑖, 𝑗 = 𝐿 𝑖 − 1, 𝑗 − 1 + 1
else: 𝐿 𝑖, 𝑗 = 𝑚𝑎𝑥 𝐿 𝑖, 𝑗 − 1 , 𝐿 𝑖 − 1, 𝑗
end if
end for
end for
return 𝐿 𝒏, 𝑛
Time
complexity:
𝑂(𝑛2)
47.
LCS: DP AlgorithmPseudocode
LCS(X, Y):
#### initial states
for 𝑖 = 0 𝑡𝑜 𝒎 𝑑𝑜
𝐿 𝑖, 0 = 0
end for
for 𝑗 = 0 𝑡𝑜 𝑛 𝑑𝑜:
𝐿 0, 𝑗 = 0
end for
#### recursive relations
for 𝑖 = 1 𝑡𝑜 𝒎 𝑑𝑜
for 𝑗 = 1 𝑡𝑜 𝑛 𝑑𝑜
if 𝑥𝑖 = 𝑦𝑗 then 𝐿 𝑖, 𝑗 = 𝐿 𝑖 − 1, 𝑗 − 1 + 1
else: 𝐿 𝑖, 𝑗 = 𝑚𝑎𝑥 𝐿 𝑖, 𝑗 − 1 , 𝐿 𝑖 − 1, 𝑗
end if
end for
end for
return 𝐿 𝒎, 𝑛
Time
complexity:
𝑂(𝒎𝑛)
48.
LCS(X, Y):
#### initialstates
for 𝑖 = 0 𝑡𝑜 𝒎 𝑑𝑜
𝐿 𝑖, 0 = 0
end for
for 𝑗 = 0 𝑡𝑜 𝑛 𝑑𝑜:
𝐿 0, 𝑗 = 0
end for
#### recursive relations
for 𝑖 = 1 𝑡𝑜 𝒎 𝑑𝑜
for 𝑗 = 1 𝑡𝑜 𝑛 𝑑𝑜
if 𝑥𝑖 = 𝑦𝑗 then 𝐿 𝑖, 𝑗 = 𝐿 𝑖 − 1, 𝑗 − 1 + 1
else: 𝐿 𝑖, 𝑗 = 𝑚𝑎𝑥 𝐿 𝑖, 𝑗 − 1 , 𝐿 𝑖 − 1, 𝑗
end if
end for
end for
return 𝐿 𝒎, 𝑛
𝐐: Find LCS of X = A B C B D A B, Y = B D C A B A
𝒋 0 1 2 3 4 5 6
𝒊 𝒚𝒋 B D C A B A
0 𝒙𝒊
1 A
2 B
3 C
4 B
5 D
6 A
7 B
0 0 0 0 0 0
0
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)?
49.
DP Algorithm DefinitionSummary
• 1st : Define Subproblems in Words
a) Start with Prefix of current problem
b) Add constraints if needed, e.g, include last
elements in LIS
• 2nd: Express Recursive Relations
• 𝑇[𝑖] in terms of 𝑇 1 , … , 𝑇 𝑖 − 1 for ∀ valid 𝑖
• Define the Initial State
• T[1] or T[0]
50.
Practical Problems
DPV 6.1:Maximum Subarray Problem
• Input: A list of numbers a1, a2, … , an
• Goal: Find Subarray (contiguous subsequence) of maximum sum
→ Cannot determine if can append
ai (do not know what it ends), so
need to revise subproblems
And the substring includes 𝐚𝐢
→ 𝐒 𝐢 = 𝐚𝐢 + 𝐦𝐚𝐱{𝟎, 𝐒[𝐢 − 𝟏]}
𝐒 𝟎 = 𝟎 , 𝐒 𝟏 = 𝐚𝟏
• 1st : Define Subproblems in Words
S i in terms of S[0], S[1], …, S[i-1]
• 2nd: Express Recursive Relations
• Define the Initial State
For i in 0, n , let S i = max sum of subarry of a1, a2, … . , ai
Q: What is the output?
Q: Anyone can write the
pseudocode?
Q: [5, 15, -30, 10, -5, 40, 10] output?
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 𝑎1 < 𝑎2 < … < 𝑎𝑛,
where each 𝑎𝑖 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 𝑎𝑛), 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 𝑥
miles during a day, the penalty for that day is 𝟐𝟎𝟎 − 𝒙 𝟐.
• 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.
53.
Practical Problems
DPV 6.2:Hotel Stops
• Input: sequence of increasing positive numbers: 𝑎1 < 𝑎2 < … < 𝑎𝑛
• Output: subsequence of numbers with minimum sum of penalties &
must include 𝑎𝑛
• If you travel 𝑥 miles during a day, the penalty for that day is
200 − 𝑥 2
• 1st : Define Subproblems in Words
• 2nd: Express Recursive Relations
• 3rd: Define the Initial State
𝑃[𝑖] = minimum sum of penalities when stop at 𝑎𝑖
𝑃[𝑖] = Min
0≤ 𝑗≤𝑖−1
{𝑃 𝑗 + (200 − (𝑎𝑖−𝑎𝑗))2}
𝑃[0] = 0
Q: What is the output?
Q: Anyone can write the
pseudocode?
Q: [60, 150, 210, 300, 520, 700]
output?
Practical Problems
DPV 6.3:Yuck Donald’s Restaurant
• Yuckdonald’s is considering opening a series of restaurants
along Quaint Valley Highway (QVH).
The 𝒏 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, 𝑚1, 𝑚2, … , 𝑚𝑛.
• The constraints are as follows:
• At each location, Yuckdonald’s may open at most one
restaurant.
• The expected profit from opening a restaurant at location 𝑖 is
𝑝𝑖, where 𝑝𝑖 > 0, 𝑎𝑛𝑑 𝑖 = 1, 2, 3, … , 𝑛
• Any two restaurants should be at least 𝒌 miles apart, where 𝒌
is a positive integer.
• Give an efficient algorithm to compute the maximum expected
total profit subject to the given constraints.
56.
Practical Problems
• 1st: Define Subproblems in Words
• 2nd: Express Recursive Relations
• 3rd: Define the Initial State
𝑓𝑜𝑟 𝑖 𝑖𝑛 1, 𝑛 , 𝑃 𝑖
= maximum sum of profit when opening the last restaurant at 𝑚𝑖
𝑃 𝑖 = Max
1≤ 𝑗≤𝑖−1
and 𝑚𝑖−𝑚𝑗 ≥𝑘
𝑃 𝑗 + 𝑝𝑖
(𝑃 1 = 𝑝1)
DPV 6.3: Yuck Donald’s Restaurant
• Input: n locations’ milestone: 𝑚1 < 𝑚2 < ⋯ < mn , with profit of each
location is : 𝑝1, 𝑝2, … , 𝑝𝑛.
• 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 𝑖 is 𝑝𝑖, where
𝑝𝑖 > 0, 𝑎𝑛𝑑 𝑖 = 1, 2, 3, … , 𝑛
• Any 2 restaurants should be at least 𝒌 miles apart (𝒌 is a positive integer).
Q: What is the output?
Q: Anyone can write the
pseudocode?
Practical Problems
DPV 6.4:String of Words
• You are given a string of 𝑛 characters 𝑛 𝑠[1 … 𝑛] , 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 𝑤,
dict 𝑤 = ቊ
𝑡𝑟𝑢𝑒 𝑖𝑓 𝑤 𝑖𝑠 𝑣𝑎𝑙𝑖𝑑 𝑤𝑜𝑟𝑑
𝑓𝑎𝑙𝑠𝑒 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• (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(𝑛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.
59.
Practical Problems
DPV 6.4:String of Words
• Input: A string of 𝑛 characters 𝑛 𝑠 1 … 𝑛
• Goal: Determines whether the string s[·
] can be reconstituted as a
sequence of valid words.
• 1st : Define Subproblems in Words
• 2nd: Express Recursive Relations
• 3rd: Define the Initial State
for 𝑖 in 1, 𝑛 , 𝑣 𝑖 = the first 𝑖 characters can be reconstituted as a
sequence of valid words
𝑣 𝑖 = 𝐎𝐑0≤𝑗<𝑖(vj ⋀ dict(substring(j, i)))
𝑣 0 = 𝑡𝑟𝑢𝑒
• 𝐎𝐑0≤𝑗<𝑖 𝑥𝑗 = 𝑥0⋁𝑥1⋁ 𝑥2⋁ … ⋁ 𝑥𝑖−1
• substring j, i is the substring of characters located in the place (𝑗, 𝑖]
Q: What is the output?
Q: Anyone can write
the pseudocode?
Practical Problems
DPV 6.11:Longest Common SubString (LCSS)
• Input: Given two strings 𝑥 = 𝑥1𝑥2 … . 𝑥𝑛 and 𝑦 = 𝑦1𝑦2 … . 𝑦𝑚,
• Goal: Find the length of their Longest Common SubString (LCSS)
• Show how to do this in time O(mn).
• 1st : Define Subproblems in Words
• 2nd: Express Recursive Relations • 3rd: Define the
Initial State
For i, j in [0, 𝑛], let 𝑳 𝒊, 𝒋 = length of LCSS in x1, 𝑥2, … , 𝒙𝒊
and y1, 𝑦2, … , 𝒚𝒋, and LCCS include 𝒙𝒊 and 𝒚𝒋
Express 𝐿 𝑖, 𝑗 in terms of 𝐿 0,0 , … , 𝐿 1, 1 , 𝐿 1, 2 , ….,
𝐿 𝑖 − 1, 𝑗 − 1 , L i − 1, j , … . , L i, j − 1
2 cases: 𝐱𝐢 ≠ 𝐲𝒋 or 𝒙𝒊 = 𝒚𝒋
for i ≥ 1, j ≥ 1:
𝐿 𝑖, 𝑗 = ൝
𝟎 𝒊𝒇 𝒙𝒊 ≠ 𝐲𝒋
𝐋 𝐢 − 𝟏, 𝐣 − 𝟏 + 𝟏 𝒊𝒇 𝐱𝐢 = 𝐲𝐣
𝐿 𝑖, 0 = 0
𝐿 0, 𝑗 = 0
Q: What is the output?
Q: Anyone can write
the pseudocode?
62.
LCSS(X, Y):
For 𝑖𝑖𝑛 0, 𝒎 :
𝐿 𝑖, 0 = 0
For 𝑗 𝑖𝑛 0, 𝑛 :
𝐿 0, 𝑗 = 0
For 𝑖 𝑖𝑛 1, 𝒎 :
For 𝑗 𝑖𝑛 1, 𝑛 :
if 𝒙𝒊 = 𝒚𝒋 then 𝐿 𝑖, 𝑗 = 𝑳 𝒊 − 𝟏, 𝒋 − 𝟏 + 𝟏
else: 𝐿 𝑖, 𝑗 = 𝟎
Return 𝑚𝑎𝑥{𝐿 𝒎, 𝑛 }
DPV 6.11: Longest Common SubString (LCSS)
63.
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 𝑖 = 1, 2, 3, … , 𝑛, the price 𝒑𝒊
in dollars that Serling Enterprises charges for a rod of
length 𝒊 inches. Rod lengths are always an integral
number of inches.
Ref: T.Cormen, C. Leiserson, R. Rivest, & C. Stein. Introduction to Algorithm 3rd
Edition. ISBN 978-0-262-03384-8
64.
Rod-Cutting Problem:
• Input:Given a rod of length 𝑛 inches and a table of prices 𝑝𝑖 for 𝑖 =
1, 2, 3, … , 𝑛,
• Goal: Determine the maximum revenue 𝑟𝑛 obtainable by cutting up
the rod and selling the pieces.
• Note: if the price 𝑝𝑛 for a rod of length 𝑛 is large enough, an
optimal solution may require no cutting at all.
• 1st : Define Subproblems in Words
• 2nd: Express Recursive Relations
• 3rd: Define the Initial State
For 𝑖 in [1, 𝑛], let 𝑅 𝒊 = maximum revenue obtainable by cutting up a rod
of length 𝑖 and selling the pieces.
𝑅 𝒊 = 𝒎𝒂𝒙 𝑝𝑖, 𝑅 𝒊 − 𝟏 + 𝑹 𝟏 , 𝑅 𝒊 − 𝟐 + 𝑹 𝟐 , … , 𝑅 𝒊 + 𝑹 𝒊 − 𝟏
= 𝒎𝒂𝒙𝟏≤𝒋<𝒊{𝒑𝒊, 𝑅 𝒊 − 𝒋 + 𝑹 𝒋 }
𝑅 1 = 𝑝1
Rewrite 𝑅 𝒊 in terms of 𝑅 𝟎 , 𝑅 𝟏 , … , 𝑅 𝒊 − 𝟏
65.
Rod_Cut(p, n):
𝑅 1= 𝑝1
for 𝑖 in 2, 𝑛 :
𝑞 = 𝒑𝒊 ## price of i-inch rod
for j in 𝟏, 𝒊 − 𝟏 :
𝑞 = 𝒎𝒂𝒙 {𝑞, R 𝒊 − 𝒋 + 𝑹 𝒋 }
𝑅 𝑖 = 𝑞
Return 𝑅 𝑛
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?