Summary of division and treatment method

Source: Internet
Author: User
If you want to reprint, you need to indicate the source: Http://blog.csdn.net/xiazdong in the third edition of the introduction of the algorithm introduced the second edition of the introduction of the "recursive" chapter of the "Division of the Law", and added "Maximum subarray" and " Strassen matrix calculation "two problems. This article will still be introduced in the explanation of some exercises to better understand the division of the law.
First, Recursive introduction
The method of division and administration is actually seen in many places, such as merging and sorting, and quick sorting, etc. are all solved by divide and conquer. The problem of each divide-and-conquer method is usually expressed as "recursive". For example, the recursive type of merge sort is as follows:
Of course, the general book usually writes:
The reason for this is to simplify the idea that the latter represents the assumption that "N is a power of 2". And the results of both representations are the same. Recursive Note: Be sure not to forget the boundary condition, that is, the T (1) value when n=1, although usually t (1) = 1.
The nature of the recursive type:
Now let's prove the conclusion that the first recursive is the same as the result of the second recursive type. The second recursive formula can be obtained through the main theorem, so it is not proved. The first recursive method is verified by substitution.
First step: Proving t (n) =o (NLGN)


The second step is to prove t (n) = Ω (NLGN)
So the proof.
Three solutions for recursion: (1) Substitution method: It is used primarily to verify the correctness of the hypothetical solution. (Note to prove the boundary condition) (2) Recursive Tree: Used to estimate the solution of a recursive formula. (3) Master Method: Used to solve some simple recursive solutions. (The proof of the principal theorem is proved by a recursive tree, which can not be delved into)
Usually: simple recursion is done with the main theorem, the annoying recursive first using recursive tree estimation, and then the substitution method to verify.
Recursive tree because it is relatively simple, so there is no explanation.
The main theorem I will simply list the formula:


Note: There is a gap between (1), (2), (3). For example, when it appears: T (n) =2t (N/2) +NLGN can not use the third case, because F (n) =nlgn, he is not a polynomial greater than n, so it is not a third case, but can be used in the second case.
Next, let's take an example to get a clearer understanding of how to solve recursion. (Introduction to Algorithmic 4-4 (f))


Ii. Introduction to the method of division and Administration
Definition: The original problem is decomposed into several independent sub-problems, solved separately, and the form of sub-problem is consistent with the original problem, but the scale is reduced.
A relatively easy-to-forget way to divide and conquer is: Base Case, that is, when to recursion, and when to stop. For example, the base case of BinarySearch is p<=q and stops when the start position is greater than the end position.
Three steps to divide and Conquer: (1) Divide: Decompose the original problem into multiple sub-problems. (2) Conquer: Recursive solution to a pair problem. (3) Combine: Merging sub-problems into the solution of the original problem.
Such as merge sort: Divide step is m= (P+Q)/2 Conquer step is Merge_sort (a,p,m) and Merge_sort (A,M+1,Q) combine step is merge (A,P,M,Q)
For example, two-point lookup: Divide step is m= (P+Q)/2 Conquer step is BinarySearch (a,p,m-1) or BinarySearch (A,M+1,Q) combine step is nil.
Assuming that divide requires F (n) time, combine needs g (n) time, which is decomposed to a sub-problem with a size of n/b, then the recursive representation is: T (n) =at (n/b) +f (n) +g (n)
Finally, we give an example of the method of partition:
Questions:Given an array of size n, and the data distribution of the array is first up and then down, that is, the first data is strictly increased, and then the data is strictly degraded, we need to find the maximum value in the array with O (LGN).
Ideas:Each time the center of the array is taken two numbers a and B, if a<b, the description is still in the ascending stage, the maximum value is in A[B...N], if a>b, then the maximum value is a[1...a]. Pseudo Code:

The next topic is also more classic, is the introduction of the algorithm study questions 4-6, here we only do B) small problem.

Answer: b)

Set J to output a "good" logarithm.
Ideas: 22 Test, if the output is "bad, good, bad bad", then all throw away, because each throw away at least one bad, if the output is "good", then leave one, when n is odd, there will be a lonely chip, if J is odd, then throw away the lonely chip; There is no lone chip when N is even.
Prove:
After removing those "bad, good, bad", when n is odd, then there must be a lonely chip at the end. When J is odd, it means that the logarithm of a good chip is at least 1 more logarithmic than the bad chip. So no matter what the rest, as long as throw away, the final good chip number is certainly more than the number of bad chips.
When J is an even number, it means that the logarithm of a good chip is at least as much as the logarithm of the bad chip testing each other. When the lonely chip is a good chip, then retain, good chip than bad chip more.
When the lonely chip is a bad chip, it can be further determined: good chip mutual testing of the logarithm of at least 2 pairs (nature 1, the back of the card), so even if the bad chip, the final good chip is still more than the bad chip.

Property 1 Proof: Because J is an even number, if the good chip mutual testing of the logarithm of at least than the bad chip mutual test logarithm, in the output "good" chip, in addition to the lonely chip, the number of good chips is equal to bad chip count, but with the lonely chip, then the bad chip is more than the good chip, and if the output Good or bad "again encountered the worst case: a bad, then the final description of the good chip in N is less than the bad chip, so with the conditions of contradiction.
When n is even, there is no lone chip at the end. Regardless of whether J is even odd, the logarithm of the bad chip tests each other at least 1 more logarithm than the bad chip. So the final good chip is more than the bad chip.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.