1 / 42

General Announcements

General Announcements. Project Due Friday, 1/30 Labs start Wednesday & Thursday Java review Weiss 1.19, 1.20 You may show up & hand in Workshops start Sunday Why do we do workshops. WORKSHOPS.

yamin
Download Presentation

General Announcements

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. General Announcements • Project Due Friday, 1/30 • Labs start Wednesday & Thursday • Java review • Weiss 1.19, 1.20 • You may show up & hand in • Workshops start Sunday • Why do we do workshops

  2. WORKSHOPS “Analysts say tech employers today seek workers who are well educated in math and science but they also want them to have intangible skills, such as the ability to work well in teams.” -”New Tech Products Mean New Tech Jobs” -Brian Deagon, IBD, 1/20/04

  3. PROOF IN COMPUTER SCIENCE CSC 172 SPRING 2004 LECTURE 2

  4. Example • Write a method to compute an • public static double power(int a , int n) • You have 5 minutes

  5. Possible solution public static double power(int a, int n) { double r = 1; double b = a; int i = n ; while (i>0){ if (i%2 == 0) { b = b * b; i = i / 2;} else { r = r * b; i--; } return r; }

  6. SURE! TRUST ME! Well, look at a100 if you don’t believe me! Note, less loops! Can you “prove” that it works? Does it work?

  7. Loop invariants • In order to verify loops we often establish an assertion (boolean expression) that is true each time we reach a specific point in the loop. • We call this assertion, a loop invariant

  8. When ever the program reaches the top of the while loop, the assertion is true Assertions INIT BODY INVARIANT TEST

  9. What is the loop invariant? • At the top of the while loop, it is true that r*bi = an • It is? • Well, at the top of the first loop • r==1 • b==a • i==n

  10. So, if it’s true at the start • Even case rnew= rold bnew == (bold)2 inew==(iold)/2 • Therefore, • rnew * (bnew)inew == rold * ((bold)2)iold/2 • == rold * ((bold)2)iold • == an

  11. So, if it’s true at the start II • Odd case rnew= rold*bold bnew == bold inew==iold-1 • Therefore, • rnew * (bnew)inew == rold *bold* (bold)iold-1 • == rold * (bold)iold • == an

  12. So, • If it’s true at the start • And every time in the loop, it remains true • Then, it is true at the end r*bi = an • And, i == 0 ( the loop ended) • What do we know?

  13. Correctness Proofs • Proof are more valuable than testing • Tests demonstrate limited correctness • Proofs demonstrate correctness for all inputs • For some time, people hoped that all formal logic would replace programming • The naïve idea that “programming is a form of math” proved to be an oversimplification

  14. Correctness Proofs • Unfortunately, in practice, these methods never worked very well. • Instead of buggy programs, • people wrote buggy logic • Nonetheless, the approach is useful for program analysis

  15. The take away message? • In the end, engineering and (process) management are at least as important as mathematics and logic for the successful completion of large software projects

  16. Example • One dimensional pattern recognition • Input: a vector x of n floating point numbers • Output: the maximum sum found in any contiguous subvector of the input. • X[2..6] or 187 • How would you solve this?

  17. Obvious solution • Check all pairs int sum; int maxsofar = 0; for (int i = 0; i<x.length;i++) for (int j = i; j<x.length;j++){ sum = 0; for (int k = i;k<=j;k++) sum += x[k]; maxsofar = max(sum,maxsofar); }

  18. An improved solution int maxSum = 0 ; for (int i = 0; i<a.length;i++) { int thisSum = 0; for (int j = i; j<a.length;j++){ thisSum += a[j]; if (thisSum > maxSum) maxSum = thisSum; } } return maxSum;

  19. How many loops ? int maxSum = 0 ; for (int i = 0; i<a.length;i++) { int thisSum = 0; for (int j = i; j<a.length;j++){ thisSum += a[j]; if (thisSum > maxSum) maxSum = thisSum; } } return maxSum;

  20. Total number of comparisons N + (N-1) + (N-2) + . . . + 1 Reversing 1 + 2 + 3 + . . . + N =

  21. In order to calculate work • Prove

  22. Simple Induction • Three Pieces • A statementS(n) to be proved • The statement must be about an integer parameter n • A basis for the proof • The statement S(b) for some specific integer b • Often b==0 or b==1 • An inductive step for the proof • Show that “If S(n) is true, then S(n+1) must also be true” • Prove the statement “S(n) implies S(n+1)” for any n. • For this part, you get to “suppose” S(n) is true • “For the sake of argument” • Aka the inductive hypothesis

  23. Prove: 1. Statement: S(n) : For any n>=1

  24. Prove: 2. Basis Select n == 1

  25. Prove: 2. Alternate Basis Select n == 2

  26. Prove: 2. Alternate Basis Select n == 3

  27. Prove: 3. Inductive Step Assume: To show:

  28. Inductive Step We know, by definition: Rewrite it:

  29. Inductive Step “By the Induction hypothesis” (we can make the following substitution)

  30. Inductive Step • The rest is just algebra

  31. Which, of course, is what we set out to prove!

  32. So, what did we do • We showed that it worked for 1 • And that if it worked for n, it must work for n+1 • So, is it true for n==7? • Why? • Is it true for n==984375984237598437594373457?

  33. Template for Simple Induction • State what S(n) is. • Explain what n represents. “any positive integer” or “length of the string” • Tell what the value of n is for the basis case n==b • Prove S(b) • State that you are assuming n>=b and S(n) • Prove S(n+1) using the assumptions (say: “B.T.I.H.”) • State that due to (4) and (6) you conclude S(n) for all n>=b

  34. Interesting Aside: Visual Proof • Proof is “convincing prose” • Not all proof is “mathematical”

  35. Visual Proof n 3 2 1 0 1 2 3 . . n

  36. Visual Proof n 3 2 1 0 1 .. n/2 . . n

  37. Visual Proof n 3 2 1 0 1 .. n/2

  38. Visual Proof n 3 2 1 0 1 .. n/2

  39. Visual Proof n 3 2 1 0 1 .. n/2

  40. Visual Proof n+1 n 3 2 1 0 1 .. n/2

  41. Visual Proof n 3 2 1 0 1 (n+1)/2 .. . n .

More Related