Friday, February 6, 2015

Solution to Is a binary tree red black?

This will be a quick (and incomplete) post. I might edit to add more details later.

The goal was to determine if a given binary tree is a red black tree.

A well known necessary condition of a red-black tree: For each node the max height is no more than twice the min height.

This is in fact used to prove that the height is $O(\log n)$.

In fact, this is also sufficient!

Using this property gives us an easy $O(n)$ time algorithm.

I will leave the details of the proof and the algorithm to you :-).

Tuesday, February 3, 2015

Solution to equal coins puzzle

This has solutions to the Equal coins puzzle posted earlier.

A brief description of the problem:

There are $2n+1$ reals numbers, $r_1, r_2, \dots, r_{2n+1}$. Given any $r_i$, you can split the remaining $2n$ into two groups of $n$ each, such that the sum of elements of one group is the same as the sum of elements of the other group.

Show that $r_i = r_j$ (i.e. all must be equal).

Solutions

All the solutions that I know use Linear Algebra (if you know/can think of one without using Linear Algebra, please share!).

Solution 1)

This is the solution I had come up with when I first saw this problem.

The property basically means that there is a $(2n+1)\times(2n+1)$ matrix $A$ such that

Every row of $A$ has exactly $n$ ones and $n$ minus ones.

The main diagonal of $A$ is $0$

$Ax = 0$ has a non-trivial solution.

Since $x = [1,1, \dots, 1]$ is an obvious solution, in order to show that all the coins/real numbers must be equal, it is enough if we show that the rank of $A$ is $2n$.

Consider the characteristic polynomial of $A$, $P_{A}(t) = \det(A - tI)$.

It is enough to show that coefficient of $t$ in $P_{A}(t)$ is non-zero.

Consider the formula for the determinant of an $N \times N$ matrix $M$

$$ \det M = \sum_{\sigma} \text{sign}(\sigma)\prod_{i=1}^N M_{i\sigma(i)}$$

where $\sigma$ runs over the permutations of $\{1,2, \dots, N\}$ and $\text{sign}$ gives the sign (based on the parity of the permutation).

For the $P_{A}(t)$, the coefficient of $t$ is given by those permutations such that there is exactly one $i$ such that $\sigma(i) = i$.

Since each product $\prod A_{j \sigma(j)}$ is $\pm 1$, the coefficient of $t$ is a sum of ones and minus ones.

It is thus enough to show that there is an odd number of such permutations.

This number is basically $(2n+1)D_{2n}$, where $D_{2n}$ is the number of derangments of $1,2 \dots, 2n$!

This is easily shown to be odd (based on a recursive formulas) and is left as an exercise :-).

I was quite happy with this solution, when Professor David Zuckerman of UT Austin showed me this neat proof:

Solution 2)

Like 1), we start with matrix $A$, but consider it over $\mathbb{F}_2$, the field of ${0,1}$. One can show that $rank(A)$ over $\mathbb{R} \ge rank(A)$ over $\mathbb{F_2}$.

Now use the fact that $rank(A) + rank(B) \ge rank(A+B)$

In $\mathbb{F}_2$, A is just the all ones matrix, except the diagonal, which is all zeros.

To that, we add the all ones matrix $B$, giving the identity. The all ones matrix $B$ trivially has rank 1.

Thus we get $rank(A) + 1 \ge 2n+1$.

Since $rank(A) \ne 2n+1$ (it has a non-trivial solution), we are done.

And the third solution, which is quite neat too and generalizes to $kn+1$ numbers.

Solution 3)

First assume that the $r_i$ were integers. We can also assume they are all non-negative, as adding a fixed constant does not change the property.

Now consider the $r_i$ which has the smallest maximum.

If $ S = r_1 + r_2 + \dots + r_{2n+1}$, then the property gives us that $S = r_i \mod 2$ for every $i$.

Thus all are even or all are odd. If all are even, we can divide by $2$. If all are odd, we can subtract the smallest number.

Thus they must be equal. This easily extends to the case when the numbers are rational.

Now the Linear Algebra application: the reals are a vector space over the rationals! (See Hamel Basis). Writing the reals as a (finite) linear combination of the basis numbers, with coefficients in $\mathbb{Q}$, gives us that the coefficients of each basis vector/number should satisfy similar properties, and thus must be equal, implying the $r_i$s are equal!


Monday, February 2, 2015

One of my favourite bridge puzzles

I don't know the source of this one (if you do, please share) and not even sure how I came across it. I am guessing this is from before 1960.

You are South, playing rubber bridge and reach 6H.

West leads a diamond.

Rubber
None 
 North
♠ AK54
♥ 532
♦ JT
♣ AKJT

   


 You
♠ 32
♥ AKQJT4
♦ -
♣ 98765

Can you guarantee 6H?

[Please feel free to comment with your solution]


Solution (Updated Feb 13th 2015)


The only problem is when trumps are 4-0 and clubs are 4-0.

The solution is to ruff the first trick, and draw four rounds of trumps, pitching a diamond from dummy. Pitching a diamond is the key play.

Now you play clubs from the top. If opponents win the third round and play a diamond, you can ruff in hand with your last trump, while pitching a club from dummy, unblocking them.


Wednesday, January 28, 2015

Is a binary tree a red-black tree? algorithm puzzle

Red black trees are quite widely used.

A red black tree is a binary search tree, but with some additional colour constraints on the nodes (swiped from the wiki page).
  • Each node is coloured either red or black.
  • The root is coloured black.
  • All NULL nodes are coloured black.
  • Every red coloured node must have two black children.
  • Every path from a given node to any of it's descendant NULL nodes must have the same number of black nodes.
These constraints ensure that the height of a red black tree on $n$ nodes is $O(\log n)$, and these constraints can be enforced during insertion and deletion, thus making insert/delete/search operations $O(\log n)$ each.

Now for the puzzle.

Suppose you are given a binary tree on $n$ nodes, without any colours on it. Can you determine if there is a red-black colouring of the nodes, which follows the above constraints? i.e. is the binary tree actually a red-black tree?

An $O(n)$ time algorithm is possible, but $O(n \log n)$ is ok too.

[Solution]

Monday, January 26, 2015

Equal Coins, math puzzle

An interesting problem, which has multiple solutions.

Suppose you are given $2n+1$ coins $c_1, c_2, \dots, c_{2n+1}$, of positive real weights $w_1, w_2, \dots, w_{2n+1}$.

These coins have the property that if you remove any coin, then the remaining $2n$ coins can be split into two groups of exactly $n$ coins each, such that the sum of weights of coins in one group, is same as the sum of weights of coins in the other.

Show that $w_i = w_j$ for any $i, j$.

i.e. all the coins must be of equal weight.

[Solution]

Saturday, January 24, 2015

Are you alert enough? (bridge hand)

Playing a very casual bridge game (it happened during one of the informal lunch bridge sessions when I was at Microsoft).

You reach a small slam in hearts, and LHO leads the heart 5.

This is what you see:

IMPS
None 
 Dummy
♠ J96
♥ 94
♦ 874
♣ AK875

 


 You
♠ KT5
♥ AKQT8762
♦ AK
♣ -

How will you play?


Solution (Posted too late)


Look at the heart spots! You are missing the J,5 and 3.

Put up the 9 on the 5! If RHO covers, you can draw the last trump by playing the 2 to the 4. If not, you can cash AK of clubs, pitching your spade losers.

Thursday, January 22, 2015

Solution to rearrange the array, algorithm puzzle.

This is a solution to the rearrange array puzzle posted earlier.

A brief description:

Suppose you are given an array of $2n$ objects

$$[a_1, a_2, \dots, a_n, b_1, b_2, \dots, b_n] $$

You need to reorder the array to be

$$[b_1, a_1, b_2, a_2, \dots, b_k, a_k, \dots, b_n, a_n] $$

Hard version: Do this in-place (preferably $O(1)$ space in WORD RAM model), and O(n) time.

(Medium version: Do it in $O(n\log n)$ time, and in-place).

Solution


This puzzle is surprisingly difficult (the $O(n)$ time, in-place version). It seems that we should easily be able to do it, but most first approaches fail. The in-place constraint is what makes it difficult (otherwise we can just make a new array and copy the elements easily).

This puzzle was given to me when I was at Microsoft, the problem apparently being open at the time (at least within the set of people at Microsoft who came across the puzzle).

I was able to solve it, and I was encouraged (by someone on an online forum) to write a paper on it. It only took me 4 years to write, but I finally managed to write it, and it is on arxiv now.

This is the paper: A Simple In-Place Algorithm for In-Shuffle. It is only 3 pages long, and should serve as a solution :-) [ok, I am feeling a bit lazy to write it here too.]

The $\Theta(n\log n)$ version also falls out from the paper.

Please feel free to comment if you have any questions regarding the solution in the paper.

Wednesday, January 21, 2015

Solution to Unit fraction chord lengths.

This is a solution to the unit fraction chord length puzzle.

A brief description:

Any continuous function $f:[0,1] \to \mathbb{R}$ such that $f(0) = f(1)$, has a chord of length $\frac{1}{n}$ where $n$ is a positive integer.

Any other chord length (non unit fraction lengths) have functions which don't have those chord lengths.

Solution


Let $g(x) = f(x) - f\left(x + \frac{1}{n}\right)$.

Then we have that

$$g(0)+ g(1/n) + \dots + g((n-1)/n) = f(0) - f(1) = 0 $$

Thus if $g(k/n) \ne 0$, then there are some $i, j$ such that $g(i/n) \lt 0$ and $g(j/n) \gt 0$, and thus by the mean value theorem, some point $c$ where $g(c) = 0$.

For the other part, given any non unit fraction $r$, the function

$$ f_r(x) = \sin^2\left(\frac{\pi x}{r}\right) - x \sin^2\left(\frac{\pi}{r}\right)$$

is a counter-example.

This result is apparently called the Universal Chord Theorem.

Tuesday, January 13, 2015

Rearrange the array, algorithm puzzle

This is one of my favourite algorithm puzzles.

Suppose you are given an array of $2n$ objects

$$[a_1, a_2, \dots, a_n, b_1, b_2, \dots, b_n] $$

You need to reorder the array to be

$$[b_1, a_1, b_2, a_2, \dots, b_k, a_k, \dots, b_n, a_n] $$

The algorithm should be in-place: i.e. space usage must be $O(\log^k n)$ words (in the WORD RAM model), for some constant $k$ (preferably $O(1)$ space, but $k \le 2$ will do too). Assume that each object ($a_i$ or $b_i$) takes up $O(1)$ space and copying them around is $O(1)$ time and space.

Medium:

Can you give an $O(n \log n)$ time algorithm?

Hard:

Can you give an $O(n)$ time algorithm?


[Solution]

Monday, January 12, 2015

Unit fraction chord lengths

Another interesting property of unit fractions (numbers of the form $\dfrac{1}{n}$ for positive integer $n$).

Part A)

Suppose $f:[0,1] \to \mathbb{R}$ is any continuous function such that $f(0) = f(1)$.

Show that for every natural number $n \gt 0$, there is some $c \in [0,1]$ ($c$ dependent on $n$ and $f$) such that $f(c) = f\left(c + \dfrac{1}{n}\right)$

Basically the graph of $f$ has a chord of length $\dfrac{1}{n}$.

Part B)

Suppose $r \in (0,1)$ is a real number such that $r$ is not a unit fraction.

Show that there is some continuous function $f_r$ (dependent on $r$) such that

$f_r:[0,1] \to \mathbb{R}$, $f_r(0) = f_r(1)$ and for any $c \in [0,1-r]$, $f_r(c) \ne f_r(c+r)$.

i.e the graph of $f_r$ has no chord of length $r$.

[Solution]



Sunday, January 11, 2015

6D making 6!!!!! [A hand played by Belladonna]

This hand was played by Giorgio Belladonna as part of one of the Omar Sharif Circus matches in 1970. This hand has probably appeared in multiple books: 'Bridge with the Blue Team', 'The Hands of Time', to name a couple.

The title (exclamation marks and all) of this blog post is from the actual hand-written surprise written by Maureen Kaufmann on the monitor sheet (source: 'The Hands of Time').

This was the hand:

IMPS
N/S 
 Garozzo
♠ 7654
♥ A83
♦ J32
♣ KQ5
 Eisenberg
♠ KJ92
♥ QJT6
♦ 854
♣ 86

   


 Goldman
♠ 83
♥ 92
♦ 9
♣ AJT97432
 Belladonna
♠ AQT
♥ K754
♦ AKQT76
♣ -

W N E S
PP4C5C
P5SP6D
allpass


Eisenberg led the club 8. Belladona played the K, covered by Goldman and ruffed.

[Think about how you will play looking at the just the North and South hands]

At this point Belladonna got up, went to the coffee table, and lit up a cigarette and walked back and forth with his head in hand, puffing away.

He then came back, played the DA and diamond to J. Cashed the CQ pitching the ST, ruffed a club high and ran trumps to arrive at this position:

IMPS
N/S 
 Garozzo
♠ 7654
♥ A83
♦ -
♣ -
 Eisenberg
♠ KJ9
♥ QJT6
♦ -
♣ -

    


 Goldman
