-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathrod_cutting.rb
More file actions
226 lines (214 loc) · 7 KB
/
rod_cutting.rb
File metadata and controls
226 lines (214 loc) · 7 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
module DynamicProgramming
class RodCutting
class << self
# Internal: Method to calculate the maximum and optimal solution for cutting a
# rod of a particular length
# Recursive Strategy
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# cut_rod(p, 9)
# => 31
def cut_rod(p, n)
return 0 if n == 0
q = -Float::INFINITY
(0..n-1).each { |i| q = [q, p[i] + cut_rod(p, n-i-1)].max }
return q
end
# Internal: Method to calculate the maximum and optimal solution for cutting a
# rod of a particular length
# Recursive Memoization Technique
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# memoized_cut_rod(p, 9)
# => 25
def memoized_cut_rod(p, n)
r = n.times.map { |x| -Float::INFINITY }
memoized_cut_rod_aux(p, n, r)
end
# Internal: Auxiliary method to calculate the maximum and optimal solution
# for cutting a rod of a particular length while populating the
# optimal solution array
# Recursive Memoization Technique
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
# r - Optimal solution array
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# memoized_cut_rod_aux(p, 9, [-Float::INFINITY, -Float::INFINITY, ... , -Float::INFINITY])
# => 25
def memoized_cut_rod_aux(p, n, r)
return r[n-1] if r[n-1] >= 0
if n == 0
q = 0
else
q = -Float::INFINITY
(0..n-1).each { |i| q = [q, p[i] + memoized_cut_rod_aux(p, n-i-1, r)].max }
end
r[n-1] = q
q
end
# Internal: Extension method to memoized_cut_rod to return a tuple of
# max revenues and array consisting of optimal index cuts
# Recursive Memoization Technique
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# memoized_cut_rod_with_recorder(p, 9)
# => [
# [1, 5, 8, 10, 13, 17, 18, 22, 25, 30],
# [1, 2, 3, 2, 2, 6, 1, 2, 3, 10]
# ]
def memoized_cut_rod_with_recorder(p, n)
r = n.times.map { |x| -Float::INFINITY }
s = n.times.map { |x| 0 }
memoized_cut_rod_aux_with_recorder(p, n, r, s)
end
# Internal: Auxiliary extenstion method to memoized_cut_rod_aux to
# calculate the optimal revenue array and the cut index array
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
# r - Optimal solution array - NOTE: To be initialized to -INFINITY array
# s - Cut index array - NOTE: To be initialized to a zero array by callee
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# memoized_cut_rod_aux(p, 9, [-Float::INFINITY, ..], [0, 0, ..])
# => [
# [1, 5, 8, 10, 13, 17, 18, 22, 25, 30],
# [1, 2, 3, 2, 2, 6, 1, 2, 3, 10]
# ]
def memoized_cut_rod_aux_with_recorder(p, n, r, s)
return r[n-1] if r[n-1] >= 0
index = 0
if n == 0
index = 0
q = 0
else
q = -Float::INFINITY
(0..n-1).each do |i|
old_optimal_revenue = memoized_cut_rod_aux_with_recorder(p, n-i-1, r, s)
if old_optimal_revenue.class == Array
old_optimal_revenue = old_optimal_revenue.first[n-i-2]
end
if p[i] + old_optimal_revenue > q
q = p[i] + old_optimal_revenue
index = i
end
end
end
s[n-1] = index+1
r[n-1] = q
[r, s]
end
# Internal: Auxiliary method to memoized_cut_rod_with_recorder to print
# the rod-lengths to be cut in order to maximize the revenue
#
# s - Optimal cut index array obtained from memoized_cut_rod_with_recorder
# n - Length of rod to be cut
#
# Examples
# s = [1, 2, 3, 2, 2, 6, 1, 2, 3, 10]
# cut_locations(s, 7)
# => [1, 6]
def cut_locations(s, n)
breakable = false
cut_list = []
while true
cut_list << s[n-1]
n = n - s[n-1]
break if n == 0
end
cut_list
end
# Internal: Method to calculate the maximum and optimal solution for cutting a
# rod of a particular length
# Recursive Memoization Technique. RUBIFIED METHOD!
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# memoized_cut_rod(p, 9)
# => 25
def sleek_memoized_cut_rod_aux(p, n, r={})
return 0 if n == 0
q = -Float::INFINITY
(0..n-1).each do |i|
prevRodCut = r[n-i-1] || (r[n-i-1] = sleek_memoized_cut_rod_aux(p, n-i-1, r))
q = [q, p[i] + prevRodCut].max
end
q
end
# Internal: Calculates the maximum price output by calculating in a bottom
# up fashion
# Iterative Bottom up strategy
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# bottom_up_cut_rod(p, 9)
# => 25
def bottom_up_cut_rod(p, n)
r = n.times.map { |x| 0 }
(1..n).each do |j|
q = -Float::INFINITY
(0..j-1).each { |i| q = [q, p[i] + r[j-i-1]].max }
r[j] = q
end
r[n]
end
# Internal: Calculates the maximum price output by calculating in a bottom
# up fashion with an optimal solution array to record the length
# of each rod cut at every iteration
# Iterative Bottom up strategy
#
# p - Price list to cut a rod of certain length
# n - Length of rod to be cut
#
# Examples
# p = [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]
# extended_bottom_up_cut_rod(p, 9)
# => 25
def extended_bottom_up_cut_rod(p, n)
s = []
s[0] = 0
r = n.times.map{ |x| 0 }
(1..n).each do |j|
q = -Float::INFINITY
(0..j-1).each do |i|
if (q < p[i] + r[j-i-1])
q = p[i] + r[j-i-1]
s[j] = i+1
end
r[j] = q
end
end
[r, s]
end
def print_cut_rod_solution(p, n)
r, s = extended_bottom_up_cut_rod(p, n)
while n > 0
p s[n]
n = n - s[n]
end
end
end
end
end