Proving the Correctness of SelectionSort and Partition Algorithms, Study notes of Algorithms and Programming

Notes from a university lecture on the correctness proofs of selectionsort and partition algorithms. The lecture covers the definition of a correct algorithm, the concept of loop invariance property, and the proof of correctness for selectionsort. It also discusses the partition algorithm and its loop invariant property.

Typology: Study notes

Pre 2010

Uploaded on 02/12/2009

koofers-user-4on
koofers-user-4on ๐Ÿ‡บ๐Ÿ‡ธ

5

(1)

9 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Outline Proving SelectionSort Partition
CS 483 - Data Structures and Algorithm Analysis
Some notes on correctness proofs
R. Paul Wiegand
George Mason University, Department of Computer Science
February 20, 2006
R. Paul Wiegand George Mason University, Department of Computer Science
CS483 Lecture II
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Proving the Correctness of SelectionSort and Partition Algorithms and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 483 - Data Structures and Algorithm Analysis

Some notes on correctness proofs

R. Paul Wiegand

George Mason University, Department of Computer Science

February 20, 2006

R. Paul Wiegand George Mason University, Department of Computer Science

Outline

1 Proving Correctness

2 The Correctness of SelectionSort

3 The Correctness of Partition

R. Paul Wiegand George Mason University, Department of Computer Science

The Loop Invariance Property

We need to define a key property of the data manipulated by the

main loop of an algorithm

The property must help us understand why the algorithm is

correct

We must show that the property holds in the initial case, is

maintained each iteration, and that when the loop terminates

the property yields correctness

Determining this property in general can be difficult

But for simple, common algorithms the property is often the key

defining feature of the algorithm

R. Paul Wiegand George Mason University, Department of Computer Science

From Loop Invariance to Correctness

Initialization โ€” The loop invariance must be true prior to the first

iteration of the loop

Maintenance โ€” If the property holds prior to an iteration of the loop, it

must still hold after the iteration is complete

Termination โ€” When the loop terminates, the invariant provides a

useful property that helps demonstrate that the algorithm

is correct

To prove correctness, we must prove the above about the loop

invariance property

The first two pieces are similar to induction. When they hold, the

loop invariant is true prior to every iteration of the loop

The termination is where is differs from induction and is the most

important piece. We are not showing that the loop invariant holds

ad infinitum, but rather that it results in a correct answer after a

finite number of steps

R. Paul Wiegand George Mason University, Department of Computer Science

What Does the Loop Invariant Mean?

Essentially, the loop invariant says that at each step, the data set can be divided

into two parts:

The part to the left of i is a sorted sublist from elements in A The part from i to the right on which the algorithm is still working

Loop Invariant: At the start of each iteration of the outer- most for loop, the sublist A[0... i โˆ’1] con- sists of the i โˆ’ 1 smallest elements orig- inally in A in sorted order. The sublist A[i... n โˆ’ 1] contain the remaining ele- ments in A.

R. Paul Wiegand George Mason University, Department of Computer Science

Proving the Partition Loop Invariant

Initialization โ€” At the first iteration, i = 0, the sublist to the left of i is empty. It is reasonable to say that an empty sublist is ordered and obeys the loop invariant. Maintenance โ€” At the start of any arbitrary iteration, no element from 0 to i โˆ’ 1 can ever be disturbed again. During the step, the inner for loop will find the smallest element in A[i + 1... n โˆ’ 1] and will swap it for the A[i ] element. This element is swapped into the i th^ position. The new A[i ] element cannot be smaller than any element in A[0... i โˆ’ 1] because the loop invariant is true prior to the start of the iteration, so it will simultaneously be the smallest element from i to the right and no smaller than any element to its left. The loop invariant is preserved. Termination โ€” When the last iteration of the algorithm terminates, the loop counter will be on n โˆ’ 2. If the i th^ element is smaller, it will be exchanged with the n โˆ’ 1 st^ element. Given that the sublist to the left of i is already sorted and neither A[n โˆ’ 2] nor A[n โˆ’ 1] can be smaller than any item in A[0... n โˆ’ 2] because of the loop invariant, the combined list will be in sorted order. If the i th^ element is not smaller, the list is already sorted.

R. Paul Wiegand George Mason University, Department of Computer Science

Reminder: QuickSort and Partition

QuickSort(A[l... r ])

if l > r s โ†โˆ’ Partition(A[l... r ]) QuickSort(A[l... s โˆ’ 1) QuickSort(A[s + 1... r )

Partition(A[l... r ])

p โ†โˆ’ A[l] i โ†โˆ’ l j โ†โˆ’ r + 1 repeat repeat i + + until p โ‰ฅ A[i ] repeat j โˆ’ โˆ’ until p โ‰ค A[j] Swap(A[i ], A[j]) until i โ‰ฅ j Swap(A[i ], A[j]) Swap(A[l], A[j])

Loop Invariant (for Partition): At the start of the first repeat loop in the Partition function, the following holds for any array index k: k โˆˆ (l, i ] โ‡’ A[k] โ‰ค p k โˆˆ [j, r ] โ‡’ A[k] โ‰ฅ p

R. Paul Wiegand George Mason University, Department of Computer Science

What Does the Loop Invariant Mean?

Essentially, the loop invariant says that at each step, the data set can be divided

into three parts:

The part to the left of i (except l) is never greater than the pivot value The part to the right of j is never less than the pivot value The part in the middle on which the algorithm is still working

Loop Invariant (for Partition): At the start of the first repeat loop in the Partition function, the following holds for any array index k: k โˆˆ (l, i ] โ‡’ A[k] โ‰ค p k โˆˆ [j, r ] โ‡’ A[k] โ‰ฅ p

R. Paul Wiegand George Mason University, Department of Computer Science

Wait ... What Did We Just Prove?

Recall: We want the Partition function to select a pivot

value and divide the sublist such that all values to the left of

that pivot point are no greater than the pivot and all values to

the right are no less than the pivot

We just proved that the algorithm implemented in the book is

correct: For all valid input lists, it performs the above function

correctly

We did not prove that QuickSort is correct ...

But to prove that QuickSort is correct, we must show that

Partition is correct

R. Paul Wiegand George Mason University, Department of Computer Science