Mathematical Induction (Wednesday 14 October 2015)
The natural numbers have a peculiar property. Any set of natural numbers, whether finite or infinite, has one element which is smaller than any of the others.
Related reading: Stein et al. p. 191-195 or Rosen p. 307+
The fact that any set of natural numbers has a smallest element makes it possible to use mathematical induction to prove predicates for all .
Related reading: Stein et al. p. 195-198 or Rosen p. 307+
Problem 1.1 Use mathematical induction to prove that the following recurrence:
is equivalent to .
Related reading: Stein et al. p. 191-195 or Rosen p. 307+
Exercise 1.1 Consider the following recurrence.
Prove that
Exercise 1.2 Describe the main steps of a proof by induction.
Exercise 1.3 Consider a ladder. If you stand on the ground, it is possible to step onto the first rung. If you stand at the th rung, it is possible to climb onto the st rung.
Prove that you can climb to the top of the ladder, starting on the ground, using a proof by contradiction.
Problem 1.2 Prove that the output array of insertion sort is sorted in increasing order.
2for i := 2 to n
3 j := i
4 while j ≥ 2 and A[j] < A[j−1]
5 swap A[j] and A[j−1]
6 j := j−1
Solution: To conduct a proof by induction, we need some predicate describing partial success of the algorith, Loop indices provide a reasonable link. We observe that during the iteration only is moved (and only to the left). Hence elements to the left must already be sorted, and elements to the right are irrelevant. Thus we can propose the following invariant :
If is true, the entire array is sorted after the last iteration, and the algorithm is correct. Therefore we prove by induction.
Firstly, we note that for , there is a single element to the left, which makes a trivially sorted subarray. Hence is true.
To prove , i.e. that the subarray remains sorted after the iteration, we need to consider the old subarray , and the new one, . It is sufficient to prove that for any .
Considering an arbitrary , there are three possibilities:
- and correspond to adjacent elements in the original array. Then as required, because the original array was sorted.
- is the new element. In this case , lest the while-condition be true, and the two elements swapped.
- is the new element. In this case , since the two elements have been swapped by the while loop.
In all three cases, the sort order is satisfied, and we conclude that the loop maintains sort order. InsertionSort is correct by mathematical induction.
Problem 1.3 Prove that the output array of insertion sort is sorted in increasing order. Base your proof on the recursive version below.
Related reading: Stein et al. p. 203-206 or Rosen p. 357-358
Solution: We base the proof on the recursive formulation of the algorithm.
To conduct a proof by induction, we need some predicate describing partial success of the algorith, The a variable should be in the set of natural numbers. In the case of recursion, we can typically link the predicate to the size of the input, as follows:
If we can prove for all , then the algorithm is correct for any (valid) input.
We note that for we have an empty array, which is trivially sorted. Hence is trivially true. It remains to prove that .
In the recursive case, the algorithm performs two operations. First, an InsertionSort on elements, which is correct by the inductive hypothesis ; and secondly an insert into a sorted array of elements. If we can prove that the insert algorithm is correct, then correctness of InsertionSort will follow.
The proof of the insert algorithm is simular
Again, we note that is true, because gives a singleton array which is trivially sorted. To prove , we note that there are two cases. If is larger than , it is larger than every element in because it is sorted. Thus is correctly inserted as a last element. If is not larger than , has to be the last element, at position , and is inserted into the subarray of elements. By the inductive hypothesis, , is correctly inserted, and follows for any non-negative by mathematical induction.
Since insert is correct, InsertionSort is correct, as argued above.
Exercise 1.4 Prove that the Tower of Hanoi algorithm is correct.
________________________________________________________________________________ ________________________________________________________________________________
- Algorithm SquareNmultiply
- if , return
- SquareNmultiply
- if ,
- else
- return
Exercise 1.5 Prove that the Square-and-Multiply algorithm is correct as given in Table 1.
- Identify a base case, and argue that the algorithm returns the correct answer in the base case.
- Consider Line 3 Write as an expression in terms of .
- Consider Lines 4–9. Write as a mathematical expression in terms of .
- Using the answers from the two previous questions, write as an expression in terms of .
- Argue that as given in your previous answer is equal to . Which arithmetic rules are you using?
- Use your previous answers to argue that the inductive case is correct.
- Give the structure of an inductive proof and finalise the argument that the algorithm in the table be correct.