Agenda. We ve discussed

Similar documents
Growth of Functions (CLRS 2.3,3)

CS473 - Algorithms I

Computational Complexity

3.1 Asymptotic notation

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

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

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

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

Data Structures and Algorithms CSE 465

Compare the growth rate of functions

Asymptotic Analysis 1

Introduction to Algorithms and Asymptotic analysis

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

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

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

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

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

Time complexity analysis

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

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

CSC Design and Analysis of Algorithms. Lecture 1

Big O (Asymptotic Upper Bound)

Data Structures and Algorithms. Asymptotic notation

data structures and algorithms lecture 2

Cpt S 223. School of EECS, WSU

CS Non-recursive and Recursive Algorithm Analysis

Introduction to Algorithms: Asymptotic Notation

COMP Analysis of Algorithms & Data Structures

Foundations II: Data Structures and Algorithms

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

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

EECS 477: Introduction to algorithms. Lecture 5

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Asymptotic Analysis Cont'd

CS 380 ALGORITHM DESIGN AND ANALYSIS

COMP Analysis of Algorithms & Data Structures

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

CS173 Running Time and Big-O. Tandy Warnow

CSE 531 Homework 1 Solution. Jiayi Xian

Lecture 2: Asymptotic Notation CSCI Algorithms I

The Time Complexity of an Algorithm

The Time Complexity of an Algorithm

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

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

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

Module 1: Analyzing the Efficiency of Algorithms

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

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

COMP 9024, Class notes, 11s2, Class 1

CS F-01 Algorithm Analysis 1

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

Problem Set 1 Solutions

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

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

Big-O Notation and Complexity Analysis

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

CS 4407 Algorithms Lecture 2: Growth Functions

The Growth of Functions and Big-O Notation

Analysis of Algorithms

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

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Ch01. Analysis of Algorithms

Introduction to Algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

COMP 382: Reasoning about algorithms

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Analysis of Multithreaded Algorithms

Module 1: Analyzing the Efficiency of Algorithms

Design and Analysis of Algorithms

Analysis of Algorithms

Divide and Conquer. Arash Rafiey. 27 October, 2016

1 Substitution method

COMPUTER ALGORITHMS. Athasit Surarerks.

How many hours would you estimate that you spent on this assignment?

Comparison of functions. Comparison of functions. Proof for reflexivity. Proof for transitivity. Reflexivity:

Algorithms Design & Analysis. Analysis of Algorithm

Lecture 2: Randomized Algorithms

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

Electrical & Computer Engineering University of Waterloo Canada February 26, 2007

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

Analysis of Algorithms

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

Homework Assignment 1 Solutions

Introduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014

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

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

Data Structures and Algorithms CMPSC 465

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Review Of Topics. Review: Induction

Introduction to Computer Science Lecture 5: Algorithms

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

P, NP, NP-Complete, and NPhard

Written Homework #1: Analysis of Algorithms

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Review for Midterm 1. Andreas Klappenecker

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Mid-term Exam Answers and Final Exam Study Guide CIS 675 Summer 2010

Transcription:

Agenda We ve discussed Now Next C++ basics Some built-in data structures and their applications: stack, map, vector, array The Fibonacci example showing the importance of good algorithms and asymptotic analysis Growth of functions Asymptotic notations Scare some people off Recurrence relations & a way to solve them Binary search and some sorting algorithms to illustrate c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 1 / 22

Some conventions lg n = log 2 n log n = log 10 n ln n = log e n c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 3 / 22

Growth of functions Consider an Intel Core i7 Extreme Edition 980X (Hex core), 3.33GHz, top speed < 150 10 9 instructions per second (IPS). For simplicity, say it s 10 9 IPS. 10 20 30 40 50 1000 lg lg n 1.7 ns 2.17 ns 2.29 ns 2.4 ns 2.49 ns 3.3 ns lg n 3.3 ns 4.3 ns 4.9 ns 5.3 ns 5.6 ns 9.9 ns n 10 ns 20 ns 3 ns 4 ns 5 ns 1 µs n 2 0.1 µs 0.4 µs 0.9 µs 1.6 µs 2.5 µs 1 ms n 3 1 µs 8 µs 27 µs 64 µs 125 µs 1 sec n 5 0.1 ms 3.2 ms 24.3 ms 0.1 sec 0.3 sec 277 h 2 n 1 µs 1 ms 1 s 18.3 m 312 h 3.4 10 282 Cent. 3 n 59 µs 3.5 s 57.2 h 386 y 227644 c 4.2 10 458 Cent. 1.6 100 ns is approx. 82 centuries (Recall fib1()). lg 10 10 = 33, lg lg 10 10 = 4.9 c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 4 / 22

Some other large numbers The age of the universe 13 G-Years = 13 10 7 centuries. Number of seconds since big-bang 10 18. 4 10 78 Number of atoms is the universe 6 10 79. The probability that a monkey can compose Hamlet is 1 10 60 so what s the philosophical implication of this? Robert Wilensky, speech at a 1996 conference We ve heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true. c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 5 / 22

