Analysis of Algorithms

Similar documents
Analysis of Algorithms

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CS Non-recursive and Recursive Algorithm Analysis

CSC Design and Analysis of Algorithms. Lecture 1

Module 1: Analyzing the Efficiency of Algorithms

Data Structures and Algorithms. Asymptotic notation

Running Time Evaluation

Principles of Algorithm Analysis

EECS 477: Introduction to algorithms. Lecture 5

Ch01. Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Growth Functions

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

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

Cpt S 223. School of EECS, WSU

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

Asymptotic Analysis of Algorithms. Chapter 4

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

Computational Complexity

COMP Analysis of Algorithms & Data Structures

Ch 01. Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

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

Introduction to Algorithms

COMP Analysis of Algorithms & Data Structures

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

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

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

COMP 9024, Class notes, 11s2, Class 1

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

The Time Complexity of an Algorithm

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

Analysis of Algorithms

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

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

Asymptotic Analysis 1

COMPUTER ALGORITHMS. Athasit Surarerks.

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

Data Structures and Algorithms

Lecture 2: Asymptotic Notation CSCI Algorithms I

Data Structures and Algorithms Chapter 2

csci 210: Data Structures Program Analysis

3.1 Asymptotic notation

Growth of Functions (CLRS 2.3,3)

The Time Complexity of an Algorithm

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

CS473 - Algorithms I

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

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

csci 210: Data Structures Program Analysis

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

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

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

Asymptotic Analysis Cont'd

What is Performance Analysis?

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

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Topic 17. Analysis of Algorithms

Copyright 2000, Kevin Wayne 1

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

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

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

CS 310 Advanced Data Structures and Algorithms

Runtime Complexity. CS 331: Data Structures and Algorithms

Algorithms and Their Complexity

Written Homework #1: Analysis of Algorithms

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

data structures and algorithms lecture 2

Introduction to Algorithms: Asymptotic Notation

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.

Harvard CS 121 and CSCI E-121 Lecture 20: Polynomial Time

CS1210 Lecture 23 March 8, 2019

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

P, NP, NP-Complete, and NPhard

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Lecture 2: Asymptotic Analysis of Algorithms

Time complexity analysis

CSE 421: Intro Algorithms. 2: Analysis. Winter 2012 Larry Ruzzo

Algorithms Design & Analysis. Analysis of Algorithm

Copyright 2000, Kevin Wayne 1

Introduction to Algorithms and Asymptotic analysis

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

Big O (Asymptotic Upper Bound)

Big-O Notation and Complexity Analysis

CS 380 ALGORITHM DESIGN AND ANALYSIS

COMP 382: Reasoning about algorithms

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

Design and Analysis of Algorithms

CISC 235: Topic 1. Complexity of Iterative Algorithms

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

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

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

CS F-01 Algorithm Analysis 1

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Transcription:

October 1, 2015 Analysis of Algorithms CS 141, Fall 2015 1

Analysis of Algorithms: Issues Correctness/Optimality Running time ( time complexity ) Memory requirements ( space complexity ) Power I/O utilization Ease of implementation 2

Correctness An algorithm is correct if, for every input size, it holts with the correct output.

Analysis of Algorithms Primitive Operations: Low-level computations independent from the programming language can be identified in pseudo-code Examples: calling a method and returning from a method arithmetic operations (e.g. addition) comparing two numbers, etc. By inspecting the pseudo-code, we can count the number of primitive operations executed by an algorithm 4

Input size and basic operation examples Problem Input size measure Basic operation Searching for key in a list of n items Number of items in the list, i.e., n Key comparison Multiplication of two matrices Matrix dimensions or total number of elements Multiplication of two numbers Checking primality of a given integer n size of n = number of digits (in binary representation) Division Typical graph problem #vertices and/or #edges Visiting a vertex or traversing an edge 5

Why Running Time?

7

8

T n = 2 for n 1 T n 1 + T n 2 + 3 for n > 1 T n F n 2 0.694n 9

T n is linear in n!!!

Average Case vs. Worst Case An algorithm may run faster on certain data sets than on others (e.g., for the sorting problem, the input is partially sorted) Finding the average case can be very difficult, so typically algorithms are measured by the worst case time complexity 11

Average Case vs. Worst Case In time-critical application domains (e.g., air traffic control, surgery, IP lookup, ) knowing the worst case time complexity is crucial 5 ms 4 ms 3 ms 2 ms worst-case } average-case? best-case 1 ms A B C D E F G Input (same input size) 12

Worst Case Time-Complexity Definition: The worst case time-complexity of an algorithm A is the asymptotic running time of A as a function of the size of the input, when the input is the one that makes the algorithm slower in the limit How do we measure the running time of an algorithm? 13

