35
26 Sep 2014 Lecture 3 1

26 Sep 2014Lecture 3 1. Last lecture: Experimental observation & prediction Cost models: Counting the number of executions of Every single kind of command

Embed Size (px)

Citation preview

CS2012 Programming Techniques II

26 Sep 2014Lecture 31

Last lecture:Experimental observation & predictionCost models: Counting the number of executions ofEvery single kind of commandOnly some important commands (e.g., array accesses)We also assumed:Each command executes in 1 time unitOnly the highest-order term of calculations counts (T(n) ~ 5n2)Today:One more cost model: Counting the number of executionsLines of Code *Best case / Worst case * / Average caseA correctness argument Loop Invariants26 Sep 2014Lecture 32How can we sort a deck of cards?26 Sep 2014Lecture 33

wikipediaInsertionSortVideo: http://www.youtube.com/watch?v=cFoLbjGUKWs

What is the algorithm?

26 Sep 2014Lecture 34

wikipediaSpecification of an AlgorithmWhat are the inputs?What are assumed properties of the inputs?What is the output?What is the important property of the output?26 Sep 2014Lecture 35InsertionSort SpecificationFirst lets make sure we know what we want to do:The specification of the algorithm AKA the problem we are trying to solve26 Sep 2014Lecture 36Input: sequence of n numbers A=(a1, an)Output: a permutation (reordering) of the input(a1, an)Such that a1 a2 an

ExpressionWe can express the algorithm at different levels of detail:In English: impreciseCan convey the main idea of the algorithmBut hides the details some of them are important!We cannot use it to analyse the algorithm

In a programming language: very preciseNecessary for implementing the algorithmDoes express the detailsSometimes too much detail can confuse the idea

In pseudocode: happy mediumResembles a programming languageHas a good amount of detail26 Sep 2014Lecture 37Modern high-level programming languages try to be more like pseudocode by hiding details.InsertionSortAlgorithm (in English)Start from 1st element of the array (optimisation: start from 2nd)Shift element back until its right positionContinue to next elementRepeat (2) and (3) until the end of the array26 Sep 2014Lecture 38

InsertionSortAlgorithm (in pseudocode)for (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 39

InsertionSortAlgorithm (in Java)left as an exercise.26 Sep 2014Lecture 310AssumptionsWe use unspecified time units (tu)Each command takes 1tuNumerical data are stored in binary formatSize 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 overflowSimple numerical operations takes 1tu (+,-,*,/,mod,exp,..)

26 Sep 2014Lecture 311for (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return ABest Casecostno of timesfor (j = 1; j=0 and A[i]>A[i+1] {1 swap A[i], A[i+1]1 i=i-11}}return A1

26 Sep 2014Lecture 312Best Casecostno of timesfor (j = 1; j=0 and A[i]>A[i+1] {1 swap A[i], A[i+1]1 i=i-11}}return A1In the best case the array is already sorted.26 Sep 2014Lecture 313Best Casecostno of timesfor (j = 1; j=0 and A[i]>A[i+1] {1 (1+1++1)n-1 times swap A[i], A[i+1]10 i=i-110}}return A11In 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 - 126 Sep 2014Lecture 314Worst Casecostno of timesfor (j = 1; j=0 and A[i]>A[i+1] {1 swap A[i], A[i+1]1 i=i-11}}return A126 Sep 2014Lecture 315Worst Casecostno of timesfor (j = 1; j=0 and A[i]>A[i+1] {1 swap A[i], A[i+1]1 i=i-11}}return A1In the worst case the array is in reverse sorted order.26 Sep 2014Lecture 31626 Sep 2014Lecture 317

Worst Case26 Sep 2014Lecture 318costno of timesfor (j = 1; j=0 and A[i]>A[i+1] {12++n swap A[i], A[i+1]11++(n-1) i=i-111++(n-1)}}return A11In 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 - 1Average Case26 Sep 2014Lecture 319costno of timesfor (j = 1; j=0 and A[i]>A[i+1] {1(2++n)/2 swap A[i], A[i+1]1(1++(n-1))/2 i=i-11(1++(n-1))/2}}return A11In 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 =

ExercisesEstimate 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 executeNow give this estimate using the tilde notation

Estimate the worst case running time cost of InsertSort by counting the number of swap operationsNow give this estimate using the tilde notation

26 Sep 2014Lecture 320Correctness26 Sep 2014Lecture 321Why 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 beginning of the algorithmAt the end of the algorithmBefore each iteration of the algorithm26 Sep 2014Lecture 322Loop exit: InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 323

Loop exit: j==nInsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 324

n=A.lengthLoop exit: j==n sorted A[0..n-1]

InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 325

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry:InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 326

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 327

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 328

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]

InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 329

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]After line 1:InsertionSort loop invariantfor (j = 1; j=0 and A[i]>A[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 330

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]After line 1:1jA[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 331

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]After line 1: 1jA[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 332

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]After line 1: 1jA[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 333

n=A.lengthLoop exit: j==n sorted A[0..n-1]Loop entry: j==1 sorted A[0..0]unsorted A[1..n-1]After line 1: 1jA[i+1] { swap A[i], A[i+1] i=i-1}}return A26 Sep 2014Lecture 334

n=A.lengthExercise: Loop invariant of 1SUM?26 Sep 2014Lecture 335int count = 0;for (int i = 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?