Monday, September 18, 2017

Online number partitioning


You have a bunch of numbers $a_1,a_2,\ldots,a_n$ each in the form of \(2^{-k}\) where $k\in\mathbb{N}$, and they sum up to \(1\). For example, $a_1=1/4, a_2=1/8, a_3=1/2, a_4=1/8$. You want to partition them into two groups of equal sum. It has to be done online: for each number $a_i$ you have to decide which group to throw into before knowing the value of $a_{i+1},\ldots,a_n$. You do not know $n$ in advance. How?

================================================















Solution:
For each number, if adding it to group \(A\) doesn't make it exceed \(1/2\), then do it. Otherwise add it to group \(B\). 

To prove its correctness, suppose that at some point we cannot add $2^{-k}$ to neither group. Let the smallest numbers in group $B$ be $2^{-m}$. We have $k<m$, or else we can add $2^{-k}$ to group $B$. Since we could not add $2^{-m}$ to group $A$, the remaining space in $A$ is less than $2^{-m}$. The remaining space in $B$ is at most $2^{-k}-2^{-m}$. Hence the total space is less than $$2^{-m}+2^{-k}-2^{-m}=2^{-k}.$$ This is a contradiction.

No comments: