1. Box Problem
Rex Yuan
May 31, 2015
Dynamic Programming Approach
This algorithm reads in a list of data with each element representing a box
and containing the width, length, and height in sorted decreasing order of the
box and the volume of the box. Then we create a list, box table to store the
data and sort it in the decreasing order of volume of the box. Then we iterate
through box table, finding all the boxes that can fit inside a box and create an
adjacency matrix, matrix, to store the information. Then we create an empty
table, table, which is filled with 0s except for the ones lying on the diagonal line
from top-left corner to bottom-right corner which are willed with 1s. We iterate
through table bottom to top and right to left. Iterating through table, we find
boxes that can fit inside a box of index i according to matrix and by checking
their sides. We store the maximum number of boxes the box of the index of
i can fit inside of in table[i][i]. Finally we create a list, results, which stores
all the element on the top-left bottom-right line of table. Then we return the
element with maximum value in results.
Time Complexity
In creating the table for dynamic programming approach, we iterate through
the table both vertically and horizontally. So the time complexity is
O(n2
)
where n is the number of input boxes.
1
2. Algorithm 1 Box Problem DP Algorithm
procedure Box(boxes)
box table = sorted boxes by decreasing order of volume
matrix = empty adjacency filled with 0s matrix for a graph of possible
box sequence
for p box in boxes do
for c box in boxes do
if p box != c box and p box.side1 ¿ c box.side1 and p box.side2 ¿
c box.side2 and p box.side3 ¿ c box.side3 then
matrix[p box][c box.index] = 1
end if
end for
end for
dp table = empty table filled with 0s and for each element in the diagonal
line from top-left corner to bottom-right corner with 1s
for i in range(len(boxes), −1, −1) do max in i = table
for j in range(i + 1, len(boxes)) do
if matrix[i][j] == 1 then
table[i][j] = max(table[i][j − 1], table[i][i] + table[i][j]
if table[i][j] ¿ max in i then max in i = table[i][j]
end if
else
table[i][j] = table[i][j − 1]
end if
end fortable[i][i] = max in i
end for
results = []
for n in range(len(boxes)) do
results.append(table[n][n])
end for
return max(results)
end procedure
2
3. Run Time Stats
I created the following table using UNIX POSIX time function and round the
mean time of 10 trials to five digits after decimal point to calculate the time
past. All samples have the ten possible characters to be encoded, that is 0 9.
Variable input length implies the length of the sequence to be decoded.
Run Time Stats
Sample Input Size Box DP
1 9 0.00023
2 500 0.25236
3 750 0.57245
3