Correctness of Algorithms

Correctness of Algorithms
Mathematical induction
Prof Hans Georg Schaathun
Høgskolen i Ålesund
Autumn 2013 – Induction 3
Recorded: October 9, 2015
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
1/8
1
2
3
4
5
Insertion sort
Exercise
Prove that the output array of insertion sort (as given in previous
videos) is sorted in increasing order.
f o r i = 2 to n
j = i
while (j ≥ 2) and (Aj < Aj−1 )
exchange Aj and Aj−1
j = j −1
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
2/8
1
2
3
4
5
Linking the problem to natural numbers
f o r i = 2 to n
j = i
while (j ≥ 2) and (Aj < Aj−1 )
exchange Aj and Aj−1
j = j −1
Loop indices provide a good link
Find a predicate P(i) which describe A at iteration i
Is some subarray sorted?
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
3/8
Observations
Only element Ai is moved
Elements to the right (j > i) are left for later.
Elements to the left (j < i) must have been sorted previously
P(i) : A1 , A2 , . . . , Ai is sorted at the start of iteration i + 1
Insertion sort is correct if P(n)
We need to prove
1
2
P(1)
P(n) ⇒ P(n + 1)
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
4/8
Base case
What does the array look like at i = 1, when the first iteration
starts?
There is a single element Ai to the left.
The left hand subarray trivially sorted
Conclusion
We can conclude that P(1) is true.
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
5/8
Inductive case
We need to prove that P(n) ⇒ P(n + 1).
The inner loop does an insert
maintaining sort order
We need to prove that this is correct
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
6/8
The insert operation
Original subarray A1 ≤ A2 ≤ . . . ≤ Ai was sorted
New subarray A01 , A02 , . . . , A0i , A0i+1
Consider adjacent elements A0j , A0j+1 after insertion
Three cases
1
2
3
A0j ≤ A0j+1 were adjacent originally
A0j+1 = Ai+1 and A0j ≤ Ai+1 lest they had been swapped in the
algorithm
A0j = Ai+1 and Ai+1 ≤ A0j+1 lest they had not been swapped in the
algorithm
Since the insert operation is correct
P(n) ⇒ P(n + 1) and correctness follows by induction
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
7/8
Exercise
Insertion sort
Consider the following selection sort algorithm (or the
recursive variant which you have done in a previous exercise).
Input Array A of length n
Output The same array A sorted in place.
1
2
3
4
f o r i := 1 to n − 1
f o r j := i + 1 to n
i f Ai > Aj
swap Ai with Aj
Question How do we know that it correctly produces a sorted array?
Prof Hans Georg Schaathun
Correctness of Algorithms
Induction 3
8/8