ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Similar documents
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Lecture 25. Designing Sequence Algorithms

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Asymptotic Running Time of Algorithms

CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

Data Structures and Algorithms Chapter 2

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

Algorithms and Programming I. Lecture#1 Spring 2015

CS 1110: Introduction to Computing Using Python Sequence Algorithms

Ch 01. Analysis of Algorithms

Analysis of Algorithms

Ch01. Analysis of Algorithms

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

CS1210 Lecture 23 March 8, 2019

csci 210: Data Structures Program Analysis

CS1110. Lecture 20: Sequence algorithms. Announcements

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Asymptotic Analysis 1

csci 210: Data Structures Program Analysis

CSC236 Week 3. Larry Zhang

Solutions. Prelim 2[Solutions]

Topic 17. Analysis of Algorithms

Time Analysis of Sorting and Searching Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

Module 1: Analyzing the Efficiency of Algorithms

Data Structures and Algorithms. Asymptotic notation

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

COMP 355 Advanced Algorithms

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Great Theoretical Ideas in Computer Science. Lecture 7: Introduction to Computational Complexity

Data Structures and Algorithms

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

with the size of the input in the limit, as the size of the misused.

CS Data Structures and Algorithm Analysis

Lecture 3: Big-O and Big-Θ

CSC2100B Data Structures Analysis

Computational Complexity

Central Algorithmic Techniques. Iterative Algorithms

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Lecture 26. Sequence Algorithms (Continued)

1 Divide and Conquer (September 3)

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

Introduction. An Introduction to Algorithms and Data Structures

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

COMP 250 Fall Midterm examination

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

What happens to the value of the expression x + y every time we execute this loop? while x>0 do ( y := y+z ; x := x:= x z )

Loop Convergence. CS 536: Science of Programming, Fall 2018

Great Theoretical Ideas in Computer Science. Lecture 9: Introduction to Computational Complexity

Data Structures. Outline. Introduction. Andres Mendez-Vazquez. December 3, Data Manipulation Examples

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

Module 1: Analyzing the Efficiency of Algorithms

Advanced Algorithmics (6EAP)

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

CS361 Homework #3 Solutions

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

An analogy from Calculus: limits

Running Time Evaluation

Quick Sort Notes , Spring 2010

CS 4407 Algorithms Lecture 2: Growth Functions

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

Principles of Algorithm Analysis

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Asymptotic Analysis Cont'd

Runtime Complexity. CS 331: Data Structures and Algorithms

3.1 Asymptotic notation

COMP Analysis of Algorithms & Data Structures

Lecture 12: Algorithm Analysis

What is Performance Analysis?

Proof Calculus for Partial Correctness

Growth of Functions (CLRS 2.3,3)

EECS 477: Introduction to algorithms. Lecture 5

Analysis of Algorithms

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

COMP Analysis of Algorithms & Data Structures

CSC236 Week 4. Larry Zhang

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Minilecture 2: Sorting Algorithms

Lecture 10: Big-Oh. Doina Precup With many thanks to Prakash Panagaden and Mathieu Blanchette. January 27, 2014

Written Homework #1: Analysis of Algorithms

Lecture 3. Big-O notation, more recurrences!!

This chapter covers asymptotic analysis of function growth and big-o notation.

Transcription:

Quotes about loops O! Thou hast damnable iteration and art, indeed, able to corrupt a saint. Shakespeare, Henry IV, Pt I, 1 ii Use not vain repetition, as the heathen do. Matthew V, 48 Your if is the only peacemaker; much virtue in if. Shakespeare, As You Like It. ASYMPTOTIC COMPLEXITY SEARCHING/SORTING Lecture 10 CS2110 Spring 2017

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

Basic Step: one constant time operation 3 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)

4 Basic Step: one constant time operation. Example of counting basic steps in a loop // 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 All operations are basic steps, take constant time. There are n loop iterations. Therefore, takes time proportional to n. Linear algorithm in n Statement/ Number of expression times done sum= 0; 1 k= 1; 1 k <= n n+1 k= k+1; n sum= sum + n; n Total basic steps executed 3n + 3

Basic Step: one constant time operation 5 // 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 All operations are basic steps, take constant time. There are n loop iterations. Therefore, takes time proportional to n. Linear algorithm in n // 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 ; All operations are basic steps, except for catenation. For each k, catenation creates and fills k array elements. Total number created: 1 + 2 + 3 + + n, or n(n+1)/2 = n*n/2 + 1/2 Quadratic algorithm in n

Linear versus quadractic 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 + 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

