Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Dynamic Programming

The document discusses the technique of dynamic programming. It provides examples of famous dynamic programming algorithms like Bellman-Ford-Moore for shortest path and Viterbi for hidden Markov models. It also lists application areas like operations research, computer science, economics, bioinformatics, and tech interviews. The bottom line is that dynamic programming is a powerful algorithm design technique that is broadly applicable.

Uploaded by

marcosrorizinf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Dynamic Programming

The document discusses the technique of dynamic programming. It provides examples of famous dynamic programming algorithms like Bellman-Ford-Moore for shortest path and Viterbi for hidden Markov models. It also lists application areas like operations research, computer science, economics, bioinformatics, and tech interviews. The bottom line is that dynamic programming is a powerful algorithm design technique that is broadly applicable.

Uploaded by

marcosrorizinf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE

D YNAMIC P ROGRAMMING
‣ introduction
‣ Fibonacci numbers
Algorithms F O U R T H E D I T I O N
‣ interview problems
‣ shortest paths in DAGs
R OBERT S EDGEWICK | K EVIN W AYNE
‣ seam carving
https://algs4.cs.princeton.edu

Last updated on 4/5/22 9:22 AM


D YNAMIC P ROGRAMMING
‣ introduction
‣ Fibonacci numbers
Algorithms ‣ interview problems
‣ shortest paths in DAGs
R OBERT S EDGEWICK | K EVIN W AYNE
‣ seam carving
https://algs4.cs.princeton.edu
Dynamic programming

Algorithm design paradigm. THE THEORY OF DYNAMIC PROGRAMMING


RICHARD BELLMAN

・Break up a problem into a series of overlapping subproblems.


1. Introduction. Before turning to a discussion of some representa-
tive problems which will permit us to exhibit various mathematical
features of the theory, let us present a brief survey of the funda-
mental concepts, hopes, and aspirations of dynamic programming.
To begin with, the theory was created to treat the mathematical
problems arising from the study of various multi-stage decision

・Build up solutions to larger and larger subproblems.


processes, which may roughly be described in the following way: We
have a physical system whose state at any time / is determined by a
set of quantities which we call state parameters, or state variables.
At certain times, which may be prescribed in advance, or which may
be determined by the process itself, we are called upon to make de-
cisions which will affect the state of the system. These decisions are
equivalent to transformations of the state variables, the choice of a
decision being identical with the choice of a transformation. The out-

(caching solutions to subproblems for later reuse) come of the preceding decisions is to be used to guide the choice of
future ones, with the purpose of the whole process that of maximizing
some function of the parameters describing the final state.
Examples of processes fitting this loose description are furnished
by virtually every phase of modern life, from the planning of indus-
trial production lines to the scheduling of patients at a medical
clinic ; from the determination of long-term investment programs for
universities to the determination of a replacement policy for ma-
chinery in factories; from the programming of training policies for
skilled and unskilled labor to the choice of optimal purchasing and in-
ventory policies for department stores and military establishments.
It is abundantly clear from the very brief description of possible
applications t h a t the problems arising from the study of these
processes are problems of the future as well as of the immediate

Richard Bellman, *46


present.
Turning to a more precise discussion, let us introduce a small
amount of terminology. A sequence of decisions will be called a
policy, and a policy which is most advantageous according to some

Application areas. preassigned criterion will be called an optimal policy.


The classical approach to the mathematical problems arising from
the processes described above is to consider the set of all possible


An address delivered before the Summer Meeting of the Society in Laramie on
September 3, 1953 by invitation of the Committee to Select Hour Speakers for An-
nual and Summer meetings; received by the editors August 27,1954.

Operations research: multistage decision processes, control theory, optimization, ... 503

・Computer science: AI, compilers, systems, graphics, databases, robotics, theory, ….


・Economics.
・Bioinformatics.
・Information theory.
・Tech job interviews.

Bottom line. Powerful technique; broadly applicable.


3
Dynamic programming algorithms

Some famous examples.

・System R algorithm for optimal join order in relational databases.


・Needleman–Wunsch/Smith–Waterman for sequence alignment.
・Cocke–Kasami–Younger for parsing context-free grammars.
・Bellman–Ford–Moore for shortest path.
・De Boor for evaluating spline curves.
・Viterbi for hidden Markov models.
・Unix diff for comparing two files.
・Avidan–Shamir for seam carving. see Assignment 6

・NP-complete graph problems on trees (vertex color, vertex cover, independent set, ...).
・…

4
Dynamic programming books

Computer Science/Programming
SEDGEWICK
ROBERT SEDGEWICK is the
Robert Sedgewick and Kevin Wayne’s Computer Science: William O. Baker Professor of Computer WAYNE
An Interdisciplinary Approach is the ideal modern introduction Science at Princeton University,
to computer science with Java programming for both students and where he was founding chairman
professionals. Taking a broad, applications-based approach, Sedgewick of the Department of Computer

Computer Science
and Wayne teach through important examples from science, mathematics, Science. He has held visiting
engineering, finance, and commercial computing. research positions at Xerox PARC,
Institute for Defense Analyses, and
The book demystifies computation, explains its intellectual underpinnings, INRIA, and served on the board
and covers the essential elements of programming and computational of directors at Adobe Systems. His
problem solving in today’s environments. The authors begin by introducing research interests include analytic
basic programming elements such as variables, conditionals, loops, combinatorics, design and analysis
arrays, and I/O. Next, they turn to functions, introducing key modular of algorithms and data structures,

An Interdisciplinar y Approach
programming concepts, including components and reuse. They present and program visualization. He has
a modern introduction to object-oriented programming, covering current written seventeen books.
programming paradigms and approaches to data abstraction.
KEVIN WAYNE is the Phillip Y.
Building on this foundation, Sedgewick and Wayne widen their focus Goldman Senior Lecturer in Computer
to the broader discipline of computer science. They introduce classical Science at Princeton University, where
sorting and searching algorithms, fundamental data structures and their he has taught since 1998, earning
application, and scientific techniques for assessing an implementation’s several teaching awards. He is an
performance. Using abstract models, readers learn to answer basic ACM Distinguished Educator and
questions about computation, gaining insight for practical application. holds a Ph.D. in operations research
Finally, the authors show how machine architecture links the theory of and industrial engineering from
computing to real computers, and to the field’s history and evolution. Cornell University.

For each concept, the authors present all the information readers need Sedgewick and Wayne are coauthors

Computer
to build confidence, together with examples that solve intriguing problems. of Introduction to Programming in
Each chapter contains question-and-answer sections, self-study drills, Java: An Interdisciplinary Approach
and challenging problems that demand creative solutions. (2015) and Algorithms, Fourth
Edition (2011), both from Addison-
Wesley. They have developed
Companion web site (introcs.cs.princeton.edu/java) contains extensive web content and MOOCs
on computer science and algorithms

Science
Extensive supplementary information, including suggested (Sedgewick and Wayne), and on the
approaches to programming assignments, checklists, and FAQs analysis of algorithms and analytic
Graphics and sound libraries combinatorics (Sedgewick). Their
Links to program code and test data pioneering MOOCs have attracted
more than 1 million learners; their
Solutions to selected exercises web content draws millions of
Chapter summaries hits annually.
Detailed instructions for installing a Java programming environment
Detailed problem sets and projects

informit.com/aw
Companion 20-part series of video lectures is available at informit.com/sedgewick
informit.com/title/9780134493831 introcs.cs.princeton.edu/java

ISBN-13: 978-0-13-407642-3
An Interdisciplinary Approach
Cover design by Chuti Prasertsith ISBN-10: 0-13-407642-7
Cover illustration by Robert Sedgewick 5 7 9 9 9
Register Your Product
Text printed on recycled paper at informit.com/register for convenient
access to downloads, updates, and
9 780134 076423 ROBERT SEDGEWICK
corrections as they become available. $79.99 U.S. | $99.99 CANADA
K E V I N WAY N E

pp. 284–289
9780134076423_Sedgewick_Computer_Science_Cover.indd 1 4/26/16 10:08 AM

5
D YNAMIC P ROGRAMMING
‣ introduction
‣ Fibonacci numbers
Algorithms ‣ interview problems
‣ shortest paths in DAGs
R OBERT S EDGEWICK | K EVIN W AYNE
‣ seam carving
https://algs4.cs.princeton.edu
Fibonacci numbers

Fibonacci numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

8
<latexit sha1_base64="lG5KV5dzjHqxJdRxPd6Wu2waeY8=">AAACsXicbVFda9swFJXdfXTZR9P2cRtcFlYGY8HqNloYG4VB2WMHS9sRB0+Wr1MRWTKSPBKMn/cb+yP6HyYnHixpLwgO5xzdq3uUllJYF0XXQbh17/6Dh9uPeo+fPH2209/dO7e6MhxHXEttLlNmUQqFIyecxMvSICtSiRfp7GurX/xGY4VWP9yixEnBpkrkgjPnqaT/5zQR8BniFKdC1dx3sk0v/gQRHMRFque1yKEBT7SuCOIYWpXepdJ/6mlSi3e0gbcrdNjAAWzavwDtxaiybiQk/UE0jJYFtwHtwIB0dZbsBntxpnlVoHJcMmvHNCrdpGbGCS7RL1FZLBmfsSmOPVSsQDupl4k18NozGeTa+KMcLNn/b9SssHZRpN5ZMHdlN7WWvEsbVy4/ntRClZVDxVeD8kqC09DGD5kwyJ1ceMC4Ef6twK+YYdz5T1qbsuxdIl/bpJ5XSnCd4QYr3dwZ1vgU6WZmt8H54ZB+HEbfPwxOjrs8t8lz8oq8IZQckRPyjZyREeHkJtgPXgQvw/fhz/BXmK6sYdDd2SdrFc7+Ai0pzMY=</latexit>

>
<0 i=0
Fi = 1 i=1
>
:
Fi 1 + Fi 2 i>1
Leonardo Fibonacci

3 5 8 13

21 34 55 89
7
Fibonacci numbers: naïve recursive approach

Fibonacci numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

