24-2 Nesting boxes
A -dimensional box with dimensions nests within another box with dimensions if there exists a permutation on such that , , , .
a. Argue that the nesting relation is transitive.
b. Describe an efficient method to determine whether or not one -dimensional box nests inside another.
c. Suppose that you are given a set of -dimensional boxes . Give an efficient algorithm to find the longest sequence of boxes such that nests within for . Express the running time of your algorithm in terms of and .
a. Suppose that box nests with box and box nests with box . Then there exist permutations and such that and . This implies , so nests with and the nesting relation is transitive.
b. Box nests inside box if and only if the increasing sequence of dimensions of is component-wise strictly less than the increasing sequence of dimensions of . Thus, it will suffice to sort both sequences of dimensions and compare them. Sorting both length sequences is done in , and comparing their elements is done in , so the total time is .
c. We will create a nesting-graph with vertices as follows. For each pair of boxes , , we decide if one nests inside the other. If nests in , draw an arrow from to . If nests in , draw an arrow from to . If neither nests, draw no arrow. To determine the arrows efficiently, after sorting each list of dimensions in we compair all pairs of boxes using the algorithm from part (b) in . By part (a), the resulted graph is acyclic, which allows us to easily find the longest chain in it in in a bottom-up manner. This chain is our answer. Thus, the total time is .