0% found this document useful (0 votes)
52 views

Recursion (Implementation) : Satish Dethe Satishd@cse - Iitb.ac - in

This document discusses recursion, defining it as a technique where a function calls itself to simplify a problem into a solvable state. It provides examples of calculating Fibonacci numbers and generating character permutations recursively. For Fibonacci, it shows the base cases and recursive calculation. For permutations, it explains generating them by recursively swapping the first character with others. Finally, it outlines a general recursive method for generating permutations of any length string by making recursive calls while swapping prefixes of increasing length.

Uploaded by

Chander Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Recursion (Implementation) : Satish Dethe Satishd@cse - Iitb.ac - in

This document discusses recursion, defining it as a technique where a function calls itself to simplify a problem into a solvable state. It provides examples of calculating Fibonacci numbers and generating character permutations recursively. For Fibonacci, it shows the base cases and recursive calculation. For permutations, it explains generating them by recursively swapping the first character with others. Finally, it outlines a general recursive method for generating permutations of any length string by making recursive calls while swapping prefixes of increasing length.

Uploaded by

Chander Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Recursion

(Implementation)
Satish Dethe
[email protected]

March 11, 2005

Contents

Definition
Structure
Examples
Implementation

March 11, 2005

Recursion
Definition: Recursion is a powerful
algorithmic technique, in which a function
calls itself (either directly or indirectly) on a
problem of the same type in order to simplify
the problem to a solvable state.
Some important program such as tower of
hannoi and tree traversal can be solve easily
using recursion .
March 11, 2005

Recursion

March 11, 2005

Recursion: Technique
A recursive definition always has at least two
parts, a boundary condition and a recursive case
The boundary condition defines a simple ( basic)
case that we know to be true.
The recursive case simplifies the problem by
advancing just by a small step, and then calling
itself.
At each level, the boundary condition is
checked. If it is reached the recursion ends. If
not, the recursion continues.
March 11, 2005

Example No. 1
Fibonacci Series: This series starts with 0 & 1 as,
first & second elements respectively. And every next
element is calculated as sum of previous two numbers.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
Input: 1
Output: 0
Input: 2
Output: 1
Input: 4
Output: 2
March 11, 2005

Example No. 1: Implementation

int Fib (int value){ // a recursive function


if (value == 1) return 0;
if (value == 2) return 1;
if (value > 2)
return ( Fib (value-1) + Fib (value-2)) ;
}
Refer fib.java

March 11, 2005

Example No. 2: Character Permutations


Give all possible permutations of chars from
given character string
Input: bc
Output: bc, cb
Input: abc
Output: abc, acb, bac, bca, cba, cab
Input: abcd
Output: abcd, abdc, acbd, acdb, adcb, adbc,
bacd, badc, bcad, bcda, bdca, bdac
March 11, 2005

Example No. 2: Character Permutations

Input: abc
Interm. Step:
[1] abc :- as it is
[2] bac :- swap 1st with 2nd char
[3] cba :- swap 1st with 3rd char
Technique:
abc [(a, bc), (a, cb)]
bac [(b, ac), (b, ca)]
cba [(c, ba), (c, ab)]
March 11, 2005

Character Permutations.
Input: abcd
..(length is 4)
Here we should make recursive calls as shown
below,
[1] (a, bcd)
a-bcd, a-bdc, a-cbd, a-cdb, a-dcb, a-dbc
[2] (b, acd)
[3] (c, bad)
[4] (d, bca)
Four calls because the strings length is 4.
March 11, 2005

General Method
Input: C1C2C3C4.......Cn-2Cn-1Cn
Make recursive call to
(C1 , C2C3C5.......Cn-2Cn-1Cn) , it will invoke
same function recursively as follows,
(C1C2, C3C4C5.......Cn-2Cn-1Cn)
(C1C2C3,C4C5.......Cn-2Cn-1Cn)
(C1C2C3C4,C5.......Cn-2Cn-1Cn)
:
:
(C1C2C3C4C5....... Cn-3,Cn-2Cn-1Cn)
March 11, 2005

Application
Parsing (Adjective and Nouns)
http://arti.vub.ac.be/~joachim/webpage/node
30.html

March 11, 2005

Thanks!

March 11, 2005

You might also like