Introduction to algorithms-3. Recursive exercises

Source: Internet
Author: User

This part of the Main introduction to algorithms involves recursive, heap sorting, and quick sorting.

Exercise 4.1-1Prove that $ T (n) = T (\ lceil n/2 \ rceil) + 1 $ is interpreted as $ O (\ lg n) $.

Q: I guess $ T (n) \ Leq C \ cdot \ lg (n-B) $ (for the reason $-B $, see Introduction to algorithms-4.1 substitution method-some minor issues), use mathematical induction, if it meets $ n/2 $, there are:

$ T (n) = T (\ lceil n/2 \ rceil) + 1 \ Leq C \ cdot \ lg (\ lceil n/2 \ rceil-B) + 1 \ Leq C \ cdot \ lg (n + 1-2b)-C \ cdot \ lg2 + 1 \ Leq C \ cdot \ lg n $

The last non-equal sign is valid when $ C> 1 $ and $ B> 1 $.

Exercise 4.1-6Recursive Formula $ T (n) = 2 T (\ SQRT {n}) + 1 $.

Idea: make $ M =\lg N $ the original recursive formula $ T (n) $ can be expressed as $ S (m) = T (2 ^ {m }) = 2 T (2 ^ {M/2}) + 1 = 2 S (M/2) + 1 $. Guess $ S (m) \ Leq cm-B $. The mathematical induction proves that if it satisfies $ M/2 $, there are:

$ S (m) = 2 S (M/2) + 1 \ leq2 (CM/2-b) + 1 = cm-2b + 1 \ Leq cm $

In $ T (n) $, $ T (n) \ Leq C \ cdot \ lg n $, that is, $ T (N) = \ mathrm {o} (\ lg n) $.

Exercise 4.2-2The solution for $ T (n) = T (N/3) + T (2n/3) + CN $ is $ \ omega (n \ lg n) $ using the recursive tree.

Idea: the recursive tree is not a Complete Binary Tree, and the leaf nodes are shallow. The shallow leaf node is from the root node along the path $ n \ rightarrow (1/3) n \ rightarrow (1/3) ^ {2} n \ rightarrow... \ rightarrow (1/3) ^ {k} n = 1 $, number of layers $ k = \ log _ {3} N $. In the shortest layer, the price for each layer is $ CN $. Therefore, $ T (n) \ geq k \ cdot Cn = cn \ cdot \ log _ {3} n = \ omega (n \ lg n) $.

Exercise 4.2-4Use the recursive tree to find the progressive closeness and confirmation of the recursive formula $ T (n) = T (n-A) + T (A) + CN $.

Idea: Draw a recursive tree. The recursive tree is extremely unbalanced. All the nodes of the right child are all leaf nodes, and the cost of the root node is $ CN $, the price for layer $ I $ is $ C (n + (1-I) a) $. A total of $ N/A $ layers (including the $0 $ layer of the root node ). Total cost:

$ T (n) = CN + \ sum _ {I = 1} ^ {n/A-1} C (n + (1-I)) = \ theta (N ^ {2}) $

Exercise 4.2-5Use the recursive tree to find the progressive closeness and confirmation of the recursive formula $ T (n) = T (1-A) n) + T (an) + CN $.

Idea: the Key to introducing algorithms is to understand the intention of the questions. This is also a sister question. It is intended to explain that when the recursive tree is divided by constants, no matter how balanced the division is, the cost is $ \ theta (N ^ {2 }) $. No matter how unbalanced the division is, the cost is $ \ theta (n \ lg n) $, because $ N $ can be large enough, to highlight the imbalance of constant division and the balance of Proportional division. Exercise 4.2-2 has proved the lower bound of a special case (there is no difference in general, the influence of the $1/3 $ coefficient enters the constant term), that is, $ T (N) = \ omega (n \ lg n) $. Now you only need to prove $ T (n) = O (N \ lg n) $: If $ T (n) \ Leq DN \ lg n $, use the replacement method:

