SlideShare a Scribd company logo
1 of 20
Download to read offline
Lecture 18:
Applications of Dynamic Programming
            Steven Skiena

   Department of Computer Science
    State University of New York
    Stony Brook, NY 11794–4400

   http://www.cs.sunysb.edu/∼skiena
Problem of the Day
A certain string processing language allows the programmer
to break a string into two pieces. Since this involves copying
the old string, it costs n units of time to break a string of n
characters into two pieces.
Suppose a programmer wants to break a string into many
pieces. The order in which the breaks are made can affect
the total amount of time used.
For example suppose we wish to break a 20 character string
after characters 3,8, and 10:
 • If the breaks are made in left-right order, then the first
   break costs 20 units of time, the second break costs 17
   units of time and the third break costs 12 units of time, a
total of 49 steps.
 • If the breaks are made in right-left order, the first break
   costs 20 units of time, the second break costs 10 units of
   time, and the third break costs 8 units of time, a total of
   only 38 steps.
Give a dynamic programming algorithm that, given the list
of character positions after which to break, determines the
cheapest break cost in O(n3 ) time.
Dynamic Programming and High Density Bar
Codes
Symbol Technology has developed a new design for bar
codes, PDF-417 that has a capacity of several hundred bytes.
What is the best way to encode text for this design?




They developed a complicated mode-switching data com-
pression scheme.
ALPHA

                                                          latch
                          latch
                                                  latch
                                  latch shift

                      LOWER                                 MIXED
                                                latch
                      CASE

                                                  latch
                          shift
                                                            shift

                                   OTHER
                                   CHARS




Latch commands permanently put you in a different mode.
Shift commands temporarily put you in a different mode.
Symbol used a greedy algorithm to encode a string, making
local decisions only. But we realized that for any prefix, you
want an optimal encoding which might leave you in every
possible mode.
The Quick Brown Fox

            Alpha
            Lower
                            X
            Mixed
            Punct.




M [i, j] = min(M [i − 1, k]+ the cost of encoding the ith
character and ending up in node j).
Our simple dynamic programming algorithm improved the
capacity of PDF-417 by an average of 8%!
Dividing the Work
Suppose the job scanning through a shelf of books is to be
split between k workers. To avoid the need to rearrange the
books or separate them into piles, we can divide the shelf into
k regions and assign each region to one worker.
What is the fairest way to divide the shelf up?
If each book is the same length, partition the books into equal-
sized regions,
           100 100 100 | 100 100 100 | 100 100 100
But what if the books are not the same length? This partition
would yield
           100 200 300 | 400 500 600 | 700 800 900
Which part of the job would you volunteer to do?
How can we find the fairest possible partition, i.e.
           100 200 300 400 500 | 600 700 | 800 900
The Linear Partition Problem
Input: A given arrangement S of nonnegative numbers
{s1, . . . , sn} and an integer k.
Problem: Partition S into k ranges, so as to minimize the
maximum sum over all the ranges.
Does fixed partition positions always work?
Does taking the average value of a part n si/k from the left
                                        i=1
always work?
Recursive Idea
Consider a recursive, exhaustive search approach. Notice that
the kth partition starts right after we placed the (k − 1)st
divider.
Where can we place this last divider? Between the ith and
(i + 1)st elements for some i, where 1 ≤ i ≤ n.
What is the cost of this? The total cost will be the larger of
two quantities, (1) the cost of the last partition n j=i+1 sj and
(2) the cost of the largest partition cost formed to the left of i.
What is the size of this left partition? To partition the
elements {s1, . . . , si} as equally as possible. But this is a
smaller instance of the same problem!
Dynamic Programming Recurrence
Define M [n, k] to be the minimum possible cost over all
partitionings of {s1, . . . , sn} into k ranges, where the cost of
a partition is the largest sum of elements in one of its parts.
Thus defined, this function can be evaluated:
                        n                        n
          M [n, k] = min max(M [i, k − 1],              sj )
                       i=1                      j=i+1

with the natural basis cases of
               M [1, k] = s1, for all k > 0 and,
                                     n
                        M [n, 1] =         si
                                     i=1
What is the running time?
It is the number of cells times the running time per cell.
A total of k · n cells exist in the table.
Each cell depends on n others, and can be computed in linear
time, for a total of O(kn2 ).
Implementation
To evaluate this efficiently, we must make sure we do the
smaller cases before the larger cases that depend upon them.
Partition[S, k]
       (* compute prefix sums: p[k] = k si *)
                                          i=1
       p[0] = 0
       for i = 1 to n do p[i] = p[i − 1] + si

      (* initialize boundary conditions *)
      for i = 1 to n do M [i, 1] = p[i]
      for i = 1 to k do M [1, j] = s1

      (* evaluate main recurrence *)
