esc101-lec17
TRANSCRIPT
-
7/31/2019 esc101-lec17
1/8
1
ESc101: Fundamentals of Com utin
2011-12-Monsoon Semester
Lecture #17, September 5, 2011
Please switch off your mobile phones.
Announcements
uiz on lab da s from 5th to th Se tember in Lab at 2:00 PM.
Please ensure that you have moodle access and Computer Center
access. If you have forgotten either password, please make sure
that you get the problem sorted out before the quiz. No makeup for the quiz.
th
Lec-17
- , .
1Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
-
7/31/2019 esc101-lec17
2/8
2
Recap
Recursion
Tower of Hanoi
Power function
Similarity with Mathematical Induction
Local variables are local to each invocation of function
Lec-17 2Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
Recap: Recursion: Principle of Induction
If a statement is true for N = 1
And if the assumption that the statement is true for N = x
implies that the statement is true for N = x+1 Then the statement is true for all natural numbers.
This is exactly what we have used in recusrion.
own a we can so ve e pro em o s ze = ase case
Shown that if we can solve the problem of size N-1 then the
problem of size N can be solved (recursive case)
Hence problem can be solved for all values of N
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
3
-
7/31/2019 esc101-lec17
3/8
3
Recap: Recursion: Power (Example)
following.
power (x, n) = ?
1, if n == 0
x * square (power(x, n/2)), if n is odd
square (power(x, n/2)), if n is even
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
4
Recap: Power function
#include
int power (int x, int n)
{ int t;
if (n == 0) return 1;
t = power (x, n/2);
if (n%2 != 0) return x * t * t;
return t * t;
}
int main ()
nt n, x;
scanf (%d %d, &n, &x);
printf (power is %d\n, power(x, n));
}
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
5
-
7/31/2019 esc101-lec17
4/8
4
Mutual Recursion: An example
.
Definitions:
Number 0 is even
Number n is even ifn-1 is odd
Number n is odd ifn-1 is even
Input: n a natural number
Output: true ifn is even, false otherwise.
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
6
Mutual Recursion: An example
-
odd (n)
if n is zero, return FALSE
else return even (n-1)
even (n)
if n is zero, return TRUE
else return odd (n-1)
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
7
-
7/31/2019 esc101-lec17
5/8
5
Mutual Recursion: An example
#include
int odd (int n)
{ if (n == 0) return 0;
e se re urn even n- ;
}
int even (int n)
{ if (n == 0) return 1;
else return odd (n-1);
}
int main ()
nt number;scanf(%d, &number);
if(odd (number) == 1) printf(%d is odd\n, number);
else printf(%d is even\n, number);
}
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
8
Double Recursion: An example
,
C (n, k) = C (n-1, k-1) + C (n-1, k)
C (n, 0) = 1
C (0, k) = 0
n and k are natural numbers.
To make computation more efficient, we can see that
n, n =
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
9
-
7/31/2019 esc101-lec17
6/8
6
Double Recursion: An example
#include
int choose (int n, int r)
{ if n == 0) return 0;
if (k == 0) return 1;
if (n == k) return 1;
return choose (n-1, k-1) + choose (n-1, k);
}
{ int n, r;
scanf(%d %d, &n, &r);
printf(The answer is %d\n, choose (n, r));
}
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
10
Recursive calls to choose
The following calls are made for n = 4, r =2:
choose (4, 2)
,
choose (2, 0)
choose (2, 1)
choose (1, 0) choose (1, 1)
choose (3, 2)
choose 2, 1
choose (1, 0)
choose (1, 1)
choose (2, 2)
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
11
-
7/31/2019 esc101-lec17
7/8
-
7/31/2019 esc101-lec17
8/8
8
Iterative Algorithm for Binomial Coefficients
Initialize the matrix elements (i, 0) to be 1 for all rows (i).
Initialize all diagonal elements (i, i) to be 1.
Now compute row wise. For row i,
If j > i, the value of matrix element is 0.
If j < i, the value of matrix element is computed as
C[i, j] = C [i-1, j-1] + C [i-1, j]
Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon
14
Any Questions?
Lec-17 15Dheeraj Sanghi, CSE Dept., IIT Kanpur
ESc101, 2011-12-Monsoon