general announcements project due friday, 1/30 labs start wednesday & thursday – java review...
Post on 15-Jan-2016
216 views
TRANSCRIPT
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
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
PROOF PROOF IN IN
COMPUTER SCIENCECOMPUTER SCIENCE
CSC 172 SPRING 2004
LECTURE 2
ExampleExample
Write a method to compute an
public static double power(int a , int n)
You have 5 minutes
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;
}
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
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
AssertionsAssertions
When ever the program reaches the top of the while loop, the assertion is true
INIT
BODY
TEST
INVARIANT
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
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
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
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?
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
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
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
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
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);
}
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;
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;
Total number of comparisonsTotal number of comparisons
N + (N-1) + (N-2) + . . . + 1
Reversing
1 + 2 + 3 + . . . + N =
n
i
i1
Prove
In order to calculate workIn order to calculate work
2
)1(
1
nni
n
i
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
Prove: Prove:
1. Statement:
S(n) : For any n>=1
2
)1(
1
nni
n
i
2
)1(
1
nni
n
i
Prove: Prove:
2. Basis
Select n == 1
2
)1(
1
nni
n
i
2
)11(11
1
i
i
Prove: Prove:
2. Alternate Basis
Select n == 2
2
)1(
1
nni
n
i
2
)12(22
1
i
i
212
1
i
i
Prove: Prove:
2. Alternate Basis
Select n == 3
2
)1(
1
nni
n
i
2
)13(33
1
i
i
3213
1
i
i
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
Inductive StepInductive Step
We know, by definition:
Rewrite it:
)1(...321)1(
1
nnin
i
)1(1
)1(
1
niin
i
n
i
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
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
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
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!
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?
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
Interesting Aside: Visual ProofInteresting Aside: Visual Proof
Proof is “convincing prose” Not all proof is “mathematical”
Visual ProofVisual Proof
1 2 3 . . n0
123
n
nin
i
...3211
Visual ProofVisual Proof
1 .. n/2. . n0
123
n
Visual ProofVisual Proof
1 .. n/20
123
n
Visual ProofVisual Proof
1 .. n/20
123
n
Visual ProofVisual Proof
1 .. n/20
123
n
Visual ProofVisual Proof
1 .. n/20
123
nn+1 2
)1(
1
nni
n
i
Visual ProofVisual Proof
1 ..(n+1)/2. .n0
123
n