Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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

3 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

4 PROOF IN COMPUTER SCIENCE CSC 172 SPRING 2004 LECTURE 2

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

6 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; }

7 Does it work? SURE! TRUST ME! Well, look at a 100 if you don’t believe me! – Note, less loops! Can you “prove” that it works? bir a1001 a2a2 50 a4a4 25 24a4a4 a8a8 12 a 16 6 a 32 3 2a 36 a 64 1 0a 100

8 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

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

10 What is the loop invariant? At the top of the while loop, it is true that r*b i = a n It is? – Well, at the top of the first loop r==1 b==a i==n

11 So, if it’s true at the start Even case r new = r old b new == (b old ) 2 i new ==(i old )/2 Therefore, – r new * (b new ) inew == r old * ((b old ) 2 ) iold/2 – == r old * ((b old ) 2 ) iold – == a n

12 So, if it’s true at the start II Odd case r new = r old *b old b new == b old i new ==i old -1 Therefore, – r new * (b new ) inew == r old *b old * (b old ) iold-1 – == r old * (b old ) iold – == a n

13 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*b i = a n And, i == 0 ( the loop ended) What do we know?

14 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

15 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

16 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

17 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? 31-415926-535897-93-2384

18 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); }

19 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;

20 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;

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

22 Prove In order to calculate work

23 Simple Induction Three Pieces 1. A statement S(n) to be proved The statement must be about an integer parameter n 2. A basis for the proof The statement S(b) for some specific integer b Often b==0 or b==1 3. 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

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

25 Prove: 2. Basis Select n == 1

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

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

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

29 Inductive Step We know, by definition: Rewrite it:

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

31 Inductive Step The rest is just algebra

32

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

34 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?

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

36 Interesting Aside: Visual Proof Proof is “convincing prose” Not all proof is “mathematical”

37 Visual Proof 123..n0 1 2 3 n

38 1..n/2..n0 1 2 3 n

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

40 Visual Proof 1..n/20 1 2 3 n

41 Visual Proof 1..n/20 1 2 3 n

42 Visual Proof 1..n/20 1 2 3 n n+1

43 Visual Proof 1..(n+1)/2..n0 1 2 3 n


Download ppt "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."

Similar presentations


Ads by Google