Algorithm: Lecture 4. Divide-and-Conquer Homework

*In this work, all the index of array starts by 1.

Given a sorted array with size 7. Assume that the probability to search elements in the array are equal. What's the averages search times to find an element of the array? (e.g. it requires 3 search times to find 9 in Example 2.1).

Solution

We use binary search to solve this problem.

  • Case 1: there are 4 positions which needs 3 iterations.

  • Case 2: 2 positions need 2 iterations.

  • Case 3: 1 position needs 1 iteration.

Let \(X\) be the search times, so the average search times is given by

\[\mathbb E X = \sum x\mathbb P (x) = \frac{17}{7} \text{ times} \]

Question Searc 2 sum

2. Give a \(\Theta(n \lg n)\) time algorithm for determining if there exist two elements in a set \(A\) whose sum is exactly some value \(x\).

Solution

The algorithm is shown in Algotithm 1 and the time complexity is

\[\Theta(n \lg n) + n \Theta(\lg n) = \Theta(n \lg n) \]

Question Fibonacci

Recursive squaring for Fibonacci numbers Let \(A=\left[\begin{array}{ll}1 & 1 \\ 1 & 0\end{array}\right]\). Similar to the case of powering a number:

\[A^n=\left\{\begin{array}{cl} A^{n / 2} \cdot A^{n / 2}, & \text { if } n \text { is even; } \\ A^{(n-1) / 2} \cdot A^{(n-1) / 2} \cdot A, & \text { if } n \text { is odd. } \end{array}\right. \]

The time for recursive squaring is \(\Theta(\lg n)\). Homework: write pseudocode for Fibonacci numbers using recursive squaring.

Solution

See Algorithm 2.

Question Maximum-subarray

4.1-2 Write pseudocode for the brute-force method of solving the maximum-subarray problem. Your procedure should run in \(\Theta\left(n^2\right)\) time.

Solution

The algorithm is given in Algorithm 3. The array index

and the time complexity is

\[\Theta(n)+ \Theta(n^2) = \Theta(n). \]

Question Strassen's Algorithm

4.2-1 Use Strassen's algorithm to compute the matrix product \(\left(\begin{array}{ll}1 & 3 \\ 7 & 5\end{array}\right)\left(\begin{array}{ll}6 & 8 \\ 4 & 2\end{array}\right)\) Show your work.

Solution

Step 1. Divide the matrices into submatrices.

\[\begin{aligned} A_{11} = 1 \\ A_{12} = 3 \\ A_{21} = 7 \\ A_{22} = 5 \\ B_{11} = 6\\ B_{12} = 8\\ B_{21} = 4\\ B_{22} = 2 \end{aligned} \]

Step 2: Create the following 10 matrices

\[\begin{array}{lll} \begin{aligned} &S_1=B_{12}-B_{22} = 6, \\ &S_2=A_{11}+A_{12} = 4, \\ &S_3=A_{21}+A_{22} = 12, \\ &S_4=B_{21}-B_{11} = -2 , \\ &S_5=A_{11}+A_{22} = 6, \\ &S_6=B_{11}+B_{22} = 8, \\ &S_7=A_{12}-A_{22} = -2, \\ &S_8=B_{21}+B_{22} = 6, \\ &S_9=A_{11}-A_{21} = -6, \\ &S_{10}=B_{11}+B_{12} = 14 . \end{aligned} \end{array} \]

Step 3.

\[\begin{aligned} &P_1=A_{11} \cdot S_1 = 6\\ &P_2=S_2 \cdot B_{22} = 8\\ &P_3=S_3 \cdot B_{11} = 72\\ &P_4=A_{22} \cdot S_4 = -10\\ &P_5=S_5 \cdot S_6 = 48 \\ &P_6=S_7 \cdot S_8 = -12\\ &P_7=S_9 \cdot S_{10} = -84 \end{aligned} \]

Step 4. Step 4: Compute the desired submatrices \(C_{11}, C_{12}, C_{21}, C_{22}\) of the result matrix \(C\) by adding and subtracting various combinations of the \(P_i\) matrices.

\[\begin{aligned} &C_{11}=P_5+P_4-P_2+P_6 =18\\ &C_{12}=P_1+P_2 = 14\\ &C_{21}=P_3+P_4 = 62\\ &C_{22}=P_5+P_1-P_3-P_7 = 66 \end{aligned} \]

So the answer is

\[C = \left( \begin{array}{cc} 18 & 14 \\ 62 & 66 \\ \end{array} \right) \]

Question Strassen's algorithm, Part II

4.2-2 Write pseudocode for Strassen's algorithm.

Solution

See Algorithm 4

Question Strassen's algorithm, Part III

4.2-3 How would you modify Strassen's algorithm to multiply \(n \times n\) matrices in which \(n\) is not an exact power of 2? Show that the resulting algorithm runs in time \(\Theta\left(n^{\lg 7}\right)\).

Solution

Suppose the matrix \(A\) and \(B\) are all \(n\times n\) matrices. A simple method is padding 0s to the matrices such that \(n = 2^k\) for some \(k\).

Going a step further, if \(n\) is even, we can decompose \(n\) in the form

\[n=\prod_{i=1}^k p_i^{n_i} = 2^{n_1} p_2^{n_2} \cdots p_k^{n_k}, \]

if not, add one column and row of 0s. Then, we can use Strassen's algorithm to calculate the size \(2^{n_1}\) matrix whose elements are \(p_2^{n_2} \cdots p_k^{n_k} \times p_2^{n_2} \cdots p_k^{n_k},\) matrices. To calculate the size \(p_2^{n_2} \cdots p_k^{n_k} \times p_2^{n_2} \cdots p_k^{n_k}\) matrices, we can continually pad one column and row of zeros and then execute the
decomposition step in (7).

Here we focus on the simple case, that is we fill the input matrices with 0s to ensure that \(n = 2^k\) for some \(k\). In the padding step, the size of matrices becomes \(2^{\lceil \log n \rceil}\).

The runtime recurrence is given by (which is given on the handout)

\[T(n)= \begin{cases}\Theta(1) & \text { if } n=1 \\ 7 T(n / 2)+\Theta\left(n^2\right) & \text { if } n>1\end{cases} \]

\[a=7, b=2, f(n)=\Theta\left(n^2\right) \Rightarrow \log _b a=\log _2 7 \in[2.80,2.81]\quad f(n)=O\left(n^{\log _b a-\varepsilon}\right)\text{ for } \varepsilon = 0.5. \]

which is the case 1 of the master theorem so \(T(n)=\Theta\left(n^{\log _2 7}\right)\). Here we substitute \(n\) by \(2^{\lceil \log_ n \rceil}\), we have

\[\Theta\left(2^{\lceil \log n \rceil \log 7} \right) \]

Now we only need to prove that \(2^{\lceil \log n \rceil \log 7} = \Theta (n ^{\log 7 } )\). If we
choose \(c \leq 1, n_0 = 1\) we have

\[\begin{aligned} \log c +\log n \log 7 &\leq \lceil \log n \rceil \log 7 \\ \Rightarrow cn ^ {\log 7} &\leq 2^{\lceil \log n \rceil \log 7} \end{aligned} \]

for any \(n \geq n_0\). Therefore, \(2^{\lceil \log n \rceil \log 7 } = \operatorname \Omega (n ^ {\log 7})\). Similarly, we choose \(c \geq 7\) and \(n_0 = 1\),

\[\begin{aligned} \lceil \log n \rceil \log 7 &\leq \log c +\log n \log 7\\ \Rightarrow 2^{\lceil \log n \rceil \log 7 } &\leq cn ^ {\log 7} \end{aligned} \]

for any \(n \geq n_0\). So \(2^{\lceil \log n \rceil \log 7 } = \operatorname O (n ^ {\log 7})\).
Hence \(2^{\lceil \log n \rceil \log 7 } = \operatorname \Theta (n^{\log 7})\).

Question

Solve \(L(n)=2 L(n / 2-1)+\Theta(n)\).

Solution

Suppose \(T(n)\) for \(n\leq 1\) is known, and can be calculated in constant time. Then we solve this by drawing a recurrence tree.

Floor \(1\). \(n\), \(1\) nodes.

Floor \(2\). \(\frac{n}{2}-1\), \(2\) nodes.

Floor \(3\). \(\frac{n}{4}-\frac{3}{2}\), \(4\) nodes.

...

Floor \(k\). \(2^{1-k} n-2^{2-k} \left(2^{k-1}-1\right)\), \(2^{k-1}\) nodes.

i.e. Floor \(\left \lceil \log \left(\frac{2 (n+2)}{3}\right)\right \rceil\), 1 or less , \(2^{\left \lceil \log \left(\frac{2 (n+2)}{3}\right)\right \rceil -1 }\) nodes.

On each floor \(x\), the time overhead is \(-2^{1-x} 2^{x-1} \left(-n+2^x-2\right) = n-2^x+2\).

Then we calculate the total time overhead:

\[\left \lceil \log \left(\frac{2 (n+2)}{3}\right)\right \rceil \leq \log \left(\frac{2 (n+2)}{3}\right) + 1 \]

\[\sum _{x=1}^{\left \lceil \log \left(\frac{2 (n+2)}{3}\right)\right \rceil}{ n-2^x+2} \leq \sum _{x=1}^{\log \left(\frac{2 (n+2)}{3}\right) + 1}{ n-2^x+2} \]

where

\[\sum _{x=1}^{\log \left(\frac{2 (n+2)}{3}\right) + 1}{ n-2^x+2} = \frac{-5 n \log (2)+3 n \log \left(\frac{2 (n+2)}{3}\right)+6 \log \left(\frac{2 (n+2)}{3}\right)-4 \log (2)}{3 \log (2)} = \operatorname O(n\log n) \]

On the other hand,

\[\log \left(\frac{2 (n+2)}{3}\right) \leq\left \lceil \log \left(\frac{2 (n+2)}{3}\right)\right \rceil \]

\[\sum _{x=1}^{\log \left(\frac{2 (n+2)}{3}\right) }{ n-2^x+2} \leq \sum _{x=1}^{\left \lceil \log \left(\frac{2 (n+2)}{3}\right)\right \rceil}{ n-2^x+2} \]

where

\[\sum _{x=1}^{\log \left(\frac{2 (n+2)}{3}\right) }{ n-2^x+2} = \frac{-4 n \log (2)+3 n \log \left(\frac{2 (n+2)}{3}\right)+6 \log \left(\frac{2 (n+2)}{3}\right)-2 \log (2)}{3 \log (2)} = \operatorname \Omega(n\log n) \]

Hence the time complexity is \(\Theta (n \log n )\).

Question

Prove that

\[\sum_{k=2}^{n-1} k \ln k \leq \frac{1}{2} n^2 \ln n-\frac{1}{8} n^2, n \geq 3 \]

Proof

We tackle this by mathematical induction. First we check the base step.
When \(n = 3\), \(LHS = 2 \ln 2\) and
\(RHS = \frac{9}{2} \ln 3 - \frac{9}{8}\). \(LHS - RHS = -4.68246 \leq 0\)
so the base step holds.

Then we show the induction step. Suppose that

\[\sum_{k=2}^{n-1} k \ln k - \frac{1}{2} n^2 \ln n + \frac{1}{8} n^2\leq 0 \]

holds for \(n\) where \(n \geq 3\). Then

\[\begin{aligned} &\sum_{k=2}^{n} k \ln k - \frac{1}{2} (n+1)^2 \ln (n + 1) + \frac{1}{8} (n + 1)^2 \\ = &\sum_{k=2}^{n-1 } k \ln k + n \ln n - \frac{1}{2} (n+1)^2 \ln (n + 1) + \frac{1}{8}(n^2 + 2n + 1) \\ =&\sum_{k=2}^{n-1 } k \ln k +\frac{1}{8}n^2 + n \ln n - \frac{1}{2} (n+1)^2 \ln (n + 1) + \frac{1}{8}( 2n + 1) \\ \leq & \frac{1}{2} n^2 \ln n +n \ln n - \frac{1}{2} (n+1)^2 \ln (n + 1) + \frac{1}{8}( 2n + 1) \\ \end{aligned} \]

Let

\[f(n) = \frac{1}{2} n^2 \ln n +n \ln n - \frac{1}{2} (n+1)^2 \ln (n + 1) + \frac{1}{8}( 2n + 1) , n\geq 3 \\ \]

take its derivative:

\[\begin{aligned} f'(n) &= (n+1) \ln (n)-(n+1) \ln (n+1)+\frac{3}{4} \\ &\leq (n+1)\left(\frac{n}{n+1} - 1\right) +\frac{3}{4}\\ &= - \frac{1}{4}, \quad \text{for } n\geq 3 \end{aligned} \]

So the function \(f\) is monotonically decreasing. Notice that \(f(3) = \frac{7}{8}+\frac{15 \ln (3)}{2}-8 \ln (4)<0\), we conclude that \(f(n) < 0\) for all \(n \geq 3\).

Hence the induction step holds.

Therefore,

\[\sum_{k=2}^{n-1} k \ln k - \frac{1}{2} n^2 \ln n + \frac{1}{8} n^2\leq 0 \]

holds for \(n \geq 3\).

posted @ 2022-10-31 18:09  miyasaka  阅读(52)  评论(0)    收藏  举报