Dynamic Programming
Dynamic Programming
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
(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
・
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
・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
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
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
8
Dynamic programming: quiz 1
⋮
~/Desktop/dp> java Fibonacci 80
23416728348467685
2.88 years
9
Fibonacci numbers: recursion tree and exponential growth
F4 F4
F3 F3 F3
F2 F2 F2 F2 F2
F1 F1 F1 F1 F1 F1 F1 F1
F0 F0 F0 F0 F0
Memoization.
Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn.
11
Fibonacci numbers: bottom-up dynamic programming (tabulation)
Tabulation.
Impact. Solves each subproblem Fi only once; Θ(n) time to compute Fn ; no recursion.
12
Fibonacci numbers: further improvements
Performance improvements.
◆i ✓ ◆i
1+ 5 1 1 Fi+1 Fi
Fn = p , = =
5 2 1 0 Fi Fi 1
13
Dynamic programming recap
Dynamic programming.
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] ]
i 1 2 3 4 5 6
profit(i) 10 9 13 20 30 25
16
HOUSE PAINTING PROBLEM: DYNAMIC PROGRAMMING FORMULATION
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
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
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
4 4
3 3 3
2 2 2 2 2
1 1 1 1 1 1 1 1
0 0 0 0 0
21
HOUSING PAINTING: BOTTOM-UP IMPLEMENTATION
Bottom-up DP implementation.
<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
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
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
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
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
v 0¢ 1¢ 2¢ 3¢ 4¢ 5¢ 6¢ 7¢ 8¢ 9¢ 10¢
# coins 0 1 2 3 4 1 2 3 1 2 2
<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
Bottom-up DP implementation.
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
u3
optimal substructure
8
< 0 v=s
distTo(v) =
: min { distTo(u) + weight(e) } v 6= s
e = u!v
・ 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
Backtracing. Can find the shortest paths themselves by maintaining edgeTo[] array.
32
Dynamic programming: quiz 4
s 0 1 1 2 3 4 5 6 7 t
C. Either A or B.
33
Shortest paths in DAGs and dynamic programming
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
9 13 20 30 25
10
s 0 0 0 0 0 0 t
1 2 3 4 5 6
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
https://www.youtube.com/watch?v=vIFCV2spKtg
37
Content-aware resizing
・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
・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
seam
41
Content-aware resizing: dynamic programming formulation
seam
42
Summary
43
© Copyright 2022 Robert Sedgewick and Kevin Wayne
44