for i = 2 to n do
       for j = 2 to k do
             M [i, j] = ∞
             for x = 1 to i − 1 do
                    s = max(M [x, j − 1], p[i] − p[x])
                    if (M [i, j] > s) then
                           M [i, j] = s
                           D[i, j] = x
DP Matrices
For the input {1, 2, 3, 4, 5, 6, 7, 8, 9}
M         k              D       k
n    1    2    3         n   1   2   3
1    1    1    1         1   –   –   –
2    3    2    2         2   –   1   1
3    6    3    3         3   –   2   2
4   10    6    4         4   –   3   3
5   15    9    6         5   –   3   4
6   21   11    9         6   –   4   5
7   28   15   11         7   –   5   6
8   36   21   15         8   –   5   6
9   45   24   17         9   –   6   7
DP Matrix: Uniform Series
For the input {1, 1, 1, 1, 1, 1, 1, 1, 1}
M       k              D       k
n   1   2   3          n   1   2   3
1   1   1   1          1   –   –   –
1   2   1   1          1   –   1   1
1   3   2   1          1   –   1   2
1   4   2   2          1   –   2   2
1   5   3   2          1   –   2   3
1   6   3   2          1   –   3   4
1   7   4   3          1   –   3   4
1   8   4   3          1   –   4   5
1   9   5   3          1   –   4   6
When can you use Dynamic Programming?
Dynamic programming computes recurrences efficiently by
storing partial results. Thus dynamic programming can only
be efficient when there are not too many partial results to
compute!
There are n! permutations of an n-element set – we cannot
use dynamic programming to store the best solution for each
subpermutation. There are 2n subsets of an n-element set
– we cannot use dynamic programming to store the best
solution for each.
However, there are only n(n − 1)/2 continguous substrings of
a string, each described by a starting and ending point, so we
can use it for string problems.
There are only n(n−1)/2 possible subtrees of a binary search
tree, each described by a maximum and minimum key, so we
can use it for optimizing binary search trees.
Dynamic programming works best on objects which are
linearly ordered and cannot be rearranged – characters in
a string, matrices in a chain, points around the boundary of
a polygon, the left-to-right order of leaves in a search tree.
Whenever your objects are ordered in a left-to-right way, you
should smell dynamic programming!
The Principle of Optimality
To use dynamic programming, the problem must observe
the principle of optimality, that whatever the initial state is,
remaining decisions must be optimal with regard the state
following from the first decision.
Combinatorial problems may have this property but may use
too much memory/time to be efficient.
Example: The Traveling Salesman Problem
Let T (i; j1, j2, . . . , jk ) be the cost of the optimal tour for i to
1 that goes thru each of the other cities once
T (i; i1, j2, . . . , ji) = M in1≤m≤k C[i, jm ] + T (jm ; j1, j2, . . . , jk )

                     T (i, j) = C(i, j) + C(j, 1)
Here there can be any subset of j1, j2, . . . , jk instead of any
subinterval - hence exponential.

More Related Content

What's hot

Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...
Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...
Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...Edureka!
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)NYversity
 
(研究会輪読) Weight Uncertainty in Neural Networks
(研究会輪読) Weight Uncertainty in Neural Networks(研究会輪読) Weight Uncertainty in Neural Networks
(研究会輪読) Weight Uncertainty in Neural NetworksMasahiro Suzuki
 
K-means, EM and Mixture models
K-means, EM and Mixture modelsK-means, EM and Mixture models
K-means, EM and Mixture modelsVu Pham
 
머피의 머신러닝: 17장 Markov Chain and HMM
머피의 머신러닝: 17장  Markov Chain and HMM머피의 머신러닝: 17장  Markov Chain and HMM
머피의 머신러닝: 17장 Markov Chain and HMMJungkyu Lee
 
Stochastic Alternating Direction Method of Multipliers
Stochastic Alternating Direction Method of MultipliersStochastic Alternating Direction Method of Multipliers
Stochastic Alternating Direction Method of MultipliersTaiji Suzuki
 
Spectral clustering Tutorial
Spectral clustering TutorialSpectral clustering Tutorial
Spectral clustering TutorialZitao Liu
 
(DL hacks輪読)Bayesian Neural Network
(DL hacks輪読)Bayesian Neural Network(DL hacks輪読)Bayesian Neural Network
(DL hacks輪読)Bayesian Neural NetworkMasahiro Suzuki
 
Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
Show, Attend and Tell: Neural Image Caption Generation with Visual AttentionShow, Attend and Tell: Neural Image Caption Generation with Visual Attention
Show, Attend and Tell: Neural Image Caption Generation with Visual AttentionEun Ji Lee
 