8
<latexit sha1_base64="lG5KV5dzjHqxJdRxPd6Wu2waeY8=">AAACsXicbVFda9swFJXdfXTZR9P2cRtcFlYGY8HqNloYG4VB2WMHS9sRB0+Wr1MRWTKSPBKMn/cb+yP6HyYnHixpLwgO5xzdq3uUllJYF0XXQbh17/6Dh9uPeo+fPH2209/dO7e6MhxHXEttLlNmUQqFIyecxMvSICtSiRfp7GurX/xGY4VWP9yixEnBpkrkgjPnqaT/5zQR8BniFKdC1dx3sk0v/gQRHMRFque1yKEBT7SuCOIYWpXepdJ/6mlSi3e0gbcrdNjAAWzavwDtxaiybiQk/UE0jJYFtwHtwIB0dZbsBntxpnlVoHJcMmvHNCrdpGbGCS7RL1FZLBmfsSmOPVSsQDupl4k18NozGeTa+KMcLNn/b9SssHZRpN5ZMHdlN7WWvEsbVy4/ntRClZVDxVeD8kqC09DGD5kwyJ1ceMC4Ef6twK+YYdz5T1qbsuxdIl/bpJ5XSnCd4QYr3dwZ1vgU6WZmt8H54ZB+HEbfPwxOjrs8t8lz8oq8IZQckRPyjZyREeHkJtgPXgQvw/fhz/BXmK6sYdDd2SdrFc7+Ai0pzMY=</latexit>

>
<0 i=0
Fi = 1 i=1
>
:
Fi 1 + Fi 2 i>1

Goal. Given n, compute Fn.

Naïve recursive approach:

public static long fib(int i)


{
if (i == 0) return 0;
if (i == 1) return 1;
return fib(i-1) + fib(i-2);
}

8
Dynamic programming: quiz 1

How long to compute fib(80) using the naïve recursive algorithm?

A. Less than 1 second.

B. About 1 minute. ~/Desktop/dp> java Fibonacci 42


267914296
C. More than 1 hour. 1.04 seconds

D. Overflows a 64-bit long integer. ~/Desktop/dp> java Fibonacci 43


433494437
1.67 seconds
seems to increase by a
factor of about 1.6𐄂 ~/Desktop/dp> java Fibonacci 44
701408733
2.70 seconds


~/Desktop/dp> java Fibonacci 80
23416728348467685
2.88 years

9
Fibonacci numbers: recursion tree and exponential growth

Exponential waste. Same overlapping subproblems are solved repeatedly.


Ex. To compute fib(6):
・ fib(5) is called 1 time.
1+ 5
p

・ fib(4) is called 2 times.


n
Fn ⇠ , = ⇡ 1.618
2

・ fib(3) is called 3 times.


・ fib(2) is called 5 times.
・ fib(1) is called F = F = 8 times.n 6 fib(6)
“overlapping subproblems”
F6
F5

F4 F4

F3 F3 F3

F2 F2 F2 F2 F2

F1 F1 F1 F1 F1 F1 F1 F1

F0 F0 F0 F0 F0

running time = # subproblems × cost per subproblem


10
Fibonacci numbers: top-down dynamic programming (memoization)

Memoization.

・Maintain an array (or symbol table) to remember all computed values.


・If value to compute is known, just return it;
otherwise, compute it; remember it; and return it.

public static long fib(int i)


{
if (i == 0) return 0;
if (i == 1) return 1;
if (f[i] == 0) f[i] = fib(i-1) + fib(i-2);
return f[i];
}

assume global long array f[], initialized to 0 (unknown)

Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn.
11
Fibonacci numbers: bottom-up dynamic programming (tabulation)

Tabulation.

・Build computation from the “bottom up.”


・Solve small subproblems and save solutions.
・Use those solutions to solve larger subproblems.

public static long fib(int n)


{
long[] f = new long[n+1];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
} smaller subproblems

Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn ; no recursion.
12
Fibonacci numbers: further improvements

Performance improvements.

・Reduce space by maintaining only two most recent Fibonacci numbers.

public static long fib(int n) {


f and g are consecutive
int f = 0, g = 1; Fibonacci numbers
for (int i = 0; i < n; i++) {
g = f + g;
f = g - f;
}
return f;
}

・Exploit additional properties of problem:


 n
p ✓
<latexit sha1_base64="reolBSzSJ90rPQboi0tiatATSqk=">AAACo3icbVFda9RAFJ1EqzVqu62PvgwuFUFcMlK0UISCUARfqnTbwiaGyeTu9tLJJMzcyC4hP9Qn/4qz6SLurhdm7uHczzmT1xodxfGvIHzwcOfR490n0dNnz/f2BweHV65qrIKxqnRlb3LpQKOBMSFpuKktyDLXcJ3ffV7Gr3+CdViZS1rUkJZyZnCKSpKnssEiSnKYoWnrUpLFeRcJ/pqLJOl9HCVgir+xHxglp8kp/8SXbqvyPGvxreh83XmGvoO/e9ziO9FtdcoGw3gU98a3gViBIVvZRXYQHCZFpZoSDCktnZuIuKa0lZZQafATGge1VHdyBhMPjSzBpW2vUcePPFPwaWX9McR79t+KVpbOLcrcZ/odb91mbEn+LzZpaHqStmjqhsCo+0HTRnOq+FJwXqAFRXrhgVQW/a5c3UorFflvWZvS965Brb2knTcGVVXABqtpTlZ2XkWxqdk2uHo/Eh9Gx9+Oh2cnKz132Uv2ir1hgn1kZ+wLu2BjptjvYCfYC/bDo/Br+D28vE8Ng1XNC7ZmYfoHMv/LbQ==</latexit>

◆i ✓ ◆i
1+ 5 1 1 Fi+1 Fi
Fn = p , = =
5 2 1 0 Fi Fi 1

13
Dynamic programming recap

Dynamic programming.

・Divide a complex problem into a number of simpler overlapping subproblems.


[ define n + 1 subproblems, where subproblem i is computing the ith Fibonacci number ]

・Define a recurrence relation to solve larger subproblems from smaller subproblems.


[ easy to solve subproblem i if we know solutions to subproblems i − 1 and i − 2 ]

8
<latexit sha1_base64="pZcKEDNVg1Xxmmn2+MhtVB0b6DU=">AAACsnicdVFda9swFJW9ry77Sru9jY3LwspgNMgh6wqlozAoe+xgacviYGT5OhWVJSPJI8H4fX+xf6K/YXKSwpJ1FwSHc47u1T1KSymso/Q6CO/df/Dw0dbjzpOnz56/6G7vnFldGY4jrqU2FymzKIXCkRNO4kVpkBWpxPP06murn/9CY4VWP9y8xEnBpkrkgjPnqaT7+yQRcARxilOhau472aYTHwKF3bhI9awWOTTgidZFIY6hVaO71OhWPUlqsRc18HGJBg3swqb9C0SdGFW2GgmdpNuj/X063B8M4F8Q9emiemRVp8l2sBNnmlcFKscls3Yc0dJNamac4BL9FpXFkvErNsWxh4oVaCf1IrIG3nsmg1wbf5SDBfv3jZoV1s6L1DsL5i7tptaSd2njyuUHk1qosnKo+HJQXklwGtr8IRMGuZNzDxg3wr8V+CUzjDv/S2tTFr1L5Gub1LNKCa4z3GClmznDGp/ibVTwf3A26Eef+vT7sHd8sMpzi7wm78gHEpHP5Jh8I6dkRDi5CV4Fb4K34TD8GbKQL61hsLrzkqxVKP8A83XNFg==</latexit>

>
<0 i=0
Fi = 1 i=1
>
:
Fi 1 + Fi 2 i>1

・Store solutions to each of these subproblems, solving each subproblem only once.
[ store solution to subproblem i in array entry f[i] ]

・Use stored solutions to solve the original problem.


[ solution to subproblem n is original problem ]
14
D YNAMIC P ROGRAMMING
‣ introduction
‣ Fibonacci numbers
Algorithms ‣ interview problems
‣ shortest paths in DAGs
R OBERT S EDGEWICK | K EVIN W AYNE
‣ seam carving
https://algs4.cs.princeton.edu
HOUSE PAINTING PROBLEM

Goal. Given a row of n black houses, paint some orange so that:


・Maximize total profit, where profit(i) = profit from painting house i orange.
・Constraint: no two adjacent houses painted orange.

i 1 2 3 4 5 6

profit(i) 10 9 13 20 30 25

profit for painting houses 1, 4, and 6 orange


(10+ 20 + 25 = 55)

16
HOUSE PAINTING PROBLEM: DYNAMIC PROGRAMMING FORMULATION

Goal. Given a row of n black houses, paint some orange so that:


・Maximize total profit, where profit(i) = profit from painting house i orange.
・Constraint: no two adjacent houses painted orange.
Subproblems. OPT(i) = max profit to paint houses 1, …, i.
Optimal value. OPT(n).

i 0 1 2 3 4 5 6

profit(i) 10 9 13 20 30 25

OPT(i) 0 10 10 23 30 53 55

keep house 6 black paint house 6 orange

OPT(6) = max { OPT(5), profit(6) + OPT(4) }


= max { 53, 25 + 30 }
= 55 17
HOUSE PAINTING PROBLEM: DYNAMIC PROGRAMMING FORMULATION

Goal. Given a row of n black houses, paint some orange so that:


・Maximize total profit, where profit(i) = profit from painting house i orange.
・Constraint: no two adjacent houses painted orange.
Subproblems. OPT(i) = max profit to paint houses 1, …, i.
Optimal value. OPT(n).
optimal substructure
(optimal solution can be constructed from
optimal solutions to smaller subproblems)
Binary choice. To compute OPT(i) , either:
・Don’t paint house i orange: OPT(i − 1). take best
・Paint house i orange: profit(i) + OPT(i − 2).
Dynamic programming recurrence.
<latexit sha1_base64="cEupkp5ZznMR3kPHtK5OpiwiQIQ=">AAADEHicbVLbihNBEO0Zb2u8ZddHXwqDkqAbZoKsC2FhwRcfBCNsdhfSIfR0apJmey5010jCmJ/wa3wTX/0Df8DvsGeSB5NswcDpU3XqVFdPlGtlKQj+eP6du/fuPzh42Hj0+MnTZ83Do0ubFUbiUGY6M9eRsKhVikNSpPE6NyiSSONVdPOhyl99RWNVll7QMsdxImapipUU5KhJ8+/nwUVbdYD3z3i/wSOcqbSUrqFdNXgfAoDXwAkXBCWoGFag4MyxnI+6J5iMXU2dVVTmJosVrdph5zZJuCVJxAK4xpiAl84a6iHgGMLOW3d0xG5TN+GbddVxrxoWuFGzuZOv9s34DKHn7Boc0+nmLpNmK+gGdcA+CDegxTYxmBx6R3yaySLBlKQW1o7CIKdxKQwpqdEtp7CYC3kjZjhyMBUJ2nFZP8gKXjlmCnFm3JcS1Oz/ilIk1i6TyFUmguZ2N1eRt+VGBcWn41KleUGYyrVRXGigDKrXhakyKEkvHRDSKDcryLkwQpL7B7Zc6t45yq2blIsiVTKb4g6raUFGVFsMd3e2Dy573fCkG3551zo/3ezzgL1gL1mbhew9O2cf2YANmfQ+ecYrvW/+d/+H/9P/tS71vY3mOdsK//c/OjHuhQ==</latexit>

