-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjeff-dp.tex
404 lines (356 loc) · 11.2 KB
/
jeff-dp.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
% https://github.com/cohomolo-gy/cats-in-context/blob/master/chapter-2/Chapter%202%20Solutions.tex
\documentclass[14pt]{report}
\usepackage{bbm}
\usepackage{bbding} % for flower.
\usepackage{physics}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{makeidx}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{listing}
\usepackage{minted}
\usemintedstyle{lovelace}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{xargs}
\usepackage{hyperref}
\hypersetup{
colorlinks,
citecolor=blue,
filecolor=blue,
linkcolor=blue,
urlcolor=blue
}
\usepackage{epsfig}
\usepackage{tabularx}
\usepackage{latexsym}
\newcommand{\N}{\ensuremath{\mathbb{N}}}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}
\newcommand{\Q}{\ensuremath{\mathbb{Q}}}
\newcommand{\R}{\ensuremath{\mathbb R}}
\def\qed{\ensuremath{\Box}}
\newcommand*{\start}[1]{\leavevmode\newline \textbf{#1} }
\newcommand*{\question}[1]{\leavevmode\clearpage \par\noindent\rule{\textwidth}{0.4pt} \textbf{Question: #1.}}
\newcommand*{\proof}[1]{\leavevmode\newline \textbf{Proof #1}}
\newcommand*{\answer}{\leavevmode\newline \textbf{Answer} }
\DeclareMathOperator{\lcm}{lcm}
\usepackage[adobe-utopia]{mathdesign}
\usepackage[T1]{fontenc}
\title{Algorithms by Jeff E: Dynamic Programming}
\author{Siddharth Bhat}
\date{Monsoon, second year of the plague}
\begin{document}
\maketitle
\question{2a: number of partitions into words}
\begin{minted}{cpp}
// substr_co = substring in closed-open interval.
int q2a(string s) {
const int n = s.size();
vector<int> dp(n+1, 0);
// dp[ls] = number of words in s[0:ls).
// ls for ``length of s''
for(int ls = 1; ls <= n; ++ls) {
for(int j = 0; j < ls; ++j) { // break off word [j..ls)
if (!isword(s.substr_co(j, ls)) { continue; }
nwords[ls] = max(nwords[ls], 1 + nwords[j-1]);
}
}
return nwords[n];
}
\end{minted}
\question{2b: decide if $s, t$ can be partitioned into words at same indexes}
\begin{minted}{cpp}
// substr_co = substring in closed-open interval.
int q2b(string s, string t) {
const int n = s.size();
vector<int> dp(n+1, 0);
// dp[i] = number of words in s[0:i)
for(int l = 1; l <= n; ++l) {
for(int j = 0; j < l; ++j) { // break off word [j, l)
if (!isword(s.substr_co(j, l)]) ||
!isword(t.substr_co(j, l)])) { continue; }
nwords[l] = max(nwords[l], 1 + nwords[j-1]);
}
}
return nwords[n] > 0;
}
\end{minted}
\question{2c: number of ways to partition $s, t$ into words at same indexes}
\begin{minted}{cpp}
// cc = closed-closed interval.
int q2c(string s, string t) {
const int n = s.size();
vector<int> dp(n+1, 0);
// dp[i] = number of words in s[0:i)
for(int i = 1; i <= n+1 ++i) {
for(int j = 1; j <= i - 1; ++j) {
if (!isword(s[cc(j, i-1)]) || !isword(t[cc(j, i-1)])) { continue; }
nwords[i] = max(nwords[i], 1 + nwords[j-1]);
}
}
return nwords[n];
}
\end{minted}
\question{3a: largest sum subarray} Standard solution: kendane's algorithm
\begin{minted}{cpp}
int q3a(const vector<int> &xs) {
const int n = xs.size();
int best = 0;
int cur = 0;
for(int i = 0; i < n; ++i) {
if (cur + xs[i] < 0) { continue; }
cur += xs[i];
best = max<int>(best, cur);
}
return best;
}
\end{minted}
$\Theta(n^2)$ solution using
\question{3b: largest product subarray}
I have a somewhat dubious $\Theta(n)$ solution. I'd love to know
a correctness proof.
\begin{minted}{cpp}
int best = 1, pos = 1, neg = 1;
int q3b(const vector<int> &xs) {
const int n = xs.size();
for(int i = 0; i < xs.size(); ++i) {
if (xs[i] == 0) { pos = neg = 1; }
else if (xs[i] < 0) {
const int prevpos = pos;
pos = neg * xs[i]; neg = prevpos * xs[i];
} else if (xs[i] > 0) { pos *= xs[i]; neg *= xs[i]; }
best = max<int>(best, pos);
}
return best;
}
\end{minted}
\question{4a: largest sum circular subarray}
Use kendane's algorithm/sliding window on $\texttt{xs} \diamond \texttt{xs}$
with a window length restriction of $|xs|$.
\begin{minted}{cpp}
int q3a(const vector<int> &xs) {
xs.append(xs);
const int n = xs.size();
int best = 0;
int cur = 0;
left = 0;
for(int i = 0; i < 2*n; ++i) {
if (cur + xs[i] < 0) { left = i; continue; }
cur += xs[i];
// closed-closed: [left, cur]
// maintain length of |n|
if (cur - left + 1 > n) {
cur -= xs[left]; left++;
}
best = max<int>(best, cur);
}
return best;
}
\end{minted}
\question{4b: long subarray sum}
Use kendane's algorithm/sliding window on $\texttt{xs}$
with a window length restriction that it must be greater than $X$.
\begin{minted}{cpp}
int q3a(const vector<int> &xs, int X) {
xs.append(xs);
const int n = xs.size();
int best = 0;
int cur = 0;
left = 0;
for(int i = 0; i < 2*n; ++i) {
if (cur + xs[i] < 0) { left = i; continue; }
cur += xs[i];
// closed-closed: [left, cur]
// only consider if has #elem > X
if (cur - left + 1 >= X) {
best = max<int>(best, cur);
}
}
return best;
}
\end{minted}
\question{4c: short subarray sum}
Use kendane's algorithm/sliding window on $\texttt{xs}$
with a window length restriction that it must be at most $X$.
\begin{minted}{cpp}
int q3a(const vector<int> &xs, int X) {
const int n = xs.size();
int best = 0;
int cur = 0;
left = 0;
for(int i = 0; i < 2*n; ++i) {
if (cur + xs[i] < 0) { left = i; continue; }
cur += xs[i];
// closed-closed: [left, cur]
// only consider if has #elem <= X
if (cur - left + 1 <= X) {
best = max<int>(best, cur);
}
}
return best;
}
\end{minted}
\question{4d: small subarray sum}
This is much more challenging, since the subarray sum is not a
monotonic quantity. The best I have is a naive $\Theta(n^2)$
algorithm that tries all subarrays.
\begin{minted}{cpp}
int q3a(const vector<int> &xs, int X) {
const int n = xs.size();
int best = 0;
for(int i = 0; i < n; ++i) {
int sum = 0;
for(int j = i; j < n; ++j) {
sum += xs[j];
if (sum < X) { best = max<int>(best, sum); }
}
}
return best;
}
\end{minted}
\question{5a: longest common subsequence}
Standard DP.
\begin{minted}{cpp}
int q5a(const vector<int> &xs, const vector<int> &ys) {
// dp[lx][ly]: LCS between xs[0:lx) and ys[0:ly) [closed-open].
// lx, ly for ``length of xs, length of ys''
vector<vector<int>> dp(1+xs.size(), vector<int>(1+ys.size(), 0));
int best = 0;
for(int lx = 1; lx <= xs.size(); ++lx) {
for(int ly = 1; ly <= ys.size(); ++ly) {
dp[lx][ly] = max(dp[lx-1][ly-1], dp[lx][ly-1], dp[lx-1][ly]);
if (xs[lx-1] == ys[ly-1]) {
dp[lx][ly] = max(dp[lx][ly], 1 + dp[lx-1][ly-1]);
}
}
}
return dp[xs.size()][ys.size()];
}
\end{minted}
\question{5b: longest common supersequence}
Standard DP.
\begin{minted}{cpp}
int q(const vector<int> &xs, const vector<int> &ys) {
// dp[lx][ly]:
// longest common super-sequence
// between xs[0:lx) and ys[0:ly) [closed-open].
// lx, ly for ``length of xs, length of ys''
const int INFTY = 1e9;
vector<vector<int>> dp(1+xs.size(), vector<int>(1+ys.size(), INFTY));
int best = 0;
for(int lx = 1; lx <= xs.size(); ++lx) {
for(int ly = 1; ly <= ys.size(); ++ly) {
if (xs[lx-1] == ys[ly-1]) {
// add one letter (the equal letter).
dp[lx][ly] = min(dp[lx][ly], 1 + dp[lx-1][ly-1]);
} else {
// add two letters (x and y).
dp[lx][ly] = min(2 + dp[lx-1][ly-1], dp[lx][ly]);
}
// try adding only x / only y, punting other letter.
dp[lx][ly] = min(1 + dp[lx][ly-1], dp[lx][ly]);
dp[lx][ly] = min(1 + dp[lx-1][ly], dp[lx][ly]);
}
}
return dp[xs.size()][ys.size()];
}
\end{minted}
\question{5c: longest bitonic subsequence}
Use naive $\Theta(n^2)$ LIS to compute longest increasing
subsequence in $x[0:i]$ and longest decreasing subsequence
at $x[i:N]$. Combine these to get longest bitonic
sequence at $i$ as $\texttt{lis}(i) + \texttt{lds}(i) - 1$.
We need a $-1$ to prevent over-counting of the middle $i$ term.
\begin{minted}{cpp}
// out[i]: lis of xs[0:i]
int mklis(vector<int> &xs, vector<int> &out) {
const int n = xs.size();
out.reserve(n, 0);
out[0] = 1;
for(int i = 1; i < n; ++l) {
// loop invariant: out[i] contains length of LIS
// of subsequence from xs[0:i).
out[i] = out[i-1]; // can make at least as long.
for(int j = 0; j < i; ++j) {
if (xs[i] > xs[j]) { // use xs[i].
out[i] = max(out[i], out[j]+1);
}
}
}
}
// similarly write mklds which computes lds[i:xs.size())
int q(const vector<int> &xs) {
vector<int> liss; mklis(xs, liss);
vector<int> ldss; mklds(xs, ldss);
int best = 1;
for(int i = 0; i < xs.size() ++i) {
best = max(best, liss[i] + ldss[i] - 1);
}
return best;
}
\end{minted}
\question{5d: longest oscillating subsequence}
For even $i$: $xs[i] < xs[i+1]$. For odd $i$, $xs[i] > xs[i+1]$.
\begin{minted}{cpp}
int q(const vector<int> &xs) {
const int n = xs.size();
// dp[i][0]: length of LOS of subseq of xs[i:n]
// with first of LOS having even index.
// dp[i][1]: length of LOS of subseq of xs[i:n]
// with first term of LOS having odd index.
vector<pair<int, int>> dp(n, 0);
dp[n-1][0] = dp[n-1][1] = 1; // can always use highest index.
for(int i = n-2; i >= 0; i--) {
for(int j = i+1; j < n; ++j) {
dp[i][0] max (dp[i][0], dp[j][0]); // no new elem
dp[i][1] max (dp[i][1], dp[j][1]); // no new elem.
if (xs[i] < xs[j]) {
// we are in the even index. j will be odd index.
dp[i][0] = max(dp[i][0], dp[j][1] + 1);
} else if (xs[i] > xs[j]) {
// we are in the odd index. j will be even index.
dp[i][1] = max(dp[i][1], dp[j][0] + 1);
}
}
}
int best = 0;
for(int i = 0; i < n; ++i) {
best = max(best, dp[i][0], dp[i][1]);
}
return best;
}
\end{minted}
\question{5d: shortest oscillating super-sequence}
For even $i$: $xs[i] < xs[i+1]$. For odd $i$, $xs[i] > xs[i+1]$.
TODO TODO!
\question{5e: longest convex subsequence}
Find a subsequence $X$ such that $2X[i] < X[i-1] + X[i+1]$. I propose
a $\Theta(n^3)$ solution. Surely faster solutions exist?
\begin{minted}{cpp}
int conv(int ip1, int i, const vector<int> &xs) {
assert(i < ip1);
// is always possible to have any sequence that just uses [i, ip1].
int best = 2;
for(int im1 = 0; im1 < i; ++im1) {
if (2*xs[i] < xs[im1] + xs[ip1]) {
best = max(best, 1 + conv(i, im1));
}
}
return best;
}
/wo
int f(vector<int> &xs) {
const int n = xs.size();
if (n <= 2) { return n; }
// dp the above conv() recurrence.
int best = 0;
for(int ip1 = 0; ip1 < n; ++i) {
for(int i = 0; i < ip1; ++i) {
best = max<int>(best, conv(ip1, i));
}
}
return best;
}
\end{minted}
\end{document}