online advertising

Friday, November 20, 2015

Algorithms: Design and Analysis, Part 1 - Problem Set 6 - Question 5

Problem:

Suppose we relax the third invariant of red-black trees to the property that there are no three reds in a row. That is, if a node and its parent are both red, then both of its children must be black. Call these relaxed red-black trees. Which of the following statements is not true?

Solution:

(Option 1) There is a relaxed red-black tree that is not also a red-black tree.
This statement is true - red -left-> red -left-> black is such a tree

(Option 2) The height of every relaxed red-black tree with n nodes is $ O(\log{n}) $
This statement is true as well. Since there are no three red node in a row, the longest path can have at most 3 times more nodes, and everything else as the proof said proceed as usual.

(Option 3) Every red-black tree is also a relaxed red-black tree.
Yeah, this is almost by definition.

(Option 4) Every binary search tree can be turned into a relaxed red-black tree (via some coloring of the nodes as black or red).
Nope, this binary search tree cannot be turned into a relaxed red-black-tree through coloring
1 -right-> 2 -right-> 3
If we wanted to not violate rule 4 we have to color them all black, but then that violate violate rule 3.

As an aside, I just hate function asking about negation, except from tricking people that do no good at all.

Algorithms: Design and Analysis, Part 1 - Problem Set 6 - Question 4

Problem:

Which of the following is not a property that you expect a well-designed hash function to have?

Solution:

(Option 1) The hash function should "spread out" most (i.e., "non-pathological") data sets (across the buckets/slots of the hash table).
Yes - if that is not the case we are screwed - spreading out is the whole point for hashing.

(Option 2) The hash function should be easy to store (constant space or close to it).
This would be nice, if we take certain size of space to store the hash function itself, it will not be fast to run the hash function either, so yes, we want it to be small.

(Option 3) The hash function should be easy to compute (constant time or close to it).
This would be nice, if it take long time to compute then we might want to use search tree instead. So yes, we want it to be small.

(Option 4) The hash function should "spread out" every data set (across the buckets/slots of the hash table).
This is impossible to expect, an adversary knowing the hash function can always cherry pick a data set so that all data goes to the same bucket for the hash function.

Algorithms: Design and Analysis, Part 1 - Problem Set 6 - Question 3

Problem:

You are given a binary tree (via a pointer to its root) with n nodes. As in lecture, let $ size(x) $ denote the number of nodes in the subtree rooted at the node $ x $. How much time is necessary and sufficient to compute $ size(x) $ for every node $ x $ of the tree?

Solution:

A simple algorithm like this can compute all the size in $ O(n) $ time

function compute_size(node)
  if (node == null)
    return 0;
  size = compute_size(node.left) + 1 + compute_size(node.right);
  node.size = size;
  return size;

The lower bound for this case is trivial, the algorithm must report the size of all nodes, the reporting alone will take $ \Omega(n) $ time.

Algorithms: Design and Analysis, Part 1 - Problem Set 6 - Question 2

Problem:

You are given a binary tree (via a pointer to its root) with n nodes, which may or may not be a binary search tree. How much time is necessary and sufficient to check whether or not the tree satisfies the search tree property?

Solution:

A top down solution would look like this:

function check(Node node, int minimum, int maximum)
  if (node == null)
    return true;
  if (node.value > maximum)
    return false;
  if (node.value < minimum)

    return false;
  return check(node.left) && check(node.right)

The algorithm inspect every element exactly once, so it is $ O(n) $.

Suppose there exist an algorithm that can run asymptotically faster than $ O(n) $, it cannot inspect all nodes. If it cannot inspect all nodes, then the adversary is free to choose what value to put in that node. In particular, for a case when the algorithm return true, the adversary can always put a value in that not inspected node to invalid the validity of the tree as follow

Suppose the node being not inspected in the rightmost node, then the algorithm can put a value smaller than any value in the tree, that would sabotage the algorithm.

Otherwise, the algorithm can put a value that is larger than any value in the tree, the adversary can also sabotage the algorithm.

Therefore, in order to not get sabotaged, the algorithm must inspect all node, that prove the problem will take at least $ \Omega(n) $ time.

Algorithms: Design and Analysis, Part 1 - Problem Set 6 - Question 1

Problem:

Suppose we use a hash function $ h $ to hash $ n $ distinct keys into an array $ T $ of length $ m $. Assuming simple uniform hashing, that is, with each key mapped independently and uniformly to a random bucket --- what is the expected number of keys that get mapped to the first bucket? More precisely, what is the expected cardinality of the set $ \{k:h(k)=1\}$.

Solution:

Intuitively, that would be $ \frac{n}{m} $, let formalize and prove this fact.

Denote $ X_i $ as the indicator random variable such that if the $ i^{th} $ element get hashed to first bucket, then $ X_i = 1 $, otherwise $ X_i = 0 $.

$ E[X_i] = 1 \times P(X_i = 1) + 0 \times P(X_i = 0) = P(X_i = 1) = \frac{1}{m} $.

Therefore, by linearity of expectation, we have $ E[S] = E[\sum\limits_{i = 1}^{n}{X_i}] = \sum\limits_{i = 1}^{n}{E[X_i]} = \sum\limits_{i = 1}^{n}{\frac{1}{m}} = \frac{n}{m} $.

Thursday, November 19, 2015

Algorithms: Design and Analysis, Part 1 - Programming Question 5

Problem:

Find the shortest paths in a undirected weighted graph with 200 nodes.

Solution:

Straightforward implementation of the Dijkstra's algorithm.

As usual, I had a bug (forget to recursively call FixDown). Brute force comparison and asserting heap invariant helped me to find and fix the bug.


Algorithms: Design and Analysis, Part 1 - Problem Set 5 - Question 5

Problem:

You are given a heap with n elements that supports Insert and Extract-Min. Which of the following tasks can you achieve in $ O(\log(n)) $ time?

Solution 

(Option 1) Find the largest element stored in the heap.
No, the maximum is among the leaf nodes, there are $ O(n) $ leaf nodes and we do not know which one it is.

(Option 2) Find the fifth-smallest element stored in the heap.
Yes, just do delete min 5 times and insert all the other elements back (or alternatively just to a tree traversal of the first 5 levels = 32 nodes, a constant)

(Option 3) Find the median of the elements stored in the heap.

No, it should not be possible. Here is an interesting description of the derivation, latter we will make it precise.

Imagine an infinite heap (there is no bottom to it, yet)

The root has to be the smallest element
The second smallest element has to be in the second layer (2 possibilities)
The third element has to be in the second or third layer (6 possibilities)
The interesting thought is the fourth element, it cannot be in the second layer, and therefore it is in third layer or fourth layer, so (12 possibilities)
...

The key is that, the precise number of possibilities for the median does not matter too much. For a complete binary tree (e.g. 7 nodes, the median will be the fourth), it has at least $ \frac{n}{2} $ possibilities for the median, so if there exist an algorithm to find the median in $ \log(n) $ time, it cannot beat the adversary who place the median somewhere else. (Similar adversary argument can also be applied for option 1)

(Option 4) None of these.
This cannot be true as option 2 is true.