general announcements

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

Upload: yamin

Post on 28-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: General Announcements

General AnnouncementsGeneral Announcements

Project Due Friday, 1/30Labs start Wednesday & Thursday

– Java review– Weiss 1.19, 1.20– You may show up & hand in

Workshops start Sunday– Why do we do workshops

Page 2: General Announcements

WORKSHOPSWORKSHOPS

“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

Page 3: General Announcements

PROOF PROOF IN IN

COMPUTER SCIENCECOMPUTER SCIENCE

CSC 172 SPRING 2004

LECTURE 2

Page 4: General Announcements

ExampleExample

Write a method to compute an

public static double power(int a , int n)

You have 5 minutes

Page 5: General Announcements

Possible solutionPossible 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;

}

Page 6: General Announcements

Does it work?Does it work?

SURE! TRUST ME!

Well, look at a100 if you don’t believe me!– Note, less loops!

Can you “prove” that it works?

b i r

a 100 1

a2 50

a4 25

24 a4

a8 12

a16 6

a32 3

2 a36

a64 1

0 a100

Page 7: General Announcements

Loop invariantsLoop 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

Page 8: General Announcements

AssertionsAssertions

When ever the program reaches the top of the while loop, the assertion is true

INIT

BODY

TEST

INVARIANT

Page 9: General Announcements

What is the loop invariant?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

Page 10: General Announcements

So, if it’s true at the startSo, if it’s true at the start

Even case

rnew= rold

bnew == (bold)2

inew==(iold)/2Therefore,

– rnew * (bnew)inew == rold * ((bold)2)iold/2

– == rold * ((bold)2)iold

– == an

Page 11: General Announcements

So, if it’s true at the start IISo, if it’s true at the start II

Odd case

rnew= rold*bold

bnew == bold

inew==iold-1Therefore,

– rnew * (bnew)inew == rold *bold* (bold)iold-1

– == rold * (bold)iold

– == an

Page 12: General Announcements

So, So,

If it’s true at the startAnd every time in the loop, it remains trueThen, it is true at the end

r*bi = an

And, i == 0 ( the loop ended)What do we know?

Page 13: General Announcements

Correctness ProofsCorrectness 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

Page 14: General Announcements

Correctness ProofsCorrectness 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

Page 15: General Announcements

The take away message?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

Page 16: General Announcements

ExampleExample

One dimensional pattern recognitionInput: a vector x of n floating point

numbersOutput: the maximum sum found in any

contiguous subvector of the input.

X[2..6] or 187How would you solve this?

31 -41 59 26 -53 58 97 -93 -23 84

Page 17: General Announcements

Obvious solutionObvious 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);

}

Page 18: General Announcements

An improved solutionAn improved solutionint 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;

Page 19: General Announcements

How many loops ?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;

Page 20: General Announcements

Total number of comparisonsTotal number of comparisons

N + (N-1) + (N-2) + . . . + 1

Reversing

1 + 2 + 3 + . . . + N =

n

i

i1

Page 21: General Announcements

Prove

In order to calculate workIn order to calculate work

2

)1(

1

nni

n

i

Page 22: General Announcements

Simple InductionSimple 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

Page 23: General Announcements

Prove: Prove:

1. Statement:

S(n) : For any n>=1

2

)1(

1

nni

n

i

2

)1(

1

nni

n

i

Page 24: General Announcements

Prove: Prove:

2. Basis

Select n == 1

2

)1(

1

nni

n

i

2

)11(11

1

i

i

Page 25: General Announcements

Prove: Prove:

2. Alternate Basis

Select n == 2

2

)1(

1

nni

n

i

2

)12(22

1

i

i

212

1

i

i

Page 26: General Announcements

Prove: Prove:

2. Alternate Basis

Select n == 3

2

)1(

1

nni

n

i

2

)13(33

1

i

i

3213

1

i

i

Page 27: General Announcements

Prove: Prove:

3. Inductive Step

Assume:

To show:

2

)1(

1

nni

n

i

2

)1(

1

nni

n

i

2

)2)(1(1

1

nni

n

i

Page 28: General Announcements

Inductive StepInductive Step

We know, by definition:

Rewrite it:

)1(...321)1(

1

nnin

i

)1(1

)1(

1

niin

i

n

i

Page 29: General Announcements

Inductive StepInductive Step

“By the Induction hypothesis”

(we can make the following substitution)

)1(1

)1(

1

niin

i

n

i

)1(2

)1()1(

1

nnn

in

i

Page 30: General Announcements

Inductive StepInductive StepThe rest is just algebra

)1(2

)1()1(

1

nnn

in

i

2

)1(2

2

)1()1(

1

nnni

n

i

Page 31: General Announcements

2

)1(2

2

)1()1(

1

nnni

n

i

2

)1(2)1()1(

1

nnni

n

i

2

222)1(

1

nnni

n

i

Page 32: General Announcements

2

232)1(

1

nni

n

i

2

)2)(1()1(

1

nni

n

i

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

Page 33: General Announcements

So, what did we doSo, what did we do

We showed that it worked for 1And 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?

Page 34: General Announcements

Template for Simple InductionTemplate for Simple Induction1. 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

Page 35: General Announcements

Interesting Aside: Visual ProofInteresting Aside: Visual Proof

Proof is “convincing prose” Not all proof is “mathematical”

Page 36: General Announcements

Visual ProofVisual Proof

1 2 3 . . n0

123

n

nin

i

...3211

Page 37: General Announcements

Visual ProofVisual Proof

1 .. n/2. . n0

123

n

Page 38: General Announcements

Visual ProofVisual Proof

1 .. n/20

123

n

Page 39: General Announcements

Visual ProofVisual Proof

1 .. n/20

123

n

Page 40: General Announcements

Visual ProofVisual Proof

1 .. n/20

123

n

Page 41: General Announcements

Visual ProofVisual Proof

1 .. n/20

123

nn+1 2

)1(

1

nni

n

i

Page 42: General Announcements

Visual ProofVisual Proof

1 ..(n+1)/2. .n0

123

n