420 likes | 499 Views
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.
E N D
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 “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
PROOF IN COMPUTER SCIENCE CSC 172 SPRING 2004 LECTURE 2
Example • Write a method to compute an • public static double power(int a , int n) • You have 5 minutes
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; }
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?
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
When ever the program reaches the top of the while loop, the assertion is true Assertions INIT BODY INVARIANT TEST
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
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
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
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?
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
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
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
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?
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); }
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;
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;
Total number of comparisons N + (N-1) + (N-2) + . . . + 1 Reversing 1 + 2 + 3 + . . . + N =
In order to calculate work • Prove
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
Prove: 1. Statement: S(n) : For any n>=1
Prove: 2. Basis Select n == 1
Prove: 2. Alternate Basis Select n == 2
Prove: 2. Alternate Basis Select n == 3
Prove: 3. Inductive Step Assume: To show:
Inductive Step We know, by definition: Rewrite it:
Inductive Step “By the Induction hypothesis” (we can make the following substitution)
Inductive Step • The rest is just algebra
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?
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
Interesting Aside: Visual Proof • Proof is “convincing prose” • Not all proof is “mathematical”
Visual Proof n 3 2 1 0 1 2 3 . . n
Visual Proof n 3 2 1 0 1 .. n/2 . . n
Visual Proof n 3 2 1 0 1 .. n/2
Visual Proof n 3 2 1 0 1 .. n/2
Visual Proof n 3 2 1 0 1 .. n/2
Visual Proof n+1 n 3 2 1 0 1 .. n/2
Visual Proof n 3 2 1 0 1 (n+1)/2 .. . n .