2010年6月16日 星期三

[Algo] 6.3 Building a heap

Building max-heap
一旦我們有了 MAX-HEAPIFY 這個 subroutine 之後,我們就可以把一個 array 建造成一個 max-heap。因為 MAX-HEAPIFY(A, i) 需要 A[i] 下面的兩個 child 都是 max-heap,所以我們一定得採取 bottom-up 的方式來建造 max-heap。

@{
\proc{Build-Max-Heap}(A)
\li \textit{heap-size}[A] = \;\textit{length}[A]
\li \For i = \lfloor length[A]/2 \rfloor \;\textbf{downto}\; 1
\li \;\;\;\;\proc{Max-Heapify}(A, i)
}@

根據 Exercise 6.1-7,我們可以知道從 $\lfoor n/2 \rfloor + 1$ 到 都是 leaves,所以我們可以把每個 leaf 都當作一個長度為 1 的 heap,這樣我們就可以 call MAX-HEAPIFY了。


Running time
在分析 BUILD-MAX-HEAP 之前,根據 Exercise 6.3-3 告訴我們:一個 n-element heap 最多只有$\lceil n/2^{h+1} \rceil$個 node 的 height 為 h,證明的部份我們稍後再提。
因為 MAX-HEAPIFY 的 running time 為$O(h)$,所以整個 BUILD-MAX-HEAP 的 running time 我們可以整理成這樣:
$\sum_{h = 0}^{\lfloor \lg n \rfloor} \lceil \frac{n}{2^{h+1}} \rceil O(h) = O(n\sum_{h = 0}^{\lfloor \lg n \rfloor} \frac{h}{2^h})$

因為$\sum_{h = 0}^\infty \frac{h}{2^h} = \frac{1/2}{(1 - 1/2)^2} = 2$,所以 BUILD-MAX-HEAP 的 running time 可以表達成$O(n\sum_{h = 0}^{\lfloor \lg n \rfloor} \frac{h}{2^h}) = O(n\sum_{h = 0}^\infty \frac{h}{2^h}) = O(n)$,是個 linear time 的 subroutine。

2010年6月9日 星期三

[Algo] Exercise 6.2

Exercise 6.2-2
Starting with the procedure MAX-HEAPIFY, write pseudocode for the procedure MIN-HEAPIFY(A, i), which performs the corresponding manipulation on a min-heap. How does the running time of MIN-HEAPIFY compare to that of MAX-HEAPIFY?

@{
\proc{Min-Heapify}(A, i)
\li l = \proc{Left}(i)
\li r = \proc{Right}(i)
\li \If l \leq \;\textit{heap-size}[A]\;\textup{and}\;A[l] < A[i]
\li \;\;\;\;smallest = l
\li \Else
\li \;\;\;\;smallest = i
\li \If r \leq \;\textit{heap-size}[A]\;\textup{and}\;A[r] < A[smallest]
\li \;\;\;\;smallest = r
\li \If smallest \not= i
\li \;\;\;\;\textup{exchange}\;A[i]\;\textup{and}\;A[smallest]
\li \;\;\;\;\proc{Min-Heapify}(A, smallest)
}@

Its running time is the same as subroutine MAX-HEAPIFY, $O(\lg n)$


Exercise 6.2-3
What is the effect of calling MAX-HEAPIFY(A, i) when the element A[i] is larger than its children?
It will do nothing, because it satisfies the max-heap property.


Exercise 6.2-4
What is the effect of calling MAX-HEAPIFY(A, i) for i > heap-size[A]/2?
From exercise 6.1-7, we know they are leaves of the heap, and for a leaf, there is no child. MAX-HEAPIFY will do nothing and just return.


