Quiz 3 Reminder and Midterm Results Reminder: Quiz 3 will be in the first 15 minutes of Monday s class. You can use any resources you have during the quiz. It covers all four sections of Unit 3. It has four questions, two of them will be choices. Analysis for choice questions are optional, and can get partial credit if the choice is wrong. Midterm has been graded. Average mark is much higher than that of quizzes. Great! Midterm papers and your notes will be returned after class. You can reuse the note for the final. Gradesource report on Midterm will be available after discussion session. 1
CSE 21 Mathematics for Algorithm and System Analysis Unit 3: Decision Trees and Recursion Section 4: Inductive Proofs and Recursive Equations 2
Review : Recursive Solution We have a recursive solution to the problem (proof, algorithm, data structure, etc.) if the following two conditions hold. 1. The set of simplest problems can be dealt with (proved, calculated, sorted, etc.). 2. The solution to any other problem can be built from solutions to simpler problems, and this process eventually leads back to the original problem. Example: recursive solution to binomial coefficients C(0, 0) = 1, C(0, k)=0 for k 0 C(n, k) = C(n 1, k 1) + C(n 1, k) for n > 0 3
Learning Outcomes By the end of this lesson, you should be able to Use induction, or inductive proof, to prove statements. Verify/prove a solution to a recursion. It might be tested in examinations. Derive solutions for two types of recursions. a n =ba n-1 +c a n =ba n-1 +ca n-2 4
Why do we need to learn them? Induction is a commonly used way to prove statements for all natural numbers, including many recursions. Recursion is one of the central ideas of computer science. We will show some recursions have direct solutions. 5
Induction Suppose A(n) is an assertion that depends on n. We use induction to prove that A(n) is true when we show that It is true for the smallest value of n and If it is true for everything less than n, then it is true for n. 6
Induction Suppose A(n) is an assertion that depends on n. We use induction to prove that A(n) is true when we show that It is true for the smallest value of n and If it is true for everything less than n, then it is true for n. 7
Induction and Recursion Commonalities between induction and recursion Both need something to get started; In both, each new thing depends on similar previous things. Differences between induction and recursion A recursion describes how to calculate a value from previously calculated values. An induction verifies whether the above description/statement is correct. 8
Theorem 6: Induction Let A(m) be an assertion, the nature of which is dependent on the integer m. Suppose that n 0 n 1, if we have proved the two statements: (a) A(n) is true for n 0 n n 1 and (b) If n > n 1 and A(k) is true for all k such that n 0 k n-1, then A(n) is true. Then A(m) is true for all m n 0. Proof by contradiction 9
Theorem 6: Weak Induction Let A(m) be an assertion, the nature of which is dependent on the integer m. If we have proved the two statements: (a) A(n 0 ) is true and (b) If n > n 0 and A(n-1) is true, then A(n) is true. Then A(m) is true for all m n 0. It is a special case of the first one, called weak induction because its statements are easier to check. The first one is called strong induction. 10
Definition 5 : Induction hypothesis In statement (a), A(n 0 ),..., A(n 1 ) are called the base cases or simplest cases. In statement (b), A(k) is true for all k such that n 0 k n-1 is called the induction assumption or induction hypothesis. proving the induction hypothesis implies A(n) is called the inductive step. 11
Example 24: Every integer is a product of primes A positive integer n > 1 is called a prime if its only divisors are 1 and n, e.g.,: 2, 3, 5, 7. If a number is not a prime, such as 12, it can be written as a product of primes (12 = 2 2 3). A prime p is a product of one prime, itself. Question: how to prove every integer n 2 is a product of primes? 12
Solution to Example 24 Base cases: n 0 =n 1 =2, A(n 0 ) is true. Induction hypothesis: suppose that for some n > 2, A(k) is true for all k such that n 0 k n-1. Inductive step: check A(n) based on the hypothesis If n is a prime, then it is a product of primes (itself). Otherwise, n = st where 1<s<n and 1<t<n. By the induction hypothesis, s and t are each a product of primes, hence n = st is a product of primes. 13
Example 25 : Sum of first n integers Formula for the sum of the first n integers S(n) = 1 + 2 +... + n. Prove S(n)=n(n+1)/2 using weak induction Base cases: n 0 =1, S(1)=(1 2)/2=1, so A(n 0 ) is true. Induction hypothesis: n > 1 and A(n-1) is true. Inductive step: S(n) = 1 + 2 +... + n = (1 + 2 +... + (n-1))+n = S(n-1)+n = (n-1)((n-1)+1)/2+n = n(n+1)/2 14
Terms for Recursion Recursion, or recursive equation, or recurrence relation: the equation that arises in the inductive proof and tells how to compute the value for n based on the value(s) for smaller number(s). e.g., S(n) = S(n-1) + n (for n > 1) initial condition(s): the value(s) for smallest number(s). It is the information on how to get started. e.g., S(1)=1 solution to the recursion: the equation on how to compute the value for n without having to compute the values for any smaller number(s). e.g., S(n) = n(n+1)/2 Differentiate this from recursive solution to a problem which is about how to solve the problem using recursion. 15
Theorem 7 : Verifying the solution to a recursion We often use the same induction pattern to prove a formula for the solution to a recursion is correct. Theorem 7: Suppose we have initial conditions that give a n for n 0 n n 1 and a recursion that allows us to compute a n when n > n 1. To verify that a n = f(n), it suffices to do two things: Step 1. Verify that f satisfies the initial conditions. Step 2. Verify that f satisfies the recursion. 16
Example 26 : Proving a formula for the solution to a recursion Let S(n) be the sum of the first n integers. The initial condition S(1) = 1 and the recursion S(n) = S(n-1) + n allow us to compute S(n) for all n 1. Prove f(n) = n(n+1)/2= S(n). Step 1: f(1)=1 (1+1)/2=1=S(1) Step 2: for n > 1, S(n)=n + S(n 1) = n + f(n 1) = n + n(n 1)/2 = n(n + 1)/2 = f(n) 17
Example 26 : Proving a formula for the solution to a recursion(2) Initial conditions: a 0 = 2, a 1 = 7 Recursion: a n = 3a n 1 2a n 2 when n > 1 Prove that a n = 5 2 n 3 for n 0. Step 1: f(0)= 5 2 0 3 =2, f(1)= 5 2 1 3 = 7 Step 2: 3 f(n 1) 2 f(n 2) =3 (5 2 n 1 3)-2 (5 2 n 2 3) = (3 5 2 2 5)2 n 2 3 = 5 2 n 3 = f(n) 18
How to get solutions to recursions? Some solutions are easy to guess Let r k = r k 1 /k for k 1, with r 0 = 1. Its first few terms are 1, 1, 1/2, 1/6, 1/24. It looks like r k = ( 1) k /k! is a solution. Some solutions are hard to guess b n = b 1 b n 1 + b 2 b n 2 + + b n 1 b 1 for n 2, with b 1 = 1. Some recursions do not have solutions partitions number of an n-set having exactly k blocks: S(n, k) = S(n 1, k 1) 1 + k S(n 1, k) Solutions to two types of recursion can be easily derived a n =ba n-1 +c a n =ba n-1 +ca n-2 Generally, it is usually easy to verify a solution to a recursion, but hard to find the solution on our own. 19
Theorem 8 : Solutions to Recursion a n = ba n 1 +c Let a 0, a 1,..., a n,... be a sequence of numbers. Suppose there are constants b and c such that b is not 0 or 1 and a n = ba n 1 +c for n 1. Then a n = Ab n + K where K = c/(1 b), and A = a 0 K = a 0 c/(1 b). It can be proved using the steps in Theorem 7: Initial Condition: Recursion: 20
Solutions to Some Recursions Let t k = 2t k 1 + 1 for k > 0, t 0 = 0. Compute its solution. K = c/(1 b) = 1/(1 2)= 1 A = a 0 K = 0 ( 1) = 1. So the solution is t k = 2 k 1. 21
Example 28 : I forgot the formulas for A and K Let a 1 = 3 and a n = 4a n 1 7 for n > 1. Compute its solution. Tricky part: We cannot use A = a 0 K directly because we do not know a 0. Since solution should be a n = A4 n + K a 1 = 3 = A4 1 + K a 2 = 4 3 7= A4 2 + K Solve the above two equations to get values of A and K. Solution: a n = 4 n /6+ 7/3. If you forgot formula for b, just add one more equation based on a 3 and solve the equations. 22
Theorem 9 : Solutions to Recursion a n = ba n 1 +ca n 2 Let a 0, a 1,..., a n,... be a sequence of numbers. Suppose there are constants b and c such that a n = ba n 1 +ca n 2 for n 2. Let r 1 and r 2 be the roots of the polynomial x 2 bx c. n n If r 1 r 2, then n 1 2 for n 0, where K 1 and K 2 are solutions to the equations K 1 + K 2 = a 0 and r 1 K 1 + r 2 K 2 = a 1. n n If r 1 = r 2, then an = K1 r1 + K 2nr2 for n 0, where K 1 and K 2 are solutions to the equations: K 1 = a 0 and r 1 K 1 + r 2 K 2 = r 1 K 1 + r 1 K 2 = a 1. Values of r 1 and r 2 : a = K1 r + K r2 r, r 1 2 b ± = b 2 2 + 4c 23
Example 29 : Applying Theorem 9 Let a 0 = 2, a 1 = 7 and a n = 3a n 1 2a n 2 for n > 2. Compute its solution. We have b = 3 and c = 2. Solving equation x 2 3x+2 = 0, we can get r 1 = 2 and r 2 = 1. r 1 r 2, so solve equation K 1 + K 2 = 2 and 2K 1 + K 2 = 7. Result is K 1 = 5, K 2 = 3. Solution is a n = 5 2 n 3 1 n = 5 2 n 3. 24
Example 29 : Applying Theorem 9 (2) A recursion is a 0 = 0, a 1 = 1, and a n = 4a n 1 4a n 2 for n > 2. Compute its solution. We have b = 4 and c = 4. Solving equation x 2 4x+4 = 0, we get r 1 = r 2 = 2. r 1 = r 2, so solve equation K 1 = 0, 2K 1 +2K 2 = 1. Result is: K 1 = 0, K 2 = 1/2. Solution is a n = K 1 2 n + K 2 n2 n = (1/2)n2 n = n2 n-1. 25
Example 29 : Applying Theorem 9 for Fibonacci recursion The recursion is : F 0 = F 1 = 1 and F k = F k 1 + F k 2 when k 2. We have b = 1 and c = 1. Solve equation is x 2 x 1 = 0, we get 1+ 5 1 5. r 1 r 2, so solve K 1 + K 2 = 1 and r 1 K 1 + r 2 K 2 = 1. Result is r1 r2. K 1 = r 1, K 5 = and r 2 2 = 5 2 = 2 Solution is Fn = 1 5 1+ 2 5 n+ 1 1 5 1 2 5 n+ 1 26
Example 30 : A shifted index Let a 1 = 0, a 2 = 1 and a n = 5a n 1 6a n 2 for n 3. Compute its solution. Tricky part: it starts with a 1 instead of a 0, so the equations for K 1 and K 2 cannot be directly used. Because b = 5 and c = 6, solve equation x 2 5x 6 = 0. We get r 1 = 6 and r 2 = 1. So a n = K 1 6 n K 2 ( 1) n. Based on a 1 and a 2, we have a 1 =0=6K 1 K 2 and a 2 =1=6 2 K 1 + K 2. Solve the equations, we get K 1 = 1/42, K 2 = 1/7. Solution is a n = (1/42) 6 n (1/7) ( 1) n. 27
Homework and Pre-Reading Assignment Homework: Exercise 4.1, 4.4, 4.5, 4.9, 4.11, in page DT-50 to DT-52 Prepare for Quiz 3 on Monday For next class, please read Section 1 of Unit Basic Concepts in Graph Theory (GT-1 to GT-7). Try to understand definitions of Graph from examples. Try to understand Equivalence Relation and how to use it to get isomorphic graphs. 28