0% found this document useful (0 votes)
8 views53 pages

Lec 33

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
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)
8 views53 pages

Lec 33

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
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/ 53

ESc101 : Fundamental of Computing

I Semester 2008-09

Lecture 33

• Clarifying doubts from previous lecture


• Proving correctness of a recursive method
• More examples of recursion
• Input/Output (if time permits)
Note I have tried to simplify these slides after the lecture. I hope you will go
through these slides and attempt the exercises given in file recur exercise.pdf.
Extra class is on Sunday 10:00 AM in CS101.

1
Understanding recursion requires only two basic tools

• The control flow when a method calls another method (may be itself) :
Lecture 30

• Mathematical Induction

2
How did you prove ?

• For all natural number n,


2 2
X n (n + 1)
i3 =
4
0≤i≤n

• ...
• more complicated assertions which you might have proved before coming to
IIT.

• ...
Principle of Mathematical Induction

3
How did you prove ?

• For all natural number n,


2 2
X n (n + 1)
i3 =
4
0≤i≤n

• ...
• more complicated problems which you might have proved before coming to
IIT.

• ...
Principle of Mathematical Induction

4
Principle of Mathematical Induction

Let P(n) be a statement defined as function of integer n.

If the following assertions hold

1. P(n) is true for some n = n0 .


2. If P(i) is true for any i ≥ n0 , then P(i + 1) is also true.

We can conclude that P(n) is true for all n > n0 .

Observe the similarity between induction and recursive formulation of a problem

5
Principle of Mathematical Induction

Let P(n) be a statement defined as function of integer n.

If the following assertions hold

1. P(n) is true for some n = n0 .


2. If P(i) is true for any i ≥ n0 , then P(i + 1) is also true.

We can conclude that P(n) is true for all n > n0 .

Observe the similarity between induction and recursive formulation of a problem

6
Principle of Mathematical Induction

Let P(n) be a statement defined as function of integer n.

If the following assertions hold

1. P(n) is true for some n = n0 .


2. If P(i) is true for any i ≥ n0 , then P(i + 1) is also true.

We can conclude that P(n) is true for all n > n0 .

Observe the similarity between induction and recursive formulation of a problem

7
Problem solved in last class

Enumerate the elements of the following sets.

1. Pattern(n, m): the set of all strings formed by n |’s and m *’s

2. Comb(A, L): all combinations of length L formed from set A

3. Permute(A, L): all permutations of length L formed using characters from set
A)
Partially solved in last class

8
Steps used in solving the problems

• understand the domain of the problem and the set to be enumerated


• To express the set recursively/inductively ?

9
Doubt 1 : Why do we have to extend

• Pattern(n, m) to PatternS(n, m, S ) ?
• Comb(A, L) to CombS(A, L, S ) ?
• Permutation(A, L) to PermutationS(A, L, S ) ?
• Partition(n) to PartitionS(n, S ) ?

10
Can we express Pattern(n, m) recursively ?

• Pattern(0,0) = {“”}
• for n > 0, m > 0, Pattern(n, m) : ??
set of strings of the form : ’|’ followed by Pattern(n − 1, m).

set of strings of the form : ’*’ followed by Pattern(n, m − 1).

Not exact recursive formulation !!

11
Can we express Pattern(n, m) recursively ?

• Pattern(0,0) = {“”}
• for n > 0, m > 0, Pattern(n, m) is union of two sets :
set of strings of the form : ’|’ followed by a string of n − 1 ’|’s, and m *’s.

set of strings of the form : ’*’ followed by a string of n ’|’s, and m − 1 *’s.

Not exact recursive formulation !!

12
Can we express Pattern(n, m) recursively ?

• Pattern(0,0) = {“”}
• for n > 0, m > 0, Pattern(n, m) is union of two sets :
set of strings of the form : ’|’ followed by Pattern(n − 1, m).

set of strings of the form : ’*’ followed by Pattern(n, m − 1).

Not exact recursive formulation !!

13
Can we express Pattern(n, m) recursively ?

• Pattern(0,0) = {“”}
• for n > 0, m > 0, Pattern(n, m) is union of two sets :
set of strings of the form : ’|’ followed by Pattern(n − 1, m).

set of strings of the form : ’*’ followed by Pattern(n, m − 1).

Not exact recursive formulation !!

14
So we generalize the definition of Pattern()

Let PatternS(n, m, S ) be the set of all strings of the form S+P where P is the
string containing n |’s and m *’s.

It is easy to observe that

Pattern(n, m) = PatternS(n, m, ” ”);

PatternS(n, m, S ) can be expressed recursively quite easily

15
Recursive formulation of PatternS(n, m, S )

for n > 0, m > 0,


