Recursive Definitions Example: Give a recursive definition of a n. a R and n N. Basis: n = 0, a 0 = 1. Recursion: a n+1 = a a n. Example: Give a recursive definition of n i=0 a i. Let S n = n i=0 a i, then: Basis: n = 0, S 0 = 0 i=0 a i = a 0. Recursion: S n+1 = S n + a n+1. 1
Example: Give a recursive definition of GCD(a, b), a < b, a, b are integers not both equal to 0. Basis: a = 0, GCD(a, b) = b. Recursion: GCD(a, b) = GCD(b mod a, a). Example: Recursive definition of Fibonacci numbers: f 0 = 0, f 1 = 1 f n = f n 1 + f n 2. Recursive programming is not always efficient. Computing the Fibonacci numbers by iterative approach is more efficient than by recursive programming. 2
Lame s Theorem: Let a, b Z + with a b. Then the number of divisions used by the Euclidean algorithm to find GCD(a, b) is five times the number of decimal digits in b, or it is O(log b). Proof. Let a = r 0, b = r 1. Use n steps of the Euclidean algorithm to find gcd(a, b). r 0 = r 1 q 1 + r 2 r 1 = r 2 q 2 + r 3... r n 2 = r n 1 q n 1 + r n r n 1 = r n q n where q 1,..., q n 1 1 and q n 2 since r n < r n 1. Thus r n 1 = f 2 r n 1 2r n 2f 2 = f 3 r n 2 r n 1 + r n f 3 + f 2 = f 4... r 2 r 3 + r 4 f n 1 + f n 2 = f n b = r 1 r 2 + r 3 f n + f n 1 = f n+1. 3
So after n divisions assuming a b we have b f n+1 but by the previous result f n+1 > α n 1, n > 2, α = 1+ 5 2, so b > α n 1. Since we have ( ) 1 + 5 log 10 α = log 10 2 > 1/5 log 10 b > (n 1) log 10 α > (n 1)/5 or n 1 < 5 log 10 b. Suppose that b has k decimal digits. Then b < 10 k log 10 b < k. Hence n 1 < 5k or n 5k (since k is an integer) thus # of divisions 5k. Also 10 k 1 b k log 10 +1 so # of divisions is bounded above by 5(log 10 + 1) = O(log b). 4
Example: Recursive definition of sets 3 S x + y S if x S and y S Then S is the set of positive integers divisible by 3. Example: Recursive definition of the set of strings. A string over an alphabet is a finite sequence of symbols from. Let denote the set of all strings over. String x and y can be concatenated to form string xy. 5
Basis: λ = φ empty string. Recursion: wx if w and x. That is concatenating a string in and a symbol in. Example: string. Recursive definition of the reversal of a Basis: λ R = λ Recursion: (ux) R = xu R for x, u. Problem: Prove that (w 1 w 2 ) R = w R 2 wr 1. 6
Recursive Algorithms An algorithm is recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example: Recursive algorithm to compute gcd(a, b). if a = 0 then gcd(a, b) := b else gcd(a, b) := gcd(b mod a, a) Student should look at many other recursive algorithms in the Textbook. 7
Counting Suppose we want to find out how many passwords are possible if a password consists of 6, 7, or 8 characters (A-Z, and 0-9), and there is at least one digit in each password. Basic Counting Principles The sum rule: If the first task is done in n 1 ways, second task in n 2 ways and they are independent, then the number of ways to do the 2 tasks is n 1 + n 2. Example: The students can choose a project from 3 lists, the first list has 15 projects, the second list has 13 and the third list has 20. How many ways a student can choose a project from three lists? 8
The product rule: If a task can be broken into 2 sub-tasks. Sub-task 1 has n 1 ways to do and sub-task 2 has n 2 ways to do after sub-task 1. Then there are n 1 n 2 ways to do the task. Example: The chairs in this auditorium are labelled with a letter and a positive integer 10. What is the number of chairs in the auditorium? Example: How many different bit strings are there of length 8? Example: Each license plate has 3 letters followed by 3 digits. How many licence plates are possible? 9
Example: A password has 6, 7 or 8 characters. A character can be a letter A Z or a digit 0 9. There is at least one digit in each password. How many passwords are there? Let P = P 6 + P 7 + P 8 P 6 = 36 6 26 6 P 7 = 36 7 26 7 P 8 = 36 8 26 8 P = 2, 684, 483, 063, 360 Note: see more examples in the textbook. 10
The inclusion-exclusion principle: avoid over counting. A 1 A 2 = A 1 + A 2 A 1 A 2. Example: How many bit strings of length 8, either start with a 1 or (inclusive or) end with 00? Inclusion: number of bit strings of length 8 and start with a 1: 2 7 = 128 number of bit strings of length 8 and end with 00: 2 6 = 64 Total inclusion: 128 + 64 = 192 But we counted twice bit strings starting with 1 and ending with 00: Exclusion: 2 5 = 32 Result = 128 + 64 32 = 160 bit strings. 11
Example: How many bit strings of length 8 that have: a) 3 consecutive 0s? b) 4 consecutive 1s? c) 3 consecutive 0s or 4 consecutive 1s? 3 consecutive 0s case 1 2 3 4 5 6 7 8 1 0 0 0 x x x x x 2 5 = 32 2 1 0 0 0 x x x x 2 4 = 16 3 x 1 0 0 0 x x x 2 4 = 16 4 x x 1 0 0 0 x x 2 4 = 16 5 y y y 1 0 0 0 x (2 3 1) 2 = 14 6 z z z z 1 0 0 0 2 4 3 = 13 total 107 yyy - any binary string except 000 (case 1) zzzz - any binary string except 0000, 0001 (case 1) or 1000 (case 2) 12
4 consecutive 1s case 1 2 3 4 5 6 7 8 1 1 1 1 1 x x x x 2 4 = 16 2 0 1 1 1 1 x x x 2 3 = 8 3 0 1 1 1 1 1 x x 2 3 = 8 4 x x 0 1 1 1 1 x 2 3 = 8 5 x x x 0 1 1 1 1 2 3 = 8 total 48 3 consecutive 0s and 4 consecutive 1s case 1 2 3 4 5 6 7 8 1 1 1 1 1 0 0 0 1 2 0 0 0 0 1 1 1 1 3 0 0 0 1 1 1 1 1 4 0 0 0 1 1 1 1 0 5 1 1 1 1 0 0 0 0 6 1 1 1 1 1 0 0 0 7 0 1 1 1 1 0 0 0 8 1 0 0 0 1 1 1 1 Total 8.
3 consecutive 0s or 4 consecutive 1s By inclusion-exclusion the answer is 107 + 48 8 = 147 Example: a) How many bit strings of length 10 containing 5 consecutive 0s? b) How many bit strings of length 10 containing exactly 5 consecutive 0s and not more?
Counting functions Example: How many functions are there from m elements to n elements? Example: How many one-to-one functions are there from m elements to n elements? Tree diagrams: a useful technique for counting small number of cases. You need different tools for different problems. Example: How many bit strings of length 4 that do not have consecutive 1s? Inclusion-exclusion is difficult to apply in this problem. 13
Example: A playoff between 2 teams consists of at most 5 games. The first team that wins 3 games wins the playoff. In how many different ways the playoff can occur? 14
The Pigeonhole Principle If k + 1 or more objects are placed in k boxes then there is at least one box containing 2 or more objects. Example: For 366 persons (assume 365 days in a year) at least 2 have the same birthday. The Generalized Pigeonhole Principle: If N objects are placed in k boxes then there is at least one box containing at least N k objects. Applications Example: Show that among any n + 1 positive integers 2n there must be one integer that divides one of the others. Proof. Write each of n + 1 positive integers as a j = 2 k jq j, j = 1,..., n + 1, where k j 0 and q j is odd. Integers q j, j = 1,..., n+1 are odd, positive and < 2n. Since there are only n such integers we have by the pigeonhole principle that at least n+1 n = 2 of the integers q j, j = 1,..., n + 1 must be equal, e. g., q i = q j = q, i j. Then a i = 2 k iq, a j = 2 k jq, hence a i a j if k i k j or a j a i if k i > k j 15
Theorem: Every sequence of n 2 +1 distinct real numbers contains a subsequence of length n + 1 that is either strictly increasing or strictly decreasing. Proof. Let a 1,..., a n 2 +1 be a sequence of n2 + 1 distinct real numbers. Associate with a k a pair of (i k, d k ), where i k is the length of the longest increasing subsequence starting at a k, and d k is the length of the longest decreasing subsequence starting at a k. Suppose to the contrary that there are no increasing or decreasing subsequences of length n + 1. Then i k, d k are positive integers less or equal to n, k = 1,..., a n 2 +1. Hence, by the product rule there are up to n 2 different ordered pairs (i k, d k ). Since there are n 2 + 1 pairs (i k, d k ) by the pigeonhole principle at least two of these ordered pairs are equal. That means there exist a s and a t, with s < t such that i s = i t and d s = d t. This is impossible. Indeed, because all the terms in the sequence are different, either a s < a t or a s > a t. If a s < a t, then since i s = i t, an increasing subsequence of length i s + 1 can be built starting at a s, by taking a s followed by an increasing sequence of length i t = i s starting at a t. This is contradiction. Similarly, if a s > a t, then we can obtain a decreasing subsequence of length d s + 1 by starting at a s, then 16
continuing to a t followed by a decreasing subsequence of length d t = d s starting at a t. This leads to a contradiction. Example: Assume a group of 6 people each pair consists of 2 friends or 2 enemies. Show that there are either 3 mutual friends or 3 mutual enemies. This is an example of a Ramsey theory problem. Proof. Let the group of 6 people be {A, B, C, D, E, F }. Choose A. In the remaining group of 5 people {B, C, D, E, F by the pigeonhole principle there are either three friends of A or three enemies of A, since 5 3 = 3. Suppose that B,C,D are enemies of A. If any two of these three people are enemies, say C,D then A,C,D form a group of three mutual enemies. Otherwise A,C,D form a group of three mutual friends. The proof in a latter case proceeds similarly.