Presentation is loading. Please wait.

Presentation is loading. Please wait.

递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.

Similar presentations


Presentation on theme: "递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be."— Presentation transcript:

1 递归算法的效率分析

2 When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be a return to the correct place in the calling block after the function code is executed; This correct place is called the return address When any function is called, the run-time stack is used --On this stack is placed an activation record for the function call

3 Stack Activation Frames The activation record contains the return address for this function call, and also the parameters, and local variables, and space for the function’s return value, if non-void The activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function code At this time the function’s return value, if non- void, is brought back to the calling block return address for use there

4 A Stake of Activation Records S(1) S(2) S(3) S(4) Main() int s (int n) { if (n ==1) return 1; else return s(n-1) + n; } void main() { int sum; sum = s(4); //instruction 100 printf(“sum=%d”,sum); return; }

5 int Func(/* in */ int a, /* in */int b ) { int result; if ( b == 0 ) // base case result = 0; else if ( b > 0 ) // first general case result = a + Func ( a, b - 1 ) ) ; // instruction 50 return result; } void main() { int x; x = Func(5,2); //instruction 100 printf(“%d\n”,x); return; } A recursive function

6 FCTVAL ? result ? b 2 a 5 Return Address 100 Run-Time Stack Activation Records original call at instruction 100 pushes on this record for Func(5,2) x = Func(5, 2);// original call at instruction 100

7 FCTVAL ? result ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2) call in Func(5,2) code at instruction 50 pushes on this record for Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

8 FCTVAL ? result ? b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2) record for Func(5,1) call in Func(5,1) code at instruction 50 pushes on this record for Func(5,0) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

9 FCTVAL 0 result 0 b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,0) is popped first with its FCTVAL record for Func(5,2) record for Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100

10 record for Func(5,2) record for Func(5,1) is popped next with its FCTVAL Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100 FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100

11 x = Func(5, 2);// original call at instruction 100 FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5 Return Address 100 record for Func(5,2) is popped last with its FCTVAL Run-Time Stack Activation Records

12 Divide and Conquer Given an instance of the problem to be solved, split this into several, smaller, sub-instances (of the same problem) independently solve each of the sub-instances and then combine the sub-instance solutions so as to yield a solution for the original instance.

13 Too much recursion Can Be Dangerous Fibonacci numbers. long fib (int n) { if (n <=1) return n; else return fib(n-1) + fib(n-2); } F5 F4 F3 F3 F2 F2 F1 F2 F1 F1 F0F1 F0 F1 F0

14 This definition will lead to exponential running time. Reason: -- too much redundant work Not necessary to use recursive Too much recursion Can Be Dangerous

15 Recursion vs. Iteration You could have written the power-function iteratively, i.e. using a loop construction Where‘s the difference ?

16 Iteration can be used in place of recursion –An iterative algorithm uses a looping structure –A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code (Nearly) every recursively defined problem can be solved iteratively  iterative optimization can be implemented after recursive design Recursion vs. Iteration

17 Deciding whether to use a Recursive Function When the depth of recursive calls is relatively “shallow” The recursive version does about the same amount of work as the nonrecursive version The recursive version is shorter and simpler than the nonrecursive solution

18 Recursion or Iteration? EFFICIENCYCLARITY

19 http://id.mind.net/~zona/mmts/geo metrySection/fractals/tree/ treeFractal.html Examples: Fractal Tree

20 Examples: The 8 Queens Problem http://mossie.cs.und.ac.za/~ murrellh/javademos/queens /queens.html Eight queens are to be placed on a chess board in such a way that no queen checks against any other queen


Download ppt "递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be."

Similar presentations


Ads by Google