1 / 12

Proof Techniques and Recursion

Proof Techniques and Recursion. Proof Techniques. Proof by induction Step 1: Prove the base case Step 2: Inductive hypothesis, assume theorem is true for all cases up to limit k Step 3: Show theorem is true for next case ( k+1 ) Proof by counterexample

lalice
Download Presentation

Proof Techniques and Recursion

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. Proof Techniques and Recursion

  2. Proof Techniques • Proof by induction • Step 1: Prove the base case • Step 2: Inductive hypothesis, assume theorem is true for all cases up to limit k • Step 3: Show theorem is true for next case (k+1) • Proof by counterexample • Prove a statement false by providing a case where it is not correct • Proof by contradiction • Assume theorem is false and show that the assumption implies a known property is false

  3. Recursion • “A function that is defined in terms of itself is called recursive.” • Example: n! • 1*2*3*…*(n-1)*n • n*factorial of (n-1)

  4. Rules of Recursion • Base cases. You must always have some bases cases, which can be solved without recursion. • Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case. • Design rule. Assume that all the recursive calls work. • Compound interest rule. Never duplicate work by solving the same instance of a problem in separate recursive calls.

  5. Function: factorial int factorial(int n) {if(n == 1) return 1; else return (n*factorial(n-1)); }

  6. Iterative Factorial int factorial(int n) {int i; int product = 1; for(i = n; i > 1; i--) { product = product * i; } return product; }

  7. Linear Recursion • At most 1 recursive call at each iteration • Example: Factorial • General algorithm • Test for base cases • Recurse • Make 1 recursive call • Should make progress toward the base case • Tail recursion • Recursive call is last operation • Can be easily converted (should be!) to iterative

  8. Higher-Order Recursion • Algorithm makes more than one recursive call • Binary recursion • Halve the problem and make two recursive calls • Example? • Multiple recursion • Algorithm makes many recursive calls • Example?

  9. Example • Design a recursive program to produce the following output: 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0

  10. Analyzing Recursive Functions • Factorial • Similar to analyzing a loop • Previous example • Version 3 of Maximum Subsequence Sum problem (pg. 53)

  11. Algorithm • find max sum of first half • find max sum of second half • starting from middle, iterate through list toward beginning and stop when max found • starting from middle, iterate through list toward beginning and stop when max found • return max of first half, second half, and sum of overlap

  12. Recurrence • T(1) = 1 • T(N) = 2T(N/2) + O(N) • T(1) = 1, T(2) = 4 = 2*2, T(4) = 12 = 4*3, T(8) = 32 = 8*4, T(16) = 80 = 16*5 • T(N) = N*(k+1) -- if N=2k • T(N) = 2k*(k+1) • T(N) = k2k + 2k • T(N) = logN(N) + N = NlogN + N = O(NlogN)

More Related