Lec 33
Lec 33
I Semester 2008-09
Lecture 33
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 ?
• ...
• more complicated assertions which you might have proved before coming to
IIT.
• ...
Principle of Mathematical Induction
3
How did you prove ?
• ...
• more complicated problems which you might have proved before coming to
IIT.
• ...
Principle of Mathematical Induction
4
Principle of Mathematical Induction
5
Principle of Mathematical Induction
6
Principle of Mathematical Induction
7
Problem solved in last class
1. Pattern(n, m): the set of all strings formed by n |’s and m *’s
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
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).
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.
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).
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).
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.
15
Recursive formulation of PatternS(n, m, 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 )
18
Recursive method for computing PatternS(n, m, S )
19
Proof of correctness is based on mathematical induction
20
Proof that the method PatternS(n,m,S) is correct
21
Proof that the method PatternS(n,m,S) is correct
22
Proving that P(k) is true for all k ≥0
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
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.
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.
Comb(A\{x}, L − 1, S + x)
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
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);
}
}
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);
}
}
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);
}
}
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);
}
}
33
Permutation of L characters chosen from set A
34
Extension of Permute to PermuteS
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
38
Recursive formulation of PermuteS when A is given as array
.... ?? ....
39
Recursive formulation of PermuteS when A is given as array
PermuteS(A, i + 1, L − 1, S + A[i])
40
Recursive formulation of PermuteS when A is given as array
Please make sincere attempt to understand and answer the above question
41
Recursive formulation of PermuteS when A is given as array
PermuteS(A, i + 1, L − 1, S + A[i])
after we have swapped A[i] and A[j];
42
Recursive formulation of PermuteS when A is given as array
PermuteS(A, i + 1, L − 1, S + A[i])
after we have swapped A[i] and A[j];
43
Recursive formulation of PermuteS when A is given as array
PermuteS(A, i + 1, L − 1, S + A[i])
after we have swapped A[i] and A[j];
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]);
}
}
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], ...}.
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]);
}
}
• 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], ...}.
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]);
}
}
• 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], ...}.
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]);
}
}
• 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], ...}.
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]);
}
}
• 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]);
}
}
• 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]);
}
}
• 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], ...}.
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);
}
}
• 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], ...}.
52
Nice recursive exercises
53