Induction and Recursion CSC2259 Discrete Structures Konstantin Busch
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU 1
Induction is a very useful proof technique In computer science, induction is used to prove properties of algorithms Induction and recursion are closely related • Recursion is a description method for algorithms • Induction is a proof method suitable for recursive algorithms Konstantin Busch - LSU 2
Use induction to prove that a proposition is true: Inductive Basis: Prove that is true Inductive Hypothesis: Assume is true Inductive Step: Prove that is true (for any positive integer k) Konstantin Busch - LSU 3
Inductive Hypothesis: Assume is true (for any positive integer k) Inductive Step: Prove that is true In other words in inductive step we prove: for every positive integer k Konstantin Busch - LSU 4
Inductive basis Inductive Step True Proposition true for all positive integers Konstantin Busch - LSU 5
Induction as a rule of inference: Konstantin Busch - LSU 6
Theorem: Proof: Inductive Basis: Inductive Hypothesis: assume that it holds Inductive Step: We will prove K. Busch - LSU 7
Inductive Step: (inductive hypothesis) End of Proof Konstantin Busch - LSU 8
Harmonic numbers Example: Konstantin Busch - LSU 9
Theorem: Proof: Inductive Basis: Konstantin Busch - LSU 10
Inductive Hypothesis: Suppose it holds: Inductive Step: We will show: Konstantin Busch - LSU 11
from inductive hypothesis End of Proof Konstantin Busch - LSU 12
Theorem: Proof: Inductive Basis: Konstantin Busch - LSU 13
Inductive Hypothesis: Suppose it holds: Inductive Step: We will show: Konstantin Busch - LSU 14
from inductive hypothesis End of Proof Konstantin Busch - LSU 15
We have shown: It holds that: Konstantin Busch - LSU 16
Triominos hole Konstantin Busch - LSU hole 17
Theorem: Proof: Every checkerboard with one square removed can be tiled with triominoes Inductive Basis: hole Konstantin Busch - LSU 18
Inductive Hypothesis: Assume that a checkerboard can be tiled with the hole anywhere Hole can be anywhere Konstantin Busch - LSU 19
Inductive Step: Konstantin Busch - LSU 20
By inductive hypothesis with a hole can be tiled squares add three artificial holes Konstantin Busch - LSU 21
23 x 23 case: Konstantin Busch - LSU 22
Replace three holes with a triomino Now, the whole area can be tiled Konstantin Busch - LSU 23
23 x 23 case: End of Proof Konstantin Busch - LSU 24
Strong Induction To prove : Inductive Basis: Prove that is true Inductive Hypothesis: Assume is true Inductive Step: Prove that Konstantin Busch - LSU is true 25
Theorem: Every integer is a product of primes (at least one prime in the product) Proof: (Strong Induction) Inductive Basis: Number 2 is a prime Inductive Hypothesis: Suppose that every integer between and is a product of primes Konstantin Busch - LSU 26
Inductive Step: If is prime then the proof is finished If is not a prime then it is composite: Konstantin Busch - LSU 27
By the inductive hypothesis: primes End of Proof Konstantin Busch - LSU 28
Theorem: Every postage amount can be generated by using 4 -cent and 5 -cent stamps Proof: (Strong Induction) Inductive Basis: We examine four cases (because of the inductive step) Konstantin Busch - LSU 29
Inductive Hypothesis: Assume that every postage amount between and can be generated by using 4 -cent and 5 -cent stamps Inductive Step: If then the inductive step follows directly from inductive basis Konstantin Busch - LSU 30
Consider: Inductive hypothesis End of Proof Konstantin Busch - LSU 31
Recursion is used to describe functions, sets, algorithms Example: Factorial function Recursive Basis: Recursive Step: Konstantin Busch - LSU 32
Recursive algorithm for factorial( ) { if then return else return } //recursive basis //recursive step Konstantin Busch - LSU 33
Fibonacci numbers Recursive Basis: Recursive Step: Konstantin Busch - LSU 34
Konstantin Busch - LSU 35
Recursive algorithm for Fibonacci function fibonacci( ) { if then //recursive basis return //recursive step else return } Konstantin Busch - LSU 36
Iterative algorithm for Fibonacci function fibonacci( ) { if then else { for } to do { } return Konstantin Busch - LSU 37
Theorem: for (golden ratio) Proof: Proof by (strong) induction Inductive Basis: Konstantin Busch - LSU 38
Inductive Hypothesis: Suppose it holds Inductive Step: We will prove for Konstantin Busch - LSU 39
is the solution to equation induction hypothesis Konstantin Busch - LSU End of Proof 40
Greatest common divisor Recursive Basis: Recursive Step: Konstantin Busch - LSU 41
Recursive algorithm for greatest common divisor //assume a>b gcd( ){ //recursive basis if then return //recursive step else return } Konstantin Busch - LSU 42
Lames Theorem: The Euclidian algorithm for , uses at most divisions (iterations) Proof: We show that there is a Fibonacci relation in the divisions of the algorithm Konstantin Busch - LSU 43
remainder divisions first zero Konstantin Busch - LSU result 44
This holds since and is integer Konstantin Busch - LSU 45
This holds since Konstantin Busch - LSU 46
End of Proof Konstantin Busch - LSU 47
Algorithm Mergesort 8 2 4 6 9 7 10 1 5 3 split 8 2 4 6 9 7 10 1 5 3 sort 2 4 6 8 9 merge 1 3 5 7 10 1 2 3 4 5 6 7 8 9 10 Konstantin Busch - LSU 48
sort( if ){ then { return } } else return Konstantin Busch - LSU 49
Input values of recursive calls 8 2 4 6 9 7 10 1 5 3 8 2 4 6 9 8 2 4 8 2 8 6 9 4 2 7 10 1 5 3 6 7 10 1 9 7 10 7 Konstantin Busch - LSU 5 3 10 50
Input and output values of merging 1 2 3 4 5 6 7 8 9 10 2 4 6 8 9 2 4 8 2 8 8 6 9 4 2 1 3 5 7 10 6 7 10 1 9 7 10 7 Konstantin Busch - LSU 5 3 10 51
merge( } ) { //two sorted lists while do { Remove smaller first element of from its list and insert it to } if then { append remaining elements to } return Konstantin Busch - LSU 52
merging 2 4 6 8 9 2 4 8 4 8 8 8 6 9 6 9 9 9 2 2 2 3 2 6 9 4 4 6 8 9 Konstantin Busch - LSU Comparison 2<6 4<6 6<8 8<9 53
The total number of comparisons to merge two lists is at most: Merged size Length of A Konstantin Busch - LSU Length of B 54
Recursive invocation tree Assume Konstantin Busch - LSU 55
Recursive invocation tree Assume Elements per list #levels of tree = Konstantin Busch - LSU 56
merging tree Konstantin Busch - LSU 57
merging tree Konstantin Busch - LSU Elements per list 58
merging tree Comparisons per level Merges per level Total cost: Konstantin Busch - LSU Elements per merge 59
If at most the number of comparisons is If the number of comparisons is at most Time complexity of merge sort: Konstantin Busch - LSU 60
- Slides: 60