box problem
TRANSCRIPT
Box Problem
Rex Yuan
May 31, 2015
Dynamic Programming Approach
This algorithm reads in a list of data with each element representing a boxand containing the width, length, and height in sorted decreasing order of thebox and the volume of the box. Then we create a list, box table to store thedata and sort it in the decreasing order of volume of the box. Then we iteratethrough box table, finding all the boxes that can fit inside a box and create anadjacency matrix, matrix, to store the information. Then we create an emptytable, table, which is filled with 0s except for the ones lying on the diagonal linefrom top-left corner to bottom-right corner which are willed with 1s. We iteratethrough table bottom to top and right to left. Iterating through table, we findboxes that can fit inside a box of index i according to matrix and by checkingtheir sides. We store the maximum number of boxes the box of the index ofi can fit inside of in table[i][i]. Finally we create a list, results, which storesall the element on the top-left bottom-right line of table. Then we return theelement with maximum value in results.
Time Complexity
In creating the table for dynamic programming approach, we iterate throughthe table both vertically and horizontally. So the time complexity is
O(n2)
where n is the number of input boxes.
1
Algorithm 1 Box Problem DP Algorithm
procedure Box(boxes)box table = sorted boxes by decreasing order of volumematrix = empty adjacency filled with 0s matrix for a graph of possible
box sequencefor p box in boxes do
for c box in boxes doif 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 thenmatrix[p box][c box.index] = 1
end ifend for
end fordp table = empty table filled with 0s and for each element in the diagonal
line from top-left corner to bottom-right corner with 1sfor i in range(len(boxes),−1,−1) do max in i = table
for j in range(i + 1, len(boxes)) doif 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
elsetable[i][j] = table[i][j − 1]
end ifend fortable[i][i] = max in i
end forresults = []for n in range(len(boxes)) do
results.append(table[n][n])end forreturn max(results)
end procedure
2
Run Time Stats
I created the following table using UNIX POSIX time function and round themean time of 10 trials to five digits after decimal point to calculate the timepast. 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 StatsSample Input Size Box DP
1 9 0.000232 500 0.252363 750 0.57245
3