Time complexity analysis

Similar documents
Growth of Functions (CLRS 2.3,3)

Computational Complexity

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

3.1 Asymptotic notation

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

Lecture 2: Asymptotic Notation CSCI Algorithms I

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

CS473 - Algorithms I

Data Structures and Algorithms. Asymptotic notation

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Big-O Notation and Complexity Analysis

P, NP, NP-Complete, and NPhard

The Time Complexity of an Algorithm

CS 4407 Algorithms Lecture 2: Growth Functions

COMP 382: Reasoning about algorithms

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Algorithms, CSE, OSU. Introduction, complexity of algorithms, asymptotic growth of functions. Instructor: Anastasios Sidiropoulos

The Time Complexity of an Algorithm

EECS 477: Introduction to algorithms. Lecture 5

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithms

Big-Oh notation and P vs NP

CS 380 ALGORITHM DESIGN AND ANALYSIS

Agenda. We ve discussed

Asymptotic Analysis 1

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Data Structures and Algorithms Running time and growth functions January 18, 2018

Asymptotic Analysis Cont'd

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

Data Structures and Algorithms CSE 465

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

CS173 Running Time and Big-O. Tandy Warnow

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

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Review Of Topics. Review: Induction

data structures and algorithms lecture 2

COMP Analysis of Algorithms & Data Structures

Foundations II: Data Structures and Algorithms

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

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

COMP Analysis of Algorithms & Data Structures

Module 1: Analyzing the Efficiency of Algorithms

Algorithms Design & Analysis. Analysis of Algorithm

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

Big O (Asymptotic Upper Bound)

Analysis of Algorithms

Introduction to Algorithms and Asymptotic analysis

COMP 9024, Class notes, 11s2, Class 1

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

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Module 1: Analyzing the Efficiency of Algorithms

CS Non-recursive and Recursive Algorithm Analysis

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Advanced Algorithmics (6EAP)

Asymptotic Analysis of Algorithms. Chapter 4

Ch01. Analysis of Algorithms

Introduction to Computer Science Lecture 5: Algorithms

Running Time Evaluation

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

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

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

Data Structures and Algorithms Chapter 2

Cpt S 223. School of EECS, WSU

Analysis of Algorithms

Recurrence Relations

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

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

CSC Design and Analysis of Algorithms. Lecture 1

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

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Spring 2014

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

Principles of Algorithm Analysis

2. ALGORITHM ANALYSIS

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

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

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

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

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Omega notation. Transitivity etc.

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

Data Structures and Algorithms

COMPUTER ALGORITHMS. Athasit Surarerks.

CISC 235: Topic 1. Complexity of Iterative Algorithms

Theory of Computation

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Winter 2013

CS Data Structures and Algorithm Analysis

Introduction to Algorithms: Asymptotic Notation

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

Defining Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 421: Intro Algorithms. Summer 2007 Larry Ruzzo

Algorithms and Their Complexity

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Kurt Schmidt. June 5, 2017

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Reading 10 : Asymptotic Analysis

Homework Assignment 1 Solutions

Divide and Conquer. Arash Rafiey. 27 October, 2016

Ch 01. Analysis of Algorithms

Transcription:

Time complexity analysis Let s start with selection sort C. Seshadhri University of California, Santa Cruz sesh@ucsc.edu March 30, 2016 C. Seshadhri (UCSC) CMPS101 1 / 40

Sorting Sorting Input An array e.g., [1, 5, 6, 7, 3, 2, 1] Output Sorted array [1, 1, 2, 3, 5, 6, 7] C. Seshadhri (UCSC) CMPS101 2 / 40

Sorting Formally Input Output An array e.g., [a 1, a 2, a 3,..., a n ] A permuted version of the array [a 1, a 2, a 3,..., a n] such that a 1 a 2 a 3... a n C. Seshadhri (UCSC) CMPS101 3 / 40

Sorting Comes up everywhere Give me webpages on Data Structures, in decreasing order of relevance Give me a list of coffee shops, in decreasing order of ratings Give me a list of grocery stores, in increasing order of distance C. Seshadhri (UCSC) CMPS101 4 / 40

Sorting Sorting with keys Input Output An array of key-value pairs e.g., [(k 1, v 1 ), (k 2, v 2 ), (k 3, v 3 ),..., (k n, v n )] A permuted version of the array [(k 1, v 1 ), (k 2, v 2 ), (k 3, v 3 ),..., (k n, v n)] such that v 1 v 2 v 3... v n C. Seshadhri (UCSC) CMPS101 5 / 40

Sorting What is an Algorithm? A sequence of well defined computational steps that takes some input transforms it into an output (with a certain property) Alternatively An algorithm describes a specific computational procedure for achieving a specified input/output relationship C. Seshadhri (UCSC) CMPS101 6 / 40

