Download presentation
Presentation is loading. Please wait.
1
Recursion: Overview What is Recursion? Reasons to think recursively? An Example How does recursion work?
2
What is Recursion? Recursion is a powerful computing concept, that simplifies the solution of the problem, resulting in shorter, more easily understood source code. Recursive method is the method that calls itself directly or indirectly. int fact(int x) { if (x==0) return 1; else return x*fact(x-1); }
3
Reasons for Thinking Recursively Reason 1: Some problems are recursively defined. Reason 2: A recursive way of problem solving is powerful. Reason 3: A programming task may become simplified. Reason 4: It is easier to prove the correctness. Reason 5: It is easier to analyze the complexity.
4
How to Write a Recursive Method Recursive method must have: 1) Base Case 2) One or more general cases that include recursive call int fact(int x) { if (x==0) return 1; else return x*fact(x-1); }
5
Activation Record and Run Time Stack main activation record method1 activation record method2 activation record Method3 activation record call method1( ); call method2( ); call method3( );
6
Run-time stack for recursive calls Assume that the main method is loaded in memory at location 5000 Assume that fact method is loaded at location 1000 Assume that the call fact method in main happened in location 5200 We have the following call to the fact method answer = fact(4); main program … answer=fact(4); int fact(int x){ if (x==0) return 1; else return x*fact(x-1); } 1000 1010 5000 5200
7
How Recursion Works? main program … answer=fact(4); int fact(int x){ if (x==0) return 1; else return x*fact(x-1); } 1000 1010 5000 5200 answer = 24 return address = 5200 n=4 fact(4)=4*fact(3) = 24 return address = 1010 n=3 fact(3)=3*fact(2) = 6 return address = 1010 n=2 fact(2)=2*fact(1) = 2 return address = 1010 n=1 fact(1)=1*fact(0) = 1 return address = 1010 n=0 fact(0)=1
8
Recursion Example Some problems are very difficult to solve iteratively and it is much easier and more natural to be implemented recursively One Problem that is best solved Recursively is the construction of the von Koch snowflake. It is an example of a continuous and non differentiable curve with an infinite length and takes finite area.
9
Von Koch "Snowflake" Curve Starting from Equilateral Triangle, replace every side with the following patterns: This Process can be repeated on all line segments.
10
Von Koch "Snowflake" Curve 1)Divide the line into three even parts. 2)Draw one third of the side 3)Turn left 60 degrees and draw the second segment. 4)Turn 120 degrees and draw one third of the side 5)Turn 60 degrees and draw one third of the size. 120 60 side
11
Von Koch Snowflake curve : the Algorithm DrawFourLines (side, level) { if (level=0) Draw a line; else DrawFourLines(side/3, level-1); Turn left 60 degrees DrawFourLines(side/3, level-1); Turn right 120 DrawFourLines(side/3,level-1); Turn left 60 degrees DrawFourLines(side/3,level-1) }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.