$ T (n) \ Leq D (1-A) n \ lg (1-A) N + Dan \ LG an + Cn = DN \ lg n + DN (A \ lg a + (1-A) \ lg (1-A) $

Because $ A \ In (0, 1) $, $ \ lg a <0, \ lg (1-A) <0 $, $ d $ has an appropriate value for $ T (n) \ Leq DN \ lg n $.

Questions 4-2An array $ A [1... n] $ contains an integer from $0 $ to $ N $, but one is missing. Another array $ B [0... n] $ contains all integers from $0 $ to $ N $. However, an integer in the array cannot be accessed separately, but only one digit can be accessed (the integer is binary ), that is to say, the operation on the array $ A $ can only be "the $ J $ bit of $ A [I] $ ". An algorithm with the price of $ \ theta (n) $ is provided to locate the missing integer.

Idea: a total of $ \ lceil \ LG n \ rceil $ bits are binary unsigned integers. Count the number of integers $1 $ in $4 $ and $ B $ at the price of $ \ theta (n) $ ), if the number of integers whose bits $1 $ in $ A $ are less than $ B $, the value of $1 $ in the missing integer is $1 $, otherwise, the first value of the missing integer is $0 $; in the array $ A $ and $ B $, the number of integers whose second digit is $1 $ or $0 $ is calculated. \ theta (n/2) $ )...... until each digit of the missing integer is determined. The cost of the entire algorithm is:

$ \ Theta (n) + \ theta (n/2) +... + \ theta (N/(2 ^ {\ LG n}) = \ theta (2n) = \ theta (n) $

Questions 4-5Fibonacci number. Define the generate function (formal power function) as follows:

$ F (x) = \ sum _ {I = 0} ^ {\ infty} f _ {I} Z ^ {I} = 0 + Z ^ {2} + 2Z ^ {3} + 3z ^ {4} + 5z ^ {5} +... $

  1. Verify $ f (x) = z + Z \ cdot f (x) + Z ^ {2} \ cdot f (x) $.
    Q: Replace $ f (x) $
    $ F (x) = z + \ sum _ {I = 0} ^ {\ infty} f _ {I} \ cdot Z ^ {I + 1} + \ sum _ {I = 0} ^ {\ infty} f _ {I} \ cdot Z ^ {I + 2} = z + Z ^ {2} + \ sum _ {I = 2} ^ {\ infty} F _ {I} \ cdot Z ^ {I + 1} + \ sum _ {I = 2} ^ {\ infty} f _ {I-1} \ cdot Z ^ {I + 1} = z + Z ^ {2} + \ sum _ {I = 3} ^ {\ infty} f _ {I} \ cdot Z ^ {I} = \ sum _ {I = 0} ^ {\ infty} f _ {I} \ cdot Z ^ {I} $
  2. Proof:
    $ F (x) =\frac {z} {1-z-z ^ {2 }}=\ frac {z} {(1-\ Phi Z) (1 + \ hat {\ Phi} Z )} =\ frac {1} {\ SQRT {5 }}\ cdot (\ frac {1} {1-\ Phi z}-\ frac {1} {1 + \ hat {\ Phi} z }) $
    Train of Thought: Convert the formula in A to get it.

There are still two proofs behind this question.

Questions 4-6Large-scale integration chip testing. There are $ N $ chips with identical designs and some defective chips ). These chips can test each other and report whether the other chip is good or bad. A good chip always faithfully reports the quality of another chip, and the Report Result of a bad chip is uncontrollable, that is:

  • A report: B is good; B Report: A is good. It is both good or bad.
  • A report: B is bad; B Report: A is bad. At least one bad one.
  • A report: B is good; B is bad. At least one bad one.
  • A report: B is bad; B Report: A is good. At least one bad one.

The number of known good chips is greater than $ n/2 $, and an algorithm with the price of $ \ theta (n) $ is provided to find all the good chips.

