Design and
Analysis of
Algorithms
DYNAMIC PROGRAMMING
PART II
Maximum Value Contiguous Subarray
Maximum Increasing Subsequence
Coin Change
Algorithms Dynamic Programming - Part II 1
 Instructor
Prof. Amrinder Arora
amrinder@gwu.edu
Please copy TA on emails
Please feel free to call as well

 Available for study sessions
Science and Engineering Hall
GWU
Algorithms Dynamic Programming - Part II 2
LOGISTICS
Algorithms
Analysis
Asymptotic
NP-
Completeness
Design
D&C
Greedy
DP
Graph
B&B
Applications
Algorithms Dynamic Programming - Part II 3
WHERE WE ARE
 Done
 Done
 Finishing today..
 Done
 http://video.google.com/videoplay?docid=16073867
32227802988#
 http://people.csail.mit.edu/bdean/6.046/dp/
Algorithms Dynamic Programming - Part II 4
DP (CONT.)
 Given an Array A(1..n)
 To find A(i..j) such that the sum of the terms in the
subarray is maximized.
 Insights
 If no negative terms in the array, then of course, we just select
the entire array.
 So, this problem is meaningful only when we have negative
numbers
 We can find sum of all contiguous subarrays in O(n3) time.
 Can we do better?
Algorithms Dynamic Programming - Part II 5
MAXIMUM VALUE CONTIGUOUS
SUBSEQUENCE
MaxValue = - infinity
for i = 1 to n {
for j = i to n {
currSum = findSubArraySum(i,j)
if CurrSum > MaxValue, then MaxValue = CurrSum
}
}
Procedure FindSubArraySum(i,j)
Double sum = 0
For int k = i to j {
sum = sum + A[k]
}
return sum
}
Algorithms Dynamic Programming - Part II 6
ALGORITHM MVCS1
Brute Force
Search!
O(n3) time
Procedure InitSubArraySums() {
for int i = 1 to n {
double sum = 0
for int k = i to n {
sum = sum + A(k)
SubArraySum[i][k] = sum
}
}
}
InitSubArraySums();
MaxValue = - infinity
for i = 1 to n {
for j = i to n {
currSum = SubArraySum[i][j]
If currSum > MaxValue, then MaxValue = currSum
}
}
Algorithms Dynamic Programming - Part II 7
ALGORITHM MVCS2
Still a full search,
but don’t
recompute sums
O(n2) time
 What can be a greedy variation of MVCS algorithm?
 What can be a D&C version of MVCS algorithm?
 How about Dynamic Programming?
Algorithms Dynamic Programming - Part II 8
USING OTHER TECHNIQUES FOR MVCS
Using Dynamic Programming Template
1. N: MVCS(i): value of MVCS ending at position i.
2. O: Prove Optimal Substructure Holds (What is our
claim?)
3. R: MVCS(i) = max {MVCS(i-1) + A[i],A[i]}
4. A: Start from left, and keep track of maximum
MVCS(i) encountered.
1. For I = 1 to n
Algorithms Dynamic Programming - Part II 9
MVCS3
O(n) time
 Using the Array used in class
 A[]: 22, -17, -71, 12, -22, 81, -10, 63
 MVCS[]: 22, 5, -66, 12, -10, 81, 71, 134
Algorithms Dynamic Programming - Part II 10
SAMPLE RUN OF MVCS3
 Given array A(1..n)
 Find a subsequence (not necessarily contiguous) that
is strictly increasing.
 For example
 {1, 7, 2, 8, 4, 1, 6, 11, 3, 15, 5, 12, 14}
Algorithms Dynamic Programming - Part II 11
LONGEST INCREASING SUBSEQUENCE
Longest subsequence is of length 7:
{1, .., 2, .., 4, .., 6, 11, .., .., .., 12, 14}
 Given array A(1..n)
 LIS(i) = longest strictly increasing subsequence that
ends at i.
 LIS(i) = max {LIS(j)} + 1, such that j < i, and A[j] <
A[i]
[Or A[j] ≤ A[i] if we are looking for that condition]
Algorithms Dynamic Programming - Part II 12
LONGEST INCREASING SUBSEQUENCE
 What is the time complexity of this algorithm?
Algorithms Dynamic Programming - Part II 13
LIS
 Using the Array used in Class
 {1, 7, 2, 8, 4, 1, 6, 11, 3, 15, 5, 12, 14}
 {1, 2, 2, 3, 3, 1, 4, 5, 3, 6, 4, 6, 7}
 What are the LIS values?
Algorithms Dynamic Programming - Part II 14
SAMPLE RUN OF LIS
 Given a set of coins with values v1, v2, … vn such that
v1 = 1, v1 < v2 < v3 < v4 < … < vn
 We want to make change for a given value S using as
few coins as possible
Algorithms Dynamic Programming - Part II 15
COIN CHANGE
 http://people.csail.mit.edu/bdean/6.046/dp/
Algorithms Dynamic Programming - Part II 16
AN INTERESTING SET OF PROBLEMS
Algorithms Dynamic Programming - Part II 17
80% OF
SUCCESS IS
SHOWING UP.
Algorithms Dynamic Programming - Part II 18
REST 80% OF
SUCCESS IS
BEING
PRESENT!
CS 6212
Analysis
Asymptotic
NP-
Completeness
Design
D&C
Greedy
DP
Graph
B&B
Applications
Algorithms Dynamic Programming - Part II 19
WHERE WE ARE
 Done
 Done
 Done
 Done