♠ 83
♥ 92
♦ -
♣ JT9
 Belladonna
♠ AQ
♥ K754
♦ T
♣ -


On the last trump, Eisenberg was caught in a strange squeeze.

If he pitched a heart, Belladona would play AK heart and small heart, setting up a heart, and endplaying Eisenberg to lead into the spade AQ.

If Eisenberg pitched a spade, Belladona would play SA and SQ, setting up the 7 and 6 in the dummy!

6D made 6!!!!!

Wednesday, January 7, 2015

Solution to streaming pivot, algorithm puzzle

The problem was posted earlier.

A brief description:

Find a pivot in a stream of distinct numbers, using sub-linear space.

Pivot = all numbers that came before it are smaller than it, all numbers that came after it are greater.

Solution


The algorithm will find the leftmost/earliest pivot, if a pivot exists.

If A[j] is a pivot, then A[j] = maximum of A[1], A[2], ..., A[j].

We keep track of the maximum seen so far, and a candidate leftmost pivot, which has the above property.

Once we have a candidate leftmost pivot, we check each subsequent number against that candidate. If we get a number which is smaller than the candidate pivot, then none of the numbers seen so far can be the leftmost pivot! Now we reset and look for a new leftmost candidate using the maximum seen so far property.

This gives us an O(1) space algorithm.

Monday, January 5, 2015

Solution to sum of unit fractions puzzle

This is a solution to the sum of unit fractions puzzle posted earlier.

The problem:

Show that every positive rational number can be written as a sum a finite number of distinct unit fractions (unit fraction = number of the form 1/n).

Solution


[This proof appears in the classic number theory book by Niven and Zuckerman]

First, consider rationals $\lt 1$ in the reduced form $\frac{a}{b}$, with $0 \lt a \lt b$ and $\gcd(a,b) = 1$.

We prove by strong induction on $a$.

If $a = 1$, there is nothing to prove.

If $a = 2$, then $b$ must be odd (say $2x+1$).

Then we can use the identity

$$\frac{2}{2x+1} = \frac{1}{x+1} + \frac{1}{(x+1)(2x+1)}$$

Now given $\dfrac{a}{b}$ (in reduced form)

Let $c$ be the smallest integer such that $ac \gt b$.

Then we must have that $ac = b + r$, for some $0 \lt r \lt a$.

If $r \gt a$, then $c$ won't be the smallest integer such that $ac \gt b$.

Now consider $\dfrac{ca}{b} = 1 + \dfrac{r}{b}$.

Thus

$\dfrac{a}{b} = \dfrac{1}{c} + \dfrac{r}{bc}$

By induction hypothesis, $\dfrac{r}{bc}$ can be written as a sum of distinct unit fractions.

Now $\dfrac{r}{bc} \lt \dfrac{1}{c}$, so none of those unit fractions can be $\dfrac{1}{c}$.

Thus $\dfrac{a}{b}$ can be written as a sum of distinct unit fractions.

Now, if $r$ is any rational $\gt 1$, then since the series $\sum_{k=1}^{\infty} \frac{1}{k}$ diverges, there is some m such that

$$ 1 + \frac{1}{2} + \dots + \frac{1}{m} \le r \lt 1 + \frac{1}{2} + \dots + \frac{1}{m+1}$$

Consider $ r - (1 + \dfrac{1}{2} + \dots + \dfrac{1}{m})$, this is a rational number < $\dfrac{1}{m+1}$, and so can be written as a sum of distinct unit fractions, and each denominator will be $\gt m$.

Thus we are done.

Thursday, January 1, 2015

Streaming pivot, algorithm puzzle

In an array A[1], A[2], ..., A[n] of distinct numbers, A[p] is called a pivot if all numbers to left of A[p] are less than A[p] and all numbers to right of A[p] are greater than A[p].

i.e. for any i < p, A[i] < A[p] and for any j > p, A[p] < A[j].

Basically, if we sort the sub-array A[1], A[2], ..., A[p-1] and sub-array A[p+1], .., A[n] independently, then whole array will be sorted (this must remind you of quicksort).

Now suppose you were given the array A as a stream of numbers A[1], A[2], ... i.e. you can access A by accessing only A[1] first, then A[2], and so on. Once you access A[5], you cannot access A[4] again (unless you save it) etc.

Given a stream of distinct numbers, can you find a pivot (or say if there is none) using an algorithm which uses sub-linear space?

[The optimal algorithm will use O(1) space, and O(n) time total: Solution]