lecture 5 loop invariants and insertion sort
play

Lecture 5: Loop Invariants and Insertion-sort COMS10007 - Algorithms - PowerPoint PPT Presentation

Lecture 5: Loop Invariants and Insertion-sort COMS10007 - Algorithms Dr. Christian Konrad 11.01.2019 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 1 / 12 Proofs by Induction Structure of a Proof by Induction 1 Statement


  1. Lecture 5: Loop Invariants and Insertion-sort COMS10007 - Algorithms Dr. Christian Konrad 11.01.2019 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 1 / 12

  2. Proofs by Induction Structure of a Proof by Induction 1 Statement to Prove: P ( n ) holds for all n ∈ N (or n ∈ N ∪ { 0 } ) (or n integer and n ≥ k ) (or similar) 2 Induction hypothesis: Assume that P ( n ) holds 3 Induction step: Prove that P ( n + 1) also holds If domino n falls then domino n + 1 falls as well 4 Base case: Prove that P (1) holds Domino 1 falls Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 2 / 12

  3. Examples Example: n ! ≥ 2 n for n ≥ 4 1 Base case ( n = 4): 4! = 24 ≥ 16 = 2 4 � 2 Induction hypothesis: n ! ≥ 2 n holds for n 3 Induction step: ( n + 1)! = ( n + 1) · n ! ≥ ( n + 1) · 2 n ≥ 2 · 2 n = 2 n +1 � This also implies that 2 n ∈ O ( n !) Example: 3 n − 1 is an even number, for every n ≥ 1 1 Base case ( n = 1): 3 1 − 1 = 2 � 2 Induction hypothesis: 3 n − 1 is an even number 3 Induction step: 3 n +1 − 1 = 3 · 3 n − 1 = 3 n + 2 · 3 n − 1 = 2 · 3 n + 3 n − 1 � Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 3 / 12

  4. Spot the Flaw Example: a n = 1, for every a � = 0 and n nonnegative integer 1 Base case ( n = 0): a 0 = 1 2 Induction hypothesis: a m = 1, for every 0 ≤ m ≤ n (strong induction) 3 Induction step: a n − 1 = a n · a n a n +1 = a 2 n − ( n − 1) = a 2 n a n − 1 = 1 · 1 = 1 . . . . 1 Problem: a 1 is computed as a 0 a 0 a − 1 and induction hypothesis does not holds for n = − 1 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 4 / 12

  5. Loop Invariants Definition: A loop invariant is a property P that if true before iteration i it is also true before iteration i + 1 Require: Array of n positive integers A Example: m ← A [0] Computing the maximum for i = 1 , . . . , n − 1 do if A [ i ] > m then Invariant: Before iteration i : m ← A [ i ] return m m = max { A [ j ] : 0 ≤ j < i } Proof: Let m i be the value of m before iter. i ( → m 1 = A [0]). Base case. i = 1: m 1 = A [0] = max { A [ j ] : 0 ≤ j < 1 } � Induction step. m i +1 = max { m i , A [ i ] } = = max { max { A [ j ] : 0 ≤ j < i } , A [ i ] } = max { A [ j ] : 0 ≤ j ≤ i } . � Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 5 / 12

  6. Loop Invariants - More Formally Main Parts: Initialization: It is true prior to the first iteration of the loop. before iteration i = 1 : m = A [0] = max { A [ j ] : j < 1 } � Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration. before iteration i > 1 : m = max { A [ j ] : j < i } � Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct. At the end of the loop m contains the maximum � Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 6 / 12

  7. Example Require: n integer s ← 1 for j = 2 , . . . , n do s ← s · j return s Invariant: At beginning of iteration j : s = ( j − 1)! 1 Let s j be the value of s prior to iteration j 2 Initialization: s 2 = 1 = (2 − 1)! � 3 Maintenance: s j +1 = s j · j = ( j − 1)! · j = j ! � 4 Termination: After iteration n , i.e., before iteration n + 1, the value of s is ( n + 1 − 1)! = n ! � Algorithm computes the factorial function Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 7 / 12

  8. Example: Insertion Sort Sorting Problem Input: An array A of n numbers Output: A reordering of A s.t. A [0] ≤ A [1] ≤ · · · ≤ A [ n − 1] Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v Insertion-Sort Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 8 / 12

  9. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v 0 1 2 3 4 5 15 7 3 9 8 1 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  10. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 1 0 2 3 4 5 15 7 3 9 8 1 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  11. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 1 0 2 3 4 5 15 7 3 9 8 1 v ← 7 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  12. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = 0 j = 1 2 3 4 5 15 7 3 9 8 1 v ← 7 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  13. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = − 1 j = 1 2 3 4 5 15 15 3 9 8 1 v ← 7 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  14. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = − 1 j = 1 2 3 4 5 7 15 3 9 8 1 v ← 7 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  15. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 2 0 1 3 4 5 7 15 3 9 8 1 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  16. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 2 0 1 3 4 5 7 15 3 9 8 1 v ← 3 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  17. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = 1 j = 2 0 3 4 5 7 15 3 9 8 1 v ← 3 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  18. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = 0 j = 2 1 3 4 5 7 15 15 9 8 1 v ← 3 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  19. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = − 1 j = 2 1 3 4 5 7 7 15 9 8 1 v ← 3 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  20. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = − 1 j = 2 1 3 4 5 7 7 15 9 8 1 v ← 3 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  21. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v i = − 1 j = 2 1 3 4 5 3 7 15 9 8 1 v ← 3 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  22. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 3 0 1 2 4 5 3 7 15 8 1 9 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  23. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 3 0 1 2 4 5 3 7 15 8 1 9 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  24. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 4 0 1 2 3 5 3 7 9 15 1 8 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  25. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 4 0 1 2 3 5 3 7 9 15 1 8 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

  26. Example: Require: Array A of n numbers for j = 1 , . . . , n − 1 do v ← A [ j ] i ← j − 1 while i ≥ 0 and A [ i ] > v do A [ i + 1] ← A [ i ] i ← i − 1 A [ i + 1] ← v j = 5 0 1 2 3 4 3 7 8 9 15 1 Dr. Christian Konrad Lecture 5: Loop Invariants and Insertion-sort 9 / 12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend