ASYMPTOTIC COMPLEXITY

Similar documents
ASYMPTOTIC COMPLEXITY

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Fall 2014

CS Lecture 19: Loop invariants

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

CS 1110: Introduction to Computing Using Python Loop Invariants

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Scientific Computing. Algorithm Analysis

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Introduction to the Analysis of Algorithms. Algorithm

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Lecture 24: Loop Invariants

Data Structures Lecture 8

Recitation 9. Prelim Review

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

EE 368. Week 6 (Notes)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Data Structures Lecture 3 Order Notation and Recursion

Algorithm Analysis. Performance Factors

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Lecture 5 Sorting Arrays

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

What is an algorithm?

Prelim 2, CS2110. SOLUTION

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Choice of C++ as Language

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Class Note #02. [Overall Information] [During the Lecture]

CS240 Fall Mike Lam, Professor. Algorithm Analysis

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

CORRECTNESS ISSUES AND LOOP INVARIANTS

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Algorithmic Analysis. Go go Big O(h)!

Asymptotic Analysis of Algorithms

Organisation. Assessment

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

STA141C: Big Data & High Performance Statistical Computing

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

CS 4349 Lecture August 21st, 2017

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

(Refer Slide Time: 1:27)

Total Score /1 /20 /41 /15 /23 Grader

Lecture Notes on Sorting

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Chapter 2: Complexity Analysis

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Outline and Reading. Analysis of Algorithms 1

Elementary maths for GMT. Algorithm analysis Part I

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Measuring algorithm efficiency

Analysis of Algorithms

Data Structures and Algorithms CSE 465

UNIT 1 ANALYSIS OF ALGORITHMS

STA141C: Big Data & High Performance Statistical Computing

Algorithms and Data Structures

Algorithmic Complexity

RECURSION (CONTINUED)

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

isinstance and While Loops

Introduction to Computer Science

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

csci 210: Data Structures Program Analysis

O(1) How long does a function take to run? CS61A Lecture 6

Week 12: Running Time and Performance

RECURSION (CONTINUED)

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Algorithms and Data Structures

Lecture 22. While Loops

Data Structure and Algorithm Homework #1 Due: 1:20pm, Tuesday, March 21, 2017 TA === Homework submission instructions ===

Data structure and algorithm in Python

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

CS 1110: WORKED EXAMPLES REGARDING LOOPS AND INVARIANTS

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 3 Notes. Class URL:

Data Structures and Algorithms

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Transcription:

Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture 10 CS2110 Spring 2019

Announcements 2 Next Mon-Tues: Spring Break No recitation next week Regrade requests will be processed this weekend Prelim is on Tuesday, 12 March. Prelim Review for prelim Sunday, 10 March, 1-3PM Next Thursday, we will tell you What time you will be assigned to take it What to do if you can t take it then but can take the other one What to do if you can t take it that evening. What to do if authorized for more time or quiet space

Help in providing code coverage 3 White-box testing: make sure each part of program is exercised in at least one test case. Called code coverage. Eclipse has a tool for showing you how good your code coverage is! Use it on A3 (and any programs you write) JavaHyperText entry: We demo it. code coverage

What Makes a Good Algorithm? 4 Suppose you have two possible algorithms that do the same thing; which is better? What do we mean by better? Faster? Less space? Simpler? Easier to code? Easier to maintain? Required for homework? FIRST, Aim for simplicity, ease of understanding, correctness. SECOND, Worry about efficiency only when it is needed. How do we measure speed of an algorithm?

Basic Step: one constant time operation 5 Constant time operation: its time doesn t depend on the size or length of anything. Always roughly the same. Time is bounded above by some number Basic step: Input/output of a number Access value of primitive-type variable, array element, or object field assign to variable, array element, or object field *** do one arithmetic or logical operation method call (not counting arg evaluation and execution of method body)

Counting Steps 6 // Store sum of 1..n in sum sum= 0; // inv: sum = sum of 1..(k-1) for (int k= 1; k <= n; k= k+1){ sum= sum + k; Statement: # times done sum= 0; 1 k= 1; 1 k <= n n+1 k= k+1; n sum= sum + k; n Total steps: 3n + 3 All basic steps take time 1. There are n loop iterations. Therefore, takes time proportional to n. 350 300 250 200 150 100 50 0 Linear algorithm in n 0 20 40 60 80 100

Not all operations are basic steps 7 // Store n copies of c in s s= ""; // inv: s contains k-1 copies of c for (int k= 1; k <= n; k= k+1){ s= s + 'c'; Statement: # times done s= ""; 1 k= 1; 1 k <= n n+1 k= k+1; n s= s + 'c'; n Total steps: 3n + 3 Catenation is not a basic step. For each k, catenation creates and fills k array elements.

String Catenation 8 s= s + c ; is NOT constant time. It takes time proportional to 1 + length of s String@00 b char[] String s String@90 b char[] String char[]@02 0 d 1 x char[] char[]@018 0 d 1 x 2 c char[]

Not all operations are basic steps 9 // Store n copies of c in s s= ""; // inv: s contains k-1 copies of c for (int k= 1; k <= n; k= k+1){ s= s + 'c'; Statement: # times # times # steps done s= ""; 1 1 1 k= 1; 1 1 1 k <= n n+1 n+11 k= k+1; n n 1 s= s + 'c'; n n k Total steps: n*(n-1)/2 3n + + 3 2n + 3 Catenation is not a basic step. For each k, catenation creates and fills k array elements. 350 300 250 200 150 100 50 0 Quadratic algorithm in n 0 20 40 60 80 100

Linear versus quadractic 10 // Store sum of 1..n in sum sum= 0; // inv: sum = sum of 1..(k-1) for (int k= 1; k <= n; k= k+1) sum= sum + n Linear algorithm // Store n copies of c in s s= ; // inv: s contains k-1 copies of c for (int k= 1; k = n; k= k+1) s= s + c ; Quadratic algorithm In comparing the runtimes of these algorithms, the exact number of basic steps is not important. What s important is that One is linear in n takes time proportional to n One is quadratic in n takes time proportional to n 2

Looking at execution speed 11 Number of operations executed 2n+2, n+2, n are all linear in n, proportional to n n*n ops 2n + 2 ops n + 2 ops n ops Constant time 0 1 2 3 size n of the array

What do we want from a definition of runtime complexity? 12 Number of operations executed 0 1 2 3 n*n ops 2+n ops 5 ops size n of problem 1. Distinguish among cases for large n, not small n 2. Distinguish among important cases, like n*n basic operations n basic operations log n basic operations 5 basic operations 3. Don t distinguish among trivially different cases. 5 or 50 operations n, n+2, or 4n operations

"Big O" Notation 13 Formal definition: f(n) is O(g(n)) if there exist constants c > 0 and N 0 such that for all n N, f(n) c g(n) c g(n) f(n) Get out far enough (for n N) f(n) is at most c g(n) Intuitively, f(n) is O(g(n)) means that f(n) grows like g(n) or slower N

Prove that (2n 2 + n) is O(n 2 ) 14 Formal definition: f(n) is O(g(n)) if there exist constants c > 0 and N 0 such that for all n N, f(n) c g(n) Example: Prove that (2n 2 + n) is O(n 2 ) Methodology: Start with f(n) and slowly transform into c g(n): Use = and <= and < steps At appropriate point, can choose N to help calculation At appropriate point, can choose c to help calculation

Prove that (2n 2 + n) is O(n 2 ) 15 Formal definition: f(n) is O(g(n)) if there exist constants c > 0 and N 0 such that for all n N, f(n) c g(n) Example: Prove that (2n 2 + n) is O(n 2 ) f(n) = <definition of f(n)> 2n 2 + n <= <for n 1, n n 2 > 2n 2 + n 2 = <arith> 3*n 2 = <definition of g(n) = n 2 > 3*g(n) Transform f(n) into c g(n): Use =, <=, < steps Choose N to help calc. Choose c to help calc Choose N = 1 and c = 3

Prove that 100 n + log n is O(n) 16 Formal definition: f(n) is O(g(n)) if there exist constants c > 0 and N 0 such that for all n N, f(n) c g(n) f(n) = <put in what f(n) is> 100 n + log n <= <We know log n n for n 1> 100 n + n = <arith> 101 n = <g(n) = n> 101 g(n) Choose N = 1 and c = 101

O( ) Examples 17 Let f(n) = 3n 2 + 6n 7 f(n) is O(n 2 ) f(n) is O(n 3 ) f(n) is O(n 4 ) p(n) = 4 n log n + 34 n 89 p(n) is O(n log n) p(n) is O(n 2 ) h(n) = 20 2 n + 40n h(n) is O(2 n ) a(n) = 34 a(n) is O(1) Only the leading term (the term that grows most rapidly) matters If it s O(n 2 ), it s also O(n 3 ) etc! However, we always use the smallest one

Do NOT say or write f(n) = O(g(n)) 18 Formal definition: f(n) is O(g(n)) if there exist constants c > 0 and N 0 such that for all n N, f(n) c g(n) f(n) = O(g(n)) is simply WRONG. Mathematically, it is a disaster. You see it sometimes, even in textbooks. Don t read such things. Here s an example to show what happens when we use = this way. We know that n+2 is O(n) and n+3 is O(n). Suppose we use = n+2 = O(n) n+3 = O(n) But then, by transitivity of equality, we have n+2 = n+3. We have proved something that is false. Not good.

Problem-size examples 19 Suppose a computer can execute 1000 operations per second; how large a problem can we solve? operations 1 second 1 minute 1 hour n 1000 60,000 3,600,000 n log n 140 4893 200,000 n 2 31 244 1897 3n 2 18 144 1096 n 3 10 39 153 2 n 9 15 21

Commonly Seen Time Bounds 20 O(1) constant excellent O(log n) logarithmic excellent O(n) linear good O(n log n) n log n pretty good O(n 2 ) quadratic maybe OK O(n 3 ) cubic maybe OK O(2 n ) exponential too slow

Search for v in b[0..] 21 Q: v is in array b Store in i the index of the first occurrence of v in b: R: v is not in b[0..i-1] and b[i] = v. Methodology: 1. Define pre and post conditions. 2. Draw the invariant as a combination of pre and post. 3. Develop loop using 4 loopy questions. Practice doing this!

Search for v in b[0..] 22 Q: v is in array b Store in i the index of the first occurrence of v in b: R: v is not in b[0..i-1] and b[i] = v. pre:b post: b inv: b 0 b.length v in here 0 i b.length " "? 0 i b.length " v in here Methodology: 1. Define pre and post conditions. 2. Draw the invariant as a combination of pre and post. 3. Develop loop using 4 loopy questions. Practice doing this!

The Four Loopy Questions 23 Does it start right? Is {Q init {P true? Does it continue right? Is {P && B S {P true? Does it end right? Is P &&!B => R true? Will it get to the end? Does it make progress toward termination?

24 pre:b post: b Q: v is in array b Search for v in b[0..] Store in i the index of the first occurrence of v in b: R: v is not in b[0..i-1] and b[i] = v. 0 b.length v in here 0 i b.length " "? i= 0; while ( b[i]!= v) { i= i+1; inv: b 0 i b.length " v in here Linear algorithm: O(b.length) Each iteration takes constant time. Worst case: b.length iterations

Binary search for v in sorted b[0..] 25 // b is sorted. Store in i a value to truthify R: // b[0..i] <= v < b[i+1..] pre:b post: b inv: b 0 b.length sorted 0 i b.length " > " 0 i k b.length "? > " b is sorted. We know that. To avoid clutter, don t write in it invariant Methodology: 1. Define pre and post conditions. 2. Draw the invariant as a combination of pre and post. 3. Develop loop using 4 loopy questions. Practice doing this!

Binary search for v in sorted b[0..] 26 // b is sorted. Store in i a value to truthify R: // b[0..i] <= v < b[i+1..] pre:b post: b inv: b 0 b.length sorted 0 i b.length " > " 0 i k b.length "? > " 0 i e k " " > " i= -1; k= b.length; while ( i+1< k) { int e=(i+k)/2; // -1 i < e < k b.length if (b[e] <= v) i= e; else k= e;

Binary search for v in sorted b[0..] 27 // b is sorted. Store in i a value to truthify R: // b[0..i] <= v < b[i+1..] pre:b post: b inv: b 0 b.length sorted 0 i b.length " > " 0 i k b.length "? > " Logarithmic: O(log(b.length)) i= -1; k= b.length; while ( i+1< k) { int e=(i+k)/2; // -1 e < k b.length if (b[e] <= v) i= e; else k= e; Each iteration takes constant time. Worst case: log(b.length) iterations

Binary search for v in sorted b[0..] 28 // b is sorted. Store in i a value to truthify R: // b[0..i] <= v < b[i+1..] This algorithm is better than binary i= -1; searches that stop when v is found. k= b.length; while ( i+1< k) { 1. Gives good info when v not in b. int e=(i+k)/2; 2. Works when b is empty. // -1 e < k b.length 3. Finds first occurrence of v, not if (b[e] <= v) i= e; arbitrary one. else k= e; 4. Correctness, including making progress, easily seen using invariant Each iteration takes constant time. Logarithmic: O(log(b.length)) Worst case: log(b.length) iterations

29 Dutch National Flag Algorithm

Dutch National Flag Algorithm Dutch national flag. Swap b[0..n-1] to put the reds first, then the whites, then the blues. That is, given precondition Q, swap values of b[0.n-1] to truthify postcondition R: Q: b R: b P1: b P2: b 0 n? 0 n reds whites blues 0 n reds whites blues? 0 n reds whites? blues Suppose we use invariant P1. What does the repetend do? 2 swaps to get a red in place

Dutch National Flag Algorithm Dutch national flag. Swap b[0..n-1] to put the reds first, then the whites, then the blues. That is, given precondition Q, swap values of b[0.n-1] to truthify postcondition R: Q: b R: b P1: b P2: b 0 n? 0 n reds whites blues 0 n reds whites blues? 0 n reds whites? blues Suppose we use invariant P2. What does the repetend do? At most one swap per iteration Compare algorithms without writing code!

Dutch National Flag Algorithm: invariant P1 Q: b R: b 0 n? 0 n reds whites blues 0 h k p n P1: b reds whites blues? h= 0; k= h; p= k; while ( p!= n ) { if (b[p] blue) p= p+1; else if (b[p] white) { swap b[p], b[k]; p= p+1; k= k+1; else { // b[p] red swap b[p], b[h]; swap b[p], b[k]; p= p+1; h=h+1; k= k+1;

Dutch National Flag Algorithm: invariant P2 Q: b R: b 0 n? 0 n reds whites blues 0 h k p n P2: b reds whites? blues h= 0; k= h; p= n; while ( k!= p ) { if (b[k] white) k= k+1; else if (b[k] blue) { p= p-1; swap b[k], b[p]; else { // b[k] is red swap b[k], b[h]; h= h+1; k= k+1; 33

Asymptotically, which algorithm is faster? 34 Invariant 1 Invariant 2 0 h k p n reds whites blues? h= 0; k= h; p= k; while ( p!= n ) { if (b[p] blue) p= p+1; else if (b[p] white) { swap b[p], b[k]; p= p+1; k= k+1; else { // b[p] red swap b[p], b[h]; swap b[p], b[k]; p= p+1; h=h+1; k= k+1; 0 h k p n reds whites? blues h= 0; k= h; p= n; while ( k!= p ) { if (b[k] white) k= k+1; else if (b[k] blue) { p= p-1; swap b[k], b[p]; else { // b[k] is red swap b[k], b[h]; h= h+1; k= k+1;

Asymptotically, which algorithm is faster? 35 Invariant 1 Invariant 2 0 h k p n reds whites blues? 0 h k p n reds whites? blues h= 0; k= h; p= k; while ( p!= n ) { might use 2 swaps per iteration if (b[p] blue) p= p+1; else if (b[p] white) { swap b[p], b[k]; p= p+1; k= k+1; else { // b[p] red swap b[p], b[h]; swap b[p], b[k]; p= p+1; h=h+1; k= k+1; h= 0; k= h; p= n; while ( k!= p ) { uses at most 1 swap per iteration if (b[k] white) k= k+1; else if (b[k] blue) { p= p-1; swap b[k], b[p]; else { // b[k] is red swap b[k], b[h]; h= h+1; k= k+1; These two algorithms have the same asymptotic running time (both are O(n))