8
>
> 0 i=0
>
<
OP T (i) = (1) i=1
>
>
>
:
max { OP T (i 1), (i) + OP T (i 2) } i 2
18
HOUSE PAINTING: NAÏVE RECURSIVE IMPLEMENTATION

Naïve recursive approach:

private int opt(int i) {


if (i == 0) return 0;
if (i == 1) return profit[1];
return Math.max(opt(i-1), profit[i] + opt(i-2));
}

Dynamic programming recurrence.


<latexit sha1_base64="cEupkp5ZznMR3kPHtK5OpiwiQIQ=">AAADEHicbVLbihNBEO0Zb2u8ZddHXwqDkqAbZoKsC2FhwRcfBCNsdhfSIfR0apJmey5010jCmJ/wa3wTX/0Df8DvsGeSB5NswcDpU3XqVFdPlGtlKQj+eP6du/fuPzh42Hj0+MnTZ83Do0ubFUbiUGY6M9eRsKhVikNSpPE6NyiSSONVdPOhyl99RWNVll7QMsdxImapipUU5KhJ8+/nwUVbdYD3z3i/wSOcqbSUrqFdNXgfAoDXwAkXBCWoGFag4MyxnI+6J5iMXU2dVVTmJosVrdph5zZJuCVJxAK4xpiAl84a6iHgGMLOW3d0xG5TN+GbddVxrxoWuFGzuZOv9s34DKHn7Boc0+nmLpNmK+gGdcA+CDegxTYxmBx6R3yaySLBlKQW1o7CIKdxKQwpqdEtp7CYC3kjZjhyMBUJ2nFZP8gKXjlmCnFm3JcS1Oz/ilIk1i6TyFUmguZ2N1eRt+VGBcWn41KleUGYyrVRXGigDKrXhakyKEkvHRDSKDcryLkwQpL7B7Zc6t45yq2blIsiVTKb4g6raUFGVFsMd3e2Dy573fCkG3551zo/3ezzgL1gL1mbhew9O2cf2YANmfQ+ecYrvW/+d/+H/9P/tS71vY3mOdsK//c/OjHuhQ==</latexit>

8
>
> 0 i=0
>
<
OP T (i) = (1) i=1
>
>
>
:
max { OP T (i 1), (i) + OP T (i 2) } i 2
19
Dynamic programming: quiz 2

What is running time of the naïve recursive algorithm as a function of n?

private int opt(int i) {


A. Θ(n) c = φ = 1.618.... if (i == 0) return 0;
(Fibonacci strikes again) if (i == 1) return profit[1];
B. Θ(n 2) return Math.max(opt(i-1), profit[i] + opt(i-2));
}
C. Θ(cn) for some c > 1.
opt(6)
D. Θ(n!) “overlapping subproblems”
6
5

4 4

3 3 3

2 2 2 2 2

1 1 1 1 1 1 1 1

0 0 0 0 0

running time = # subproblems × cost per subproblem


20
Dynamic programming aphorism

“ Those who cannot remember the


past are condemned to repeat it. ”
— Dynamic Programming
(Jorge Agustín Nicolás Ruiz de Santayana y Borrás)

21
HOUSING PAINTING: BOTTOM-UP IMPLEMENTATION

Bottom-up DP implementation.

int[] opt = new int[n+1];


opt[0] = 0;
opt[1] = profit[1];
for (int i = 2; i <= n; i++)
opt[i] = Math.max(opt[i-1], profit[i] + opt[i-2]);

solutions to smaller subproblems already available

<latexit sha1_base64="cEupkp5ZznMR3kPHtK5OpiwiQIQ=">AAADEHicbVLbihNBEO0Zb2u8ZddHXwqDkqAbZoKsC2FhwRcfBCNsdhfSIfR0apJmey5010jCmJ/wa3wTX/0Df8DvsGeSB5NswcDpU3XqVFdPlGtlKQj+eP6du/fuPzh42Hj0+MnTZ83Do0ubFUbiUGY6M9eRsKhVikNSpPE6NyiSSONVdPOhyl99RWNVll7QMsdxImapipUU5KhJ8+/nwUVbdYD3z3i/wSOcqbSUrqFdNXgfAoDXwAkXBCWoGFag4MyxnI+6J5iMXU2dVVTmJosVrdph5zZJuCVJxAK4xpiAl84a6iHgGMLOW3d0xG5TN+GbddVxrxoWuFGzuZOv9s34DKHn7Boc0+nmLpNmK+gGdcA+CDegxTYxmBx6R3yaySLBlKQW1o7CIKdxKQwpqdEtp7CYC3kjZjhyMBUJ2nFZP8gKXjlmCnFm3JcS1Oz/ilIk1i6TyFUmguZ2N1eRt+VGBcWn41KleUGYyrVRXGigDKrXhakyKEkvHRDSKDcryLkwQpL7B7Zc6t45yq2blIsiVTKb4g6raUFGVFsMd3e2Dy573fCkG3551zo/3ezzgL1gL1mbhew9O2cf2YANmfQ+ecYrvW/+d/+H/9P/tS71vY3mOdsK//c/OjHuhQ==</latexit>

8
>
> 0 i=0
>
<
OP T (i) = (1) i=1
>
>
>
:
max { OP T (i 1), (i) + OP T (i 2) } i 2

Proposition. Computing OPT(n) takes Θ(n) time and uses Θ(n) extra space.
22
HOUSING PAINTING: TRACE

Bottom-up DP implementation trace.

OPT(1) = profit(1)
OPT(2)
OPT(3)
OPT(4)
OPT(5)
OPT(6) max { OPT(5),
OPT(1), profit(6)
OPT(2),
OPT(3),
OPT(4), profit(2) + OPT(4)
profit(3)
profit(4)
profit(5) OPT(0) }
OPT(1)
OPT(2)
OPT(3)
= max
10 { 53,
10, 25
23,
30, 9 ++030
13
20
30 10
23
} }
= 55
10
23
30
53

i 0 1 2 3 4 5 6

profit(i) 10 9 13 20 30 25

OPT(i) 0 10 10 23 30 53 55

OPT(i) = max profit for painting houses 1, 2, …, i


23
HOUSING PAINTING: TRACEBACK

Q. We computed the optimal value. How to reconstruct an optimal solution?


A. Traceback path that led to optimal value.
OPT(1) = profit(1)
OPT(2)
OPT(3)
OPT(4)
OPT(5)
OPT(6) max { OPT(5),
OPT(1), profit(6)
OPT(2),
OPT(3),
OPT(4), profit(2) + OPT(4)
profit(3)
profit(4)
profit(5) OPT(0) }
OPT(1)
OPT(2)
OPT(3)
= max
10 { 53,
10, 25
23,
30, 9 ++030
13
20
30 10
23
} }
= 55
10
23
30
53

i 0 1 2 3 4 5 6

profit(i) 10 9 13 20 30 25

OPT(i) 0 10 10 23 30 53 55

OPT(i) = max profit for painting houses 1, 2, …, i


24
COIN CHANGING

Problem. Given n coin denominations { d1, d2, …, dn } and a target value V,


find the fewest coins needed to make change for V (or report impossible).

Ex. Coin denominations = {1, 10, 25, 100 }, V = 130.


Greedy (8 coins). 131¢ = 100 + 25 + 1 + 1 + 1 + 1 + 1 + 1.
Optimal (5 coins). 131¢ = 100 + 10 + 10 + 10 + 1.

vending machine
(out of nickels)

8 coins 5 coins
(131¢) (131¢)

Remark. Greedy algorithm is optimal for U.S. coin denominations {1, 5, 10, 25, 100 }.
25
Dynamic programming: quiz 3

Which subproblems for coin changing problem?

A. OPT(i) = fewest coins needed to make change for target value V knowing solution to OPT(1), OPT(2), ..., OPT(i−1),
doesn’t seem to help with solving OPT(i)
using only denominations d1, d2, ..., di .

B. OPT(v) = fewest coins needed to make change for amount v, knowing solution to OPT(1), OPT(2), ..., OPT(v−1),
makes it easy to solve OPT(v)

for v = 0, 1, 2, …, V.

C. Either A or B.

D. Neither A nor B.

26
COIN CHANGING: DYNAMIC PROGRAMMING FORMULATION

Problem. Given n coin denominations { d1, d2, …, dn } and a target value V,


find the fewest coins needed to make change for V (or report impossible).

Subproblems. OPT(v) = fewest coins needed to make change for amount v.


Optimal value. OPT(V).

Ex. Coin denominations { 1, 5, 8 } and V = 10.

v 0¢ 1¢ 2¢ 3¢ 4¢ 5¢ 6¢ 7¢ 8¢ 9¢ 10¢

# coins 0 1 2 3 4 1 2 3 1 2 2

OPT(10) = min { 1 + OPT(10 – 1), 1 + OPT(10 – 5), 1 + OPT(10 – 8) }


= min { 1 + 2, 1 + 1, 1 + 2 }
= 2 27
COIN CHANGING: DYNAMIC PROGRAMMING FORMULATION

Problem. Given n coin denominations { d1, d2, …, dn } and a target value V,


find the fewest coins needed to make change for V (or report impossible).

Subproblems. OPT(v) = fewest coins needed to make change for amount v.


Optimal value. OPT(V).

Multiway choice. To compute OPT(v),


・Select a coin of denomination di ≤ v for some i. take best
・Use fewest coins to make change for v − di.
optimal substructure
Dynamic programming recurrence.

