Can we do better if we only want to generate all binary strings of length $2n$ which have precisely $n$ zeros and $n$ ones?
By “can we do better”, I mean we should have lower complexity than the silly algorithm which first builds the entire tree above and then tries to find those paths with an equal number of “left” and “right” edges.
Asked By : Vidit Nanda
Answered By : tranisstor
But how many such binary strings with $n$ 0’s and $n$ 1’s are there? We choose $n$ 1’s for our strings of length $2n$ and use Stirling’s formula in step 2: $$ {2n choose n} = frac{(2n)!}{(n!)^2} = frac{4^n}{sqrt{pi n}} (1 + mathcal{O}(1/n))$$ EDIT
Thanks to Peter Shor’s comments we can also analyse the number of steps needed by the second algorithm, which counts the 1’s and 0’s. I’m citing his comment from below:
We want to find all binary sequences with exactly $n$ 0’s and $n$ 1’s. We traverse the binary tree where each node is a sequence of at most $2n$ 0’s and 1’s. We don’t need to visit any node with more than $n$ 0’s or more than $n$ 1’s. how many nodes do we need to visit? There are ${i+j choose i}$ strings with $i$ 0’s and $j$ 1’s. Summing this over all $i,j leq n$ gives $sum_{i=0}^n sum_{j=0}^n {i+j choose i} = {2n+2 choose n+1} − 1$. Now, we need to visit each of these nodes at a constant average cost per node. We can do this by visiting each left child first, and each right child second.
Using Stirling’s formula again we obtain $$ {2n + 2 choose n + 1} – 1 = 4^{n+1} frac{1}{sqrt{n+1}} (1 + mathcal{O}(1/n)) – 1 = mathcal{O}(frac{4^n}{sqrt{n}}) $$ as the running time of the new algorithm.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/12992