The world of loss function
The world of loss functionThe world of loss function
The world of loss function홍배 김
 
Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)
Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)
Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)Universitat Politècnica de Catalunya
 
Machine Learning for Trading
Machine Learning for TradingMachine Learning for Trading
Machine Learning for TradingLarry Guo
 
Jensen's inequality, EM 알고리즘
Jensen's inequality, EM 알고리즘 Jensen's inequality, EM 알고리즘
Jensen's inequality, EM 알고리즘 Jungkyu Lee
 
Variational Autoencoders For Image Generation
Variational Autoencoders For Image GenerationVariational Autoencoders For Image Generation
Variational Autoencoders For Image GenerationJason Anderson
 
Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...
Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...
Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...Universitat Politècnica de Catalunya
 
Detailed Description on Cross Entropy Loss Function
Detailed Description on Cross Entropy Loss FunctionDetailed Description on Cross Entropy Loss Function
Detailed Description on Cross Entropy Loss Function범준 김
 
Hands-on Tutorial of Machine Learning in Python
Hands-on Tutorial of Machine Learning in PythonHands-on Tutorial of Machine Learning in Python
Hands-on Tutorial of Machine Learning in PythonChun-Ming Chang
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 

What's hot (20)

Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...
Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...
Restricted Boltzmann Machine | Neural Network Tutorial | Deep Learning Tutori...
 
Machine Learning 1
Machine Learning 1Machine Learning 1
Machine Learning 1
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)
 
K-means and GMM
K-means and GMMK-means and GMM
K-means and GMM
 
(研究会輪読) Weight Uncertainty in Neural Networks
(研究会輪読) Weight Uncertainty in Neural Networks(研究会輪読) Weight Uncertainty in Neural Networks
(研究会輪読) Weight Uncertainty in Neural Networks
 
K-means, EM and Mixture models
K-means, EM and Mixture modelsK-means, EM and Mixture models
K-means, EM and Mixture models
 
머피의 머신러닝: 17장 Markov Chain and HMM
머피의 머신러닝: 17장  Markov Chain and HMM머피의 머신러닝: 17장  Markov Chain and HMM
머피의 머신러닝: 17장 Markov Chain and HMM
 
Stochastic Alternating Direction Method of Multipliers
Stochastic Alternating Direction Method of MultipliersStochastic Alternating Direction Method of Multipliers
Stochastic Alternating Direction Method of Multipliers
 
Spectral clustering Tutorial
Spectral clustering TutorialSpectral clustering Tutorial
Spectral clustering Tutorial
 
(DL hacks輪読)Bayesian Neural Network
(DL hacks輪読)Bayesian Neural Network(DL hacks輪読)Bayesian Neural Network
(DL hacks輪読)Bayesian Neural Network
 
Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
Show, Attend and Tell: Neural Image Caption Generation with Visual AttentionShow, Attend and Tell: Neural Image Caption Generation with Visual Attention
Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
 
The world of loss function
The world of loss functionThe world of loss function
The world of loss function
 
Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)
Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)
Backpropagation (DLAI D3L1 2017 UPC Deep Learning for Artificial Intelligence)
 
Machine Learning for Trading
Machine Learning for TradingMachine Learning for Trading
Machine Learning for Trading
 
Jensen's inequality, EM 알고리즘
Jensen's inequality, EM 알고리즘 Jensen's inequality, EM 알고리즘
Jensen's inequality, EM 알고리즘
 