<latexit sha1_base64="o2H80UrmBhbkbDptSOy+lmYiU7M=">AAAC93icbVFNbxMxEPUuXyV8peXIZUQEKkJEuxSViqioEhduBKlpi+Io8npnEyte78r2Roms/S3cEFd+CD+Af4N3mwNJGcn205s3nudxUkphbBT9CcJbt+/cvbd3v/Pg4aPHT7r7BxemqDTHES9koa8SZlAKhSMrrMSrUiPLE4mXyeJTk79cojaiUOd2XeIkZzMlMsGZ9dS0+/vL8Pxw+Qro4JQOOjTBmVCO+wtN3aEDiABeArW4suBAZFDDEk49S+m4f4z5xGtoLhSVIhfWTB01VWIs4wtHl6ZkHF0/OhKq9gUgfBP40Gzp1GOJsKzr5swsUNfwMbyG1g+8aTSNK6BazOZeUN808rE10qGo0o3labcX9aM24CaIN6BHNjGc7gcHNC14laOyXDJjxnFU2olj2gou0c+gMujfsWAzHHuoWI5m4tq51/DCMylkhfZLWWjZfyscy41Z54lX5szOzW6uIf+XG1c2O5k4ocrKouLXjbJKgi2g+URIhUZu5doDxrXwXoHPmWbc+q/e6tLeXSLfeolbVUrwIsUdVtqV1ayZYrw7s5vg4m0/Pu7HX9/1zk4289wjz8hzckhi8p6ckc9kSEaEB0fBtyAJeLgOv4c/wp/X0jDY1DwlWxH++gu83+dK</latexit>

8
>
<0 v=0
OP T (v) =
>
: min { 1 + OP T (v di ) } v>0
i : di v

notation: min is over all coin denominations of value ≤ v 28


COIN CHANGING: BOTTOM-UP IMPLEMENTATION

Bottom-up DP implementation.

int[] opt = new int[V+1];


opt[0] = 0; <latexit sha1_base64="o2H80UrmBhbkbDptSOy+lmYiU7M=">AAAC93icbVFNbxMxEPUuXyV8peXIZUQEKkJEuxSViqioEhduBKlpi+Io8npnEyte78r2Roms/S3cEFd+CD+Af4N3mwNJGcn205s3nudxUkphbBT9CcJbt+/cvbd3v/Pg4aPHT7r7BxemqDTHES9koa8SZlAKhSMrrMSrUiPLE4mXyeJTk79cojaiUOd2XeIkZzMlMsGZ9dS0+/vL8Pxw+Qro4JQOOjTBmVCO+wtN3aEDiABeArW4suBAZFDDEk49S+m4f4z5xGtoLhSVIhfWTB01VWIs4wtHl6ZkHF0/OhKq9gUgfBP40Gzp1GOJsKzr5swsUNfwMbyG1g+8aTSNK6BazOZeUN808rE10qGo0o3labcX9aM24CaIN6BHNjGc7gcHNC14laOyXDJjxnFU2olj2gou0c+gMujfsWAzHHuoWI5m4tq51/DCMylkhfZLWWjZfyscy41Z54lX5szOzW6uIf+XG1c2O5k4ocrKouLXjbJKgi2g+URIhUZu5doDxrXwXoHPmWbc+q/e6tLeXSLfeolbVUrwIsUdVtqV1ayZYrw7s5vg4m0/Pu7HX9/1zk4289wjz8hzckhi8p6ckc9kSEaEB0fBtyAJeLgOv4c/wp/X0jDY1DwlWxH++gu83+dK</latexit>

8
>
<0 v=0
for (int v = 1; v <= V; v++)
OP T (v) =
{ >
: min { 1 + OP T (v di ) } v>0
// opt[v] = min_i { 1 + opt[v - d[i]] } i : di v
opt[v] = INFINITY;
for (int i = 1; i <= n; i++)
if (d[i] <= v)
opt[v] = Math.min(opt[v], 1 + opt[v - d[i]]);
}

Proposition. DP algorithm takes Θ(n V) time and uses Θ(V) extra space.
Note. Not polynomial in input size; underlying problem is NP-complete.

n, log V 29
D YNAMIC P ROGRAMMING
‣ introduction
‣ Fibonacci numbers
Algorithms ‣ interview problems
‣ shortest paths in DAGs
R OBERT S EDGEWICK | K EVIN W AYNE
‣ seam carving
https://algs4.cs.princeton.edu
Shortest paths in directed acyclic graphs: dynamic programming formulation

Problem. Given a DAG with positive edge weights, find shortest path from s to t.
Subproblems. distTo(v) = length of shortest s ↝v path.
Goal. distTo(t).

u1

Multiway choice. To compute distTo(v) :


・ Select an edge e = u→v entering v. take best among
s 10 uu2 2 v

・Concatenate with shortest s ↝ u path. distTo(u) + weight(e)

u3

optimal substructure

Dynamic programming recurrence.


