24-2 Nesting boxes

A dd-dimensional box with dimensions (x1,x2,,xd)(x_1, x_2, \ldots, x_d) nests within another box with dimensions (y1,y2,,yd)(y_1, y_2, \ldots, y_d) if there exists a permutation π\pi on {1,2,,d}\{1, 2, \ldots, d\} such that xπ(1)<y1x_{\pi(1)} < y_1, xπ(2)<y2x_{\pi(2)} < y_2, \ldots, xπ(d)<ydx_{\pi(d)} < y_d.

a. Argue that the nesting relation is transitive.

b. Describe an efficient method to determine whether or not one dd-dimensional box nests inside another.

c. Suppose that you are given a set of nn dd-dimensional boxes {B1,B2,,Bn}\{B_1, B_2, \ldots, B_n\}. Give an efficient algorithm to find the longest sequence Bi1,Bi2,,Bik\langle B_{i_1}, B_{i_2}, \ldots, B_{i_k} \rangle of boxes such that BijB_{i_j} nests within Bij+1B_{i_{j + 1}} for j=1,2,,k1j = 1, 2, \ldots, k - 1. Express the running time of your algorithm in terms of nn and dd.

a. Suppose that box x=(x1,,xd)x = (x_1, \dots, x_d) nests with box y=(y1,,yd)y = (y_1, \dots, y_d) and box yy nests with box z=(z1,,zd)z = (z_1, \dots, z_d). Then there exist permutations π\pi and σ\sigma such that xπ(1)<y1,,xπ(d)<ydx_{\pi(1)} < y_1, \dots, x_{\pi(d)} < y_d and yσ(1)<z1,,yσ(d)<zdy_{\sigma(1)} < z_1, \dots, y_{\sigma(d)} < z_d. This implies xπ(σ(1))<z1,,xπ(σ(d))<zdx_{\pi(\sigma(1))} < z_1, \dots, x_{\pi(\sigma(d))} < z_d, so xx nests with zz and the nesting relation is transitive.

b. Box xx nests inside box yy if and only if the increasing sequence of dimensions of xx is component-wise strictly less than the increasing sequence of dimensions of yy. Thus, it will suffice to sort both sequences of dimensions and compare them. Sorting both length dd sequences is done in O(dlgd)O(d\lg d), and comparing their elements is done in O(d)O(d), so the total time is O(dlgd)O(d\lg d).

c. We will create a nesting-graph GG with vertices B1,,BnB_1, \dots, B_n as follows. For each pair of boxes BiB_i , BjB_j, we decide if one nests inside the other. If BiB_i nests in BjB_j, draw an arrow from BiB_i to BjB_j. If BjB_j nests in BiB_i, draw an arrow from BjB_j to BiB_i. If neither nests, draw no arrow. To determine the arrows efficiently, after sorting each list of dimensions in O(ndlgd)O(nd\lg d) we compair all pairs of boxes using the algorithm from part (b) in O(n2d)O(n^2 d). By part (a), the resulted graph is acyclic, which allows us to easily find the longest chain in it in O(n2)O(n^2) in a bottom-up manner. This chain is our answer. Thus, the total time is O(ndmax(lgd,n))O(nd\max(\lg d, n)).