Linear search for v in b[0..] 7 pre: b post: b 0 b.length v in here 0 h b.length v not here v? Methodology: 1. Draw the invariant as a combination of pre and post 2. Develop loop using 4 loopy questions. Practice doing this! Once you get the knack of doing this, you will know these algorithms not because you memorize code but because you can develop them at will from the pre- and post-conditions

Linear search for v in b[0..] 8 pre: b post: b inv: b 0 b.length v in here 0 h b.length v not here v? 0 h b.length v not here v in here h= 0; while ( b[h]!= v ) { h= h+1; } Each iteration takes constant time. In the worst case, requires b.length iterations. Worst case time: proportional to b.length. Average (expected) time: A little statistics tells you b.length/2 iterations, still proportional to b.length

Linear search as in problem set: b is sorted 9 pre: b post: b inv: b 0 b.length? 0 h b.length <= v > v 0 h t b.length <= v? > v h= 1; t= b.length; while ( h+1!= t ) { if (b[h+1] <= v) h= h+1; else t= h+1; } b[0] > v? one iteration. b[b.length-1] v? b.length iterations Worst case time: proportional to size of b

b is sorted ---use a binary search? 10 pre: b 0 b.length? post: b 0 h b.length v > v inv: b 0 h t b.length v? > v Since b is sorted, can cut? segment in half. As in a dictionary search

Binary search for v in b: b is sorted 11 pre: b 0 b.length? post: b 0 h b.length <= v > v inv: b 0 h t b.length <= v? > v h= 1; t= b.length; while (h!= t 1) { int e= (h + t) / 2; // h < e < t if (b[e] <= v) h= e; } else t= e; inv: b b b 0 h e t <= v?? > v 0 h e t <= v v v? > v 0 h e t <= v? >v > v > v

Binary search: an O(log n) algorithm 12 inv: b 0 h t b.length = n <= v? > v h= 1; t= b.length; while (h!= t 1) { int e= (h+t)/2; if (b[e] <= v) h= e; else t= e; } inv: b Each iteration cuts the size of the? segment in half. 0 h e t <= v?? > v n = 2**k? About k iterations Time taken is proportional to k, or log n. A logarithmic algorithm

Binary search for v in b: b is sorted 13 pre: b 0 b.length? post: b 0 h b.length <= v > v inv: b 0 h t b.length <= v? > v h= 1; t= b.length; while (h!= t 1) { int e= (h + t) / 2; // h < e < t if (b[e] <= v) h= e; else t= e; } This algorithm is better than binary searches that stop when v is found. 1. Gives good info when v not in b. 2. Works when b is empty. 3. Finds rightmost occurrence of v, not arbitrary one. 4. Correctness, including making progress, easily seen using invariant

Looking at execution speed Process an array of size n 14 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? 15 Number of operations executed n*n ops 2+n ops 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 0 1 2 3 5 ops size n of problem 3. Don t distinguish among trivially different cases. 5 or 50 operations n, n+2, or 4n operations

Definition of O( ) 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) c g(n) f(n) Graphical view N Get out far enough (for n N) f(n) is at most c g(n)

What do we want from a definition of runtime complexity? 17 Number of operations executed n*n ops 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) 0 1 2 3 2+n ops 5 ops size n of problem Roughly, f(n) is O(g(n)) means that f(n) grows like g(n) or slower, to within a constant factor

Prove that (n 2 + n) is O(n 2 ) 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) Example: Prove that (n 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 (n 2 + n) is O(n 2 ) 19 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 (n 2 + n) is O(n 2 ) f(n) = <definition of f(n)> n 2 + n <= <for n 1, n n 2 > n 2 + n 2 = <arith> 2*n 2 = <definition of g(n) = n 2 > 2*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 = 2

Prove that 100 n + log n is O(n) 20 Formal definition: f(n) is O(g(n)) if there exist constants c and N 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

Do NOT say or write f(n) = O(g(n)) 21 Formal definition: f(n) is O(g(n)) if there exist constants c and N 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.

O( ) Examples 22 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

Commonly Seen Time Bounds 23 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 OK O(n 3 ) cubic maybe OK O(2 n ) exponential too slow

Problem-size examples 24 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

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] 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 (values in 0..n-1 unknown) Four possible simple invariants. We show two. 25

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? Don t need long mnemonic names for these variables! The invariant gives you all the info you need about them! 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 } //REQUIRES // TWO SWAPS // you can finish it 26

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 Use inv P1: perhaps 2 swaps per iteration. Use inv P2: at most 1 swap per iteration. h= 0; k= h; p= n; while ( k!= p ) { } if (b[k] white) k= k+1; else if (b[p] 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; 27