Docsity

CSE 230 Intermediate Programming in C and C++ Recursion, Study notes of C programming

Partial preview of the text

Download CSE 230 Intermediate Programming in C and C++ Recursion and more Study notes C programming in PDF only on Docsity!

CSE 230

Intermediate Programming

in C and C++

Recursion

Fall 2017

Stony Brook University

Instructor: Shebuti Rayana

What is recursion?

■ Sometimes, the best way to solve a

problem is by solving a smaller version

of the exact same problem first

■ Recursion is a technique that solves a

problem by solving a smaller problem

of the same type

Recursive Function: Example

■ This code

computes the sum

of first n positive

integers.

■ For n = 4

Function Call Value returned sum(1) 1 sum(2) 2+sum(1) or 2+ sum(3) 3+sum(2) or 3+2+ Sum(4) 4+sum(3) or 4+3+2+

Recursive Function

■ There is a base case (or cases) that is

tested upon entry

■ And a general recursive case

  • in which one of the variables, is passed as
an argument in such a way as to ultimately
lead to the base case.

Coding the Factorial Function

§ Recursive Implementation

int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }

■ For n > 12 this function will return

incorrect value as the final result is too big

to fit in an integer

Another Example: n choose k

(combinations)

■ Given n things, how many different sets of

size k can be chosen?

n n- 1 n- 1 = + , 1 < k < n (recursive solution) k k k- 1 n n! = , 1 < k < n (closed-form solution) k k!(n-k)! with base cases: n n = n (k = 1), = 1 (k = n) 1 n

n choose k implementation

int Combinations(int n, int k)
if(k == 1) // base case 1
return n;
else if (n == k) // base case 2
return 1;
else
return(Combinations(n-1, k) +
Combinations(n-1, k-1));

Recursion vs Iteration

■ Iteration can be used in place of recursion
  • An iterative algorithm uses a looping construct
  • 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

How to write a recursive function?

■ Determine the size factor

■ Determine the base case(s)

(the one for which you know the answer)

■ Determine the general case(s)

(the one where the problem is expressed as
a smaller version of itself)

■ Verify the algorithm

(use the "Three-Question-Method")

Recursion: Calculation of

Fibonacci Sequence

■ Recursive solution

"

%

'(%

'

'*%

, for 𝑖 = 1 , 2 , …
  • Except for 𝑓" and 𝑓% , every element in the sequence is the sum of the previous two elements
■ The sequence begins 0, 1, 1, 2, 3, 5, 8, …

int Fibonacci(int n) { if(n <= 1 ) // base case return n; else return(Fibonacci(n-1) + Fibonacci(n-2)); }

Recursion: Calculation of

Fibonacci Sequence

Pitfalls of Recursion

§ Missing base case – failure to provide an escape case. § No guarantee of convergence – failure to include within a recursive function a recursive call to solve a subproblem that is not smaller. § Excessive space requirements - a function calls itself recursively an excessive number of times before returning; the space required for the task may be prohibitive. § Excessive recomputation – illustrated in the recursive Fibonacci method which ignores that several sub- Fibonacci values have already been computed.