Exercise 6.2-5
The code for MAX-HEAPIFY is quite efficient in terms of constant factors, except possibly for the recursive call in line 10, which might cause some compilers to produce inefficient code. Write an efficient MAX-HEAPIFY that uses an iterative control construct (a loop) instead of recursion.
@{
\proc{Iterative-Max-Heapify}(A, i)
\li \While i \leq \;\textit{heap-size}[A]
\li \;\;\;\;l = \proc{Left}(i)
\li \;\;\;\;r = \proc{Right}(i)
\li \;\;\;\;\If l \leq \;\textit{heap-size}[A] \;\textup{and}\; A[l] > A[i]
\li \;\;\;\;\;\;\;\;largest = l
\li \;\;\;\;\Else
\li \;\;\;\;\;\;\;\;largest = i
\li \;\;\;\;\If r \leq \;\textit{heap-size}[A] \;\textup{and}\; A[r] > A[largest]
\li \;\;\;\;\;\;\;\;largest = r
\li \;\;\;\;\If largest \not= i
\li \;\;\;\;\;\;\;\;\textup{exchange}\;A[i]\;\textup{and}\;A[largest]
\li \;\;\;\;\Else
\li \;\;\;\;\;\;\;\;\textup{break the loop}
}@


Exercise 6.2-6
Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is $\Omega(\lg n)$. (Hint: For a heap with n nodes, give node values that cause MAX-HEAPIFY to be called recursively at every node on a path from the root down to a leaf.)
For a heap with n nodes, the worst-case is that a node value causing MAX-HEAPIFY to be called recursively at every node on a path from the root down to a leaf. The running time is $\Theta(1)$ for fixing up the relationships among the elements A[i], A[LEFT(i)] and A[RIGHT(i)]. If the height of a heap is h, the running time is $(h + 1)\cdot\Theta(1) = \Theta(h) = \Theta(\lg n)$. So we prove that it requires $\Omega(\lg n)$ time.

2010年6月8日 星期二

[Algo] 6.2 Maintaining the heap property

MAX-HEAPIFY 的用途
這個 subroutine 是用來整理 heap 裡的 element 位置,讓 heap 遵守 max-heap property。這個 subroutine 的 input 是一個 array A 跟一個 array index i。call 這個 subroutine 的時候是假設以 LEFT(i) 跟 RIGHT(i) 為 root 的 subtree 都是標準的 max-heap,我們要做的就是把 A[i] 這個 node 擺到以它為 root 的 subtree 裡面到一個適當的位置。


@{
\proc{Max-Heapify}(A, i)
\li l = \proc{Left}(i)
\li r = \proc{Right}(i)
\li \If l \leq \textit{heap-size}[A] \;\textup{and}\;A[l] > A[i]
\li \;\;\;\;largest = l
\li \Else
\li \;\;\;\;largest = i
\li \If r \leq \textit{heap-size}[A] \;\textup{and}\;A[r] > A[largest]
\li \;\;\;\;largest = r
\li \If largest \not= i
\li \;\;\;\;\textup{exchange}\;A[i]\;\textup{and}\;A[largest]
\li \;\;\;\;\proc{Max-Heapify}(A, largest)
}@

MAX-HEAPIFY 的圖解






























MAX-HEAPIFY 的精神
一開始先比較A[i], A[LEFT(i)], A[RIGHT(i)]這三者之間誰是最大的,因為 root 必須是這棵 subtree 中最大的 element,所以我們把這三者最大的 node 拿來當作新的 root,當然 A[i] 就往下跳,變成更小的 subtree 的 root 了。這個 subroutine MAX-HEAPIFY 就得以繼續遞迴的 call 下去,整個過程可以看上圖得知。




MAX-HEAPIFY 的 running time
首先在第 1~9 行裡面都是在找出這三者之間的大小關係,所以我們可以把它的 running time 視為$\Theta(1)$。重點是 recurrsively call MAX-HEAPIFY 這個 subroutine 的 running time。worst-case 發生在整棵 tree 是呈現半滿的狀態,而且這某半邊的 subtree 是棵 complete 的 binary tree,這個時候這某半邊的 subtree 的 size 是整棵 tree 的 $2n/3$。所以我們可以得到這個遞迴式:$T(n) \leq T(2n/3) + \Theta(1)$,根據 master theorem,我們可以知道 $T(n) = O(\lg n) = O(h)$

[Algo] Exercise 6.1

Exercise 6.1-1
What are the minimum and maximum numbers of elements in a heap of height h?
(1) minimum
$1 + 2 + 2^2 + \cdots + 2^{h-1} + 1 = \frac{2^h - 1}{2-1} + 1 = 2^h$
(2) maximum
$1 + 2 + 2^2 + \cdots + 2^h = \frac{2^{h+1} - 1}{2-1} = 2^{h+1} - 1$


Exercise 6.1-2
Show that an n-element heap has height $\lfloor \lg n \rfloor$.
Let h be the height of an n-element heap. From exercise 6.1-1, we know $2^h \leq n < 2^{h+1}$. We can obtain $h \leq \lg n < h+1$, thus $h = \lfloor \lg n \rfloor$


Exercise 6.1-3
Show that in any subtree of a max-heap, the root of the subtree contains the largest value occuring anywhere in that subtree.
如果最大的 element 不是出現在 subtree 的 root 的話,那就會違背 max-heap property。


Exercise 6.1-4
Where in a max-heap might the smallest element reside, assuming that all elements are distinct?
因為 max-heap property,最小的 node 一定會出現在 leaf 裡面,但不保證是最後一個 leaf。


Exercise 6.1-5
Is an array that is in sorted order a min-heap?
yes, 但是 min-heap 不一定是 in sorted order。


Exercise 6.1-6
Is the sequence <23, 17, 14, 6, 13, 10, 1, 5, 7, 12> a max-heap?
No, 畫圖出來就知道了。6 的 right child 是 7。


Exercise 6.1-7
Show that, with the array representation for storing an n-element heap, the leaves are the nodes indexed by $\lfloor n/2 \rfloor + 1, \lfloor n/2 \rfloor + 2, \ldots, n$.
最後一個 node 必為leaf,而且他的 parent(index: $\lfloor n/2 \rfloor$)必為 array 中最後一個非 leaf 的 node,所以從這個 node 之後的所有 node就是 leaf 了。

[Algo] 6.1 Heaps

heap 的定義
The (binary) heap data structure is an array object that can be viewed as a nearly complete binary tree. 這個 heap 不是指 memory layout 中的 heap,在這裡它是個特別的 data structure,但它僅僅是個 array 而已,雖然形狀看起來像棵 binary tree。我們在探討 heap 的特性時是把它想像成一顆 binary tree,但是他儲存的方式是個 array。但是 heap 當然可以是個 d-ary tree,不一定非得要是 binary tree。

heap 的 attribute
A 為一個 array 可以用來代表一個 heap,我們可以把 array A 視為一個 object,一個有heap特性的 object,接下來我們來看這個 object 有哪些 attribute:
(1) length[A]:array A 裡面中的 element 的個數
(2) heap-size[A]:array A 中被儲存為 heap 裡面的 element 個數
當然,我們可以從這兩個 attributes 得到一些特性:  
(i)heap-size[A] $\leq$ length[A]
以及
(ii)A[1 ... heap-size[A]]裡面的 element 才是 heap 裡面的 element。

heap 的圖解















heap procedures
tree 的 root 為 A[1],給定一個 index i,我們可以得到它的 parent, left child, right child:
@{
\proc{Parent}(i)
\li \Return $\lfloor i/2 \rfloor$
}@
@{
\proc{Left}(i)
\li \Return $2i$
}@
@{
\proc{Right}(i)
\li \Return $2i + 1$
}@

heap property
有兩種不同的 binary heap:
(1) max-heap
    (i) max-heap property:for every node other than the root, A[PARENT(i)] $\geq$ A[i],注意的是左右 sibling 是沒有誰必須大於誰的規定
    (ii) root 是整個 heap 裡面最大的 element
    (iii) 任何一個 node 底下的 subtree 裡,都是這個 node 的值最大
(2) min-heap
    min-heap 基本上跟 max-heap 的性質類似,只不過大小顛倒相反而已。

heap height 的定義
(1) We define the height of a node in a heap to be the number of edges on the longest simple downward path from the node to a leaf.
(2) We define the height of the heap to be the height of its root.
所以 heap 的 height 都是由 leaf 往上數上來的,leaf 的 height 都是 0,上圖的 heap 的 height 是 3,值是 8 的那個 node 的 height 是 1。

一個有 n 個 element 的 binary heap,他的 height 是$\Theta(\lg n)$,所有 basic operations 的 running time 都是跟這個 heap 的 height 有關係,所以都是 $O(\lg n)$。

[Algo] Problem 5

Problem 5-1 Probabilistic counting
With a b-bit counter, we can ordinarily only count up to $2^b - 1$. With R. Morris's probabilistic counting, we can count up to a much larger value at the expense of some loss of precision.
We let a counter value of i represent a count of $n_i$ for $i = 0, 1, \ldots , 2^b-1$, where the $n_i$ form an increasing sequence of nonnegative values. We assume that the initial value of the counter is 0, representing a count of $n_0 = 0$. The INCREMENT operation works on a counter containing the value i in a probabilistic manner. If $i = 2^b - 1$, then an overflow error is reported. Otherwise, the counter is increased by 1 with probability $1/(n_{i+1} - n_i)$, and it remains unchanged with probability $1 - 1/(n_{i+1} - n_i)$.
If we select $n_i = i$ for all $i \geq 0$, then the counter is an ordinary one. More interesting situations arise if we select, say, $n_i = 2^{i - 1}$ for $i > 0$ or $n_i = F_i$ (the ith Fibonacci number-see Section 3.2).
For this problem, assume that $n_{2^b - 1}$ is large enough that the probability of an overflow error is negligible.

a. Show that the expected value represented by the counter after n INCREMENT operations have been performed is exactly n.
b. The analysis of the variance of the count represented by the counter depends on the sequence of the $n_i$. Let us consider a simple case: $n_i = 100i$ for all $i \geq 0$. Estimate the variance in the value represented by the register after n INCREMENT operations have been performed.

a. 令 $X$ 為經過 nINCREMENT operation 後 counter 所代表的值的 random variable,$X_i$為經過第 iINCREMENT operation 的 counter 所代表值的增加量的random variable。注意:唯有把增加量當作一個 random variable,我們才有辦法利用到$E[X] = E[X_1 + X_2 + \cdots ]$的特性。所以我們可以得到:


b. 根據上題我們可以假設類似的條件,令$X$為經過 nINCREMENT operations 後 counter所代表的值的 random variable,$X_i$為經過第 iINCREMENT operation 的 counter 所代表值的增加量的random variable。因為任兩個 random variable $X_i$, $X_j$都是彼此獨立的,所以我們也可以利用這個特性:
$Var(X) = Var(X_1) + Var(X_2) + \cdots + Var(X_n)$ 來計算 $Var(X)$。



Problem 5-2 Searching an unsorted array
This problem examines three algorithms for searching for a value x in an unsorted array A consisting of n elements.
Consider the following randomized strategy: pick a random index i into A. If $A[i] = x$, then we terminate; otherwise, we continue the search by picking a new random index into A. We continue picking random indices into A until we find an index j such that $A[j] = x$ or until we have checked every element of A. Note that we pick from the whole set of indices each time, so that we may examine a given element more than once.
a. Write pseudocode for a procedure RANDOM-SEARCH to implement the strategy above. Be sure that your algorithm terminates when all indices into A have been picked.
b. Suppose that there is exactly one index i such that $A[i] = x$. What is the expected number of indices into A that must be picked before x is found and RANDOM-SEARCH terminates?
c. Generalizing your solution to part (b), suppose that there are $k \geq 1$ indices i such that $A[i] = x$. What is the expected number of indices into A that must be picked before x is found and RANDOM-SEARCH terminates? Your answer should be a function of n and k.
d. Suppose that there are no indices i such that $A[i] = x$. What is the expected number of indices into A that must be picked before all elements of A have been checked and RANDOM-SEARCH terminates?

Now consider a deterministic linear search algorithm, which we refer to as DETERMINISTIC-SEARCH. Specifically, the algorithm searches A for x in order, considering $A[1], A[2], A[3], \ldots, A[n]$ until either $A[i] = x$ is found or the end of the array is reached. Assume that all possible permutations of the input array are equally likely.
e. Suppose that there is exactly one index i such that $A[i] = x$. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?
f. Generalizing your solution to part (e), suppose that there are $k \geq 1$ indices i such that $A[i] = x$. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH? Your answer should be a function of n and k.
g. Suppose that there are no indices i such that $A[i] = x$. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?

Finally, consider a randomized algorithm SCRAMBLE-SEARCH that works by first randomly permuting the input array and then running the deterministic linear search given above on the resulting permuted array.
h. Letting k be the number of indices i such that $A[i] = k$, give the worst-case and expected running times of SCRAMBLE-SEARCH for the cases in which $k = 0$ and $k = 1$. Generalize your solution to handle the case in which $k \geq 1$.
i. Which of the three searching algorithms would you use? Explain your answer.
a. 
@{
\proc{Random-Search}(A, n, x)
\li check = 0
\li \textup{create arrays}\;C[1..n]
\li \For i = 1 \;\To n
\li \;\;\;\;C[i] = \;\textup{false}
\li \While check \not= n
\li \;\;\;\;index = \proc{Random}(1, n)
\li \;\;\;\;\If A[index] = x
\li \;\;\;\;\;\;\;\;\Return \textup{true}
\li \;\;\;\;\Elif C[index] = \;\textup{false}
\li \;\;\;\;\;\;\;\;C[index] = \;\textup{true}
\li \;\;\;\;\;\;\;\;check = check + 1
\li \Return \textup{false}
}@


b.



c.



d.
Just like the coupon collector's problem, in order to "collect" each of $n$ different indices, it must acquire approximately $n\ln n$ randomly obtained indices to succeed.


e.
$E[X] = \sum_{i = 1}^n i \cdot \frac{1}{n} = \frac{1}{n} \cdot \frac{n(n+1)}{2} = \frac{n+1}{2}$
The worst-case running time of DETERMINISTIC-SEARCH is n.


f.
The expected running time is $\frac{n+1}{k+1}$.(From wikipedia)
The worst case is that all element x are put to the end of array A. So the worst-case running time is $n - k + 1$.


g.
The expected running time of DETERMINISTIC-SEARCH is n. It is the very worst-case.


h.
(1) k = 0
It is the very worst-case, and the expected running time of SCRAMBLE-SEARCH is the sum of the cost of shuffling the array and the cost of running DETERMINISTIC-SEARCH, $n + n = 2n$
(2) k = 1
The worst-case is that x is moved to the end of A after shuffling. The expected running time is $n + \frac{n+1}{2} = \frac{3n + 1}{2}$
(3) generalized case
The worst case is that all x are moved to the end of A after shuffling. The expected running time is $n + \frac{n+1}{k+1}$

2010年6月1日 星期二

[Algo] Exercise 5.4

Exercise 5.4-1
How many people must there be in a room before the probability that someone has the same birthday as you do is at least 1/2? How many people must there be before the probability that at least two people have a birthday on July 4 is greater than 1/2?

(1)
$1 - (364/365)^{n-1} \geq 1/2 \Rightarrow 1/2 \geq (364/365)^{n-1}$
$\Rightarrow \ln 1/2 \geq (n-1) \ln(364/365) \Rightarrow n-1 \leq \frac{\ln 1/2}{\ln 364/365}$
$n-1 \leq 252.6519\ldots \Rightarrow n \leq 253$
Thus there must be 252 other people such that the probability that someone has the same birthday is at least 1/2.

(2)
用 1 減掉("都沒有人的生日是7/4的機率" + "只有一個人的生日是7/4的機率")就是我們所求:
$1 - (364/365)^n - C^n_1(1/365)(364/365)^{n-1} \geq 1/2$
We can use computer to solve this equation, and while $n \geq 613$ this inequality holds.

Exercise 5.4-2
Suppose that balls are tossed into b bins. Each toss is independent, and each ball is equally likely to end up in any bin. What is the expected number of ball tosses before at least one of the bins contains two balls?

By pigeon's rule, we can conclude that at most b + 1 tosses there must be a bin containing two balls. Thus, we can obtain the expected number of ball tosses by the definition:
$E[X] = \sum_{i = 2}^{b + 1}i\mbox{p}(i)$
where i is the number of ball tosses.
We can obtain that $$\mbox{p}(i) = \frac{P^b_{i-1}(i-1)}{b^{i}}$$, and the expected number of ball tosses is
$\sum_{i=2}^{b+1}\frac{P^b_{i-1}\cdot i(i-1)}{b^i}$
$=\sum_{i=2}^{b+1}\frac{b(b-1)(b-2)\cdots(b-i)\cdot i(i-1)}{b^i}$
$=\sum_{i=2}^{b+1}(\frac{b}{b})\cdot(\frac{b-1}{b})\cdots(\frac{b-i}{b})\cdot i(i-1)$

Exercise 5.4-3
For the analysis of the birthday paradox, is it important that the birthdays be mutually independent, or is pairwise independence sufficient? Justify your answer.

The birthday paradox holds even when the birthdays are only pairwise independent. (當裡面有雙胞胎的時候,這時候生日可能就不是獨立事件了) 在分析 birthday paradox 的時候,我們會用到獨立這個條件的只有這個地方:
$\mbox{Pr}\{b_i=b_j\}=\sum_{r=1}^n\mbox{Pr}\{b_i=r\quad\mbox{and}\quad b_j=r\}$
Obviously, it involves only two variables $b_i$ and $b_j$. Thus, pairwise independence is sufficient.

Exercise 5.4-4
How many people should be invited to a party in order to make it likely that there are three people with the same birthday?

Suppose that there are k people invited, and all years have n( = 365) days.
$$\mbox{Pr\{there are exactly 3 people with the same birthday\}} = \frac{n \cdot C^{n-1}_{k-3} \cdot \frac{k!}{3!}}{n^k}$$
$$\mbox{Pr\{there are at least 3 people with the same birthday\}} = 1 - \frac{P^n_k}{n^k} - \frac{n \cdot C^{n-1}_{k-2} \cdot \frac{k!}{2!}}{n^k}$$

Exercise 5.4-5
What is the probability that a k-string over a set of size n is actually a k-permutation? How does this question relate to the birthday paradox?

(1) $$\frac{P^n_k}{n^k} = 1\cdot(\frac{n-1}{n})\cdot(\frac{n-1}{n})\cdots(\frac{n-k+1}{n})$$
(2) n is the days all year have, k is how many people are invited to the party, and k-permutation denotes all people have distinct birthdays, no one has the same birthday as the other's.

Exercise 5.4-6
Suppose that n balls are tossed into n bins, where each toss is independent and the ball is equally likely to end up in any bin. What is the expected number of empty bins? What is the expected number of bins with exactly one ball?

(1) Let $X$ be the random variable of empty bins. By the linearity property of expected number, we can obtain that$\mbox{E}[X] = \mbox{E}[X_1 + X_2 + \cdots X_n]$, where $X_i$ is the indicator random variable.
$X_i$ = I{bin i is empty}
$\mbox{E}[X_i] = \mbox{Pr}\{$bin is empty$\} = (\frac{n-1}{n})^n$
$$\Rightarrow\mbox{E}[X]=n\cdot(\frac{n-1}{n})^n=\frac{(n-1)^n}{n^{n-1}}$$
(2) Same as the above, let $X$ be the random variable of bins with exactly one ball, and $X_i$ is the indicator random variable.
$X_i$ = I{bin contains exactly one ball}

$\mbox{E}[X_i]=\mbox{Pr}\{$ bin contains exactly one ball $\}=\frac{(n-1)^{n-1}\cdot n}{n^n}$
$$\Rightarrow\mbox{E}[X]=n\cdot\frac{(n-1)^{n-1}\cdot n}{n^n}=\frac{(n-1)^{n-1}}{n^{n-2}}$$

Exercise 5.4-7
Sharpen the lower bound on streak length by showing that in n flips of a pair coin, the probability is less than $1/n$ that no streak longer than $\lg n - 2\lg\lg n$ consecutive heads occurs.