Recursion: Data and File Structures Laboratory
Recursion: Data and File Structures Laboratory
http://www.isical.ac.in/~dfslab/2018/index.html
1 void main(void)
2 { ...
3 u = f(x, y*z);
4 ...
5 }
6
7 int f(int a, int b)
8 { ...
9 if (a > 0)
10 p = g(b);
11 else
12 p = h(b / 2);
13 return p;
14 }
15
16 int g(int m)
17 { ... }
18
19 int h(int n)
20 { ... }
DFS Lab (ISI) Recursion 2 / 20
Function calls
1 void main(void)
2 { ...
3 u = f(x, y*z); 1. Let a = x, b = y*z.
4 ...
5 } 2. Execute the statements in f().
6 (a) If a is positive, let m = b.
7 int f(int a, int b) Execute the statements in g(), and
8 { ...
store the obtained value in p.
9 if (a > 0)
10 p = g(b); (b) Otherwise, let n = b/2.
11 else Execute the statements in h(), and
12 p = h(b / 2); store the obtained value in p.
13 return p;
(c) In either case, return value of p to
14 }
15
calling function.
16 int g(int m)
17 { ... }
3. Store the value returned by f in u.
18 4. Continue from line 4.
19 int h(int n)
20 { ... }
DFS Lab (ISI) Recursion 2 / 20
Terminology
Caller
Callee
Formal parameters (or simply parameters)
Example: a, b – formal parameters for f
Actual parameters (or actuals / arguments)
Example: x, y*z – arguments passed to f on line 3
For arrays
To “simulate” call by reference
For efficiency
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.
.
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).
(N − 1, N − 1)
/* Recurse */
/* upper-left sub-square */
z_curve(top_left_row,
top_left_column,
(top_left_row + bottom_right_row)/2,
(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 */
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;
}
.
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.
if(k==n) {
for (i = 0; i < n; i++) {
printf("%d ", A[i]);
}
putchar(’\n’);
return;
}
return;
}
DFS Lab (ISI) Recursion 15 / 20
Review question – slide I
1 typedef struct {
2 char name[64];
3 int roll, rank;
4 float percent;
5 } STUDENT;
6
7 STUDENT *read_data1(void)
8 { STUDENT s;
9 scanf("%s %d %d %f",
10 &(s.name[0]), &(s.roll),
11 &(s.rank), &(s.percent));
12 return &s;
13 }
14
15 STUDENT read_data2(void)
16 { STUDENT s;
17 scanf("%s %d %d %f",
18 &(s.name[0]), &(s.roll),
19 &(s.rank), &(s.percent));
20 return s;
21 }
22