# Generating random grids ## The Task Given an amount $p = p_1 \cdot p_2\cdot \dots \cdot p_n$ of random points in an $n$-dimensional unit-cube ($[0..1]^n$), find a regular grid^[i.e. no intersections of cells, each point inside the grid is connected to $2n$ points] with grid-dimensions of $p_1 \times \dots \times p_n$. ## The Algorithm This Algorithm is a simple greedy-algorithm recursing over the dimensionality to construct one valid solution. Choose two dimensions $i$ and $j$. Sort the points by dimension $i$, divide them into chunks of $p_j$ points and sort the points inside these chunks along $x_j-x_i$. Now you connect every $k$-th ($0 \leq k < p_j$) point of each chunk to generate lines along the $i$-th dimension. By definition these paths cannot intersect each other in an projection onto the $i$/$j$-plane (see [Proof](#proof)). Let the "starting point" (i.e. the smallest in dimension $i$ on the line) be the representative for the line. Recurse over the other dimensions without choosing dimension $i$ again in a similar way. In the recursive call the "1:1 point merging" will become "1:1 line-merging" (and "1:1 grid-merging") where you connect the $k$-th component of each line/grid. The resulting grid will be regular because no cell will overlap with another one due to careful construction^[TODO: Proof the same argument as above holds for arbitrary grids using only properties of the representing point] and each point inside gets 2 new neighbors in each of the $n$ calls of the function resulting in $2n$ neighbors. ## Application For our thesis we only need the cases of $n=2$ and $n=3$, but having a higher-dimensional solution around may come in handy in the future. ## Proof It suffices to show that given 2 points of chunk $i$ and 1 point of chunk $i+1$ there exists no point s.t. the line-segments created by these four points intersect. Keep in mind that the intersection is in the $i$/$j$-projection and we are working in a unit-square. Let $a,b$ be the points in the $i$th-chunk and $x,y$ the points in the $i+1$-th chunk. Further shall a,b and x be fixed. We know (w.l.o.g., by construction), that - $\lbrace a_i, b_i \rbrace < \lbrace x_i, y_i \rbrace$ - $a_j - a_i < b_j - b_i$ Assume $y$ is placed in a way that the generated lines intersect. We have two cases to consider: 1. $x_j - x_i < y_j - y_i$ In this case the algorithm says that we have to connect $x$ to $a$ and $y$ to $b$. As we can only choose $y$, this point has to have a greater distance to $b$ than the $x/a$-line. $\lambda a + (1-\lambda) x$ for $\lambda \in [0..1]$ are all points on the line.