[WPI] [cs2223] [cs2223 text] [News] [Syllabus] [Classes]
Note: This page contains an attached Excel spreadsheet. A zip version is also atached for those whose browsers do not reliably download spreadsheet files.
There are three types of proofs commonly used in analyzing algorithms - direct proofs, indirect proofs, and inductive proofs. These notes compliment the material in Chapter 1 of the text.
Direct Proof
The direct proof combines known true statements (definitions or theorem, which are statements proven to be true) to produce other new statements.
The definitions of even and odd in the system of integers are:
In order to prove that 37 is odd, we need merely to find an integer j which fulfills the definitiion of oddness. That number is j=18, so 37 is odd.
Whenver you are trying to prove that something doesn't exist, the indirect proof, or proof by contradiction, is almost always used. Thatis because proof of non-existence using direct proofs would require a very large - usually infinite - number of proofs. You would have to show that each of the possible answers doesn't work in order to prove there is no answer to the question. The indirect proof reaches the same result but in a different way. There are three steps:
For example, let's prove that no integer can be both even and odd. Assume that there is such an integer, n, which is both even and odd - this is our added statement, the opposite of what we want to prove. Then, using the definitions above, we can write n two different ways and combine them.
This is a contradiction. We have shown that the difference between two integers is a number which is not an integer. But the set of integers is closed under the operation of addition - that means that the difference between any two integers has to be an integer - so this is a contradiction. Thus the statement "there is a number which is both odd and even" has been proven to be false so the statement "there is no number which is both odd and even" is true.
Prove that no two successive integers have a common divisor - except one, which is a divisor for all integers. Begin by assuming that there are two succesive integers, n and m, which have a common divisor j.
Again, we have the same contradiction - the difference between two integers is a non-integer. We have proven that no two successive integers can have a common divison.
Prove there is no largest prime number - which is equivalent to proving there are an infinite number of prime numbers. A prime number is is an integer which cannot be divided by any other integer except itself or one. Assume that PN is the largest prime number. Then form the number R - the product of all of the prime numbers - and add one to it.
By the last theorem (proof), S cannot be divided by any of the prime numbers, so it must be prime. This is a contradiction, so we have provent here is no largest prime number.
The proof by induction is used to produce a sequence of proofs.
For example, here are two definitions of An. Proving that they are equivalent for all positive values of n, requires a sequence of proofs - one for each value of n. Instead, we would use an inductive proof.
There are two parts to the inductive proof.
Look at both definitions of An and extend them to the next step in the sequence, using the assumption that they are equivalent at the n-th step. If the new expressions are equivalent, then the inductive step has been completed.
This result is exactly what we obtain when we replace n by n+1 in the second definition of An so we have shown the two definitions to be equivalent. This is known as the arithmetic series. Page 1 of the attached spreadsheet shows that the definitions of the first 100 terms are equivalent. It does not, however, show that all of the terms are equivalent - only a proof can do that.
Consider this statement.
The second page of the attached spreadsheet shows that this is true for the first 100 terms in the sequence. Can you prove this is true for all values of n>0?
Prove this statement, the solution of the geometric series.
When n=0, both sides give A0=1, so the basis step is established. The inductive step is:
This result is exactly what we obtain when we replace n by n+1 in the second definition of An so we have proven that the definitions are equivalent. The third page of the attached spreadsheet shows that this is correct for the first 100 terms in the sequence (to within the accuracy of Excel).
The selection sort was described on pages 62 and 106 of the text. We go through an array taking each element in sequence and looking for the smallest number in the rest of the sequence. If a smaller number is found, we swap the two numbers. The result is a sorted array.
Here is a sorting function which uses this algorithm.
void selection(int n, int *array) // selection sort an array { int temp; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (array[j] < array[i]) { temp = array[i]; // swap the values array[i] = array[j]; array[j] = temp; } return; } // end selection()
We want to calculate the number of comparisons made by the command
if (array[j] < array[i])
and the number of swaps, which is just the number of times that the comparison
evaluates as true
.
We can replace the for
statements by summations to calculate
the number of comparisons for an array of size n:
We used the fact that there is one comparison per time through the inner loop. The solution, which uses a result from the discussion about summations in Class 4 and the geometric series from above, is:
The function selection()
was put into a simulation program.
The source code is in the CCC directory:
/cs/cs2223/classes/class06/
The attached script shows operation of the program. Note that the simulation also shows that the number of swaps in the best case - the data were already sorted - and the worst case - the data were in inverse order - are exactly what we predicted in class.
[cs2223 text] [News] [Syllabus] [Classes] |