6

I'm looking for an algorithm that can partition a rectangle into N smaller rectangles, at random. N will be small (on the order of 5-10 in most cases). It need not be uniformly at random, but at the very least, I'd like to avoid algorithms which completely exclude interesting classes of partitions.

One algorithm I've come across a few times (including a question on Math.SE about the number of ways to do n partitions), is to randomly divide the rectangle in two by drawing an horizontal or vertical line across it, then do the same thing recursively to the resulting smaller rectangles until you get what you want.

The trick here is that although you can get a number of different partitions using this scheme:

binary partitionings

There are some ways of dividing a rectangle into smaller rectangles that this method will never produce:

enter image description here

You can easily see that the divide-and-conquer partitioning will never generate the above, because there's no line that could possibly be the first cut, since no single straight line divides the entire square into two smaller rectangles.

So basically I'm looking for an algorithm to randomly divide a rectangle up into N smaller rectangles, where the results meet the following criteria:

  • Partitioned into exactly N smaller rectangles
  • Everything must actually be a rectangle (no L shapes, etc)
  • No empty space left over

The algorithm as a whole should meet the following criteria:

  • There is no valid partitioning that the algorithm is incapable of producing
  • Can be biased towards certain types of configurations, though the less bias the better.
  • Preferably works with integer indices.
  • Does not operate by generating all possible partitions and then picking one at random.

I don't care about asymptotic complexity much, because I'll be working with small numbers.

philomory
  • 162
  • 1
  • 5

2 Answers2

1

Here's a simple idea that I believe can generate all partitions. There are two types of subdivisions:

  1. Given a rectangle, randomly generate a horizontal or vertical line to partition it into two subrectangles. (The usual "split in two" rule.)
  2. Given a rectangle, randomly generate a rectangle that fits inside it. Consider the 4 corners of this inner contained rectangle: for each of them, flip a coin and draw a line from this corner outwards towards the outer containing rectangle, with the direction of the line (horizontal or vertical) being determined by what the coin says. Regardless of the directions of the 4 lines, the result will always be 5 rectangles. (Some partitions that can be generated by rule 1 can also be generated this way -- in fact, only 2 of the 16 possible configurations of lines will produce partitions that could not be produced by rule 1.)

At any point in time you will have a set of rectangles (initially, just the original one). Randomly choose one to subdivide (or you could, e.g., choose the biggest, etc.), flip a coin to decide which of the two rules to apply to it, and then apply that rule; keep doing this until you have $n$ rectangles.

Disclaimer: I have no idea how to even think about the set of all partitions, let alone whether the above procedure generates them all with equal probability... Probably not.

j_random_hacker
  • 5,509
  • 1
  • 17
  • 22
-3

(Disclaimer : I have not thought about throughly nor tested this idea ) ... Why not keep track of the largest available area amongst all of the sections, pick the one with the maximum area and draw a random line segment (either vertical or horizontal) within that selected bounding area. This would create two new section and recurse.

PNL QLF
  • 34
  • 2