Algorithm: Lecture 4. Divide-and-Conquer Homework
*In this work, all the index of array starts by 1.
Question: Binary Search
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
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
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:
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
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.
Step 2: Create the following 10 matrices
Step 3.
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.
So the answer is
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
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)
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
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
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\),
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:
where
On the other hand,
where
Hence the time complexity is \(\Theta (n \log n )\).
Question
Prove that
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
holds for \(n\) where \(n \geq 3\). Then
Let
take its derivative:
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,
holds for \(n \geq 3\).
本文来自博客园,作者:miyasaka,转载请注明原文链接:https://www.cnblogs.com/kion/p/16845263.html