Example def imax(a): currentmax = A[0] for i in range(len(a)): if currentmax < A[i]: currentmax = A[i] return currentmax Max iterative Max recursive def rmax(a): if len(a) == 1: return A[0] return max(rmax(a[1:]),a[0]) Time-complexity is O(n)

Asymptotic notation Section 0.3 of the textbook 15

The Big-Oh Notation Definition: Given functions f(n) and g(n), we say that f(n) is O(g(n)) if and only if there are positive constants c and n 0 such that f(n) c g(n) for n n 0 16

The Big-Oh Notation 17

Asymptotic Notation Big - O Theorem

f(n) = 2n+6 g(n) = n Example f(n) =2n + 6 For functions f(n) and g(n) (to the right) there are positive constants c and n 0 such that: f(n) c g(n) for n n 0 c g ( n ) = 4 n g ( n ) = n Note: the picture itself is NOT a proof, but it may help you finding the parameters 3 n 19

Proof f(n)=2n+6 g(n)=n 2n+6 4n??? 2n+6 4n when n 3 So, if we choose c=4, then n 0 =3 satisfies f(n) c g(n) for n n 0 Conclusion: 2n+6 is O(n) 20

Asymptotic Notation Big - O

Asymptotic Notation Note: Even though it is correct to say 7n - 3 is O(n 3 ), a more precise statement is 7n - 3 is O(n), that is, one should make the approximation as tight as possible Simple Rule: Drop lower order terms and constant factors 7n-3 is O(n) 8n 2 log n + 5n 2 + n is O(n 2 log n) 22

Theorem Let a > 0, b > 0, c > 1. Then Asymptotic Notation Big - O Proof: (c) Let d = c 1/b, then d >1, and =, since d > 1 - summation of the geom. sequences, where A = 1 / (d - 1), where B = A b

Special classes of algorithms constant: O(1) logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) cubic: O(n 3 ) polynomial: O(n k ), k 1 exponential: O(a n ), n > 1 Asymptotic Notation 24

Asymptotic Notation Relatives of the Big-Oh Ω(f(n)): Big Omega asymptotic lower bound Θ(f(n)): Big Theta asymptotic tight bound 25

Big Omega Definition: Given two functions f(n) and g(n), we say that f(n) is Ω(g(n)) if and only if there are positive constants c and n 0 such that f(n) c g(n) for n n 0 Property: f(n) is Ω(g(n)) iff g(n) is O(f(n)) 26

Big Theta Definition: Given two functions f(n) and g(n), we say that f(n) is Θ(g(n)) if and only if there are positive constants c 1, c 2 and n 0 such that c 1 g(n) f(n) c 2 g(n) for n n 0 Property: f(n) is Θ(g(n)) if and only if f(n) is O(g(n)) AND f(n) is Ω(g(n)) 27

Summary A O(f(n)) means the algorithm A won t take longer than f(n), give or take a constant multiplier and lower order terms (upper bound) A Θ(f(n)) means the algorithm A will take as long as f(n), give or take a constant multiplier and lower order terms (tight bound) A Ω(f(n)) means the algorithm A will take longer than f(n), give or take a constant multiplier and lower order terms (lower bound) 28

Establishing order of growth using limits 0 order of growth of f(n) < order of growth of g(n) lim f(n)/g(n) = n c > 0 order of growth of f(n) = order of growth of g(n) order of growth of f(n) > order of growth of g(n) Examples: 10n vs. n 2 n(n+1)/2 vs. n 2 29

Orders of growth: some important functions All logarithmic functions log a n belong to the same class Θ(log n) no matter what the logarithm s base a > 1 is All polynomials of the same degree k belong to the same class: a k n k + a k-1 n k-1 + + a 0 in Θ(n k ) Exponential functions a n have different orders of growth for different a s order log n < order n < order n log n < order n k (k 2 constant) < order a n < order n! < order n n Caution: Be aware of very large constant factors 30

31

Time analysis for iterative algorithms Steps Decide on parameter n indicating input size Identify algorithm s basic operation Determine worst case(s) for input of size n Set up a sum for the number of times the basic operation is executed Simplify the sum using standard formulas and rules 32

Example Give the number f(n) of letters Z printed by Algorithm PrintZs below: (first using a summation notation, and then - a closed-form formula for f(n) ) Analyze the worst-case time complexity of the following algorithm, and give a tight bound using the big-theta notation 33

Example Give the number f(n) of letters Z printed by Algorithm PrintZs below: (first using a summation notation, and then - a closed-form formula for f(n) ) Analyze the worst-case time complexity of the following algorithm, and give a tight bound using the big-theta notation 34