1 / 51

CS322

Week 6 - Monday. CS322. Last time. What did we talk about last time? Proof by induction Strong induction. Questions?. Logical warmup Revenge of the hats. 20 prisoners are scheduled to be executed tomorrow

anneke
Download Presentation

CS322

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6 - Monday CS322

  2. Last time • What did we talk about last time? • Proof by induction • Strong induction

  3. Questions?

  4. Logical warmupRevenge of the hats • 20 prisoners are scheduled to be executed tomorrow • They will be lined up in a row facing the same direction, each randomly wearing a white hat or a black hat • Thus, one prisoner can see 19 prisoners, the next can see 18 prisoners, etc. • Tomorrow, each prisoner, in order, starting with the last (who can see the other 19) will be asked the color of his hat • He can answer "white" or "black" • If he answers correctly, he is spared, otherwise he is shot • If anyone answers something other than "white" or "black," everyone is killed immediately • Since the prisoners were told the scenario a day a head of time, what strategy can they develop to save the maximum number of prisoners?

  5. More Induction Examples

  6. Divisibility • Prove that, for all integers n ≥ 1, 22n– 1 is divisible by 3 • Hint: Use induction

  7. Inequality • Prove that, for all real number n ≥ 3, 2n + 1 < 2n • Hint: Use induction

  8. Fibonacci • The Fibonacci sequence is 1, 1, 2, 3, 5, 8, … • We can define it (recursively) as follows: • F1 = F2 = 1 • Fn = Fn-1 + Fn-2, n≥ 2 • Prove that, for all integers n ≥ 1, F4n is divisible by 3 • Hint: Use induction

  9. Strong Induction

  10. Strong induction • There are situations where we need more than the fact that the kth element maintains some property to prove the k + 1st element has the same property • Strong induction allows us to use the kth element, the k-1st, element, the k-2nd element, and so on • This is usually most helpful when the subterms you are doing induction on are of unknown size

  11. Proof by strong induction • To prove a statement of the following form: • n  Z, where n a, property P(n) is true • Use the following steps: • Basis Step: Show that the property is true for P(a), P(a+1), … P(b-1) , P(b), where a ≤ b, bZ • Induction Step: • Suppose that the property is true for some a ≤ i < k, where k  Z and k > b • Now, show that, with that assumption, the property is also true for k

  12. Example Theorem: For all integers n  2, n is divisible by a prime Proof: • Basis step: (n = 2) The property is true for n = 2 because 2 is divisible by 2 • Induction step: Assume that all numbers i where 2 ≤ i < k are divisible by a prime, where k > 2 and k  Z • Case 1:k is prime • If k is prime, k = k∙1, therefore k is divisible by a prime, namely itself • Case 2: k is composite • If k is composite, k = a∙b, where a,b  Z and 2 ≤ a < k and 2 ≤ b < k • By the induction hypothesis, a is divisible by some prime p • Thus, k = p∙c∙b = p∙d and k is divisible by prime p • Since we have shown the basis step and induction step of strong mathematical induction, the claim is true QED

  13. Example Theorem: It takes exactly n-1 steps to assemble a jigsaw puzzle with n pieces Proof: • Basis step: (n = 1) A puzzle with 1 piece takes 0 steps to put together • Induction step: Assume it takes i – 1 steps to put together puzzles of size i where 1 ≤ i < k, where k > 1 and k  Z • The last step in a puzzle of size k is putting together a subpuzzle of size j and a subpuzzle of size k – j where j  Z and 1 ≤ j < k and 1 ≤ k - j < k • By the induction hypothesis, it took j – 1 steps to put together one subpuzzle and k – j – 1 steps to put together the other • Thus, the total number of steps is j – 1 + k + j – 1 + 1 = k – 1 steps • Since we have shown the basis step and induction step of strong mathematical induction, the claim is true QED

  14. Well-Ordering Principle for Integers • It turns out that the concept of truth through mathematical induction is equivalent to another principle • Well-Ordering Principle for the Integers: • Let S be a set containing one or more integers all of which are greater than some fixed integer. Then S has a least element.

  15. Recursion In order to understand recursion, you must first understand recursion.

  16. Recursively defined sequences • As you've seen with Fibonacci, it's possible to define a sequence recursively • This is called a recurrence relation • The initial conditions give the starting point • Example: • Initial conditions • c0 = 1 • c1 = 2 • Recurrence relation • ck = ck-1 + kck-2 + 1, for all integers k  2 • Find c2, c3, and c4

  17. Writing recurrence relations in multiple ways • Consider the following recurrence relation: • sk = 3sk-1 – 1, for all integers k 1 • Now consider this one: • sk+1 = 3sk – 1, for all integers k 0 • Both recurrence relations have the same meaning

  18. Differences in initial conditions • Even if the recurrence relations are equivalent, different initial conditions can cause a different sequence • Example: • ak = 3ak-1, for all integers k 2 • a1 = 2 • bk = 3bk-1, for all integers k 2 • b1= 1 • Find a1, a2, and a3 • Find b1, b2, and b3

  19. Practice • Using just your wits, you should be able to figure out recursive definitions for many sequences • Give a recurrence relation for positive even integers: 2, 4, 6, 8, … • Give a recurrence relation for the triangular numbers: 1, 3, 6, 10, 15, … • Give a recurrence relation for the perfect squares: 1, 4, 9, 16, 25, … • Give a recurrence relation for factorial: 1, 2, 6, 24, 120, 720 …

  20. The proof is on fire • Perhaps you believe that you have the correct recurrence relation for perfect squares • Can you prove it? • Hint: Use mathematical induction • Recursion and induction and two sides of the same coin • The right cross and the left jab, if you will, of the computer scientist's arsenal

  21. Tower of Hanoi • Imagine that you have a tower of disks such that each is smaller than the one it rests on • Rules: • There are 3 pegs, and all the disks are on peg 1 • No two disks are the same size • A larger disk may not be placed on a smaller disk • Disks can only be moved one at a time • Move all the disks to peg 3

  22. Power of Hanoi • What's the smallest number of moves needed to move the disks? • Consider the following algorithm for moving k disks from the starting pole to the ending pole: • (Recursively) transfer the top k – 1 disks from the starting pole to the temporary pole • Move the bottom disk from the starting pole to the ending pole • (Recursively) move the top k – 1 disks from the temporary pole to the ending pole

  23. Tower of annoy • How do we represent the running time of this algorithm recursively? • We have to (recursively) move k – 1 disks, then a single disk, then (recursively) another k – 1 disks • mk = mk-1 + 1 + mk-1 or • mk = 2mk-1 + 1 • Clearly, it takes 1 move to move a single disk, so m1 = 1 • Find m2, m3, m4, and m5

  24. Fibonacci • We all know and love Fibonacci by now • Recall that it is supposed to model rabbit populations • For the first month of their lives, they cannot reproduce • After that, they reproduce every single month

  25. Fibonacci rules • From this information about rabbit physiology (which is a simplification, of course) we can think about pairs of rabbits • At time k, rabbits born at time k – 1 will not reproduce • Any rabbits born at k – 2 or earlier will, however • So, we assume that all the rabbits from time k – 2 have doubled between time k – 1 and k • Thus, our recurrence relation is: • Fk = Fk – 1 + Fk – 2, k 2 • Assuming one starting pair of rabbits, our initial conditions are: • F0 = 1 • F1 = 1

  26. Compound interest • It's boring, but useful • Interest is compounded based on some period of time • We can define the value recursively • Let i is the annual percentage rate (APR) of interest • Let m be the number of times per year the interest is compounded • Thus, the total value of the investment at the kth period is • Pk = Pk-1 + Pk-1(i/m), k 1 • P0 = initial principle

  27. Solving Recurrence Relations

  28. Recursion • … is confusing • We don't naturally think recursively (but perhaps you can raise your children to think that way?) • As it turns out, the total number of moves needed to solve the Tower of Hanoi for n disks is 2n – 1 • Likewise, with an interest rate of i, a principle of P0, and m periods per year, the investment will yield Po(i/m + 1)k after k periods

  29. Finding explicit formulas by iteration • Consequently, we want to be able to turn recurrence relations into explicit formulas whenever possible • Often, the simplest way is to find these formulas by iteration • The technique of iteration relies on writing out many expansions of the recursive sequence and looking for patterns • That's it

  30. Iteration example • Find a pattern for the following recurrence relation: • ak = ak-1 + 2 • a0 = 1 • Start at the first term • Write the next below • Do not combine like terms! • Leave everything in expanded form until patterns emerge

  31. Arithmetic sequence • In principle, we should use mathematical induction to prove that the explicit formula we guess actually holds • The previous example (odd integers) shows a simple example of an arithmetic sequence • These are recurrences of the form: • ak = ak-1 + d, for integers k ≥ 1 • Note that these recurrences are always equivalent to • an = a0 + dn, for all integers n ≥ 0 • Let's prove it

  32. Geometric sequence • Find a pattern for the following recurrence relation: • ak = rak-1, k ≥ 1 • a0 = a • Again, start at the first term • Write the next below • Do not combine like terms! • Leave everything in expanded form until patterns emerge

  33. Geometric sequence • It appears that any geometric sequence with the following form • ak = rak-1, k ≥ 1 • is equivalent to • an = a0rn, for all integers n ≥ 0 • This result applies directly to compound interest calculation • Let's prove it

  34. Employing outside formulas • Sure, intelligent pattern matching gets you a long way • However, it is sometimes necessary to substitute in some known formula to simplify a series of terms • Recall • Geometric series: 1 + r + r2 + … + rn = (rn+1 – 1)/(r – 1) • Arithmetic series: 1 + 2 + 3 + … + n = n(n + 1)/2

  35. Tower of Hanoi solution • Find a pattern for the following recurrence relation: • mk = 2mk-1 + 1, k ≥ 2 • m1 = 1 • Again, start at the first term • Write the next below • Do not combine like terms! • Leave everything in expanded form until patterns emerge • Use the arithmetic series or geometric series equations as needed

  36. How many edges are in a complete graph? • In a complete graph, every node is connected to every other node • If we want to make a complete graph with k nodes, we can take a complete graph with k – 1 nodes, add a new node, and add k – 1 edges (so that all the old nodes are connected to the new node • Recursively, this means that the number of edges in a complete graph is • sk = sk-1 + (k – 1), k ≥ 2 • s1 = 0 (no edges in a graph with a single node) • Use iteration to solve this recurrence relation

  37. Mistakes were made! • You might make a mistake when you are solving by iteration • Consider the following recursive definition • ck = 2ck-1 + k, k ≥ 1 • c0 = 1 • Careless analysis might lead you to the explicit formula • cn = 2n + n, n ≥ 0 • How would this be caught in a proof by induction verification?

  38. Solving Recurrence Relations

  39. Recursion • … is confusing • We don't naturally think recursively (but perhaps you can raise your children to think that way?) • As it turns out, the total number of moves needed to solve the Tower of Hanoi for n disks is 2n – 1 • Likewise, with an interest rate of i, a principle of P0, and m periods per year, the investment will yield Po(i/m + 1)k after k periods

  40. Finding explicit formulas by iteration • Consequently, we want to be able to turn recurrence relations into explicit formulas whenever possible • Often, the simplest way is to find these formulas by iteration • The technique of iteration relies on writing out many expansions of the recursive sequence and looking for patterns • That's it

  41. Iteration example • Find a pattern for the following recurrence relation: • ak = ak-1 + 2 • a0 = 1 • Start at the first term • Write the next below • Do not combine like terms! • Leave everything in expanded form until patterns emerge

  42. Arithmetic sequence • In principle, we should use mathematical induction to prove that the explicit formula we guess actually holds • The previous example (odd integers) shows a simple example of an arithmetic sequence • These are recurrences of the form: • ak = ak-1 + d, for integers k ≥ 1 • Note that these recurrences are always equivalent to • an = a0 + dn, for all integers n ≥ 0 • Let's prove it

  43. Geometric sequence • Find a pattern for the following recurrence relation: • ak = rak-1, k ≥ 1 • a0 = a • Again, start at the first term • Write the next below • Do not combine like terms! • Leave everything in expanded form until patterns emerge

  44. Geometric sequence • It appears that any geometric sequence with the following form • ak = rak-1, k ≥ 1 • is equivalent to • an = a0rn, for all integers n ≥ 0 • This result applies directly to compound interest calculation • Let's prove it

  45. Employing outside formulas • Sure, intelligent pattern matching gets you a long way • However, it is sometimes necessary to substitute in some known formula to simplify a series of terms • Recall • Geometric series: 1 + r + r2 + … + rn = (rn+1 – 1)/(r – 1) • Arithmetic series: 1 + 2 + 3 + … + n = n(n + 1)/2

  46. Tower of Hanoi solution • Find a pattern for the following recurrence relation: • mk = 2mk-1 + 1, k ≥ 2 • m1 = 1 • Again, start at the first term • Write the next below • Do not combine like terms! • Leave everything in expanded form until patterns emerge • Use the arithmetic series or geometric series equations as needed

  47. How many edges are in a complete graph? • In a complete graph, every node is connected to every other node • If we want to make a complete graph with k nodes, we can take a complete graph with k – 1 nodes, add a new node, and add k – 1 edges (so that all the old nodes are connected to the new node • Recursively, this means that the number of edges in a complete graph is • sk = sk-1 + (k – 1), k ≥ 2 • s1 = 0 (no edges in a graph with a single node) • Use iteration to solve this recurrence relation

  48. Mistakes were made! • You might make a mistake when you are solving by iteration • Consider the following recursive definition • ck = 2ck-1 + k, k ≥ 1 • c0 = 1 • Careless analysis might lead you to the explicit formula • cn = 2n + n, n ≥ 0 • How would this be caught in a proof by induction verification?

  49. Upcoming

  50. Next time… • Second-order linear homogeneous recurrence relations with constant coefficients • Generalized recursion • Introduction to set theory

More Related