Doan Nhat Quang Recursion Recursive Methods Definition A

Doan nhat quang recursion recursive methods

This preview shows page 21 - 33 out of 53 pages.

Doan Nhat Quang Recursion 15 / 38
Image of page 21
Recursion Recursive Methods Definition A function is indirectly recursive if it calls its invoker and eventually results in the original call. Compute the factorial of a given integer n 1 i n t f a c t o r i a l ( i n t n) { 2 i f (n == 1) 3 r e t u r n 1; 4 e l s e 5 r e t u r n multi (n ) ; 6 } 7 i n t multi ( i n t n) { 8 r e t u r n n * f a c t o r i a l (n - 1); 9 } Doan Nhat Quang Recursion 15 / 38
Image of page 22
Recursion Recursion Designing Recursive Algorithms General strategy: Divide and Conquer I How can we divide the problem into smaller sub-problems? I How does each recursive call make the problem smaller? I How do we define the base case? I Will we always reach the base case? Doan Nhat Quang Recursion 16 / 38
Image of page 23
Recursion Recursive Methods Attention!! I Instructions must be clear and precise . I Stopping conditions or base cases are required to avoid infinite recursive calls . Doan Nhat Quang Recursion 17 / 38
Image of page 24
Recursion Euclid’s Algorithm I Finds the greatest common divisor of two non-negative integers I Recursive definition of gcd algorithm I if gcd (a, b) = a (if b is 0) I if gcd (a, b) = gcd (b, a % b) (if b != 0) 1 i n t gcd ( i n t a , i n t b) { 2 i f (b == 0) 3 r e t u r n a ; 4 e l s e 5 r e t u r n gcd (b , a % b ) ; 6 } Doan Nhat Quang Recursion 18 / 38
Image of page 25
Recursion Euclid’s Algorithm 1 i n t gcd ( i n t a , i n t b) { 2 i n t temp ; 3 while (b != 0) { 4 temp = b ; 5 b = a % b ; 6 a = temp ; 7 } 8 r e t u r n a ; 9 } 1 i n t gcd ( i n t a , i n t b) { 2 i f (b == 0) 3 r e t u r n a ; 4 e l s e 5 a = a%b ; 6 r e t u r n gcd (b , a ) ; 7 } Doan Nhat Quang Recursion 19 / 38
Image of page 26
Recursion Fibonacci Series Example: Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, ... 1 i n t f i b o ( i n t n) { 2 i f (( n == 0) | | (n == 1)) // base cases 3 r e t u r n n ; 4 r e t u r n f i b o (n - 1) + f i b o (n - 2); 5 } Doan Nhat Quang Recursion 20 / 38
Image of page 27
Recursion Recursive Algorithms The picture shows that the solu- tion computes solutions to the sub- problems more than once for no reason: 7 6 5 4 3 2 1 0 1 1 2 3 5 8 13 21 Complexity is exponential, O (2 n ) How to reduce the complexity for this problem? Doan Nhat Quang Recursion 21 / 38
Image of page 28
Recursion Fibonacci Series I Reserve a string using Linked Lists and recursion. I Find the less bill for a given amount of money. Doan Nhat Quang Recursion 22 / 38
Image of page 29
Recursion Recursive Methods Tracing a recursive method: I As always, go line by line I Recursive methods may have many copies I Every method call creates a new copy and transfers flow of control to the new copy I Each copy has its own: I code I parameters I local variables Doan Nhat Quang Recursion 23 / 38
Image of page 30
Recursion Recursive Methods Tracing a recursive method after completing a recursive call: I Control goes back to the calling environment. I Recursive call must execute completely before control goes back to previous call. I Execution in previous call begins from point immediately following recursive call. Doan Nhat Quang Recursion 24 / 38
Image of page 31
Recursion Recursive Types I Tail recursion : a recursive method makes its recursive call as its last step.
Image of page 32
Image of page 33

You've reached the end of your free preview.

Want to read all 53 pages?

  • Spring '18
  • DK Daniels
  • Recursion, Doan Nhat Quang

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes