1 / 35

Last lecture: Experimental observation & prediction

Last lecture: Experimental observation & prediction Cost models : Counting the number of executions of Every single kind of command Only some important commands (e.g., array accesses) We also assumed: Each command executes in 1 time unit

zeph-ruiz
Download Presentation

Last lecture: Experimental observation & prediction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3

  2. Lecture 3 Last lecture: • Experimental observation & prediction • Cost models: Counting the number of executions of • Every single kind of command • Only some important commands (e.g., array accesses) • We also assumed: • Each command executes in 1 time unit • Only the highest-order term of calculations counts (T(n) ~ 5n2) Today: • One more cost model: Counting the number of executions • Lines of Code * • Best case / Worst case * / Average case • A correctness argument – Loop Invariants

  3. Lecture 3 How can we sort a deck of cards? wikipedia

  4. Lecture 3 InsertionSort • Video: http://www.youtube.com/watch?v=cFoLbjGUKWs • What is the algorithm? wikipedia

  5. Lecture 3 Specification of an Algorithm • What are the inputs? • What are assumed properties of the inputs? • What is the output? • What is the important property of the output?

  6. Lecture 3 InsertionSort – Specification First let’s make sure we know what we want to do: • The specification of the algorithm • AKA the problem we are trying to solve Input: sequence of nnumbers A=(a1, … an) Output: a permutation (reordering) of the input (a’1, … a’n) Such that a’1 ≤ a’2 ≤ … ≤ a’n

  7. Lecture 3 Expression • We can express the algorithm at different levels of detail: • In English: imprecise • Can convey the main idea of the algorithm • But hides the details – some of them are important! • We cannot use it to analyse the algorithm • In a programming language: very precise • Necessary for implementing the algorithm • Does express the details • Sometimes too much detail – can confuse the idea • In pseudocode: happy medium • Resembles a programming language • Has a “good amount” of detail Modern high-level programming languages try to be more like pseudocodeby hiding details.

  8. Lecture 3 InsertionSort • Algorithm (in English) • Start from 1st element of the array (optimisation: start from 2nd) • Shift element back until its right position • Continue to next element • Repeat (2) and (3) until the end of the array

  9. Lecture 3 InsertionSort • Algorithm (in pseudocode) • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A

  10. Lecture 3 InsertionSort • Algorithm (in Java) • left as an exercise.

  11. Lecture 3 Assumptions • We use unspecified time units (tu) • Each command takes 1tu • Numerical data are stored in binary format • Size of an int is 1 memory word. Of an array A[0..n-1] is n words. • Program variables can store arbitrarily large numbers • no overflow • Simple numerical operations takes 1tu (+,-,*,/,mod,exp,..) • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A

  12. Lecture 3 Best Case cost no of times • for (j = 1; j<A.length; j++){ 1 • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 • while i>=0 and A[i]>A[i+1] { 1 • swap A[i], A[i+1] 1 • i=i-1 1 • }} • return A 1

  13. Lecture 3 Best Case cost no of times • for (j = 1; j<A.length; j++){ 1 • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 • while i>=0 and A[i]>A[i+1] { 1 • swap A[i], A[i+1] 1 • i=i-1 1 • }} • return A 1 In the best case the array is already sorted.

  14. Lecture 3 Best Case cost no of times • for (j = 1; j<A.length; j++) { 1 n • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 n-1 • while i>=0 and A[i]>A[i+1] { 1 (1+1+…+1)n-1 times • swap A[i], A[i+1] 1 0 • i=i-1 1 0 • }} • return A 1 1 In the best case the array is already sorted. The time (as a function of the input size n): T(n) = n + n-1 + n-1 + 1 = 3n - 1

  15. Lecture 3 Worst Case cost no of times • for (j = 1; j<A.length; j++) { 1 • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 • while i>=0 and A[i]>A[i+1] { 1 • swap A[i], A[i+1] 1 • i=i-1 1 • }} • return A 1

  16. Lecture 3 Worst Case cost no of times • for (j = 1; j<A.length; j++) { 1 • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 • while i>=0 and A[i]>A[i+1] { 1 • swap A[i], A[i+1] 1 • i=i-1 1 • }} • return A 1 In the worst case the array is in reverse sorted order.

  17. Lecture 3

  18. Lecture 3 Worst Case cost no of times • for (j = 1; j<A.length; j++) { 1n • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 n-1 • while i>=0 and A[i]>A[i+1] { 1 2+…+n • swap A[i], A[i+1] 1 1+…+(n-1) • i=i-1 1 1+…+(n-1) • }} • return A 1 1 In the worst case the array is in reverse sorted order. T(n) = n + n-1 + Sumx=2..n(x) + 2Sumx=1..n-1(x-1) + 1 = n + n-1 + (n(n+1)/2 - 1) + 2n(n-1)/2 + 1 = (3/2)n2 + (3/2)n - 1

  19. Lecture 3 Average Case cost no of times • for (j = 1; j<A.length; j++) { 1 n • //shift A[j] into the sorted A[0..j-1] • i=j-1 1 n-1 • while i>=0 and A[i]>A[i+1] { 1 (2+…+n)/2 • swap A[i], A[i+1] 1 (1+…+(n-1))/2 • i=i-1 1 (1+…+(n-1))/2 • }} • return A 1 1 In the average case we shift each A[j] about j/2 positions to the left T(n) = n + n-1 + Sumx=2..n(x)/2 + Sumx=1..n-1(x-1)/2+ 1 = …

  20. Lecture 3 Exercises • Estimate the worst case running time cost of 2SUM by counting the number of times each line of code is executed. • Assume each line takes 1 time unit to execute • Now give this estimate using the tilde notation • Estimate the worst case running time cost of InsertSortby counting the number of swap operations • Now give this estimate using the tilde notation

  21. Lecture 3 Correctness

  22. Lecture 3 Why is our algorithm correct? We will make an argument of its correctness using a loop invariant. A loop invariant is a property which is true: • At the beginningof the algorithm • At the endof the algorithm • Before each iteration of the algorithm

  23. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A Loop exit:

  24. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==n

  25. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1]

  26. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry:

  27. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1

  28. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0]

  29. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]

  30. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1] After line 1:

  31. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1] After line 1:1≤j<n

  32. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0] unsorted A[1..n-1] After line 1:1≤j<n sorted A[0..j-1]

  33. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0] unsorted A[1..n-1] After line 1:1≤j<n sorted A[0..j-1] unsorted A[j..n-1]

  34. Lecture 3 InsertionSort – loop invariant • for (j = 1; j<A.length; j++) { • //shift A[j] into the sorted A[0..j-1] • i=j-1 • while i>=0 and A[i]>A[i+1] { • swap A[i], A[i+1] • i=i-1 • }} • return A n=A.length Loop exit: j==nsorted A[0..n-1] Loop entry: j==1 sorted A[0..0] unsorted A[1..n-1] After line 1:1≤j<n sorted A[0..j-1] unsorted A[j..n-1] Invariant:property that holds at the beginning of the loop at every iteration

  35. Lecture 3 Exercise: Loop invariant of 1SUM? • intcount = 0; • for (inti = 0; i < N; i++) • if (a[i] == 0) • count++; Properties: • When we exit the loop (and the code)? • At the start of the code? • Immediately after line 1 is executed?

More Related