The Art Gallery Guardian

Pack a histogram


Consider we have a histogram of values and distinct colors. The th bar is a rectangle of size , where is the value and has color . The total area of all the bars are . We are interested in make a few cuts to pack it into squares each one of area , such that each square is consist of at most different colors.

Formally, we are seeking an algorithm for the following problem:

Problem1

Input: .

Output: , such that , and for all ,

Here is an algorithm that make sure this can be done in linear time. The idea is to separate the numbers into two bins. is in the large bin if , and it is in the small bin otherwise. The idea is always pick two elements, one in large bin one in small bin, say and , where . Now we create a set , and put back into the bins.

If we can always prove that there is an element in the small bin during the execution of the algorithm, then we find an linear time algorithm for this problem. This is clear by simple induction.

This problem come up in order to implement the alias method. I recommend a good read on this topic Darts, Dice, and Coins: Sampling from a Discrete Distribution by Keith Schwarz.

Posted by Chao Xu on .
Tags: classical, algorithm.