forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_programming.py
More file actions
338 lines (293 loc) · 10.4 KB
/
dynamic_programming.py
File metadata and controls
338 lines (293 loc) · 10.4 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This file contains Python implementations of dynamic programming
from Intro to Algorithms (Cormen et al.).
The aim here is not efficient Python implementations
but to duplicate the pseudo-code in the book as closely as possible.
Also, since the goal is to help students to see how the algorithm
works, there are print statements placed at key points in the code.
The performance of each function is stated in the docstring, and
loop invariants are expressed as assert statements when they
are not too complex.
This file contains:
cut_rod()
memo_cut_rod()
memo_cut_rod_aux()
bottom_up_cut_rod():
matrix_chain_order()
"""
import sys
NEG_REV = -1
calls = 0
def cut_rod(prices, n):
"""
Args:
prices: an array of prices 0..n for rods of various lengths
n: length of rod
Returns:
max_rev: the maximum revenue possible from cutting a rod of length n.
Note: This is not a good value to return in a real world
application: what we would really like returned is what
cuts to make to *achieve* that maximum revenue!
Our textbook handles this in the extended version of bottom up rod
cutting.
Note2: CLRS calls variable q.
"""
if n > len(prices):
print("Not enough prices for rod of len " + str(n))
return None
global calls
calls += 1
if n == 0:
return 0
max_rev = NEG_REV # CLRS uses negative infinity, but that hardly seems
# necessary: if rods do not sell at a positive price,
# best to just go out of business immediately!
for i in range(0, n):
max_rev = max(max_rev,
prices[i] + cut_rod(prices[:n - i - 1], n - i - 1))
print("Handling rod of len " + str(n) + "; max revenue currently is " +
str(max_rev))
print("Calls = " + str(calls))
return max_rev
def memo_cut_rod(prices, n):
"""
Args:
prices: an array of prices 0..n for rods of various lengths
n: length of rod
Returns:
max_rev: maximum revenue possible
"""
if n == 0:
return 0
max_rev = NEG_REV
revs = [NEG_REV for i in range(n)]
return memo_cut_rod_aux(prices, n, revs)
def memo_cut_rod_aux(prices, n, revs):
"""
Args:
prices: an array of prices 0..n for rods of various lengths
revs: revenues previously calculated
n: length of rod
Returns:
max_rev: maximum revenue
"""
global calls
max_rev = NEG_REV
if revs[n - 1] >= 0:
print("Using memo value with n = " + str(n)
+ " and revs[n - 1] = "
+ str(revs[n - 1]))
return revs[n - 1]
if n == 0:
max_rev = 0
else:
calls += 1
for i in range(0, n):
max_rev = max(max_rev,
prices[i]
+ memo_cut_rod_aux(prices[:n - i - 1],
n - i - 1,
revs))
print("Handling rod of len " + str(n)
+ "; max revenue currently is " +
str(max_rev))
revs[n - 1] = max_rev
print("Calls = " + str(calls))
return max_rev
def bottom_up_cut_rod(prices, n):
"""
Args:
prices: an array of prices for different rod lengths
n: length of rod
Returns:
max_rev: maximum revenue possible
"""
if n > len(prices):
print("Not enough prices for rod of len " + str(n))
return None
max_rev = NEG_REV
revs = [0 for i in range(n + 1)] # CLRS only initializes first elem,
# but no harm doing all
for j in range(1, n + 1): # 0th element holds a 0
max_rev = NEG_REV
for i in range(j):
print("Checking max_rev between " + str(max_rev)
+ " and j = " + str(j) + "; i = " + str(i)
+ "; j - i - 1 = " + str(j - i - 1))
print("prices[i] = " + str(prices[i]) +
"; revs[j - i - 1] = " + str(revs[j - i - 1]))
max_rev = max(max_rev, prices[i] + revs[j - i - 1])
print("Maximum revenue calculated at step " + str(j)
+ " = " + str(max_rev))
revs[j] = max_rev
return revs[n]
def ext_bottom_up_cut_rod(prices, n):
"""
This calculates the maximum revenue, but also returns a list of what
cuts generate it.
Args:
prices: an array of prices for different rod lengths
n: length of rod
Returns:
max_rev: maximum revenue possible
"""
if n > len(prices):
print("Not enough prices for rod of len " + str(n))
return None
revs = [0 for i in range(n + 1)] # CLRS only initializes first elem,
# but no harm doing all
cuts = [0 for i in range(n + 1)]
for j in range(1, n + 1): # 0th element holds a 0
print("\n********\n Working on foot: " + str(j))
max_rev = NEG_REV
for i in range(j):
prev_revs = revs[j - i - 1]
print("Revs = " + str(revs))
print("Comparing max_rev of " + str(max_rev)
+ " with " + str(prices[i])
+ " plus prev_rev of " + str(prev_revs))
if max_rev < prices[i] + prev_revs:
max_rev = prices[i] + prev_revs
cuts[j] = i + 1
revs[j] = max_rev
return (revs, cuts, max_rev)
def print_cut_rod(prices, n):
"""
Args:
prices: price array
n: length of rod
Returns:
None
Prints optimal cuts
"""
(revs, cuts, max_rev) = ext_bottom_up_cut_rod(prices, n)
while n > 0:
print("Make a cut of: " + str(cuts[n]))
n = n - cuts[n]
print("And we achieve revenue of " + str(max_rev))
# some price arrays for testing rod cutting:
p1 = [3]
p2 = [3, 7]
p3 = [3, 7, 9]
p4 = [3, 7, 9, 12]
p7 = [3, 5, 10, 11, 14, 17, 20]
p10 = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
"""
On to determining optimal matrix multiplication order.
"""
BIG_NUM = sys.maxsize
def matrix_chain_order(p):
"""
Args:
p: a list of dimensions so that
matrix i's dimensions are found at
p[i - 1] and p[i].
Returns:
list of m and list of s for parenthisation
"""
n = len(p) - 1
m = [[BIG_NUM for x in range(n)] for x in range(n)]
for i in range(n):
m[i][i] = 0
s = [[-1 for x in range(n)] for x in range(n)]
for l in range(2, n + 1):
print("Working on chain length: " + str(l))
for i in range(0, n - l + 1):
j = i + l - 1
print("i = " + str(i) + "; j = " + str(j))
m[i][j] = BIG_NUM
for k in range(i, j):
print("k = " + str(k))
print("Cost = m[i][k] (" + str(m[i][k]) +
") + m[k+1][j] (" + str(m[k + 1][j]) +
") + p[i] (" + str(p[i]) +
") * p[k+1] (" + str(p[k + 1]) +
") * p[j+1] (" + str(p[j + 1]) + ")")
q = (m[i][k] + m[k + 1][j]
+ (p[i] * p[k+1] * p[j+1]))
print("Comparing q = " + str(q)
+ " with m[i][j] = "
+ str(m[i][j]) + " with i = "
+ str(i) + " and j = " + str(j)
+ " and k = " + str(k))
if q < m[i][j]:
m[i][j] = q
s[i][j] = k
return (m, s)
def print_optimal_parens(s, i, j):
"""
Args:
s: the list returned by matrix_chain_order()
i: start index
j: end index
Returns: None; prints results.
"""
if i == j:
print(" A", end="")
else:
print("(", end="")
print_optimal_parens(s, i, s[i][j])
print_optimal_parens(s, s[i][j] + 1, j)
print(")", end="")
# an M of size n holds the dimensions for n - 1 matrices!
# dims 2 ... n - 1 are the 2nd dim of one M and the 1st dim of the
# next one.
M2 = [10, 100, 5]
M3 = [10, 100, 5, 50]
M4 = [10, 100, 5, 50, 80]
clrs_test = [30, 35, 15, 5, 10, 20, 25] # this is the example from page 376
########################
def optimal_bst(p, q, n):
"""
Args:
p: probabilities of actual items
q: probabilities of failed searches (1 bigger than p!)
n: number of items in p
Returns:
e: expected search costs (a 2D list)
root: of the constructed BST
"""
# three 2D arrays: e is expected search costs, w is probabilities,
# and root is ?
e = [[BIG_NUM for x in range(n + 1)] for x in range(n + 1)]
w = [[0 for x in range(n + 1)] for x in range(n + 1)]
root = [[BIG_NUM for x in range(n)] for x in range(n)]
for i in range(0, n + 1):
e[i][i] = q[i]
print("i = " + str(i) + "; e[i][i] = " + str(e[i][i]))
w[i][i] = q[i]
print("i = " + str(i) + "; w[i][i] = " + str(w[i][i]))
for l in range(0, n):
print("\n*********")
print("*********")
print("*********")
print("Outer loop; l = " + str(l))
for i in range(0, n - l):
j = i + l
print("\n*********")
print("*********")
print("Middle loop; i = " + str(i) +
"; j = " + str(j))
print("w[i][j + 1] = " + str(w[i][j]) + " + "
+ str(p[j]) + " + " + str(q[j + 1]))
w[i][j + 1] = w[i][j] + p[j] + q[j + 1]
w[i][j + 1] = round(w[i][j + 1], 2)
for r in range(i + 1, j + 2):
print("\n*********")
print("Inner loop; r = " + str(r))
t = e[i][r - 1] + e[r][j + 1] + w[i][j + 1]
print("t = " + str(t))
if t < e[i][j + 1]:
e[i][j + 1] = round(t, 2)
print("Storing in e[" + str(i) + "]["
+ str(j) + "] t of " + str(e[i][j + 1]))
root[i][j] = r
return (e, root)
# probabilities from CLRS:
smallp = [.15, .10, .05, .10, .20]
smallq = [.05, .10, .05, .05, .05, .10]
# probabilities from CLRS:
p = [.15, .10, .05, .10, .20]
q = [.05, .10, .05, .05, .05, .10]