Download Proof - Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm wikipedia , lookup

Algorithm characterizations wikipedia , lookup

Corecursion wikipedia , lookup

Recursion (computer science) wikipedia , lookup

Transcript
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Proof Methods
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Introduction
– Logical WFFs are to Proof as Control Structures are to
Application Development
• They are the necessary building blocks
• They require a higher-level strategy for deciding which to use
and when
• In logic, the higher-level strategy is called the proof method
– Many proof methods exist however none is guaranteed
to prove a true theorem
• I.E., no general proof algorithm exists
Hypothesis
generation
– Rather we must intelligently choose our proof
technique based on the particulars of the problem
– Important Terms
Hypothesis
confirmation
• Inductive Reasoning – a general conclusion is drawn from a
set of experiences.
• Deductive Reasoning – a specific conclusion is shown to
follow from a set of premises
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Exhaustive Proof
– A single counterexample to a conjecture is sufficient to
disprove that conjecture
• Example: Finding large prime numbers (for cryptography)
• We observe that f(n) = n2 – n + 41 produces prime numbers
for n = 1 (41), 2 (43), 3 (47), and 4 (53)
• Conjecture: f(n) is prime for all positive integers n.
• Counterexample: n = 41
–
f(41) = 412 – 41 + 41 = 412
– Can one prove a conjecture by example?
• In general, no
• In rare cases, yes
–
–
–
–
Conjecture deals with finite collection
Can show conjecture true for each element in collection
Called proof by exhaustion
Example: f(n) is prime where 0  n  40
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Direct Proof
– To prove P  Q, assume P is true and use formal logic
to show that Q is also true
• Example: Our previous proofs were direct proofs
• Problem: Too slow and cumbersome to be practical
– Can use same idea of direct proof but less formal
notation (called informal direct proof)
• Example: Conjecture: “If an integer is divisible by 6, then
twice that integer is divisible by 4” (taken from text, p.88)
• Informal Proof
–
–
–
–
Let x be divisible by 6
Then x = 6k where k is an integer
Then 2x = 2(6k) = 12k = 4(3k)
Since 3k is an integer, 2x is divisible by 4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Contraposition Proof
– To prove P  Q, prove that ¬Q  ¬P
• (¬Q  ¬P)  (P  Q) is a tautology
• Example: n2 is odd  n is odd
• Contrapositive: n is even  n2 is even
–
–
–
If n is even, then n = 2k for some integer k
n2 = nn = (2k)(2k) = 2(2k2)
Therefore n2 is even
– Converse
• The contrapositive is not the converse
–
Converse of Q  P is P  Q
• “If and only if” conjectures require
–
–
Proof of the implication
Proof of the converse
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Contradiction Proof (1)
– A contradiction is a wff whose truth value is always
false
• Example: (¬A  A)
• Notice the following is a tautology
–
(P  ¬Q  false)  (P  Q)
• Try to develop the truth table for the above formula
– To prove an conjecture true, prove that the negation of
the conjecture is false
• In other words, to prove P  Q, assume P and ¬Q and show
that this leads to a contradiction
–
Remember this doesn’t prove that Q is true
» only that P  Q is true
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Contradiction Proof (2)
– Example: Prove that if 3n + 2 is odd then n is odd
• Let P be “3n + 2 is odd”
• Let Q be “n is odd”
• Assume P  ¬Q
–
3n + 2 is odd  n is even
• If n is even, then
–
–
•
•
•
•
n = 2k for some integer k
3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1)
Therefore 3n + 2 is even
But 3n + 2 is odd (see assumption)
Therefore 3n + 2  n is even leads to (implies) false
By contradiction, 3n + 2  n is odd must be true
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Contradiction Proof (3)
– Example: The Halting Problem (1)
• “There is an algorithm that takes as input a computer program
and input to that program and determines whether the
program will eventually stop when run with this input.”
• Recall that an algorithm must terminate, therefore you can’t
just run the program on the input to see if it terminates – what
if it doesn’t?
• Assume there is a solution to the Halting Problem,
–
–
–
call it H(P, I)
H(P,I) == “halt” when P terminates on I
H(P,I) == “never” when P does not terminate on I
• Isn’t a program just 0s and 1s?
• Aren’t 0s and 1s just data?
• So we can run H(P,P) and according to our assumption, it will
return either “halt” or “never”
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Contradiction Proof (4)
– Example: The Halting Problem (2)
• Consider the following algorithm K(P)
temp = H(P,P)
if temp == “halt” then
while (true) do {}
end if
• What happens if we try K(K)?
–
–
if H(K,K) is “never”, then by definition K(K) halts
» a contradiction
if H(K,K) is “halt”, then by definition K(K) doesn’t halt
» a contradiction
• Therefore, no such H(P, I) can exist!
–
–
Often used in computer science as the base line
“If we could write program X we could write H and
therefore program X is not possible.”
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Contradiction Proof (5)
– Example: The Halting Problem (3)
• Does a general virus detection program exist?
Virus?(P,I) returns true if P with input
I installs a virus and false if it
does not
•
•
•
•
Assume Virus?(P, I) exists
Assume P(I) does not produce a virus
Define V(P,I) to be begin P(I), virus end
Use it to define Halts(P, I):
if (Virus?(V(P,I)),I)) then // P will install virus
return true // P must halt to get to virus
else
return false // P must not have halted
end if
• We already know we can’t define Halts(P,I) and therefore we
can’t define Virus?(P,I).
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Induction Proof (1)
– Highly useful proof method in computer science
• used in statement counting
• used as basis of recursion
– Induction involves the properties of positive integers
– Proof by induction involves two steps
Basis step • Prove conjecture for the first case
Inductive step • Prove that if the conjecture is true for a given case, it is true
for the next case
– Leads to the following first principle of induction
• P(1) is true
• k P(k) true  P(k+1) true
P(n) true for all positive integers n.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Induction Proof (2)
– Example: Use Induction to prove that n < 2n for all
positive integers n.
• Basis: P(1): 1 < 2
• Induction: P(k+1): Assume that if k < 2k, we can prove (k+1)
< 2(k+1)
k < 2k
k + 1 < 2k + 1 add one to each side
k + 1  2k + 2k using 1  2k
k + 1 < 2k+1
Note: Don’t be confused – mathematical induction is a
deductive technique, not a method of inductive
reasoning!
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Induction Proof (3)
n
– Example: Use Induction to prove that  i
i 1
for all positive integers n.
 n(n  1) / 2
