11. Recursion_outside

This sometimes requires we define additional

• Notes
• 21

This preview shows pages 6–13. Sign up to view the full content.

This sometimes requires we define additional  paramaters that are passed to the method. For example, we defined the array reversal  method as ReverseArray( A, i,  j ), not  ReverseArray( A ). ©  2010 Goodrich, Tamassia

This preview has intentionally blurred sections. Sign up to view the full version.

Using Recursion 7 Computing Powers The power function, p(x,n)=x n , can be defined  recursively: This leads to an power function that runs in  O(n) time (for we make n recursive calls). We can do better than this, however. - = = else ) 1 , ( 0 if 1 ) , ( n x p x n n x p ©  2010 Goodrich, Tamassia
Using Recursion 8 Recursive Squaring We can derive a more efficient linearly  recursive algorithm by using repeated squaring: For example, 2 4 = 2 ( 4 / 2 ) 2 = ( 2 4 / 2 ) 2 = ( 2 2 ) 2 = 4 2 = 16 2 5 = 2 1 +( 4 / 2 ) 2 = 2 ( 2 4 / 2 ) 2 = 2 ( 2 2 ) 2 = 2 ( 4 2 ) = 32 2 6 = 2 ( 6 / 2)2 = ( 2 6 / 2 ) 2 = ( 2 3 ) 2 = 8 2 = 64 2 7 = 2 1 +( 6 / 2 ) 2 = 2 ( 2 6 / 2 ) 2 = 2 ( 2 3 ) 2 = 2 ( 8 2 ) = 128 . = - = even is 0 if odd is 0 if 0 if ) 2 / , ( ) 2 / ) 1 ( , ( 1 ) , ( 2 2 x x x n x p n x p x n x p ©  2010 Goodrich, Tamassia

This preview has intentionally blurred sections. Sign up to view the full version.

Using Recursion 9 Recursive Squaring Method Algorithm  Power ( x, n ):       Input:  A number  and integer  n =  0       Output:  The value  x n      if  = 0 then return  1      if  is odd  then y  =  Power ( x,  ( n -  1) 2) return  ∙  y      else y =  Power ( x, n/  2) return   y ©  2010 Goodrich, Tamassia
Using Recursion 10 Analysis Algorithm  Power ( x, n ):       Input:  A number  and  integer  n =  0       Output:  The value  x n      if  = 0 then return  1      if  is odd  then y  =  Power ( x,  ( n -  1) 2) return  x ∙ y ∙ y      else y =  Power ( x, n/  2) return  y ∙ y It is important that we use a  variable twice here rather than  calling the method twice. Each time we make a recursive  call we halve the value of n;  hence, we make log n recursive  calls. That is, this method runs  in O(log n) time. ©  2010 Goodrich, Tamassia

This preview has intentionally blurred sections. Sign up to view the full version.

Using Recursion 11 Tail Recursion Tail recursion occurs when a linearly recursive  method makes its recursive call as its last step. The array reversal method is an example. Such methods can be easily converted to non- recursive methods (which saves on some resources). Example: Algorithm  IterativeReverseArray( A, i, j  ):       Input:  An array  and nonnegative integer indices  and  j       Output:  The reversal of the elements in  starting at index  and ending at  j      while  i <  j  do Swap  A [ ] and  A ] i  = i  + 1 j  = j -  1      return ©  2010 Goodrich, Tamassia
Using Recursion 12 Binary Recursion

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern