Algorithmic Analysis | Series 4 (Resolve recursion)

Source: Internet
Author: User

in the previous article, we discussed the analysis of loops. Many algorithms are recursive in nature. when we analyze them, we get the recursive relationship of time complexity. The run time we get is the input of size n as the function of N, and the run time of the input of the smaller size. For example, in "merge sort", in order to sort the given array, we divide it into two halves and repeat the two processes recursively. Finally we merge the results. The time complexity of the merge sort can be written as t (n) = 2T (N/2) + CN. There are many other algorithms, such as binary search, Hanoi, and so on.

There are three main ways to resolve recurrence.

1) Alternative methods : We guess the solution, and then we use mathematical induction to prove that the guess is correct or incorrect.

For example consider, the recurrence t (n) = 2T (n/2) + as T (n)= istrue  for= 2T (n/2) + n    <= cn/2log (n/2) + n    =  cnlogn-cnlog2 + n    =  CNLOGN-CN + n    <= Cnlogn

2) Repeating Tree method: In this method, we draw a repeating tree and calculate the time it takes for each level of the tree. Finally, we summarize the work at all levels. to draw a recurring tree, we start with the given recurrence and continue drawing until we find a pattern between the levels. The pattern is usually a series of arithmetic or geometry.

For example consider the recurrence relation T (n)= T (n/4) + T (n/2) +cn2 cn2/T (n/4) T (n/2) If We further BreakDown the expression T (n/4) and T (n/2), weGetfollowing recursion tree. CN2/\ C (n2)/ -C (N2)/4/      \          /T (n/ -) T (n/8) T (n/8) T (n/4) breaking down further gives us following CN2/\ C (n2)/ -C (N2)/4/      \            /C (N2)/ theC (N2)/ -C (N2)/ -C (N2)/ -/    \      /    \    /    \       /\ To know the value of T (n), we need to calculate sum of the tree nodes level by level. If We sum the above tree level by level, weGetThe following seriest (n)= C (n^2+5(n^2)/ -+ -(n^2)/ the) + .... The above series isGeometrical progression with ratio5/ -. toGetAn upper bound, we can sum the infinite series. WeGetThe sum as(n2)/(1-5/ -) which isO (N2)

3) Main method:
The Main method is the direct way to get the solution. the Main method only applies to the following types of recurrence or can be converted to the following types of duplicates.

where 1 1

There are three scenarios:


1. if f (n) =θ (n C) where C <log b A is t (n) =θ (n Log b a)

2. if f (n) =θ (n ? ) ) where C = Log b A t (n) =θ (n ? log n).

3. if f (n) =θ (n ? ) ) where c> logs b A t (n) =θ (F (n ))

How does this work?
The main method is mainly derived from recursive tree method. If we draw a recursive tree of t (n) = at (n/b) + f (n), we can see that the work done at the root is f (n), all the leaves work is θ (n c) c is log b A. and the height of the recursive tree is log bn in the recursive tree method, we calculate the total amount of work done. If most of the work is done on the leaves, then the leaves are the main part, and our result becomes the work on the leaves (case 1). If the work on the leaves and roots is gradual, then our result becomes a height multiplied by any level of work (case 2). If the work done at the root is asymptotically, then our results will be done in the root directory (case 3).

Using the Main method
Merge sort : T (n) = 2T (N/2) +θ (n) Examples of some standard algorithms that can evaluate their time complexity . Situation 2 because C for 1,log b A] is also 1. So the solution is θ (n logn)

Binary search : t (n) = t (N/2) +θ (1). the Case 2 also occurs when C is 0 and log b A is also 0. So the solution is θ (LOGN)

Attention:
1) The main theorem can be used to solve the recurrence of the form t (n) = at (n/b) + f (n). Given three cases there are some gaps between them. For example, you cannot solve duplicate t (n) = 2T (N/2) + N/logn using the Main method.

2) Case 2 can be extended to f (n) =θ (n C Log k N)
if f (n) =θ (n C Log k N) for some constants k> = 0,c = Log b A, then T (n) =θ (n C Log k + 1 N)

Algorithmic Analysis | Series 4 (Resolve recursion)

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.