The time complexity of computing recursive procedure

1. recursive function of time complexity analysis

(1) a recursive implementation process

Example: Find N !.
This is a simple "tired by" problems can be solved with a recursive algorithm.
!! = n-n-* (n--. 1) n->. 1
!! = 0. 1,. 1. 1 = = n-0,1
Thus, the following recursive algorithm:

Java code
FACT (n-int) {
IF (n-n-== == 0 ||. 1)
return. 1;
the else
return FACT n-* (n--. 1);
}
in n = 3 as an example, see the operation process is as follows:
FACT ( . 3) ----- FACT (2) ----- FACT (. 1) ------ FACT (2) ----- FACT (. 3)
----------- -------------------> ------------------------------ >
recursive backtracking
recursive algorithm running constantly calls itself reduce the size of the process, when the size is reduced to 1, that is recursive to fact (1), satisfy the conditions to stop the recursive stop, start back (return to calling algorithm) and calculated from the fact ( 1) = 1 returns to calculate the fact (2); calculating 2 FACT (1) = 2 returns to the fact (3); calculated. 3 FACT (2) =. 6, the end of the recursion.
Starting module algorithm is terminated modules.

(2) recursive implementation mechanism

每一次递归调用,都用一个特殊的数据结构"栈"记录当前算法的执行状态,特别地设置地址栈,用来记录当前算法的执行位置,以备回溯时正常返回。递归模块的形式参数是普通变量,每次递归调用得到的值都是不同的,他们也是由"栈"来存储。 

Several forms (3) recursive call

一般递归调用有以下几种形式(其中a1、a2、b1、b2、k1、k2为常数)。 

<1> straightforward recursive call: f (n) {... a1 * f ((n - k1) / b1); ...};

<2> direct complex recursive call: F (n-) {... A1 * F ((n-- K1) / B1); A2 * F ((n-- K2) / B2); ...};
<. 3> Indirect recursive call: F (n-) {A1 * F ... ((n-- K1) / B1); ...},
G (n-) {F * A2 ... ((n-- K2) / B2); ...}.

2. recursive algorithm efficiency analysis method

Analysis recursive algorithm is more, the most commonly used method is iterative.
The basic step iterative method is first reduced to the corresponding recursive algorithm recurrence equation and then through iteration, the right end of the recurrence equation is converted into a series, and the final demand series, and then gradually advanced estimated.
! <1> Example: n
recurrence equation of the algorithm is: T (n) = T ( n - 1) + O (1);
Iterative expand: T (n-) = T (n-- 1) + O (1)
= T (n-- 2) + O (. 1) + O (. 1)
= T (n--. 3) + O (. 1) + O (. 1) + O (. 1)
= ...
= O (. 1) + ... + O ( . 1) O + (. 1) O + (. 1)
= O n-* (. 1)
= O (n-)
of this example is linear time complexity.
<2> Example: The following recursive equation:

  T(n) = 2T(n/2) + 2, 且假设n=2的k次方。 
  T(n) = 2T(n/2) + 2 
       = 2(2T(n/2*2) + 2) + 2 
       = 4T(n/2*2) + 4 + 2 
       = 4(2T(n/2*2*2) + 2) + 4 + 2 
       = 2*2*2T(n/2*2*2) + 8 + 4 + 2 
       = ... 
       = 2的(k-1)次方 * T(n/2的(i-1)次方) + $(i:1~(k-1))2的i次方 
       = 2的(k-1)次方 + (2的k次方)  - 2 
       = (3/2) * (2的k次方) - 2 
       = (3/2) * n - 2 
       = O(n) 
  这个例子的时间复杂性也是线性的。 

<3> Example: The following recursive equation:

  T(n) = 2T(n/2) + O(n), 且假设n=2的k次方。 
  T(n) = 2T(n/2) + O(n) 
       = 2T(n/4) + 2O(n/2) + O(n) 
       = ... 
       = O(n) + O(n) + ... + O(n) + O(n) + O(n) 
       = k * O(n) 
       = O(k*n) 
       = O(nlog2n) //以2为底 
 
  一般地,当递归方程为T(n) = aT(n/c) + O(n), T(n)的解为: 
  O(n)          (a<c && c>1) 
  O(nlog2n)     (a=c && c>1) //以2为底 
  O(nlogca)     (a>c && c>1) //n的(logca)次方,以c为底 

Three kinds of recursive calls forms described above, it is commonly used the first case, the second form sometimes occur, and less third form (indirect recursive calls) used, and the analysis algorithm
complicated. Here's an example of a second form of recursive calls.
<4> recursive equation: T (n) = T ( n / 3) + T (2n / 3) + n
For a better understanding, to draw the corresponding recursive tree recursive process:
n------- -> n-
n-/ 2N. 3 /. 3 --------> n-
n-/. 9 2N / 2N. 9 / 4N. 9 /. 9 --------> n-
... ... ... ... ...
--- -----
total O (nlogn)
integrated recursion value tree of layers of non-recursive terms, and each layer is equal to n, the longest path from the root to the leaves are:

  n --> (2/3)n --> (4/9)n --> (12/27)n --> ... --> 1 
 设最长路径为k,则应该有: 
  
 (2/3)的k次方 * n = 1 
 得到 k = log(2/3)n  // 以(2/3)为底 
 于是 T(n) <= (K + 1) * n = n (log(2/3)n + 1) 
 即 T(n) = O(nlogn) 
由此例子表明,对于第二种递归形式调用,借助于递归树,用迭代法进行算法分析是简单易行的。
Published 10 original articles · won praise 0 · Views 855

Guess you like

Origin blog.csdn.net/SkyingData/article/details/104084377