SlideShare a Scribd company logo
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

More Related Content

PPTX
Dynamic Programming - Part 1
PPTX
NP-Completeness - II
PPTX
Graph Traversal Algorithms - Breadth First Search
PPT
5.3 dynamic programming 03
PDF
01. design & analysis of agorithm intro & complexity analysis
PPT
Analysis of Algorithm
PPTX
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
PPTX
Greedy Algorithms
Dynamic Programming - Part 1
NP-Completeness - II
Graph Traversal Algorithms - Breadth First Search
5.3 dynamic programming 03
01. design & analysis of agorithm intro & complexity analysis
Analysis of Algorithm
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Greedy Algorithms

What's hot (20)

DOC
Unit 3 daa
PPT
Lecture 8 dynamic programming
PPTX
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
PDF
PPTX
Dynamic programming - fundamentals review
PDF
Backtracking & branch and bound
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPTX
unit-4-dynamic programming
PDF
Dynamic programming
PDF
Skiena algorithm 2007 lecture16 introduction to dynamic programming
PPTX
Design and Analysis of Algorithms
PDF
Branch and bound technique
PPTX
PDF
Design & Analysis Of Algorithm
PPTX
Asymptotic Notation and Data Structures
PPTX
Introduction to Algorithms and Asymptotic Notation
RTF
Design and Analysis of algorithms
PDF
design and analysis of algorithm
DOC
algorithm Unit 3
Unit 3 daa
Lecture 8 dynamic programming
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Dynamic programming - fundamentals review
Backtracking & branch and bound
DESIGN AND ANALYSIS OF ALGORITHMS
unit-4-dynamic programming
Dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Design and Analysis of Algorithms
Branch and bound technique
Design & Analysis Of Algorithm
Asymptotic Notation and Data Structures
Introduction to Algorithms and Asymptotic Notation
Design and Analysis of algorithms
design and analysis of algorithm
algorithm Unit 3
Ad

Similar to Dynamic Programming - Part II (20)

PPTX
Design and Analysis of Algorithm-Lecture.pptx
PPTX
Longest increasing subsequence
PPTX
Dynamic programmng2
PPTX
Dynamic Programing.pptx good for understanding
PPTX
Module 2ppt.pptx divid and conquer method
PPTX
Introduction to dynamic programming
PPT
Dynamic programming
PPTX
Algorithm Design Technique
PDF
Dynamic Programming
PPTX
8_dynamic_algorithm powerpoint ptesentation.pptx
PPTX
Chapter 5.pptx
PPT
Dynamic pgmming
PPT
Learn about dynamic programming and how to design algorith
PPTX
AAC ch 3 Advance strategies (Dynamic Programming).pptx
PDF
Unit 4 of design and analysis of algorithms
PDF
L21_L27_Unit_5_Dynamic_Programming Computer Science
PPTX
week 9 lec 15.pptx
PPTX
The Volcano/Cascades Optimizer
PPTX
Annotaed slides for dynamic programming algorithm
PPT
9 - DynamicProgramming-plus latihan.ppt
Design and Analysis of Algorithm-Lecture.pptx
Longest increasing subsequence
Dynamic programmng2
Dynamic Programing.pptx good for understanding
Module 2ppt.pptx divid and conquer method
Introduction to dynamic programming
Dynamic programming
Algorithm Design Technique
Dynamic Programming
8_dynamic_algorithm powerpoint ptesentation.pptx
Chapter 5.pptx
Dynamic pgmming
Learn about dynamic programming and how to design algorith
AAC ch 3 Advance strategies (Dynamic Programming).pptx
Unit 4 of design and analysis of algorithms
L21_L27_Unit_5_Dynamic_Programming Computer Science
week 9 lec 15.pptx
The Volcano/Cascades Optimizer
Annotaed slides for dynamic programming algorithm
9 - DynamicProgramming-plus latihan.ppt
Ad

More from Amrinder Arora (20)

PPTX
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
PDF
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
PDF
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
PDF
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
PDF
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
PDF
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
PPTX
Online algorithms in Machine Learning
PPTX
NP completeness
PPTX
Algorithmic Puzzles
PPTX
Divide and Conquer - Part 1
PPTX
Set Operations - Union Find and Bloom Filters
PPTX
Binomial Heaps and Fibonacci Heaps
PPTX
R-Trees and Geospatial Data Structures
PPTX
Tries - Tree Based Structures for Strings
PPTX
Splay Trees and Self Organizing Data Structures
PPTX
BTrees - Great alternative to Red Black, AVL and other BSTs
PPTX
Binary Search Trees - AVL and Red Black
PPTX
Graphs, Trees, Paths and Their Representations
PPTX
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Graph Traversal Algorithms - Depth First Search Traversal
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Online algorithms in Machine Learning
NP completeness
Algorithmic Puzzles
Divide and Conquer - Part 1
Set Operations - Union Find and Bloom Filters
Binomial Heaps and Fibonacci Heaps
R-Trees and Geospatial Data Structures
Tries - Tree Based Structures for Strings
Splay Trees and Self Organizing Data Structures
BTrees - Great alternative to Red Black, AVL and other BSTs
Binary Search Trees - AVL and Red Black
Graphs, Trees, Paths and Their Representations
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Advanced IT Governance
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Electronic commerce courselecture one. Pdf
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Modernizing your data center with Dell and AMD
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Advanced Soft Computing BINUS July 2025.pdf
NewMind AI Monthly Chronicles - July 2025
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Advanced IT Governance
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Review of recent advances in non-invasive hemoglobin estimation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Reach Out and Touch Someone: Haptics and Empathic Computing
Electronic commerce courselecture one. Pdf
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Modernizing your data center with Dell and AMD
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Advanced Soft Computing BINUS July 2025.pdf

Dynamic Programming - Part II

  • 1. Design and Analysis of Algorithms DYNAMIC PROGRAMMING PART II Maximum Value Contiguous Subarray Maximum Increasing Subsequence Coin Change Algorithms Dynamic Programming - Part II 1
  • 2.  Instructor Prof. Amrinder Arora [email protected] 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
  • 5.  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
  • 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() { 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
  • 8.  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
  • 9. 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
  • 10.  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
  • 11.  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}
  • 12.  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
  • 13.  What is the time complexity of this algorithm? Algorithms Dynamic Programming - Part II 13 LIS
  • 14.  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
  • 15.  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
  • 16.  http://people.csail.mit.edu/bdean/6.046/dp/ Algorithms Dynamic Programming - 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 Dynamic Programming - Part II 19 WHERE WE ARE  Done  Done  Done  Done