KEMBAR78
04-Induction and Recursion.ppt ppt bai tap | PPT
Chapter 4
Induction and Recursion
Quy nạp và Đệ quy
Objectives
Mathematical Induction
Strong Induction and Well-Ordering
Recursive Definitions and Structural
Induction
Recursive Algorithms
Program Correctness
4.1- Mathematical Induction
Introduction
Mathematical Induction
Examples of Proofs by Mathematical
Induction
Principle of
Mathematical
Induction
Principle of Mathematical Induction
To prove P(n) is true for all possible
integers n, where P(n) is a propositional
function, we complete two step:
Basic step:
Verifying P(1) is true
Inductive step:
Show P(k) → P(k+1) is true for all k>0
Induction: Example 1
Prove that 1 + 2 + 3+ …+ n = n(n+1)/2 for all integers n>0
Solution.
Let P(n)=“ 1+2+3+…+ n = n(n+1)/2”.
 Basic step: P(1) = “1 = 1(1+1)/2”  true
 Inductive step: With arbitrary k>0,
P(k) =“ 1+ 2+…+ k = k(k+1)/2” is true.
We have
1+2+3+…+k+(k+1)= k(k+1)/2 + (k+1)
= [k(k+1)+ 2(k+1)]/2
= (k+1)(k+2)/2
= (k+1)((k+1)+1)/2
P(k+1)=“1+2+3+…+k+1 = (k+1)(k+2)/2” is true.
P(k)  P(k+1): true
Proved.
Example 2 p.268
 Conjecture a formula for the sum of the first n positive odd
integers. Then prove your conjecture using mathematical
induction.
 Solution.
The sum of the first n positive odd integers for n=1, 2, 3, 4, 5 are:
1=1, 1+3=4, 1+3+5=9,
1+3+5+7=16, 1+3+5+7+9=25.
 Conjecture: 1+3+5+…+(2n-1)=n2
.
 Proof. Let P(n)=“1+3+5+…+(2n-1)=n2
.”
- Basic step. P(1)=“1=1” is true.
- Inductive step. (P(k) P(k+1)) is true.
Suppose P(k) is true. That is, “1+3+5+…+(2k-1)=k2
”
We have, 1+3+5+…+(2k-1)+(2k+1)=k2
+2k+1 = (k+1)2
.
So, P(k+1) is true.
Proved.
Induction: Examples 2..13 – pages:
268..278
 1+3+5+…+ (2n-1) = n2
 20
+21
+22
+23
+…+2n
= ∑ 2n
= 2n+1
-1
 ∑arj
= a + ar + ar2
+ … + arn
= (arn+1
-a)/(r-1)
 n < 2n
 2n
< n! , n>3
 n3
-n is divisible by 3, n is positive integer
 The number of subsets of a finite set: a set with n elements
has 2n
subsets.
 ….
 Let H(j)= 1/1 + + 1/3 + … + 1/j
½
Prove that H(2n
)  1 + n/2 for all n  0
4.2- Strong Induction and Well-Ordering
Principle of Strong Induction
To prove P(n) is true for all positive integers n,
where P(n) is a propositional function, two steps
are performed:
Basic step:
Verifying P(1) is true
Inductive step:
Show [P(1) ^ P(2) ^ … ^ P(k)] → P(k+1) is true
for all k>0
Strong Induction: Example 1
Prove that if n is an integer greater than 1, then n
can be written as the product of primes
P(n) : n can be written as the product of primes
Basic steps: P(2) = true // 2=2 , product of 1 primes
P(4) = true // 4=2.2
Inductive step:
Assumption: P(j)=true for all positive j ≤ k
- Case k+1 is a prime  P(k+1) =true
- Case k+1 is a composite  k+1= ab, 2 ≤ a ≤ b<k+1
 P(k) is true
Strong Induction: Example 2
Prove that every amount of postage of 12 cents
or more can be formed using just 4-cents and 5-
cents stamps
P(n) : “ n cents can be formed using just 4-cent and 5-cent stamps”
P(12) is true : 12 cents = 3. 4 cents
P(13) is true : 13= 2.4 + 1.5
P(14) is true: 14= 1.4 + 2.5
P(15) is true: 15= 3.5
Assumption: P(j) is true with 12 ≤j ≤ k, k>15  P(k-3) is true
k+1= (k-3) + 4 , k>12
 P(k+1) is true because k+1 is the result of adding a 4-cent stamp to the
amount k-3
 Proved
Using Strong Induction in Computational
Geometry
 Definitions
Strong Induction in Computational Geometry
Lemma 1: Every simple polygon has an interior
diagonal.
Proof: page 290
Strong Induction in Computational Geometry
Theorem 1:
A simple polygon with n sides, where n is integer
with n ≥ 3, can be triangulated into n-2 triangles
Proof: page 289
4.3- Recursive Definition and
Structural Induction
 Introduction
 Recursively Defined Functions
 Recursively Defined Sets and Structures
 Structural Induction
 Generalized Induction
 Recursive Algorithms
Recursion: Introduction
 Objects/ functions may be difficultly defined.
 Define an object/function in terms of itself
 Examples:





 
0
,
2
.
2
0
,
1
2 1
n
n
n
n










 


0
,
0
,
0
1
0
0
n
i
n
n
i n
i
n
i
Recursively Defined Functions
 Recursive ( inductive) function
Two steps to define a function with the set of
nonnegative integers as its domain:
 Basis step: Specify the value of the function at zero.
 Recursive step: Give a rule for finding its value at
an integer from its values at smaller integers
 Example: Find f(1), f(2), f(4),f(6) of the following
function:








3
),
1
(
3
3
,
)
(
n
n
f
n
n
n
n
f
Recursively Defined Functions
 Example: Give the recursive definition of ∑ai , i=0..k
 Basis step: ∑ai =a0, i=0
 Inductive step:
a0 + a1 + … + ak-1 + ak
( ∑ai , i=0..k-1)
∑ai =ak + ( ∑ai , i=0..k-1)
Recursively Defined Functions
Definition 1: Fibonacci numbers









1
),
2
(
)
1
(
1
,
0
,
1
)
(
n
n
f
n
f
n
n
f
Theorem 1: Lamé’s theorem: Let a,b be integers, a ≥ b.
Then the number of divisions used by the Euclidean
algorithm to find gcd(a,b) is less than or equal to five
times the number of decimal digits in b.
Proof: page 298
Example gcd(25,7), b= 7 , 1 digit
x y r
25 7 25 mod 7=4
7 4 7 mod 4=3
4 3 4 mod 3=1
3 1 3 mod 1=0 ( 4 divisions)
1 0 Stop
procedure gcd(a,b)
x:=a; y:=b
while y  0
begin
r := x mod y
x:=y
y:= r
end { gcd(a,b) is x}
Recursively Defined Sets and Structures
 Example S= { 3,6,9,12,15, 18,21,…}
Step 1: 3S
Step 2: If xS and yS then x+yS
 Definition 2: The set ∑* of string over alphabet ∑
can be defined recursively by:
Basis step: λ  ∑*, λ is the empty string with no
symbols
Recursive step: If w ∑* and x ∑ then wx ∑*
Example: ∑ ={0,1}  ∑* is the set of string made by 0 and 1 with
arbitrary length and arbitrary order of symbols 0 and 1
Recursively Defined Sets and Structures
 Definition 3: String Concatenation
Basis step: If w  ∑* then w.λ=w, λ is the empty string
Recursive step: If w1 ∑* and w2 ∑* and x ∑
then w1.(w2x) =(w1.w2)x
Example: ∑ ={0,1}  ∑* is the set of string made by 0 and
1 with arbitrary length and arbitrary order of symbols 0
and 1
4.4- Recursive Algorithms
 Definition 1: An algorithm is called recursive if it
solves a problem by reducing it to an instance of
the same problem with smaller input.
Example: Recursive algorithm for computing n!
n!= 1 , n=0
n!= 1.2.3.4…n = n.(n-1)!, n>0
procedure factorial (n: nonnegative integer)
if n=0 then factorial(n) :=1
else factorial(n) = n.factorial(n-1)
Recursive Algorithms…
Example: Recursive algorithm for computing an
an
= 1 , n=0
an
= a.a.a…a = a.an-1
, n>0
procedure power (a: nonzero real number
n: nonnegative integer)
if n=0 then power(a,n) :=1
else power(a,n)=a.power(a,n-1)
Recursive Algorithms…
Example: Recursive algorithm for computing bn
mod m m ≥ 2, n
≥ 0, 1 ≤ b < m.
bn
mod m = (b.(bn-1
mod m) mod m
b0
mod m = 1
Using division to improve performance: ( n steps backward to 0 faster)
If n is even  bn
= bn/2
.bn/2
 bn
mod m = ((bn/2
mod m). (bn/2
mod m)) mod m
 bn
mod m = (bn/2
mod m)2
mod m
If n is odd bn
= b.b n/2
.bn/2
 bn
mod m = ([(bn/2
mod m)2
mod m].(b mod m))mod m
Algorithm: page 313
Recursive Algorithms…
Example: Recursive algorithm for computing gcd(a,b) a,b: non
negative integer, a < b
If a>b then swap a,b
gcd(a,b)=b , a=0
gcd (a,b) = gcd(b mod a, a)
Algorithm: page 313
Recursive Algorithms…
Example: Recursive algorithm for linear
searching the value x in the sequence
ai, ai+1,…, aj, sub-sequence of an.
1≤ i ≤ n, 1 ≤ j ≤ n
i>j  location =0
ai=x  location = i
location (i, j, x) = location ( i+1, j, x)
Algorithm: page 314 – You should modify it.
Recursive Algorithms…
Example: Recursive algorithm for binary searching
the value x in the increasingly ordered sequence ai,
ai+1,…, aj-1, sub-sequence of an. 1≤ i ≤ n, 1 ≤ j ≤ n
1≤ i ≤ n, 1 ≤ j ≤ n
procedure binary-search(x, i, j)
if i>j then location=0
m= (i+j)/2
if x= am then location =m
else if x< am then location= binary-search(x, i, m-1)
else location= binary-search(x, m+1, j)
Algorithm: page 314 – You should modify it.
Proving Recursive Algorithms Correct
 Using mathematical induction.
 Example: prove the algorithm that computes n! is correct.
procedure f (n: nonnegative integer)
if n=0 then f(n) :=1
else f(n) = n.f(n-1)
If n=0, first step of the algorithm tells us
f(0)=1  true
Assuming f(n) is true for all n≥0
f( n)= 1.2.3….(n)
(n+1).f(n)= 1.2.3…n.(n+1) = (n+1)!
f(n+1) = (n+1)!
Conclusion: f(n) is true for all integer n,
n≥0
More examples: Page 315
Recursion and Iteration
procedure rfibo (n: nonnegative integer)
If n=0 then rFibo(0)=0
Else if n=1 then rFibo(1)=1
Else rFibo(n) := rFibo(n-2) + rFibo(n-1)
procedure iFibo (n: nonnegative integer)
If n=0 then y:=0
Else if n=1 then y:=1
Else Begin
x:=0 ; y:=1
for i:= 2 to n
Begin
z:= x+y; x:= y; y:=z
End
End { iFibo(n) = z}
Recursive algorithm uses far
more computation than
iterative one
Merge
Sort
Merge?
Procedure mergeSort (
L=a1…an)
If n>1 then
Begin
m:= n/2
L1 := a1…am
L2 := am+1…an
L:=merge(mergeSort(L1),mergeSort(L2))
End
{L is sorted}
Merge Sort
 Merge two sorted lists L1, L2 to list L, an
increasing ordered list.
procedure Merge (L1, L2: sorted list)
L:= empty list
While L1 and L2 are both no empty
Begin
remove smaller of first element of L1 and L2
and put it to the right end of L
if removal of this element makes one list empty
then remove all elements from the other list and
append them to L
End { L has increasing order }
Theorem 1: The number of
comparisons needed to merge sort
a list with n elements is O(nlog n)
L1: 1 2 2 5 7 9 12 15 17 19 L2: 3 5 8 9 11 15
L : 1 2 2 3 5 5 7 8 9 9 11 12 15 15 17 19
≤
Thanks

04-Induction and Recursion.ppt ppt bai tap

  • 1.
    Chapter 4 Induction andRecursion Quy nạp và Đệ quy
  • 2.
    Objectives Mathematical Induction Strong Inductionand Well-Ordering Recursive Definitions and Structural Induction Recursive Algorithms Program Correctness
  • 3.
    4.1- Mathematical Induction Introduction MathematicalInduction Examples of Proofs by Mathematical Induction
  • 4.
    Principle of Mathematical Induction Principle ofMathematical Induction To prove P(n) is true for all possible integers n, where P(n) is a propositional function, we complete two step: Basic step: Verifying P(1) is true Inductive step: Show P(k) → P(k+1) is true for all k>0
  • 5.
    Induction: Example 1 Provethat 1 + 2 + 3+ …+ n = n(n+1)/2 for all integers n>0 Solution. Let P(n)=“ 1+2+3+…+ n = n(n+1)/2”.  Basic step: P(1) = “1 = 1(1+1)/2”  true  Inductive step: With arbitrary k>0, P(k) =“ 1+ 2+…+ k = k(k+1)/2” is true. We have 1+2+3+…+k+(k+1)= k(k+1)/2 + (k+1) = [k(k+1)+ 2(k+1)]/2 = (k+1)(k+2)/2 = (k+1)((k+1)+1)/2 P(k+1)=“1+2+3+…+k+1 = (k+1)(k+2)/2” is true. P(k)  P(k+1): true Proved.
  • 6.
    Example 2 p.268 Conjecture a formula for the sum of the first n positive odd integers. Then prove your conjecture using mathematical induction.  Solution. The sum of the first n positive odd integers for n=1, 2, 3, 4, 5 are: 1=1, 1+3=4, 1+3+5=9, 1+3+5+7=16, 1+3+5+7+9=25.  Conjecture: 1+3+5+…+(2n-1)=n2 .  Proof. Let P(n)=“1+3+5+…+(2n-1)=n2 .” - Basic step. P(1)=“1=1” is true. - Inductive step. (P(k) P(k+1)) is true. Suppose P(k) is true. That is, “1+3+5+…+(2k-1)=k2 ” We have, 1+3+5+…+(2k-1)+(2k+1)=k2 +2k+1 = (k+1)2 . So, P(k+1) is true. Proved.
  • 7.
    Induction: Examples 2..13– pages: 268..278  1+3+5+…+ (2n-1) = n2  20 +21 +22 +23 +…+2n = ∑ 2n = 2n+1 -1  ∑arj = a + ar + ar2 + … + arn = (arn+1 -a)/(r-1)  n < 2n  2n < n! , n>3  n3 -n is divisible by 3, n is positive integer  The number of subsets of a finite set: a set with n elements has 2n subsets.  ….  Let H(j)= 1/1 + + 1/3 + … + 1/j ½ Prove that H(2n )  1 + n/2 for all n  0
  • 8.
    4.2- Strong Inductionand Well-Ordering Principle of Strong Induction To prove P(n) is true for all positive integers n, where P(n) is a propositional function, two steps are performed: Basic step: Verifying P(1) is true Inductive step: Show [P(1) ^ P(2) ^ … ^ P(k)] → P(k+1) is true for all k>0
  • 9.
    Strong Induction: Example1 Prove that if n is an integer greater than 1, then n can be written as the product of primes P(n) : n can be written as the product of primes Basic steps: P(2) = true // 2=2 , product of 1 primes P(4) = true // 4=2.2 Inductive step: Assumption: P(j)=true for all positive j ≤ k - Case k+1 is a prime  P(k+1) =true - Case k+1 is a composite  k+1= ab, 2 ≤ a ≤ b<k+1  P(k) is true
  • 10.
    Strong Induction: Example2 Prove that every amount of postage of 12 cents or more can be formed using just 4-cents and 5- cents stamps P(n) : “ n cents can be formed using just 4-cent and 5-cent stamps” P(12) is true : 12 cents = 3. 4 cents P(13) is true : 13= 2.4 + 1.5 P(14) is true: 14= 1.4 + 2.5 P(15) is true: 15= 3.5 Assumption: P(j) is true with 12 ≤j ≤ k, k>15  P(k-3) is true k+1= (k-3) + 4 , k>12  P(k+1) is true because k+1 is the result of adding a 4-cent stamp to the amount k-3  Proved
  • 11.
    Using Strong Inductionin Computational Geometry  Definitions
  • 12.
    Strong Induction inComputational Geometry Lemma 1: Every simple polygon has an interior diagonal. Proof: page 290
  • 13.
    Strong Induction inComputational Geometry Theorem 1: A simple polygon with n sides, where n is integer with n ≥ 3, can be triangulated into n-2 triangles Proof: page 289
  • 14.
    4.3- Recursive Definitionand Structural Induction  Introduction  Recursively Defined Functions  Recursively Defined Sets and Structures  Structural Induction  Generalized Induction  Recursive Algorithms
  • 15.
    Recursion: Introduction  Objects/functions may be difficultly defined.  Define an object/function in terms of itself  Examples:        0 , 2 . 2 0 , 1 2 1 n n n n               0 , 0 , 0 1 0 0 n i n n i n i n i
  • 16.
    Recursively Defined Functions Recursive ( inductive) function Two steps to define a function with the set of nonnegative integers as its domain:  Basis step: Specify the value of the function at zero.  Recursive step: Give a rule for finding its value at an integer from its values at smaller integers  Example: Find f(1), f(2), f(4),f(6) of the following function:         3 ), 1 ( 3 3 , ) ( n n f n n n n f
  • 17.
    Recursively Defined Functions Example: Give the recursive definition of ∑ai , i=0..k  Basis step: ∑ai =a0, i=0  Inductive step: a0 + a1 + … + ak-1 + ak ( ∑ai , i=0..k-1) ∑ai =ak + ( ∑ai , i=0..k-1)
  • 18.
    Recursively Defined Functions Definition1: Fibonacci numbers          1 ), 2 ( ) 1 ( 1 , 0 , 1 ) ( n n f n f n n f Theorem 1: Lamé’s theorem: Let a,b be integers, a ≥ b. Then the number of divisions used by the Euclidean algorithm to find gcd(a,b) is less than or equal to five times the number of decimal digits in b. Proof: page 298 Example gcd(25,7), b= 7 , 1 digit x y r 25 7 25 mod 7=4 7 4 7 mod 4=3 4 3 4 mod 3=1 3 1 3 mod 1=0 ( 4 divisions) 1 0 Stop procedure gcd(a,b) x:=a; y:=b while y  0 begin r := x mod y x:=y y:= r end { gcd(a,b) is x}
  • 19.
    Recursively Defined Setsand Structures  Example S= { 3,6,9,12,15, 18,21,…} Step 1: 3S Step 2: If xS and yS then x+yS  Definition 2: The set ∑* of string over alphabet ∑ can be defined recursively by: Basis step: λ  ∑*, λ is the empty string with no symbols Recursive step: If w ∑* and x ∑ then wx ∑* Example: ∑ ={0,1}  ∑* is the set of string made by 0 and 1 with arbitrary length and arbitrary order of symbols 0 and 1
  • 20.
    Recursively Defined Setsand Structures  Definition 3: String Concatenation Basis step: If w  ∑* then w.λ=w, λ is the empty string Recursive step: If w1 ∑* and w2 ∑* and x ∑ then w1.(w2x) =(w1.w2)x Example: ∑ ={0,1}  ∑* is the set of string made by 0 and 1 with arbitrary length and arbitrary order of symbols 0 and 1
  • 21.
    4.4- Recursive Algorithms Definition 1: An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example: Recursive algorithm for computing n! n!= 1 , n=0 n!= 1.2.3.4…n = n.(n-1)!, n>0 procedure factorial (n: nonnegative integer) if n=0 then factorial(n) :=1 else factorial(n) = n.factorial(n-1)
  • 22.
    Recursive Algorithms… Example: Recursivealgorithm for computing an an = 1 , n=0 an = a.a.a…a = a.an-1 , n>0 procedure power (a: nonzero real number n: nonnegative integer) if n=0 then power(a,n) :=1 else power(a,n)=a.power(a,n-1)
  • 23.
    Recursive Algorithms… Example: Recursivealgorithm for computing bn mod m m ≥ 2, n ≥ 0, 1 ≤ b < m. bn mod m = (b.(bn-1 mod m) mod m b0 mod m = 1 Using division to improve performance: ( n steps backward to 0 faster) If n is even  bn = bn/2 .bn/2  bn mod m = ((bn/2 mod m). (bn/2 mod m)) mod m  bn mod m = (bn/2 mod m)2 mod m If n is odd bn = b.b n/2 .bn/2  bn mod m = ([(bn/2 mod m)2 mod m].(b mod m))mod m Algorithm: page 313
  • 24.
    Recursive Algorithms… Example: Recursivealgorithm for computing gcd(a,b) a,b: non negative integer, a < b If a>b then swap a,b gcd(a,b)=b , a=0 gcd (a,b) = gcd(b mod a, a) Algorithm: page 313
  • 25.
    Recursive Algorithms… Example: Recursivealgorithm for linear searching the value x in the sequence ai, ai+1,…, aj, sub-sequence of an. 1≤ i ≤ n, 1 ≤ j ≤ n i>j  location =0 ai=x  location = i location (i, j, x) = location ( i+1, j, x) Algorithm: page 314 – You should modify it.
  • 26.
    Recursive Algorithms… Example: Recursivealgorithm for binary searching the value x in the increasingly ordered sequence ai, ai+1,…, aj-1, sub-sequence of an. 1≤ i ≤ n, 1 ≤ j ≤ n 1≤ i ≤ n, 1 ≤ j ≤ n procedure binary-search(x, i, j) if i>j then location=0 m= (i+j)/2 if x= am then location =m else if x< am then location= binary-search(x, i, m-1) else location= binary-search(x, m+1, j) Algorithm: page 314 – You should modify it.
  • 27.
    Proving Recursive AlgorithmsCorrect  Using mathematical induction.  Example: prove the algorithm that computes n! is correct. procedure f (n: nonnegative integer) if n=0 then f(n) :=1 else f(n) = n.f(n-1) If n=0, first step of the algorithm tells us f(0)=1  true Assuming f(n) is true for all n≥0 f( n)= 1.2.3….(n) (n+1).f(n)= 1.2.3…n.(n+1) = (n+1)! f(n+1) = (n+1)! Conclusion: f(n) is true for all integer n, n≥0 More examples: Page 315
  • 28.
    Recursion and Iteration procedurerfibo (n: nonnegative integer) If n=0 then rFibo(0)=0 Else if n=1 then rFibo(1)=1 Else rFibo(n) := rFibo(n-2) + rFibo(n-1) procedure iFibo (n: nonnegative integer) If n=0 then y:=0 Else if n=1 then y:=1 Else Begin x:=0 ; y:=1 for i:= 2 to n Begin z:= x+y; x:= y; y:=z End End { iFibo(n) = z} Recursive algorithm uses far more computation than iterative one
  • 29.
    Merge Sort Merge? Procedure mergeSort ( L=a1…an) Ifn>1 then Begin m:= n/2 L1 := a1…am L2 := am+1…an L:=merge(mergeSort(L1),mergeSort(L2)) End {L is sorted}
  • 30.
    Merge Sort  Mergetwo sorted lists L1, L2 to list L, an increasing ordered list. procedure Merge (L1, L2: sorted list) L:= empty list While L1 and L2 are both no empty Begin remove smaller of first element of L1 and L2 and put it to the right end of L if removal of this element makes one list empty then remove all elements from the other list and append them to L End { L has increasing order } Theorem 1: The number of comparisons needed to merge sort a list with n elements is O(nlog n) L1: 1 2 2 5 7 9 12 15 17 19 L2: 3 5 8 9 11 15 L : 1 2 2 3 5 5 7 8 9 9 11 12 15 15 17 19 ≤
  • 31.