Variational Autoencoders For Image Generation
Variational Autoencoders For Image GenerationVariational Autoencoders For Image Generation
Variational Autoencoders For Image Generation
 
Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...
Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...
Deep Generative Models I (DLAI D9L2 2017 UPC Deep Learning for Artificial Int...
 
Detailed Description on Cross Entropy Loss Function
Detailed Description on Cross Entropy Loss FunctionDetailed Description on Cross Entropy Loss Function
Detailed Description on Cross Entropy Loss Function
 
Hands-on Tutorial of Machine Learning in Python
Hands-on Tutorial of Machine Learning in PythonHands-on Tutorial of Machine Learning in Python
Hands-on Tutorial of Machine Learning in Python
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 

Similar to Skiena algorithm 2007 lecture18 application of dynamic programming

Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingzukun
 
Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02mansab MIRZA
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video searchzukun
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingGopi Saiteja
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysischidabdu
 
PRAM algorithms from deepika
PRAM algorithms from deepikaPRAM algorithms from deepika
PRAM algorithms from deepikaguest1f4fb3
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103Sure Interview
 
Skiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distanceSkiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distancezukun
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptxSunilWork1
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingzukun
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 
Public Key Cryptography
Public Key CryptographyPublic Key Cryptography
Public Key CryptographyAbhijit Mondal
 
Attention is all you need (UPC Reading Group 2018, by Santi Pascual)
Attention is all you need (UPC Reading Group 2018, by Santi Pascual)Attention is all you need (UPC Reading Group 2018, by Santi Pascual)
Attention is all you need (UPC Reading Group 2018, by Santi Pascual)Universitat Politècnica de Catalunya
 

Similar to Skiena algorithm 2007 lecture18 application of dynamic programming (20)

Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video search
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
PRAM algorithms from deepika
PRAM algorithms from deepikaPRAM algorithms from deepika
PRAM algorithms from deepika
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
Skiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distanceSkiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distance
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptx
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Lec 2-2
Lec 2-2Lec 2-2
Lec 2-2
 
Public Key Cryptography
Public Key CryptographyPublic Key Cryptography
Public Key Cryptography
 
Attention is all you need (UPC Reading Group 2018, by Santi Pascual)
Attention is all you need (UPC Reading Group 2018, by Santi Pascual)Attention is all you need (UPC Reading Group 2018, by Santi Pascual)
Attention is all you need (UPC Reading Group 2018, by Santi Pascual)
 

More from zukun

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009zukun
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVzukun
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Informationzukun
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statisticszukun
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibrationzukun
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionzukun
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluationzukun
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-softwarezukun
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptorszukun
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectorszukun
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-introzukun
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video searchzukun
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video searchzukun
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learningzukun
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionzukun
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick startzukun
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysiszukun
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structureszukun
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities zukun
 
Icml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant featuresIcml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant featureszukun
 

More from zukun (20)

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCV
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Information
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statistics
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibration
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer vision
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluation
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-software
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptors
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectors
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-intro
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video search
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video search
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learning
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer vision
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick start
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysis
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structures
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities
 
Icml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant featuresIcml2012 learning hierarchies of invariant features
Icml2012 learning hierarchies of invariant features
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

Skiena algorithm 2007 lecture18 application of dynamic programming

  • 1. Lecture 18: Applications of Dynamic Programming Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/∼skiena
  • 2. Problem of the Day A certain string processing language allows the programmer to break a string into two pieces. Since this involves copying the old string, it costs n units of time to break a string of n characters into two pieces. Suppose a programmer wants to break a string into many pieces. The order in which the breaks are made can affect the total amount of time used. For example suppose we wish to break a 20 character string after characters 3,8, and 10: • If the breaks are made in left-right order, then the first break costs 20 units of time, the second break costs 17 units of time and the third break costs 12 units of time, a
  • 3. total of 49 steps. • If the breaks are made in right-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, a total of only 38 steps. Give a dynamic programming algorithm that, given the list of character positions after which to break, determines the cheapest break cost in O(n3 ) time.
  • 4. Dynamic Programming and High Density Bar Codes Symbol Technology has developed a new design for bar codes, PDF-417 that has a capacity of several hundred bytes. What is the best way to encode text for this design? They developed a complicated mode-switching data com- pression scheme.
  • 5. ALPHA latch latch latch latch shift LOWER MIXED latch CASE latch shift shift OTHER CHARS Latch commands permanently put you in a different mode. Shift commands temporarily put you in a different mode. Symbol used a greedy algorithm to encode a string, making local decisions only. But we realized that for any prefix, you want an optimal encoding which might leave you in every possible mode.
  • 6. The Quick Brown Fox Alpha Lower X Mixed Punct. M [i, j] = min(M [i − 1, k]+ the cost of encoding the ith character and ending up in node j). Our simple dynamic programming algorithm improved the capacity of PDF-417 by an average of 8%!
  • 7. Dividing the Work Suppose the job scanning through a shelf of books is to be split between k workers. To avoid the need to rearrange the books or separate them into piles, we can divide the shelf into k regions and assign each region to one worker. What is the fairest way to divide the shelf up? If each book is the same length, partition the books into equal- sized regions, 100 100 100 | 100 100 100 | 100 100 100 But what if the books are not the same length? This partition would yield 100 200 300 | 400 500 600 | 700 800 900
  • 8. Which part of the job would you volunteer to do? How can we find the fairest possible partition, i.e. 100 200 300 400 500 | 600 700 | 800 900
  • 9. The Linear Partition Problem Input: A given arrangement S of nonnegative numbers {s1, . . . , sn} and an integer k. Problem: Partition S into k ranges, so as to minimize the maximum sum over all the ranges. Does fixed partition positions always work? Does taking the average value of a part n si/k from the left i=1 always work?
  • 10. Recursive Idea Consider a recursive, exhaustive search approach. Notice that the kth partition starts right after we placed the (k − 1)st divider. Where can we place this last divider? Between the ith and (i + 1)st elements for some i, where 1 ≤ i ≤ n. What is the cost of this? The total cost will be the larger of two quantities, (1) the cost of the last partition n j=i+1 sj and (2) the cost of the largest partition cost formed to the left of i. What is the size of this left partition? To partition the elements {s1, . . . , si} as equally as possible. But this is a smaller instance of the same problem!
  • 11. Dynamic Programming Recurrence Define M [n, k] to be the minimum possible cost over all partitionings of {s1, . . . , sn} into k ranges, where the cost of a partition is the largest sum of elements in one of its parts. Thus defined, this function can be evaluated: n n M [n, k] = min max(M [i, k − 1], sj ) i=1 j=i+1 with the natural basis cases of M [1, k] = s1, for all k > 0 and, n M [n, 1] = si i=1 What is the running time?
  • 12. It is the number of cells times the running time per cell. A total of k · n cells exist in the table. Each cell depends on n others, and can be computed in linear time, for a total of O(kn2 ).
  • 13. Implementation To evaluate this efficiently, we must make sure we do the smaller cases before the larger cases that depend upon them. Partition[S, k] (* compute prefix sums: p[k] = k si *) i=1 p[0] = 0 for i = 1 to n do p[i] = p[i − 1] + si (* initialize boundary conditions *) for i = 1 to n do M [i, 1] = p[i] for i = 1 to k do M [1, j] = s1 (* evaluate main recurrence *)
  • 14. for i = 2 to n do for j = 2 to k do M [i, j] = ∞ for x = 1 to i − 1 do s = max(M [x, j − 1], p[i] − p[x]) if (M [i, j] > s) then M [i, j] = s D[i, j] = x
  • 15. DP Matrices For the input {1, 2, 3, 4, 5, 6, 7, 8, 9} M k D k n 1 2 3 n 1 2 3 1 1 1 1 1 – – – 2 3 2 2 2 – 1 1 3 6 3 3 3 – 2 2 4 10 6 4 4 – 3 3 5 15 9 6 5 – 3 4 6 21 11 9 6 – 4 5 7 28 15 11 7 – 5 6 8 36 21 15 8 – 5 6 9 45 24 17 9 – 6 7
  • 16. DP Matrix: Uniform Series For the input {1, 1, 1, 1, 1, 1, 1, 1, 1} M k D k n 1 2 3 n 1 2 3 1 1 1 1 1 – – – 1 2 1 1 1 – 1 1 1 3 2 1 1 – 1 2 1 4 2 2 1 – 2 2 1 5 3 2 1 – 2 3 1 6 3 2 1 – 3 4 1 7 4 3 1 – 3 4 1 8 4 3 1 – 4 5 1 9 5 3 1 – 4 6
  • 17. When can you use Dynamic Programming? Dynamic programming computes recurrences efficiently by storing partial results. Thus dynamic programming can only be efficient when there are not too many partial results to compute! There are n! permutations of an n-element set – we cannot use dynamic programming to store the best solution for each subpermutation. There are 2n subsets of an n-element set – we cannot use dynamic programming to store the best solution for each. However, there are only n(n − 1)/2 continguous substrings of a string, each described by a starting and ending point, so we can use it for string problems.
  • 18. There are only n(n−1)/2 possible subtrees of a binary search tree, each described by a maximum and minimum key, so we can use it for optimizing binary search trees. Dynamic programming works best on objects which are linearly ordered and cannot be rearranged – characters in a string, matrices in a chain, points around the boundary of a polygon, the left-to-right order of leaves in a search tree. Whenever your objects are ordered in a left-to-right way, you should smell dynamic programming!
  • 19. The Principle of Optimality To use dynamic programming, the problem must observe the principle of optimality, that whatever the initial state is, remaining decisions must be optimal with regard the state following from the first decision. Combinatorial problems may have this property but may use too much memory/time to be efficient.
  • 20. Example: The Traveling Salesman Problem Let T (i; j1, j2, . . . , jk ) be the cost of the optimal tour for i to 1 that goes thru each of the other cities once T (i; i1, j2, . . . , ji) = M in1≤m≤k C[i, jm ] + T (jm ; j1, j2, . . . , jk ) T (i, j) = C(i, j) + C(j, 1) Here there can be any subset of j1, j2, . . . , jk instead of any subinterval - hence exponential.