Data Structures and Algorithms - recursion

Recursion (Recursion)

Recursive definition

If an object contains its own part, or use it to their own definition, the object is called recursively;
if a procedure calls itself directly or indirectly, this process is called recursive process.
This procedure or function call this component appears in the definition of a procedure or function is called recursively.

Because each function call, the system will allocate a new storage area space for arguments and local variables, function calls itself is possible. This type of function is called a recursive function.
When a function calls itself, a process called direct recursion.
If a procedure or function call a procedure or function p q, and q and calls p, called indirect recursion.
If a recursive procedure or recursive function recursive call is the last statement executes the statement claimed that this recursive call is tail recursion.

int fac(int n)
{    
        if (n==0||n==1) 			
             return  1;		
        else 			
             return  n * fac(n-1);	
}

It is seeking n! (N is a positive integer) of recursive functions. In this function fac (n) solution process, direct call fac (n-1) itself, so it is a recursive function directly. Also, because the recursive call is the last statement, so it belongs to the tail-recursive.

When to use recursion

In the following three cases, the recursive method often used:
1. Define recursive
many mathematical formulas, such as the number of columns is defined recursive. These problems solving process can be defined recursively converted directly to the corresponding recursive algorithm.
[Example] factorial Factorial of recursive algorithm

Here Insert Picture Description
[Example] Fibonacci definitions of Number of
Here Insert Picture Description
Fibonacci recursive algorithm of Number of
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ......

long Fib ( long n ) 
{    if ( n <= 1 ) 
	return n;		    else 
	return Fib (n-1) + Fib (n-2);
}	

2. The data structure is recursive
[Example] single chain structure
Here Insert Picture Description

The next data type is itself (Node) type

Here Insert Picture Description
For recursive data structure, using recursive algorithm method is convenient and effective writing.
[Example] Find the last node list
Here Insert Picture Description
[Example] Find the node equal to a predetermined value in the list
Here Insert Picture Description
3 is recursive Solving the Problems
[Example] Tower of Hanoi (Tower of Hanoi) Problem
Here Insert Picture Description
Task: A on the pylon the disc moved to the pylon C.
Rule: each movement of a disc 1;
2 disc can only be moved pylon A, B, C;
3. large disc can not be placed on a small disc.
Here Insert Picture Description

Recursive model

Recursive recursive algorithm model is abstract, it reflects a recursive structure for recursive problem.

For example, the front recursive factorial model corresponding to the following recursive algorithm:
Fun (. 1). 1 = (. 1)
Fun (n-) * = n-Fun (. 1-n-) n->. 1 (2)
wherein the first equation gives the relationship between the value of the recursive termination condition, the second equation gives fun (n) value and the fun (n-1), we called the first recursive formula outlet, the second formula son called recursively body.

Generally, a recursive model is recursive and recursive outlet body composed of two parts, recursively to determine when the end of the former, which determines when the recursion relation recursive solution. The general form of recursive exports follows:
f (s1) = m1
where m1 and s1 are constants, some problems may have several recursive recursive exports. The general format of recursive body were as follows:
F (Sn) = G (F (Sn-. 1), C)
Sn here is a recursive "big problem", sn-1 recursive "small problem", c can be directly (in non-recursive method) to solve the problem, g is a non-recursive function can be evaluated directly.

Recursive implementation process

The idea is to put a recursive or bad can not directly solve the "big problem" into one or a few "minor problems" to solve, then further decomposition of these "small problem" into smaller "small problem" to solve, so decompose until each "small problem" can be solved directly (recursive decomposition to exit).

一旦遇到递归出口,分解过程结束,开始求值过程,所以分解过程是“量变”过程,即原来的“大问题”在慢慢变小,但尚未解决,遇到递归出口后,便发生了“质变”,即原递归问题便转化成直接问题。

递归分解要保证“大问题”与“小问题”相似,即求解过程与环境都相似。
递归的执行过程由分解和求值两部分构成

递归工作栈

递归过程在实现时,需要自己调用自己。
主程序第一次调用递归过程为外部调用;
递归过程每次递归调用自己为内部调用。
层层向下递归,退出时的次序正好相反:

一个递归函数的运行过程类似于多个函数的嵌套调用,差别仅在于“调用函数和被调用函数是同一个函数”。

为了保证“每一层的递归调用”都是对“本层”的数据进行操作,在执行递归函数的过程中需要一个“递归工作栈”。它的作用是:

  • 将递归调用时的实参和函数返回地址传递给下一层执行的递归函数;
  • 保存本层的参数和局部变量,以便从下一层返回时重新使用它们。

递归的设计方法

递归的求解的过程均有这样的特征:先将整个问题划分为若干个子问题,通过分别求解子问题,最后获得整个问题的解。而这些子问题具有与原问题相同的求解方法,于是可以再将它们划分成若干个子问题,分别求解,如此反复进行,直到不能再划分成子问题,或已经可以求解为止。

这种自上而下将问题分解、求解,再自下而上引用、合并,求出最后解答的过程称为递归求解过程。这是一种分而治之的算法设计方法。

递归函数的一般设计格式

Recursion(参数表n)
{
	if(递归结束条件) //递归出口
		return(一般为常量); //可直接求解的步骤
	else
		return Recursion(较小的n); //递归项
}

递归算法到非递归算法的转换

递归算法有两个基本特性:

  • 一是递归算法是一种分而治之的、把复杂问题分解为简单问题的求解问题方法,对求解某些复杂问题,递归算法分析问题的方法是十分有效的;

  • 二是递归算法的时间效率通常比较差。因此,对求解某些问题时,我们希望用递归算法分析问题,用非递归算法具体求解问题。这就需要把递归算法转换为非递归算法。

Conversion method
for tail-recursive and recursive algorithms way, an alternative algorithm may be a cyclic structure. (Direct conversion)
they used to run simulation system stack when the stack by analyzing information is only stored must be preserved, replacing non-recursive algorithm recursive algorithm. (Indirect conversion, the need to use the stack)
by using the parameters saved system stack, since the stack LIFO features with the execution of the recursive algorithm, which may be substituted with a non-recursive algorithm recursive algorithm. (Need to use the stack)

Recursion:
Advantages: easy to understand, clear structure;
disadvantages: the need for a large number of stack operations, press-fitting function parameters and return address, the stack size and the level of recursion is limited about the system;
cycle:
advantages: more efficient;
disadvantages: some time is very difficult to understand, requires a lot of imitation stack operations at the time of transition from recursion to loop;
[proposed] simple question to make use of cycles to solve, and for complex or significant recursive idea is recursive, so easy to understand and maintain.

Published 141 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42554191/article/details/103978183