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

Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing

This document contains lecture notes on recursion from ESc101: Fundamentals of Computing at IIT Kanpur. It begins with announcements about upcoming quizzes and exams. It then recaps recursion principles like recursion, the tower of Hanoi problem, and using recursion to calculate powers. It introduces the concepts of mutual and double recursion with examples of checking if a number is even or odd and calculating binomial coefficients. It discusses the iterative approach to calculating binomial coefficients without repetition by storing values in a matrix.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing

This document contains lecture notes on recursion from ESc101: Fundamentals of Computing at IIT Kanpur. It begins with announcements about upcoming quizzes and exams. It then recaps recursion principles like recursion, the tower of Hanoi problem, and using recursion to calculate powers. It introduces the concepts of mutual and double recursion with examples of checking if a number is even or odd and calculating binomial coefficients. It discusses the iterative approach to calculating binomial coefficients without repetition by storing values in a matrix.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #17, September 5, 2011

Please switch off your mobile phones.

Announcements
Quiz on lab days from 5th to 9th September 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.

Mid semester exam at 7:30 AM on 14th September 2011 Mid-semester September, 2011.

Lec-17

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Recursion
Tower of Hanoi Power function Similarity with Mathematical Induction Local variables are local to each invocation of function

Lec-17

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Recursion: Principle of Induction


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.


Sh Shown th t we can solve the problem of size = 1 (base case) that l th bl f i (b ) 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

Recap: Recursion: Power (Example)


Recursive definition of power can be expressed as 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

Recap: Power function


#include <stdio.h> 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 () { i int 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

Mutual Recursion: An example


Problem: To check whether a number is odd or even. even Definitions:
Number 0 is even Number n is even if n-1 is odd Number n is odd if n-1 is even

Input: n a natural number Output: true if n is even, false otherwise.

Lec-17

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Mutual Recursion: An example


Pseudo-code Pseudo code 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

Mutual Recursion: An example


#include <stdio.h> int odd (int n) { if (n == 0) return 0; else return even ( 1) l t (n-1); } int even (int n) { if (n == 0) return 1; else return odd (n-1); } int main () { i t number; int b 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


Binomial Coefficients or C(n, r) can be computed as: C(n
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
C (n, n) = 1 ( )

Lec-17

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Double Recursion: An example


#include <stdio.h> int choose (int n, int r) { if (n == 0) return 0 ( 0; if (k == 0) return 1; if (n == k) return 1; return choose (n-1, k-1) + choose (n-1, k); } int main () { 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 (3 1) (3,
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

Recursive calls to choose


n = 5, r = 3 choose (5, 3) choose (4, 2)
choose (3, 1)
choose (2, 0) choose (2, 1) choose (1, 0) choose (1, 1)

choose (4, 3)
choose (3, 2)
choose (2, 1) choose (1, 0) (1 choose (1, 1) choose (2, 2)

choose (3, 3)

choose (3, 2)
choose (2, 1) (2 choose (1, 0) choose (1, 1) choose (2, 2)

Lec-17

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Double Recursion: An example


What is good about the algorithm
Intuitive to write program just the way you would write the formula There are no multiplications, only additions h l i li i l ddi i The way value is being computed, one would be able to compute binomial coefficients for large n and r.
If you just computed factorial of n, very soon it will exceed the size of memory area.

What is bad about the algorithm


T much repetition of work Too h titi f k This is often a problem in double recursion

Can we think of an iterative algorithm based on additions, but without repetition.


Lec-17 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

Iterative Algorithm for Binomial Coefficients


Store the binomial coefficients as a matrix 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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

You might also like