Dominating Terms When n is sufficiently large, order the following functions: f 1 (n) = 2000n 2 + 1, 000, 000n + 3 f 2 (n) = 100n 2 f 3 (n) = n 5 + 10 7 n f 4 (n) = 2 n + n 10,000 f 5 (n) = 2 n f 6 (n) = 3n 10 6 (Only need to look at the dominating term) c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 6 / 22

Our functions We will only look at functions of the type f : N R + as they are used for time and space complexity estimation. c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 8 / 22

Behind comparing functions Mathematically, f (n) g(n) for sufficiently large n means f (n) lim n g(n) =. We also say f (n) is asymptotically larger than g(n). They are comparable (or of the same order) if f (n) lim n g(n) = c > 0 and f (n) is asymptotically smaller than g(n) when f (n) lim n g(n) = 0. Question Are there f (n) and g(n) not falling into one of the above cases? c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 9 / 22

Asymptotic notations f (n) = O(g(n)) iff c > 0, n 0 > 0 : f (n) cg(n), for n n 0 f (n) = Ω(g(n)) iff c > 0, n 0 > 0 : f (n) cg(n), for n n 0 f (n) = Θ(g(n)) iff f (n) = O(g(n)) & f (n) = Ω(g(n)) f (n) = o(g(n)) iff c > 0, n 0 > 0 : f (n) cg(n), for n n 0 f (n) = ω(g(n)) iff c > 0, n 0 > 0 : f (n) cg(n), for n n 0 Note: we shall be concerned only with functions f of the form f : N + R +, unless specified otherwise. f (n) = x(g(n)) isn t mathematically correct; f (n) x(g(n)) is, but not commonly used. c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 10 / 22

An illustration of big-o c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 11 / 22

An illustration of big-ω c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 12 / 22

An illustration of Θ c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 13 / 22

Some examples a(n) = n b(n) = n 5 + 10 7 n c(n) = (1.3) n d(n) = (lg n) 100 e(n) = 3n 10 6 f (n) = 3180 g(n) = n 0.0000001 h(n) = (lg n) lg n c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 14 / 22

A few properties f (n) = o(g(n)) f (n) = O(g(n)) & f (n) Θ(g(n)) (1) f (n) = ω(g(n)) f (n) = Ω(g(n)) & f (n) Θ(g(n)) (2) f (n) = O(g(n)) g(n) = Ω(f (n)) (3) lim n lim n f (n) = Θ(g(n)) g(n) = Θ(f (n)) (4) f (n) = + g(n) f (n) = ω(g(n)) (5) f (n) = c > 0 g(n) f (n) = Θ(g(n)) (6) f (n) lim = 0 n g(n) f (n) = o(g(n)) (7) Remember: we only consider functions from N + R +. c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 15 / 22

A reminder: L Hôpital s rule if Examples: lim f (n) and lim n f (n) lim n g(n) = lim f (n) n g (n) g(n) are both 0 or both ± n lg n lim = 0 (8) n n (lg n) lg n lim =? (9) n n c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 16 / 22

Stirling s approximation For all n 1, n! = ( n ) n 2πn e α n, e where It then follows that 1 12n + 1 < α n < 1 12n. n! = ( n ) n ( ( )) 1 2πn 1 + Θ. e n The last formula is often referred to as the Stirling s approximation c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 17 / 22

More examples a(n) = lg n! b(n) = n 5 + 10 7 n c(n) = 2 lg n d(n) = (lg n) 100 e(n) = 3 n f (n) = lg lg n (lg n) g(n) = 2 n0.001 h(n) = (lg n) lg n i(n) = n! c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 18 / 22

Special functions Some functions cannot be compared, e.g. n 1+sin(n π 2 ) and n. lg n = min{i 0 : lg (i) n 1}, where for any function f : N + R +, { f (i) n if i = 0 (n) = f (f (i 1) (n)) if i > 0 Intuitively, compare How about rigorously? lg n vs lg n lg n vs (lg n) ɛ, ɛ > 0 2 n vs n n lg (lg n) vs lg(lg n) c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 19 / 22

Asymptotic notations in equations 5n 3 + 6n 2 + 3 = 5n 3 + Θ(n 2 ) means the LHS is equal to 5n 3 plus some function which is Θ(n 2 ). o(n 6 ) + O(n 5 ) = o(n 6 ) means for any f (n) = o(n 6 ), g(n) = O(n 5 ), the function h(n) = f (n) + g(n) is equal to some function which is o(n 6 ). Be very careful!! O(n 5 ) + Ω(n 2 ) O(n 5 ) + Ω(n 2 )? = Ω(n 2 )? = O(n 5 ) c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 20 / 22

Tight and not tight n log n = O(n 2 ) is not tight n 2 = O(n 2 ) is tight c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 21 / 22

When comparing functions asymptotically Determine the dominating term Use intuition first Transform intuition into rigorous proof. c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++ 22 / 22