<latexit sha1_base64="cETBrH0OFzew3BYtWq8OoQ6R8og=">AAADIHicbVHLattAFB2pr8R9xEmX3VxqGhxajFxaajCBQDddphAnAY8xo9GVPUQaqTMjx2bQptv2K/o13ZUu0w/puiNZi9jphYHDuWfOfYV5IrQJghvPv3f/wcNHO7utx0+ePttr7x+c66xQHEc8SzJ1GTKNiZA4MsIkeJkrZGmY4EV49bHKXyxQaZHJM7PKcZKymRSx4Mw4atr+26IpM3NhbOSKnWVld3EEdHhMh9CiIc6EtNzZ67JFh44L4JCmYba0IoYS6nDsAo5BA6XjoDfAdLKW0kJGrjAai41hAVSJ2dwwpbJrWJS2Lq1SmwpZlpURTTA2QC3QN7DdV1H19boSNYlrrMzKLh7V8toaaAmHcKvFujsq8QvoFkUZNdNM252gF9QBd0G/AR3SxOl03zugUcaLFKXhCdN63A9yM7FMGcETdOspNOaMX7EZjh2ULEU9sfWBSnjlmAjiTLknDdTs7R+WpVqv0tApq+H0dq4i/5cbFyYeTKyQeWFQ8nWhuEjAZFBdGyKhkJtk5QDjSrhegc+ZYty402xUqb1z5BuT2GUhBc8i3GITszSKVVvsb+/sLjh/2+u/7wWf33VOBs0+d8gL8pJ0SZ98ICfkEzklI8I96n31vnnf/R/+T/+X/3st9b3mz3OyEf6ff4ZQ+yc=</latexit>

8
< 0 v=s
distTo(v) =
: min { distTo(u) + weight(e) } v 6= s
e = u!v

notation: min is over all edges that enter v


31
Shortest paths in directed acyclic graphs: bottom-up solution

Bottom-up DP implementation. Takes Θ(E + V) time with two tricks:

・ Solve subproblems in topological order. ensures that “small” subproblems are solved before “large” ones

・Form reverse digraph G R (to support iterating over edges incident to vertex v). u1

uu2 v
Equivalent (but simpler) computation. Relax vertices in topological order.
u3

Topological topological = new Topological(G);


for (int v : topological.order())
for (DirectedEdge e : G.adj(v))
relax(e);

Backtracing. Can find the shortest paths themselves by maintaining edgeTo[] array.

32
Dynamic programming: quiz 4

Given a DAG, how to find longest path from s to t in Θ(E + V) time?

s 0 1 1 2 3 4 5 6 7 t

longest path from s to t in a DAG (all edge weights = 1)

A. Negate edge weights; use DP algorithm to find shortest path.

B. Replace min with max in DP recurrence.

C. Either A or B.

D. No poly-time algorithm is known (NP-complete).

33
Shortest paths in DAGs and dynamic programming

DP subproblem dependency digraph.


・Vertex v corresponds to subproblem v.
・Edge v→w means subproblem v must be solved before subproblem w.
・Digraph must be a DAG. Why?
Ex 1. Modeling the coin changing problem as a shortest path problem in a DAG.

0 1 1 2 3 4 5 6 7 8 9 1 10
1
1
s t

coin denominations = { 1, 5, 8 }, V = 10

34
Shortest paths in DAGs and dynamic programming

DP subproblem dependency digraph.


・Vertex v corresponds to subproblem v.
・Edge v→w means subproblem v must be solved before subproblem w.
・Digraph must be a DAG. Why?
Ex 2. Modeling the house painting problem as a longest path problem in a DAG.

profit for painting


house 6 orange

9 13 20 30 25

10
s 0 0 0 0 0 0 t

1 2 3 4 5 6

n = 6; profits = { 10, 9, 13, 20, 30, 25 }

35
D YNAMIC P ROGRAMMING
‣ introduction
‣ Fibonacci numbers
Algorithms ‣ interview problems
‣ shortest paths in DAGs
R OBERT S EDGEWICK | K EVIN W AYNE
‣ seam carving
https://algs4.cs.princeton.edu
Content-aware resizing

Seam carving. [Avidan–Shamir] Resize an image without distortion for display


on cell phones and web browsers.

https://www.youtube.com/watch?v=vIFCV2spKtg
37
Content-aware resizing

Seam carving. [Avidan–Shamir] Resize an image without distortion for display


on cell phones and web browsers.

In the wild. Photoshop, ImageMagick, GIMP, ...


38
Content-aware resizing

To find vertical seam in a picture:

・Grid graph: vertex = pixel; edge = from pixel to 3 downward neighbors (SW, S, SE).
・Weight of pixel = “energy function” of 4 neighboring pixels (N, E, S, W).

39
Content-aware resizing

To find vertical seam in a picture:

・Grid graph: vertex = pixel; edge = from pixel to 3 downward neighbors (SW, S, SE).
・Weight of pixel = “energy function” of 4 neighboring pixels (N, E, S, W).
・Seam = shortest path (sum of vertex weights) from top to bottom.
seam

40
Content-aware resizing

To remove vertical seam in a picture:

・Delete pixels on seam (one in each row).

seam

41
Content-aware resizing: dynamic programming formulation

Problem. Find a min energy path from top to bottom.


Subproblems. distTo( col, row) = energy of min energy path from any top pixel to pixel (col, row).
Goal. min { distTo( col, H−1) }.
Dynamic programing recurrence. For you to figure out in Assignment 6.

seam

42
Summary

How to design a dynamic programming algorithm.

・Find good subproblems.


・Develop DP recurrence for optimal value.
– optimal substructure
– overlapping subproblems

・Determine dependency order in which to solve subproblems.


・Cache computed results to avoid unnecessary re-computation.
・Reconstruct the solution: backtrace or save extra state.

43
© Copyright 2022 Robert Sedgewick and Kevin Wayne

44

You might also like