• Basis: 1 = 1(2) / 2
• Induction: You try it!
Basic Process:
1. Establish theorem
2. Show true for base case
3. Assume inductive hypothesis LHSih = RHSih
4. State “want to show” as LHSwts = RHSwts
5. Give definition of LHSwts
6. Translate to use LHSih
7. Substitute RHSih for LHSih
8. Manipulate to find RHSwts
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Induction Proof (4)
–
Practice Problems
•
•
•
•
Sec 2.2, #1
Sec 2.2, #22
Sec 2.2, #29
Sec 2.2, #43
Try:
(a) ∑ i = [n (n+1)]/2
(b) ∑ i3 = [n2 (n+1)2]/4
1≤ i≤n
1≤ i≤n
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Induction Proof (5)
– Sometimes it is convenient to use the second principle
of induction
• P(1) is true
• k[ P(r) true for 1  r  k  P(k+1) is true ]
– Example: Show that if n is an integer greater than 1,
then n can be written as the product of primes.
• Let P(n) be the statement that “n can be written as a product
of primes”
• Basis: P(2) = 2
• Induction: Assume P(i) is true for all integers 1 < i  k.
–
–
Case 1: k + 1 is prime, therefore can be written as
product of primes
Case 2: k + 1 is not prime
» k+1 = a * b where 2  a  b < k + 1
» by inductive hypothesis, both P(a) and P(b) are true
» therefore P(k+1) is true
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Induction Proof (6)
– Example: Your bank ATM delivers cash using only $20
and $50 bills. Prove that you can collect, in addition to
$20, any multiple of $10 that is $40 or greater (text, p.
109).
• Let P(n) be that “$n(10) can be generated using only $20 and
$50 bills”.
• P(4): $40 = $20 + $20
• P(5): $50
• Assume P(r) is true for any r, 4  r  k and consider P(k+1)
• We can assume k+1  6 (if not, use above cases).
• so (k+1)-2  4 and by inductive hypothesis $[(k+1) -2](10)
can be generated using only $20 and $50 bills.
• Therefore $(k+1)(10) can be generated by adding another $20
• Therefore P(k+1) is true
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Proof of Correctness (1)
– Previously we studied how to prove a program was
correct that involved only sequence and selection
If {Q}P1{R1}  {R1}P2{R2}  {R2}P3{R}
Then {Q}P1;P2;P3{R}
If {Q  B}P1{R}  {Q  ¬B}P2{R}
Then {Q}if B then P1 else P2{R}
– What about iteration?
• Centers on concept of loop invariant
• Want to reason about a relationship that is true after each
iteration of the loop, including the last iteration
– Leads to the following loop rule definition
If {Q  B}P {Q}
Then {Q} while B do P {Q
loop invariant
 ¬B}
