23.12.2013 Views

2. Loop invariants, Merge Sort, Inductive Proofs

2. Loop invariants, Merge Sort, Inductive Proofs

2. Loop invariants, Merge Sort, Inductive Proofs

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Datastructures and Algorithms Spring-Summer 2011/12<br />

Lab<br />

Valeria Fionda, Mouna Kacimi,<br />

Werner Nutt, Simon Razniewski<br />

<strong>2.</strong> <strong>Loop</strong> <strong>invariants</strong>, <strong>Merge</strong> <strong>Sort</strong>, <strong>Inductive</strong> <strong>Proofs</strong><br />

1. <strong>Loop</strong> <strong>invariants</strong>: SelectKth<br />

1. Inner <strong>Loop</strong>:<br />

• mini points to the smallest element in the subarray [i, .., j − 1]<br />

• Formally,<br />

∀l : i ≤ l ≤ j − 1 =⇒ A[mini] ≤ A[l]<br />

<strong>2.</strong> Outer <strong>Loop</strong><br />

• All elements to the left of i are ordered<br />

• All elements to the left of i are smaller than elements to the right of i<br />

(or, A[i − 1] is the smallest among A[i − 1]..A[n])<br />

• Formally,<br />

∀l : 1 < l ≤ i − 1 =⇒ A[l − 1] ≤ A[l] ∧<br />

∀m : 1 ≤ i − 1 ≤ m ≤ n =⇒ A[i − 1] ≤ A[m]<br />

3. Prove that at the end of the algorithm the k-smallest element of the input<br />

array is the one at position k.<br />

• Initialization: i = 1, j = <strong>2.</strong> The <strong>invariants</strong> holds because there are no<br />

elements to the left of i. i = 1 yields ∀i ∈ [1..0] : A[i] < A[j], ∀j > i.<br />

• Maintenance Consider the iteration i, the <strong>invariants</strong> of the outer loop<br />

say that ∀l : 1 < l ≤ i − 1 =⇒ A[l − 1] ≤ A[l] ∧<br />

∀m : 1 ≤ i − 1 ≤ m ≤ n =⇒ A[i − 1] ≤ A[m]<br />

At the end of the inner loop we have that j = n+1, and thus according<br />

to the invariant of the inner loop (∀l : i ≤ l ≤ j − 1 =⇒ A[mini] ≤<br />

A[l]) we have that ∀l : i ≤ l ≤ n =⇒ A[mini] ≤ A[l]. At the<br />

end of the loop we exchange the element in position i with the one<br />

in position mini thus, by exchanging A[i] with A[mini] it must hold<br />

∀k : i ≤ k ≤ n =⇒ A[i] ≤ A[k]. Moreover from the first invariant<br />

of the outer loop we have that ∀l : 1 < l ≤ i =⇒ A[l − 1] ≤ A[l].


• Termination At the end i = k + 1. Thus according to the <strong>invariants</strong><br />

of the outer loop we have ∀l : 1 < l ≤ k =⇒ A[l − 1] ≤ A[l] ∧<br />

∀m : 1 ≤ k ≤ m ≤ n =⇒ A[k] ≤ A[m]<br />

As for the invariant of the inner loop, considering i = k, we have:<br />

∀m : k ≤ m ≤ n =⇒ A[k] ≤ A[m] (result of inner loop),<br />

<strong>2.</strong> Algorithm Correctness: Merging of Arrays<br />

1. Pseudocode:<br />

MERGE (A, B)<br />

1 i := 1<br />

2 j := 1<br />

3 k := 1<br />

4 n1 := A.length<br />

5 n2 := B.length<br />

6 while i


1 i := 1<br />

2 j := 1<br />

3 for k := 1 to n1 + n2 do<br />

4 if A[i] n1<br />

or j > n2, and thus, when evaluating the condition “if A[i] n2 or (i


3. <strong>Inductive</strong> <strong>Proofs</strong>: Fibonacci numbers<br />

• Base case: n = 1 → 3n = 3 → fib(3n) = fib(3) = 2 is even.<br />

• <strong>Inductive</strong> step: Suppose fib(3k) be even. We have that fib(3(k + 1)) =<br />

fib(3k + 3) = fib(3k + 2) + fib(3k + 1) = fib(3k + 1) + fib(3k) +<br />

fib(3k + 1) = 2 · fib(3k + 1) + fib(3k). Since fib(3k) is even due to the<br />

induction hypothesis, fib(3(k + 1)) = fib(3k + 3) is even.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!