Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Theoretical computer science wikipedia , lookup
Genetic algorithm wikipedia , lookup
Operational transformation wikipedia , lookup
Smith–Waterman algorithm wikipedia , lookup
Dijkstra's algorithm wikipedia , lookup
Algorithm characterizations wikipedia , lookup
Selection algorithm wikipedia , lookup
Fisher–Yates shuffle wikipedia , lookup
Expectation–maximization algorithm wikipedia , lookup
Euclidean algorithm wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
Sequences and Mathematical Induction The Use of Induction as it Relates to Computer Science Mathematical Induction • Purpose is to prove the correctness of statements based on definite patterns – General Principle: let P(n) be a property defined for integers n and let a be a fixed integer. Suppose the following is true: • P(a) is true • For all integers k ≥ a, if P(k) is true then P(k+1) is true – To do this, assume that a property is true for k and show it to be true for k+1 – Then the statement “For all integers n ≥ a, P(n)” is true – In other words, if something is true for any arbitrary element in a domain, prove that it must be true for the next element of the domain and the statement is proven Use Induction in a Proof • Prove that: – First, identify the property, P(n), which is shown above – Basis step: prove P(1) is true: which is true because 1(2)/2 = 1 – Inductive step: prove P(k) for any integer k ≥ 1 • Obtain P(k) by substituting k for n: » This is the inductive hypothesis • Obtain P(k+1): – This is what must be proven Induction in a Proof Continued • We assume that is true – We know that is the same as saying because k is the element prior to (k+1), and therefore must be included in the set • From this, we find is true because adding the same thing to both sides of an equation is legal • Using basic algebra, we transform • We have now shown that for any element k in the domain of P(k), P(k+1) must also be true – The sum of all integers > 0 up to k has been found Another Proof by Induction • For all integers n ≥ 1, is divisible by 3 – Basis: is equal to 4-1=3, which is divisible by three – Inductive step: Assume is divisible by 3 • Substitute k+1 for k: • By the law of exponents: • Subtract from divisible by three • We now know that to yield which is is true. – Both terms are divisible by 3 because the sum of 2 numbers divisible by three is also divisible by three Proving a Property of a Sequence • You are given the following: – – Prove for all k ≥ 2 for all integers n ≥ 1 • Basis: For n = 1: • Inductive Step: – We must show – is true , by the inductive hypothesis: – We have now proven a property for this sequence Strong Mathematical Induction • This is similar to normal induction, but varies slightly – Let P(n) be an integer property and a and b are fixed integers where a ≤ b. Suppose the following • P(a), P(a+1)…and P(b) are all true • For any integer k > b, if P(i) is true for all integers i with a ≤ i < k, then P(k) is true • In general, the use of either kind of induction is interchangeable. Rarely will strong induction be a necessity. The Well-Ordering Principle for Integers • Let S be a set of one or more positive integers all of which are greater than some fixed integer. If this is so, then S has a least element – EX: all of the positive integers of the form 46-7k, where k is an integer. • The least element is 4 because when k=6, the output is 4 but when k=7, the output is -3 – A statement of this form is very useful in proving that algorithms terminate in a finite number of steps • If a given set is in decreasing order, meaning , and for all i, i ≥ 1, the sequence is finite because the well-ordering principle states that the set has a least element . This set can represent the guard value of a loop (EX: while x > 0…) The Correctness of Algorithms • There are two types of errors to worry about – Syntax errors violate the rules of the programming language (writing ik instead of if) – Logic errors encompass everything else. Examples would be runtime issues, incorrect formulae, and false assumptions • In order for a program (algorithm) to be correct, it must satisfy the following – The code must be robust – meaning that it can handle the full range of inputs (Like giving an error message for inputting a letter instead of a number) – The code must be proven logically correct – meaning that the desired output will result from all inputs within the algorithm’s desired functional domain Assertions • One must state what his code should do using predicates – Pre-conditions • A statement of what must be true for the algorithm to work – Post-conditions • A statement of the algorithm’s final state, which is the output of the algorithm if the pre-condition is true – In order to prove an algorithm’s correctness, one must show that if pre-conditions are true and all of the statements in the algorithm execute, the post-condition must be true • Often, one must “divide-and-conquer” by breaking an algorithm into pieces and strategically placing assertions. Then, you would prove each individual set of assertions to prove the whole algorithm • As an exercise, try to prove the correctness of the Euclidean Algorithm for obtaining the greatest common divisor of two integers – Hint: don’t worry about error guarding. Assume that all inputs are correct Looping • A loop can be implemented as a form of mathematical induction – Let G be the guard for a while loop. The loop [pre-condition for the loop] should have the form: [post-condition for the loop] while (G) [The body of the loop, – A loop is correct iff it containing no branching statements outside of the satisfies its postloop] end while conditions when its preconditions are true and it terminates over a finite number of steps Loop Invariant Theorem • A loop invariant I(n) is a predicate whose domain is a set of integers and… – Basis Property: Pre-condition implies I(0) is true before the first iteration – Inductive Property: For all integers k ≥ 0, if the guard (G) and I(k) are both true before an iteration, I(k+1) is true after iteration – Eventual Falsity of Guard: after a finite number of iterations, G becomes false – Correctness of the Post-condition: if N is the least number of iterations after which G is false and I(N) is true, then the values of the algorithm variables will be as specified in the post-condition An Example of a Loop • [pre: m is a nonnegative integer, x is a real number, i=0, and product = 0.] while (i ≠ m) 1. product := product + x 2. i := i +1 end while [post: product = mx] – The loop invariant is “I(n): i=n and product = nx” – The guard of the loop is “G: i ≠ m” – Based on this information, one can prove the correctness of this algorithm Why Learn This? • For starters, mathematical induction is one of the most powerful methods of proof in existence. Add to it that it lies almost exclusively at the heart of computer science. The ability of a program to recognize, interpret, and capitalize on distinguishable patterns is essential for that program to be viable on the market. That being said, the ability to prove that a given algorithm is correct all of the time, without sacrificing the efficiency of the code, makes the difference between a good programmer and an educated programmer. Educated programmers get paid more!