Dynamic Programming - Part II

  • 1.
    Design and Analysis of Algorithms DYNAMICPROGRAMMING PART II Maximum Value Contiguous Subarray Maximum Increasing Subsequence Coin Change Algorithms Dynamic Programming - Part II 1
  • 2.
     Instructor Prof. AmrinderArora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well   Available for study sessions Science and Engineering Hall GWU Algorithms Dynamic Programming - Part II 2 LOGISTICS
  • 3.
  • 4.
  • 5.
     Given anArray A(1..n)  To find A(i..j) such that the sum of the terms in the subarray is maximized.  Insights  If no negative terms in the array, then of course, we just select the entire array.  So, this problem is meaningful only when we have negative numbers  We can find sum of all contiguous subarrays in O(n3) time.  Can we do better? Algorithms Dynamic Programming - Part II 5 MAXIMUM VALUE CONTIGUOUS SUBSEQUENCE
  • 6.
    MaxValue = -infinity for i = 1 to n { for j = i to n { currSum = findSubArraySum(i,j) if CurrSum > MaxValue, then MaxValue = CurrSum } } Procedure FindSubArraySum(i,j) Double sum = 0 For int k = i to j { sum = sum + A[k] } return sum } Algorithms Dynamic Programming - Part II 6 ALGORITHM MVCS1 Brute Force Search! O(n3) time
  • 7.
    Procedure InitSubArraySums() { forint i = 1 to n { double sum = 0 for int k = i to n { sum = sum + A(k) SubArraySum[i][k] = sum } } } InitSubArraySums(); MaxValue = - infinity for i = 1 to n { for j = i to n { currSum = SubArraySum[i][j] If currSum > MaxValue, then MaxValue = currSum } } Algorithms Dynamic Programming - Part II 7 ALGORITHM MVCS2 Still a full search, but don’t recompute sums O(n2) time
  • 8.
     What canbe a greedy variation of MVCS algorithm?  What can be a D&C version of MVCS algorithm?  How about Dynamic Programming? Algorithms Dynamic Programming - Part II 8 USING OTHER TECHNIQUES FOR MVCS
  • 9.
    Using Dynamic ProgrammingTemplate 1. N: MVCS(i): value of MVCS ending at position i. 2. O: Prove Optimal Substructure Holds (What is our claim?) 3. R: MVCS(i) = max {MVCS(i-1) + A[i],A[i]} 4. A: Start from left, and keep track of maximum MVCS(i) encountered. 1. For I = 1 to n Algorithms Dynamic Programming - Part II 9 MVCS3 O(n) time
  • 10.
     Using theArray used in class  A[]: 22, -17, -71, 12, -22, 81, -10, 63  MVCS[]: 22, 5, -66, 12, -10, 81, 71, 134 Algorithms Dynamic Programming - Part II 10 SAMPLE RUN OF MVCS3
  • 11.
     Given arrayA(1..n)  Find a subsequence (not necessarily contiguous) that is strictly increasing.  For example  {1, 7, 2, 8, 4, 1, 6, 11, 3, 15, 5, 12, 14} Algorithms Dynamic Programming - Part II 11 LONGEST INCREASING SUBSEQUENCE Longest subsequence is of length 7: {1, .., 2, .., 4, .., 6, 11, .., .., .., 12, 14}
  • 12.
     Given arrayA(1..n)  LIS(i) = longest strictly increasing subsequence that ends at i.  LIS(i) = max {LIS(j)} + 1, such that j < i, and A[j] < A[i] [Or A[j] ≤ A[i] if we are looking for that condition] Algorithms Dynamic Programming - Part II 12 LONGEST INCREASING SUBSEQUENCE
  • 13.
     What isthe time complexity of this algorithm? Algorithms Dynamic Programming - Part II 13 LIS
  • 14.
     Using theArray used in Class  {1, 7, 2, 8, 4, 1, 6, 11, 3, 15, 5, 12, 14}  {1, 2, 2, 3, 3, 1, 4, 5, 3, 6, 4, 6, 7}  What are the LIS values? Algorithms Dynamic Programming - Part II 14 SAMPLE RUN OF LIS
  • 15.
     Given aset of coins with values v1, v2, … vn such that v1 = 1, v1 < v2 < v3 < v4 < … < vn  We want to make change for a given value S using as few coins as possible Algorithms Dynamic Programming - Part II 15 COIN CHANGE
  • 16.
     http://people.csail.mit.edu/bdean/6.046/dp/ Algorithms DynamicProgramming - Part II 16 AN INTERESTING SET OF PROBLEMS
  • 17.
    Algorithms Dynamic Programming- Part II 17 80% OF SUCCESS IS SHOWING UP.
  • 18.
    Algorithms Dynamic Programming- Part II 18 REST 80% OF SUCCESS IS BEING PRESENT!
  • 19.
    CS 6212 Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms DynamicProgramming - Part II 19 WHERE WE ARE  Done  Done  Done  Done