– Trick is to find a useful loop invariant that allows us to
say what we wish to say
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Proof of Correctness (2)
– Notice the need for induction
• Wish to prove Q(n) is true for all n  0 (Q(0) means prior to first
execution of P’s body)
– Example: WTS the following code results in f = n!
i = 0;
f = 1;
while ( i <= n ) {
i = i + 1;
f = f * i;
}
– Let fi be value of f after ith iteration
– Desired “Q” is fk = k!, “B” is (i ≤ n)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Proof of Correctness (3)
– Must prove Q is loop invariant
• Q(0): prior to loop execution  f0 = 1 = 0!, i0 = 0
• Q(k+1): after (k+1)th loop execution 
–
–
By inductive hyp, Q(k) says fk = k! and ik = k
By code,
» ik+1 = ik + 1 = k + 1
» fk+1 = fk * ik+1 = fk * (k+1) = k! * (k+1) = (k+1)!
– Therefore “loop rule” tells us that after n iterations, f = n!
– Induction is similar to recursion. What is recursion?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Recursive Algorithms
– Special class of algorithms called recursion
– Quite important in Computer Science
– Process of solving a large problem by reducing it to one or
more subproblems that are:
• Identical in structure to the original problem and
• Somewhat easier to solve
– Technique in general:
• Subdivide into subproblems
• Continue to subdivide each of these subproblems into newer ones
that are less complex
• Eventually, subproblems become so simple that you can solve
them without further subdivision.:
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A Simple Recursive Example
– Campaign funding
• Get $1000 from single donor or
• Get 1000 $1 donations. There is a potentially recursive solution.
void collect1000 {
for (int i = 0; i < 1000; i++ ) {
collect $1 from person i ;
}
}
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A Simple Recursive Example
– Decomposed until you hit $1. That is where you will have to
stop.
– Divide and conquer paradim.
$1000 (goal)
$100 $100 $100 $100 $100 $100 $100 $100 $100 $100
$10 $10…..$10
$1 $1 ….. $1
……….
...
$10
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A Simple Recursive Example
– Campaign funding
• Get $1000 from single donor or
• Get 1000 $1 donations. There is a potentially recursive
solution.
• Potential solution:
Each non-leaf node in the solution tree can be designed
as a Java function.
Notice that each node has the same structural similarity,
i.e., collect $x
Generaliza x
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A Simple Recursive Example
– Task of collecting $x can be broken down into 2 cases:
• When $x is $1
• When $x is greater than $1
– Informally:
void collect1000 {
if (x == 1)
contribute $1 directly;
else {
find 10 people ;
Have each collect x/10 dollars;
Return money to superior;
}
}
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• How to develop recursive functions:
1.
2.
3.
Test to see if current problem represents a simple case.
If it does, function can be implemented easily.
If not, divide into similar structured subproblems,
each of which is solved by applying the same recursive
strategy.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
•
Characteristics of Recursive Algorithms:
1.
2.
3.
Naturally suited for the divide-and-conquer strategy.
Recursiveness  property of the solution of the problem and
not an attribute of the problem.
A problem must have three distinct properties:
(a) Must be possible to decompose the original problem into simpler
and smaller instances of the same problem.
(b) As decompositions continues, a large problem can be broken downInto
less complex one. They eventually become so simple that these
functions can be solved without further decomposition.
(c) Once each of these simpler subproblems has been solved, it must be
possible to combine these solutions to produce a solution to the original
problem
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
•
General Structure of Recursive Algorithms:
void solve(ProblemClass instance) {
if (instance is simple)
solve instance directly
else {
Divide instances into new subinstances i1, i2, …in;
solve(i1);
solve(i2);
solve(i3); // and so on…..for each subproblem
Reassemble subproblem solutions to solve entire problem;
}
}
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Recursion (1)
– Notice that induction is based on finding a smaller
problem of the same type (and with a known solution)
in a larger problem
n 1
n
i 1
i 1
• For example, using the fact that  i involves  i
– Induction is a form of wishful thinking
• In attempting to prove the inductive case we assume (or wish)
that we had already proven the smaller case
– We can use this same approach in writing programs
• For example, suppose we have a collection of values called l.
–
How could we find the length of the collection?
» Write a “for-loop” to count the elements (iterative)
» Wish we knew the length of the collection with the
first item removed then add one to that (recursive)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Recursion (2)
– Notice that we have found a smaller problem of the
same type in the larger problem!
– Of course, we can’t just wish we had the length of the
smaller collection – we must actually find it
– How does induction handle this?
• It finds the “easiest answer” and builds each successive
answers from it
• The “easiest answer” is the basis (or base case)
– What is the easiest case when calculating the length of
a collection?
• Length( empty collection ) == 0
– How do you build successive answers from it?
• Length( [a | L] ) = 1 + Length(L)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Recursion (3)
– Such a definition that involves itself is called a
recursive definition
• Example: Webster’s Definition of “inference”
–
“1: the act or process of inferring”
– Of course, to be useful a concept must have at least one
non-recursive definition
• This is called the base definition
• Example: Webster’s Definition of “inference”
–
“2: the act of passing from one proposition, statement, or
judgment considered as true to another whose truth is
believed to follow from that of the former”
– A recursive algorithm is therefore an algorithm that
uses itself to solve a problem.
• Obviously must include both the recursive case and the base
case
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
•
Application: Recursion (4)
– Euclid’s Method for Greatest Common Divisor
– a common divisor of two numbers or determining if two numbers
are relatively prime.
1. Divide the larger by the smaller and note the remainder:
2047/391 = (391 X 5) + 92
2. Divide the remainder (92) into the previous divisor (391):
391/92 = (92 X 4) + 23
3. Repeat steps 1 and 2 until the remainder is 1 or zero.
a Divide the remainder (23) into the previous divisor (92):
92/23 = (23 X 4) + 0
4. When the remainder is zero the last divisor is the GCD!
23 X 89 =2047 and 23 X 17 = 391.
Therefore 89/17 = 2047/391
5. When the remainder is 1 the two numbers have NO common divisor and are
relatively prime.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Recursion (4)
– Example: Euclid’s Method for Greatest Common
Divisor (1)
• GCD(a,b) where a  b > 0
== b if a mod b == 0
== GCD(b, a mod b) otherwise
• In Java
public class Main {
public static int GCD(int a, int b){
if ( a % b == 0 ){
return b;
}
else {
return GCD(b, a % b);
}
}
…
}
Unstructured!
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Recursion (5)
– Example: Euclid’s Method for Greatest Common
Divisor (2)
• Iterative version in Java
public class Main {
public static int GCD(int a, int b){
int rem = a % b;
while ( rem != 0 )
a = b;
b = rem;
rem = a % b;
}
return b;
}
…
}
Which one follows more naturally from the definition?
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Application: Recursion (6)
– Recursion is a powerful algorithmic technique used
extensively in computer science
• Follows naturally from recursive definitions (which occur
more frequently than you might think)
• Lends itself naturally to recursive data structures (which occur
more frequently than you might think)
• Fits naturally with the “run-time stack” memory model
– Doesn’t always lead to most efficient solution
• Efficiency hinges on recursive algorithm generating an
iterative or recursive process
–
–
recursive process: A process that generates a chain of
deferred operations
iterative process: A process that does not generate a chain
of deferred operations
– Do all programming languages support recursion?
– Do all recursive programs generate recursive
processes?