Idea: If you find a good chip, you can use $ \ theta (n) $ to find all the good chips. The process of finding a good chip is as follows: Randomly combine them into n/2 groups for mutual testing, and pick out the combination of these two chips (the first one above, split each combination into one chip and put it together to form a subset of the original $ N $ chip (when the size of the subset is smaller than $ n/2 $, the cost of the algorithm is $ \ theta (n) $ ). Repeat this method on this subset until there is only one chip left in the subset, And the chip is a good chip. Of course, if $ N $ is an odd number, let's assume that $ N $ is a stream of $65536 $.

The following points need to be proved:FirstIt must be proved that this subset is not empty, that is, this subset can be selected: because the number of good chips in a is greater than n/2, there will be a combination of good chips and good chips to test each other, therefore, the subset cannot be empty;SecondIt must be proved that the subset size is not greater than $ n/2 $. Only in this way can the cost of the algorithm be controlled at $ \ theta (n) + \ theta (n/2) +... + \ theta (1) = \ theta (n) $ or less: randomly selectedOne pair of chipsAdd a subset. Even in the most extreme cases, good chips only work with good chip combinations. Bad chips and bad chipset are combined to "cheat" testers to report each other as well, then only $ n/2 $ chips were selected;LastIt must be proved that the number of good chips in the subset is still more than half of the number of the subset elements, so that the condition is passed until there is only one element in the subset: considering that the subset chip is not selected, either a combination of good and bad chips, or a combination of bad and bad chips, there must be no combination of good and good chips, therefore, the number of bad chips that are not selected into a subset is greater than or equal to the number of good chips. Considering that the number of good chips in all chips is greater than the number of bad chips, the number of good chips in the subset must be greater than the number of bad chips.

The final result of getting a chip must be a good chip. Then we can use it to test other chips and get all the good chips.

Questions 4-7Monge matrix. This question is long and difficult, but the conclusion is very interesting. Therefore, write down the conclusion and the specific proof process will be omitted.

A $ m \ times N $ matrix can be called a Monge matrix. if and only: For all $ I, J, K, L $1 \ Leq I <k \ Leq M $ and $1 \ Leq j <L \ Leq M $, then: $ A [I, j] + A [K, L] \ Leq A [I, L] + A [K, J] $. The following is a Monge matrix:

$ \ Begin {matrix} 10 & 17 & 13 \ 17 & 22 & 16 \ 24 & 28 & 22 \ 11 & 13 & 6 \ end {matrix} $

Intuitively, a small number is distributed in the upper left and lower right lines, and a large number is distributed in the upper left and lower right lines. There are several interesting conclusions:

  1. A sufficient condition for a matrix to be Monge is: For all $ I = ,..., m-1 $ and $ j = 1, 2 ,..., n-1 $, including: $ A [I, j] + A [I + 1, J + 1] \ Leq A [I, j + 1] + A [I + 1, j] $. That is, when and only when every small matrix of $2 \ times 2 $ in the matrix is a Monge matrix, the entire large matrix must be a Monge matrix.
  2. Considers $ f (I) $ to represent the column index with the smallest number in the $ I $ row. The matrix above, $ F (1) = 1, F (2) = f (3) = f (4) = 3 $. Then $ f (I) $ is a non-strictly incrementing function, and any $ I <J $ has $ f (I) <F (j) $.
  3. If several rows or columns are randomly extracted from the Monge matrix, the new matrix is still the Monger matrix. This feature can be used in recursive algorithms.

Exercise 6.5-8An algorithm at $ O (N \ lg k) $ is provided to combine $ K $ sorted linked lists into a sorted linked list. $ N $ indicates the number of elements in the output chain table. (The minimum heap is used as a K-link ).

Idea: similar to the minimum heap sorting, build a minimum heap. Each node represents a sorted linked list. The Node value is the first value of the linked list, and the root node is the smallest linked list of the first element. Take the first element from the root node linked list and put it into the output. The cost is $ O (1) $, which is the smallest element. Then the value of the root node increases, this operation may violate the minimum heap nature and run "Keep minimum heap nature" at the cost of $ O (\ lg k) $. $ O (\ lg k) + O (1) = O (\ lg k) $ cost is required for finding an element, to sort all $ N $ elements, you must pay $ O (N \ lg k) $.

Questions 6-2Analyze the $ d $ cross heap. $ D $ the same as the binary heap. The only exception is that each non-leaf node has $ d $ children instead of $2 $.

  1. How to represent $ d $ in the array (similar to binary heap ). I found a regular pattern on the draft and got stuck ...... The answer is: The $ J $ index of the node for $ I $ is $ d \ cdot (I-1) + J + 1 $, the index for the parent node is $ (I-2)/d + 1 $. The key idea is: The $ J $ child node of the node whose index value is $ I $. All the nodes above it are: the index value is the child node of all nodes ($ I-1 $) prior to the node of $ I $, plus your own front $ J-1 $ child nodes, plus the root node.
  2. $ D $ what is the height of the Cross stack? Answer $ \ lceil \ log _ {d} n \ rceil $.
  3. Several implementations of $ d $ cross-stack extract-Max, insert, and increase-key are provided. It's easy. Just copy the binary heap, but compare $ d $ nodes when comparing brother nodes.

Questions 6-3The young matrix is also a difficult but interesting problem. A young matrix of $ m \ times N $. Data in each row is sorted from left to right, and data in each column is sorted from top to bottom. A matrix can contain $ \ infty $. For example, a matrix:

$ \ Begin {matrix} 0 & 0 & 1 & 5 \ 2 & 5 & \ infty \ 3 & \ infty \ end {Matrix} $

  1. If $ A [] = \ infty $ in the matrix $ A $, all elements of the matrix are $ \ infty $, $ A [m, n] \ NEQ \ infty $, all elements of the matrix are not $ \ infty $. This is almost obvious.
  2. The young matrix is similar to the binary heap and can implement the extract-min algorithm and insert algorithm.
    Extract-min Algorithm: extracts an element $ A [I, j] $ and compares $ A [I + 1, J] $ and $ A [I, J + 1] $, select a small fill to $ A [I, j] $, for example, $ A [I + 1, J] $. Because $ A [I + 1, J] \ geq A [I, j] $, $ A [I, j] \ geq A [I-1, J] $, $ A [I, j] \ geq A [I, J-1] $, so the newly filled element meets $ A _ {New} [I, j] \ geq A [I-1, j], A _ {New} [I, j] \ geq A [I, J-1] $. Because $ A [I + 1, J] \ Leq A [I, j + 1] $ (compared previously), $ A [I + 1, j] \ Leq A [I + 2, J] $. The latter is about to become $ A [I + 1, J] $, so $ A _ {New} [I, j] \ Leq A [I, j + 1], A _ {New} [I, j] \ Leq A _ {New} [I + 1, J] $, the four directions meet the cycle-changing pattern, and then execute this set for $ A [I + 1, J] $ until $ \ infty $ is encountered. The minimum element is extracted from $ A [] $.
    The insert algorithm is similar, but it is inserted from $ A [M, N] $. When the numbers on the left and top are compared in sequence, the larger one is "squeezed, it can also prove that the cycle is not changing, basically the same as extract-min.
  3. The cost of sorting by using the young matrix of $ n \ times N $ is $ O (N ^ {3}) $. The cost of obtaining the heap top element is $ O (2n) $. There are $ n ^ {2} $ elements, and the price is $ O (N ^ {3}) $. Note that $ n ^ {2} $ elements are arranged here. The cost of $ N $ elements is $ O (N \ SQRT {n}) $.
  4. Determine whether a given number exists in the matrix within the running time $ O (m + n) $. Starting from the lower left corner of the matrix, if the given number is large, it is right. Otherwise, it is up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.