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

Recursion

Uploaded by

Soham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Recursion

Uploaded by

Soham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Recursion

Computing Laboratory

http://www.isical.ac.in/~dfslab

Computing Lab (ISI) Recursion 1 / 18


Recursion

A recursive function is a function that calls itself.

The task should be decomposable into sub-tasks that are smaller, but
otherwise identical in structure to the original problem.
The simplest sub-tasks (called the base case) should be (easily)
solvable directly, i.e., without decomposing it into similar
sub-problems.

Computing Lab (ISI) Recursion 2 / 18


Recursive vs. iterative implementations

int factorial ( int n ) {


int prod;

if (n < 0) return (-1);


prod = 1;
while (n > 0) {
prod *= n;
n = n - 1;
}
return (prod);
}

Computing Lab (ISI) Recursion 3 / 18


Recursive vs. iterative implementations

int factorial ( int n ) /* Recursive implementation */


{
if (n < 0) return (-1); /* Error condition */
if (n == 0) return (1); /* Base case */
return(n * factorial(n-1)); /* Recursive call */
}

Computing Lab (ISI) Recursion 4 / 18


Z curve

Problem statement
Consider a 2-D matrix of size 2m × 2m . The entries of the matrix are, in
row-major order, 1, 2, 3, . . . , 22m . Print the entries of the matrix in Z-curve
order (as shown in the picture below).

Computing Lab (ISI) Recursion 5 / 18


Z curve: structure

(0, 0) Problem structure:


Base case: single
element
Recursive structure:
break given square
into 4 sub-squares
process squares in
Z-curve order

(N − 1, N − 1)

Computing Lab (ISI) Recursion 6 / 18


Z curve: code I

void z_curve(int top_left_row, int top_left_column,


int bottom_right_row, int bottom_right_column,
int **matrix)
{
/* Base case */
if (top_left_row == bottom_right_row &&
top_left_column == bottom_right_column) {
printf("%d ", matrix[top_left_row][top_left_column]);
return;
}

/* Recurse */

/* upper-left sub-square */
z_curve(top_left_row,
top_left_column,
(top_left_row + bottom_right_row)/2,

Computing Lab (ISI) Recursion 7 / 18


Z curve: code II

(top_left_column + bottom_right_column)/2,
matrix);

/* upper-right sub-square */
z_curve(top_left_row,
(top_left_column + bottom_right_column)/2 + 1
(top_left_row + bottom_right_row)/2,
bottom_right_column,
matrix);

/* lower-left sub-square */
z_curve((top_left_row + bottom_right_row)/2 + 1,
top_left_column,
bottom_right_row,
(top_left_column + bottom_right_column)/2,
matrix);

/* lower-right sub-square */

Computing Lab (ISI) Recursion 8 / 18


Z curve: code III

z_curve((top_left_row + bottom_right_row)/2 + 1,
(top_left_column + bottom_right_column)/2 + 1,
bottom_right_row, bottom_right_column,
matrix);
return;
}

Computing Lab (ISI) Recursion 9 / 18


Permutations

Algorithm
To generate all permutations of 1, 2, 3, . . . , n, do the following:
1. Generate all permutations of 2, 3, . . . , n, and add 1 to the beginning.
2. Generate all permutations of 1, 3, 4, . . . , n and add 2 to the beginning.
...
n. Generate all permutations of 1, 2, . . . , n − 1 and add n to the
beginning.

Computing Lab (ISI) Recursion 10 / 18


Permutations: code

void permute(int *A, int k, int n)


{
int i;

if(k==n) {
for (i = 0; i < n; i++) {
printf("%d ", A[i]);
}
putchar('\n');
return;
}

for(i = k; i < n; i++){


SWAP(A, i, k);
permute(A, k+1, n);
SWAP(A, k, i);
}

return;
}
Computing Lab (ISI) Recursion 11 / 18
Problems – I

1. Towers of Hanoi: see


https://en.wikipedia.org/wiki/Tower_of_Hanoi

CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=228623

2. Write a recursive function with prototype int C(int n, int r); to


compute the binomial coefficient using the following definition:
( ) ( ) ( )
n n−1 n−1
= +
r r r−1

Supply appropriate boundary conditions.

Computing Lab (ISI) Recursion 12 / 18


Problems – II

3. Define a function G(n) as:




 0 if n = 0



1 if n = 1
G(n) =

 2 if n = 2



G(n − 1) + G(n − 2) + G(n − 3) if n ≥ 3

Write recursive and iterative (i.e., non-recursive) functions to compute


G(n).

Computing Lab (ISI) Recursion 13 / 18


Problems – III

4. What does the following function compute?


int f ( int n )
{
int s = 0;
while (n--) s += 1 + f(n);
return s;
}
5. http://cse.iitkgp.ac.in/~abhij/course/lab/PDS/Spring15/
A4.pdf

Computing Lab (ISI) Recursion 14 / 18


Problems – IV

6. In chess, a knight can move to a cell that is horizontally 2 cells and vertically 1 cell away, or
vertically 2 cells and horizontally 1 cell away (see Fig. 6). Thus, eight moves are possible for a
knight in general.

Computing Lab (ISI) Recursion 15 / 18


Problems – V

Given an m × n chess-like board, and a knight at position (i, j) (where 0 ≤ i < m and
0 ≤ j < n), compute the probability that the knight remains on the board after a given number
(say k) of steps, assuming that, at each step, all eight moves are equally likely. Note that, once
the knight moves off the board, it can never return to the board.
Input Format
mnijk
Output Format
The probability value rounded up to 6 decimal places.
Sample Input 0
12011
Sample Output 0
0.000000
Sample Input 1
88331
Sample Output 1
1.000000
Sample Input 2
66001
Sample Output 2
0.250000
Sample Input 3

Computing Lab (ISI) Recursion 16 / 18


Problems – VI

44002
Sample Output 3
0.125000
Explanation for the above
0 1 2 3
The knight starts at the top-left corner
(0,0). After 1 move, it may either move
0 0/2/2 2
off the board (permanently), or go to the
squares marked 1 (in red / blue). After
another move from the square marked
1 1 2 with a red (respectively, blue) 1, it lands
on one of the squares marked with a red
(respectively, blue) 2, or moves off the
2 2 1 board. The knight stays on the board for
8 distinct sequences of moves (of 2
steps each). The total number of such
3 2 2/2 moves of 2 steps each is 8 × 8 = 64.

Computing Lab (ISI) Recursion 17 / 18


Problems – VII

7. Write a program that takes a positive integer n ≤ 5, and a non-negative integer k ≤ 100 as
command-line arguments, and computes the total number of cells reachable from any starting
cell within an infinite, n-dimensional grid in k steps or less. See the examples below. You are
only permitted to travel in a direction that is parallel to one of the grid lines; diagonal movement
is not permitted.
NOTE: If you are interested, you may compute a closed-form expression for the required
number, but DO NOT use the closed-form expression to compute the answer to this problem.

Starting Cell

n = 1, k = 2; number of cells reachable = 5

2 1 2

2 1 0 1 2

2 1 2

n = 2, k = 2; number of cells reachable = 13

For n = 3, your output for k = 0, 1 and 2 should be 1, 7, and 25, respectively.

Computing Lab (ISI) Recursion 18 / 18

You might also like