[
′ ′ ′ ′
PatternS(n, m, S) = PatternS(n−1, m, S+ | ) PatternS(n, m−1, S+ ∗)

16
Conslusion :

we extend

• Pattern(n, m) to PatternS(n, m, S )
• Comb(A, L) to CombS(A, L, S )
• Permutation(A, L) to PermutationS(A, L, S )
• Partition(n) to PartitionS(n, S )

... ?? ...

17
Therefore,

we extend

• Pattern(n, m) to PatternS(n, m, S )
• Comb(A, L) to CombS(A, L, S )
• Permutation(A, L) to PermutationS(A, L, S )
• Partition(n) to PartitionS(n, S )

.. to express the sets exactly in an inductive/recursive manner

18
Recursive method for computing PatternS(n, m, S )

public static long PatternS(int n, int m, String S)


{ if(n==0 && m==0)
System.out.println(S);
else
{
if(n!=0) PatternS(n-1,m,S+’|’);
if(m!=0) PatternS(n,m-1,S+’*’);
}
}
What is the guarantee that this is method is correct ?

19
Proof of correctness is based on mathematical induction

20
Proof that the method PatternS(n,m,S) is correct

What is the inductive assertion ?


P (n,m) : The method PatternS(n, m, S) for any string S prints all strings of the
form S+P where P is the string containing n |’s and m *’s.

21
Proof that the method PatternS(n,m,S) is correct

The inductive assertion is :


P (k) :
The method PatternS(n, m, S) for all nonnegative integers n, m with n + m =k
and any string S will print all strings of the form S+P where P is the string
containing n |’s and m *’s.

22
Proving that P(k) is true for all k ≥0

Base Case : It is easy to conclude that P(0) is true.

Induction step : We have to prove P(k) for k > 0 given that P(k − 1) holds.
The Proof uses the principle of mathematical induction and uses

• the description of the method PatternS(n, m, S )


• the recursive formulation of the set PatternS(n,m,S).
(We use bold letters to distinguish set from the method)

Note : The detailed proof has been provided in the file inductive proof.pdf
available on the website.

23
All combinations of length L formed by characters from set A

24
Let us first generalize Comb to CombS

S∩A=∅
Combs(A,L,S) : All strings formed by concatenating S with L characters selected
from A where order does not matter among characters.

It can be seen that

Comb(A, L) = CombS(A, L, ” ”)

25
Recursive formulation of CombS(A,L,S) : when L6=0 and |A| >L
Consider any x ∈ A.
CombS(A,L,S) consists of two disjoint groups.

• Those combinations in which x is present.

Comb(A\{x}, L − 1, S + x)

• Those combinations in which x is not present.

Comb(A\{x}, L, S)

26
Complete recursive formulation of CombS(A,L,S)

Let x ∈ A.
CombS(A,L,S) =




 S if L =0







 CombS(A\{x}, L − 1, S +′ x′ )

if L > 0 and |A| = L.
=




′ ′

CombS(A\{x}, L − 1, S + x )
 S




 CombS(A\{x}, L, S ) if L > 0 and |A| > L.

27
Complete recursive formulation of CombS(A,L,S) with A as array

CombS(A, i, L, S ) = All strings formed by concatenating S with L characters


selected from {A[i], A[i + 1], ...} where order does not matter among
characters.

Recall in the above definition, we assume that S does not have any character
from {A[i], A[i + 1], ...}.

28
Complete recursive formulation of CombS(A,L,S) with A as array

CombS(A, i, L, S ) =




 S if L =0







 CombS(A, i + 1, L − 1, S + A[i])

if L > 0 and A.length − i = L.
=





CombS(A, i + 1, L − 1, S + A[i])
 S




 CombS(A, i + 1, L, S ) if L > 0 and A.length − i > L.

29
Recursive method for CombS(A,L,S)
public static void CombS(char[] A, int i, int L, String S)
{ int current_size_of_A= A.length-i;
if(L==0) System.out.println(S);
else
{
CombS(A,i+1,L-1,S+A[i]);
if(current_size_of_A>L)
CombS(A,i+1,L,S);
}
}

What is proof of correctness ?


for all Strings S, CombS(A, i, L, S) prints All strings formed by concatenating S
with L characters selected from {A[i], A[i + 1], ...} where order does not
matter among characters.

30
Recursive method for CombS(A,L,S)
public static void CombS(char[] A, int i, int L, String S)
{ int current_size_of_A= A.length-i;
if(L==0) System.out.println(S);
else
{
CombS(A,i+1,L-1,S+A[i]);
if(current_size_of_A>L)
CombS(A,i+1,L,S);
}
}

What is proof of correctness ?


for all Strings S, CombS(A, i, L, S) prints All strings formed by concatenating S
with L characters selected from {A[i], A[i + 1], ...} where order does not
matter among characters.

31
Recursive method for CombS(A,L,S)
public static void CombS(char[] A, int i, int L, String S)
{ int current_size_of_A= A.length-i;
if(L==0) System.out.println(S);
else
{
CombS(A,i+1,L-1,S+A[i]);
if(current_size_of_A>L)
CombS(A,i+1,L,S);
}
}

What is inductive Assertion : P (k ) ?


For all arrays A, integer i, L such that A.length − i + L = k , and any string S
CombS(A, i, L, S) prints All strings formed by concatenating S with L characters
selected from {A[i], A[i + 1], ...} where order does not matter among
characters.

32
Recursive method for CombS(A,L,S)
public static void CombS(char[] A, int i, int L, String S)
{ int current_size_of_A= A.length-i;
if(L==0) System.out.println(S);
else
{
CombS(A,i+1,L-1,S+A[i]);
if(current_size_of_A>L)
CombS(A,i+1,L,S);
}
}

P (k ) : (try to prove similar to the proof of PatternS(n,m,S))


For all arrays A, nonnegative integers i, L such that A.length − i + L = k ,
and any string S , the method CombS(A, i, L, S) prints All strings formed by
concatenating S with L characters selected from {A[i], A[i + 1], ...} where
order does not matter among characters.

33
Permutation of L characters chosen from set A

Domain : L is non-negative integer, A is a set of character with |A| ≥ L.

Permute(A, L) : the set of all strings of length L whose characters belong to A.

Aim : To design a program to compute Permute(A, L)

34
Extension of Permute to PermuteS

PermuteS(A, L, S ) : the set of all strings with S as prefix and followed by a


permutation of L characters from A.

It can be seen that

Permute(A, L) = PermuteS(A, L, “”)

35
Recursive formulation of PermuteS(A,L,S)


 S

 if L =0
PermuteS(A, L, S) =


x∈A PermuteS(A\{x}, L − 1, S +′ x′ )
 S
if L >0

36
Recursive formulation of PermuteS(A,L,S)


 S

 if L =0
PermuteS(A, L, S) =


x∈A PermuteS(A\{x}, L − 1, S +′ x′ )
 S
if L >0

37
Recursive formulation of PermuteS when A is given as array

PermuteS(A, i, L, S ) : the set of all strings with S as prefix and followed by a


permutation of L characters from {A[i], A[i + 1], ...}.

38
Recursive formulation of PermuteS when A is given as array

How to express subset of those strings from PermuteS(A, i, L, S ) which begin


with A[i] ?

.... ?? ....

39
Recursive formulation of PermuteS when A is given as array

How to express subset of those strings from PermuteS(A, i, L, S ) which begin


with A[i] ?

PermuteS(A, i + 1, L − 1, S + A[i])

40
Recursive formulation of PermuteS when A is given as array

How to express subset of those strings from PermuteS(A, i, L, S ) which begin


with A[j], j >i?
.... ?? ....
.... ?? ....

Please make sincere attempt to understand and answer the above question

41
Recursive formulation of PermuteS when A is given as array

How to express subset of those strings from PermuteS(A, i, L, S ) which begin


with A[j], j >i?

PermuteS(A, i + 1, L − 1, S + A[i])
after we have swapped A[i] and A[j];

Mathematically it is correct, but we have to be cautious during implementation


because ...
here we involve changing the contents of array A.

42
Recursive formulation of PermuteS when A is given as array

How to express subset of those strings from PermuteS(A, i, L, S ) which begin


with A[j], j >i?

PermuteS(A, i + 1, L − 1, S + A[i])
after we have swapped A[i] and A[j];

Mathematically it is correct, but we have to be cautious during implementation


because ...
here we involve changing the contents of array A.

43
Recursive formulation of PermuteS when A is given as array

How to express subset of those strings from PermuteS(A, i, L, S ) which begin


with A[j], j >i?

PermuteS(A, i + 1, L − 1, S + A[i])
after we have swapped A[i] and A[j];

Mathematically it is correct, but we have to be cautious during implementation


because ...
here we are changing the contents of array A.

44
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(A, i, L) :

For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

The state of Array A is same before and after PermuteS(A, i, L, S ).

45
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(k) :

• For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

• The state of Array A is same before and after PermuteS(A, i, L, S ).

46
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(k) :

• For any arrays A, nonnegative integers i, L with A.length − i + L = k and any string S,
the method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of
L characters chosen from {A[i], A[i + 1], ...}.

The assertion is not true for the above method PermuteS(A, i, L, S ).

47
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(k) :

• For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

Reason : since contents of array changes in recursive calls

48
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(k) :

• For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

Try execution on : : A=[a,b,c],i=0 and L = 3. It does not print any string starting with b

49
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(k) :

• For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

Idea : change the above method and augment the assertion accordingly.

50
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);

}
}

Inducive Assertion : P(k) :

• For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

• The state of Array A is same before and after PermuteS(A, i, L, S ).

51
The recursive method for enumerating PermuteS(A,i,L,S)
public static void PermuteS(char[] A, int i, int L, String S)
{ if(L==0) System.out.println(S);
else
for(int j = i; j<A.length; j = j+1)
{
swap(A,i,j);
PermuteS(A,i+1,L-1,S+A[i]);
swap(A,i,j);
}
}

Inducive Assertion : P(k) :

• For any array A, nonnegative integers i, L with A.length − i + L = k and any string S, the
method PermuteS(A, i, L, S ) prints all strings of the form S+P where P is a permutation of L
characters chosen from {A[i], A[i + 1], ...}.

• The state of Array A is same before and after PermuteS(A, i, L, S ).

52
Nice recursive exercises

Available in file recur exercise.pdf on the course webpage.

53

You might also like