350 likes | 415 Views
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
E N D
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
Lecture 3 How can we sort a deck of cards? wikipedia
Lecture 3 InsertionSort • Video: http://www.youtube.com/watch?v=cFoLbjGUKWs • What is the algorithm? wikipedia
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?
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
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.
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
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
Lecture 3 InsertionSort • Algorithm (in Java) • left as an exercise.
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
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
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.
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
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
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.
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
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 = …
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
Lecture 3 Correctness
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
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:
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
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]
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:
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
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]
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]
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:
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
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]
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]
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
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?