Selection Sort (Let length of array A be n.) SELECTION-SORT(A) 1 For i = 1 to n: 1 Find minimum element b in A[i... n]. 2 Set b to be ith element in sorted order C. Seshadhri (UCSC) CMPS101 7 / 40

Selection Sort (Let length of array A be n.) Selection-Sort(A) 1 for i = 1 to n 2 min = A[i] / Initialize min 3 minind = i / Initialize min index 4 for j = i to n 5 if A[j] < min // Aha! Min needs to be changed 6 min = A[j] // So change min and min index 7 minind = j 8 // Swap A[i] with A[minInd] 9 A[minInd] = A[i] 10 A[i] = min C. Seshadhri (UCSC) CMPS101 8 / 40

How many comparisons? Selection-Sort on an array of n items makes at most how many comparisons? (Give the best answer.) (R) n (B) n 1 (G) (n 1)(n 2)/2 (O) n 2 2n C. Seshadhri (UCSC) CMPS101 9 / 40

How many comparisons? Selection-Sort on an array of n items makes at most how many comparisons? (Give the best answer.) (R) n (B) n 1 (G) (n 1)(n 2)/2 (O) n 2 2n C. Seshadhri (UCSC) CMPS101 10 / 40

How long does Selection-Sort take? It really depends on your machine! How can we mathematically talk about the running time of Selection-Sort? C. Seshadhri (UCSC) CMPS101 11 / 40

Analyzing Selection-Sort Definition The running time of Selection-Sort (or your favorite algorithm) on input A is the number of elementary operations that Selection-Sort takes on A. Beats wall clock time. Challenges: What is elementary? Ok, for sorting, any assignment or comparison counts as one operation. Running time is not one number; it depends heavily on input (especially size). C. Seshadhri (UCSC) CMPS101 12 / 40

Analyzing Selection-Sort Definition The running time of Selection-Sort (or your favorite algorithm) on input A is the number of elementary operations that Selection-Sort takes on A. Beats wall clock time. Challenges: What is elementary? Ok, for sorting, any assignment or comparison counts as one operation. Running time is not one number; it depends heavily on input (especially size). C. Seshadhri (UCSC) CMPS101 12 / 40

Worst-case Analysis Definition The worst-case running time or worst-case time complexity of Selection-Sort (or your favorite algorithm) is the function T (n) where: T (n) is the maximum running time over all inputs of size n. C. Seshadhri (UCSC) CMPS101 13 / 40

Back to Selection Sort (Let length of array A be n.) Selection-Sort(A) 1 for i = 1 to n 2 min = A[i] / Initialize min 3 minind = i / Initialize min index 4 for j = i to n 5 if A[j] < min // Aha! Min needs to be changed 6 min = A[j] // So change min and min index 7 minind = j 8 // Swap A[i] with A[minInd] 9 A[minInd] = A[i] 10 A[i] = min C. Seshadhri (UCSC) CMPS101 14 / 40

Exactly finding T (n) It s a real pain. Once you get the basic idea, it s boring to figure out exactly. C. Seshadhri (UCSC) CMPS101 15 / 40

The solution? C. Seshadhri (UCSC) CMPS101 16 / 40

The solution? C. Seshadhri (UCSC) CMPS101 17 / 40

Basic Idea Focus on the asymptotic growth of T (n) Asymptotic: Fancy way of saying that we will focus on how the function grows as n. C. Seshadhri (UCSC) CMPS101 18 / 40

Oh Notation Given time complexity T (n) and convenient/simple function f (n) we say that T (n) O(f (n)) if there exists constants c and n 0 such that 0 T (n) c f (n) for all n n 0. Notational convention: T (n) = O(f (n)), T (n) is O(f (n)), T (n) is in O(f (n)). We re providing a convenient upper bound for T (n). C. Seshadhri (UCSC) CMPS101 19 / 40

In Pictures C. Seshadhri (UCSC) CMPS101 20 / 40

Is 2n + 30 = O(n)? (R) Yes (B) No C. Seshadhri (UCSC) CMPS101 21 / 40

Example - 1 2n + 30 = O(n) Need to show that there exists c and n 0 such that 0 2n + 30 c n for all n n 0 Let c = 3 and n 0 = 30. C. Seshadhri (UCSC) CMPS101 22 / 40

Is 2n + 30 = O(n 2 )? (R) Yes (B) No C. Seshadhri (UCSC) CMPS101 23 / 40

Example - 2 2n + 30 = O(n 2 ) Need to show that there exists c and n 0 such that 0 2n + 30 c n 2 for all n n 0 Let c = 2 and n 0 = 6. C. Seshadhri (UCSC) CMPS101 24 / 40

Is 5n 3 + 10n = O(n 2 )? (R) Yes (B) No C. Seshadhri (UCSC) CMPS101 25 / 40

Example - 3 Reductio ad absurdum 5n 3 + 10n / O(n 2 ) C. Seshadhri (UCSC) CMPS101 26 / 40

Example - 3 Reductio ad absurdum 5n 3 + 10n / O(n 2 ) C. Seshadhri (UCSC) CMPS101 26 / 40

Example - 3 5n 3 + 10n / O(n 2 ) Proof by contradiction. Suppose c and n 0 existed. 0 5n 3 + 10n c n 2 for all n n 0 0 5n + 10 n c for all n n 0 But as n we have 5n + 10 n. C. Seshadhri (UCSC) CMPS101 27 / 40

Selection Sort (Let length of array A be n.) Selection-Sort(A) 1 for i = 1 to n 2 min = A[i] / Initialize min 3 minind = i / Initialize min index 4 for j = i to n 5 if A[j] < min // Aha! Min needs to be changed 6 min = A[j] // So change min and min index 7 minind = j 8 // Swap A[i] with A[minInd] 9 A[minInd] = A[i] 10 A[i] = min Selection-Sort time complexity is O(n 2 ). C. Seshadhri (UCSC) CMPS101 28 / 40

Omega Notation Given functions T (n) and f (n) we say that T (n) Ω(f (n)) if there exists constants c and n 0 such that 0 cf (n) T (n) for all n n 0. Notational convention: T (n) = Ω(f (n)) C. Seshadhri (UCSC) CMPS101 29 / 40

Theta Notation Given functions T (n) and f (n) we say that T (n) Θ(f (n)) if there exists constants c 1, c 2 and n 0 such that 0 c 1 f (n) T (n) c 2 f (n) for all n n 0. Notational convention: T (n) = Θ(f (n)) C. Seshadhri (UCSC) CMPS101 30 / 40

An Alternate View Point T (n) T (n) = O(f (n)) = lim n f (n) const T (n) T (n) = Ω(f (n)) = lim n f (n) const T (n) T (n) = Θ(f (n)) = const 1 lim n f (n) const 2 C. Seshadhri (UCSC) CMPS101 31 / 40

In Pictures C. Seshadhri (UCSC) CMPS101 32 / 40

Example - 4 5n 2 + 10n = Θ(n 2 ) Need to show that there exists c 1, c 2 and n 0 such that Rewrite as 0 c 1 n 2 5n 2 + 10n c 2 n 2 for all n n 0 0 c 1 5 + 10 n c 2 for all n n 0 But as n we have 5 + 10 n 5. So select c 1 = 4 and c 2 = 6, with n 0 = 10. C. Seshadhri (UCSC) CMPS101 33 / 40

Prove For any two functions f (n) and g(n), we have f (n) = Θ(g(n)) f (n) = O(g(n)) and f (n) = Ω(g(n)). C. Seshadhri (UCSC) CMPS101 34 / 40

The time complexity of Selection-Sort is: (R) Θ(n 2 ) (B) O(n 2 ) but not Θ(n 2 ). (G) Ω(n 2 ) but not Θ(n 2 ). (O) I have no idea what s going on. C. Seshadhri (UCSC) CMPS101 35 / 40

Selection Sort (Let length of array A be n.) Selection-Sort(A) 1 for i = 1 to n 2 min = A[i] / Initialize min 3 minind = i / Initialize min index 4 for j = i to n 5 if A[j] < min // Aha! Min needs to be changed 6 min = A[j] // So change min and min index 7 minind = j 8 // Swap A[i] with A[minInd] 9 A[minInd] = A[i] 10 A[i] = min Selection-Sort time complexity is Θ(n 2 ). C. Seshadhri (UCSC) CMPS101 36 / 40

Little o and ω Notation o Notation O notation is not asymptotically tight. For instance n = O(n) and also n = O(n 2 ). Given functions g(n) and f (n) we say that f (n) o(g(n)) if there exists a constant n 0 such that 0 f (n) cg(n) for all c > 0 and n n 0. Carefully note the order of the qualifiers. Intuitively, f (n) becomes insignificant compared to g(n) f (n) lim n g(n) = 0. C. Seshadhri (UCSC) CMPS101 37 / 40

Little o and ω Notation Example - 5 5n 2 + 10n / o(n 2 ) lim n 5 + 10 n = 5 0 5n 2 + 10n = o(n 3 ) 5 lim n n + 10 = 0 n 2 C. Seshadhri (UCSC) CMPS101 38 / 40

Little o and ω Notation ω Notation Given functions g(n) and f (n) we say that f (n) ω(g(n)) if there exists a constant n 0 such that 0 cg(n) f (n) for all c > 0 and n n 0. Carefully note the order of the qualifiers. Intuitively, f (n) grows much faster as compared to g(n) f (n) lim n g(n) =. C. Seshadhri (UCSC) CMPS101 39 / 40

Little o and ω Notation Questions? C. Seshadhri (UCSC) CMPS101 40 / 40