Topic 17. Analysis of Algorithms

Similar documents
What is Performance Analysis?

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

csci 210: Data Structures Program Analysis

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

Running Time Evaluation

CS 310 Advanced Data Structures and Algorithms

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Asymptotic Running Time of Algorithms

csci 210: Data Structures Program Analysis

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Introduction. How can we say that one algorithm performs better than another? Quantify the resources required to execute:

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

Solving Recurrences. Lecture 23 CS2110 Fall 2011

CISC 235: Topic 1. Complexity of Iterative Algorithms

Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

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

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

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

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Module 1: Analyzing the Efficiency of Algorithms

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

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

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

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

Algorithms and Their Complexity

Lecture 1 - Preliminaries

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

Algorithms 2/6/2018. Algorithms. Enough Mathematical Appetizers! Algorithm Examples. Algorithms. Algorithm Examples. Algorithm Examples

Analysis of Algorithms Review

In complexity theory, algorithms and problems are classified by the growth order of computation time as a function of instance size.

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

Data Structures and Algorithms Chapter 2

Runtime Complexity. CS 331: Data Structures and Algorithms

Enumerate all possible assignments and take the An algorithm is a well-defined computational

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

Searching. Sorting. Lambdas

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

Computational Complexity

Recurrences COMP 215

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

CSC2100B Data Structures Analysis

CS 4407 Algorithms Lecture 2: Growth Functions

Cpt S 223. School of EECS, WSU

Computational Complexity - Pseudocode and Recursions

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

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

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

CPSC 467: Cryptography and Computer Security

Copyright 2000, Kevin Wayne 1

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

Complexity Theory Part I

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

Analysis of Algorithms

CS1210 Lecture 23 March 8, 2019

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

Analysis of Algorithms

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

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

Theory of Computation

INF2220: algorithms and data structures Series 1

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Ch01. Analysis of Algorithms

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Data Structures and Algorithms Chapter 3

Recursion. Computational complexity

Algorithms Design & Analysis. Analysis of Algorithm

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

COMPUTER ALGORITHMS. Athasit Surarerks.

A point p is said to be dominated by point q if p.x=q.x&p.y=q.y 2 true. In RAM computation model, RAM stands for Random Access Model.

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

CS 161 Summer 2009 Homework #2 Sample Solutions

Introduction. An Introduction to Algorithms and Data Structures

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

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

CS Non-recursive and Recursive Algorithm Analysis

Asymptotic Algorithm Analysis & Sorting

Answer the following questions: Q1: ( 15 points) : A) Choose the correct answer of the following questions: نموذج اإلجابة

Friday Four Square! Today at 4:15PM, Outside Gates

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

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

Ch 01. Analysis of Algorithms

Homework 1 Solutions

Time Analysis of Sorting and Searching Algorithms

COMP 182 Algorithmic Thinking. Algorithm Efficiency. Luay Nakhleh Computer Science Rice University

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

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

CS Data Structures and Algorithm Analysis

Lecture 2: Asymptotic Notation CSCI Algorithms I

Lecture 6 September 21, 2016

Advanced Algorithmics (6EAP)

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Running Time. Overview. Case Study: Sorting. Sorting problem: Analysis of algorithms: framework for comparing algorithms and predicting performance.

Transcription:

Topic 17 Analysis of Algorithms

Analysis of Algorithms- Review Efficiency of an algorithm can be measured in terms of : Time complexity: a measure of the amount of time required to execute an algorithm Space complexity: amount of memory required Which measure is more important? It often depends on the limitations of the technology available at time of analysis (e.g. processor speed vs memory space) 12-2

Time Complexity Analysis Objectives of time complexity analysis: To determine the feasibility of an algorithm by estimating an upper bound on the amount of work performed To compare different algorithms before deciding which one to implement Time complexity analysis for an algorithm is independent of the programming language and the machine used 12-3

Time Complexity Analysis Time complexity expresses the relationship between the size of the input and the execution time for the algorithm Usually expressed as a proportionality, rather than an exact function 12-4

Time Complexity Measurement Essentially based on the number of basic operations in an algorithm: Number of arithmetic operations performed Number of comparisons Number of times through a critical loop Number of array elements accessed etc. Think of this as the work done 12-5

Example: Polynomial Evaluation Consider the polynomial P(x) = 4x 4 + 7x 3 2x 2 + 3x 1 + 6 Suppose that exponentiation is carried out using multiplications. Two ways to evaluate this polynomial are: Brute force method: P(x) = 4*x*x*x*x + 7*x*x*x 2*x*x + 3*x + 6 Horner s method: P(x) = (((4*x + 7) * x 2) * x + 3) * x + 6 12-6

Method of analysis What are the basic operations here? multiplication, addition, and subtraction We ll only consider the number of multiplications, since the number of additions and subtractions are the same in each solution We ll examine the general form of a polynomial of degree n, and express our result in terms of n We ll look at the worst case (maximum number of multiplications) to get an upper bound on the work 12-7

Method of analysis General form of a polynomial of degree n is P(x) = a n x n + a n-1 x n-1 + a n-2 x n-2 + + a 1 x 1 + a 0 where a n is non-zero for all n >= 0 (this is the worst case) 12-8

Analysis of Brute Force Method P(x) = a n * x * x * * x * x + a n-1 * x * x * * x * x + a n-2 * x * x * * x * x + + a 2 * x * x + a 1 * x + n multiplications n-1 multiplications n-2 multiplications 2 multiplications 1 multiplication a 0 12-9

Number of multiplications needed in the worst case is T(n) = n + (n-1) + (n-2) + + 3 + 2 + 1 = n (n + 1) / 2 (see below) = n 2 / 2 + n / 2 Sum of first n natural numbers: Write the n terms of the sum in forward and reverse orders: T(n) = 1 + 2 + 3 + + (n-2) + (n-1) + n T(n) = n + (n-1) + (n-2) + + 3 + 2 + 1 Add the corresponding terms: 2*T(n) = (n+1) + (n+1) + (n+1) + + (n+1) + (n+1) + (n+1) = n (n+1) Therefore, T(n) = n (n+1) / 2 12-10

Analysis of Horner s Method P(x) = ( ((( a n * x + a n-1 ) * x + a n-2 ) * x + + a 2 ) * x + a 1 ) * x + 1 multiplication 1 multiplication 1 multiplication 1 multiplication 1 multiplication n times a 0 12-11

Analysis of Horner s Method Number of multiplications needed in the worst case is : T(n) = n 12-12

Big-O Notation Analysis of Brute Force and Horner s methods came up with exact formulae for the maximum number of multiplications In general, though, we want upper bounds rather than exact formulae: we will use the Big-O notation introduced earlier 12-13

Big-O : Formal Definition Time complexity T(n) of an algorithm is O(f(n)) ( we say of the order f(n) ) if for some positive constant C and for all but finitely many values of n (i.e. as n gets large) T(n) <= C * f(n) What does this mean? this gives an upper bound on the number of operations, for sufficiently large n 12-14

Big-O Analysis We want our f(n) to be an easily recognized elementary function that describes the performance of the algorithm 12-15

Big-O Analysis Example: Polynomial Evaluation What is f(n) for Horner s method? T(n) = n, so choose f(n) = n So, we say that the number of multiplications in Horner s method is O(n) ( of the order of n ) and that the time complexity of Horner s method is O(n) 12-16

Big-O Analysis Example: Polynomial Evaluation What is f(n) for the Brute Force method? Choose the highest order (dominant) term of T(n) = n 2 /2 + n/2 with a coefficient of 1, so that f(n) = n 2 12-17

Discussion Why did we use the dominant term? It determines the basic shape of the function Why did we use a coefficient of 1? For large n, n 2 /2 is essentially n 2 12-18

Recall: Shape of Some Typical Functions 1200 t(n) = n 3 t(n) = n 2 1000 800 600 t(n) = nlog 2 n 400 200 t(n) = n 10 20 30 40 50 60 70 80 90 100 n 12-19 1-19

Big-O Analysis Example: Polynomial Evaluation Is f(n) = n 2 a good choice? i.e. for large n, how does T(n) compare to f(n)? T(n)/ f(n) approaches ½ for large n So, T(n) is approximately n 2 /2 for large n n 2 /2 <= T(n) <= n 2, so f(n) is a close upper bound 12-20

Big-O Analysis Example: Polynomial Evaluation So, we say that the number of multiplications in the Brute Force method is O(n 2 ) ( of the order of n 2 ) and that the time complexity of the Brute Force method is O(n 2 ) Think of this as proportional to n 2 12-21

Big-O Analysis: Summary We want f(n) to be an easily recognized elementary function We want a tight upper bound for our choice of f(n) n 3 is also an upper bound for the Brute Force method, but not a good one! Why not? Look at how T(n) compares to f(n) = n 3 T(n) / n 3 approaches 0 for large n, i.e. T(n) is miniscule when compared to n 3 for large n 12-22

Big-0 Example: Polynomial Evaluation Comparison n and T(n) =n (Horner) 5 10 20 100 1000 T(n)= n 2 /2 + n/2 (Brute Force) 15 55 210 5050 500500 f(n) = n 2 (upper bound for Brute Force) 25 100 400 10000 1000000 n is the degree of the polynomial. Recall that we are comparing the number of multiplications. 12-23

Big-0 Example: Polynomial 600 Evaluation 500 f(n) = n 2 # of mult s 400 300 T(n) = n 2 /2 + n/2 200 100 T(n) = n 5 10 15 20 25 30 35 n (degree of polynomial) 12-24

Time Complexity and Input Run time can depend on the size of the input only (e.g. sorting 5 items vs. 1000 items) Run time can also depend on the particular input (e.g. suppose the input is already sorted) This leads to several kinds of time complexity analysis: Worst case analysis Average case analysis Best case analysis 12-25

Worst, Average, Best Case Worst case analysis: considers the maximum of the time over all inputs of size n Used to find an upper bound on algorithm performance for large problems (large n) Average case analysis: considers the average of the time over all inputs of size n Determines the average (or expected) performance Best case analysis: considers the minimum of the time over all inputs of size n 12-26

Discussion What are some difficulties with average case analysis? Hard to determine Depends on distribution of inputs (they might not be evenly distributed) So, we usually use worst case analysis (why not best case analysis?) 12-27

Example: Linear Search The problem: search an array a of size n to determine whether the array contains the value key Return array index if found, -1 if not found Set k to 0. While (k < n-1) and (a[k] is not key) Add 1 to k. If a[k] == key Else Return k. Return 1. 12-28

Total amount of work done: Before loop: a constant amount of work Each time through loop: 2 comparisons and a constant amount of work (the and operation and addition) After loop: a constant amount of work So, we consider the number of comparisons only Worst case: need to examine all n array locations So, T(n) = 2*n, and time complexity is O(n) 12-29

Simpler (less formal) analysis: Note that work done before and after the loop is independent of n, and work done during a single execution of loop is independent of n In worst case, loop will be executed n times, so amount of work done is proportional to n, and algorithm is O(n) 12-30

Average case for a successful search: Number of comparisons necessary to find the key? 1 or 2 or 3 or 4 or n Assume that each possibility is equally likely Average number of comparisons : (1+2+3+ +n)/n = (n*(n+1)/2)/n = (n+1)/2 Average case time complexity is therefore O(n) 12-31

Example: Binary Search General case: search a sorted array a of size n looking for the value key Divide and conquer approach: Compute the middle index mid of the array If key is found at mid, we re done Otherwise repeat the approach on the half of the array that might still contain key etc 12-32

Binary Search Algorithm Set first to 0. Set last to n-1. Do { Set mid to (first + last) / 2. If key < a[mid], Set last to mid 1. Else Set first to mid + 1. } While (a[mid] is not key) and (first <= last). If a[mid] == key Return mid. Else Return 1. 12-33

Amount of work done before and after the loop is a constant, and is independent of n Amount of work done during a single execution of the loop is constant Time complexity will therefore be proportional to number of times the loop is executed, so that is what we will analyze 12-34

Worst case: key is not found in the array Each time through the loop, at least half of the remaining locations are rejected: After first time through, <= n/2 remain After second time through, <= n/4 remain After third time through, <= n/8 remain After k th time through, <= n/2 k remain 12-35

Suppose in the worst case that the maximum number of times through the loop is k; we must express k in terms of n Exit the do..while loop when the number of remaining possible locations is less than 1 (that is, when first > last): this means that n/2 k < 1 12-36

Also, n/2 k-1 >=1; otherwise, looping would have stopped after k-1 iterations Combining the two inequalities, we get n/2 k < 1 <= n/2 k-1 Invert and multiply through by n to get 2 k > n >= 2 k-1 12-37

Next, take base-2 logarithms to get k > log 2 (n) >= k-1 which is equivalent to log 2 (n) < k <= log 2 (n) + 1 So, binary search algorithm is O(log 2 (n)) in terms of the number of array locations examined 12-38

Big-O Analysis in General To determine the time complexity of an algorithm: Look at the loop structure Identify the basic operation(s) Express the number of operations as f 1 (n) + f 2 (n) + Identify the dominant term f i Then the time complexity is O(f i ) 12-39

Examples of dominant terms: n dominates log 2 (n) n log 2 (n) dominates n n 2 dominates n log 2 (n) n m dominates n k when m > k a n dominates n m for any a > 1 and m >= 0 That is, log 2 (n) < n < n log 2 (n) < n 2 < < n m < a n for a > 1 and m >2 12-40

Recall: Shape of Some Typical Functions 1200 f(n) = n 3 f(n) = n 2 1000 800 600 f(n) = nlog 2 n 400 200 f(n) = n 10 20 30 40 50 60 70 80 90 100 n 12-41 1-41

Examples of Big-O Analysis Independent nested loops: int x = 0; for (int i = 1; i <= n/2; i++){ for (int j = 1; j <= n*n; j++){ x = x + i + j; } } Number of iterations of inner loop is independent of the number of iterations of the outer loop (i.e. the value of i ) How many times through outer loop? How many times through inner loop? Time complexity of algorithm? 12-42

Dependent nested loops: int x = 0; for (int i = 1; i <= n; i++){ for (int j = 1; j <= 3*i; j++){ x = x + j; } } Number of iterations of inner loop depends on the value of i in the outer loop On k th iteration of outer loop, how many times through inner loop? Total number of iterations of inner loop = sum for k running from 1 to n Time complexity of algorithm? 12-43

Usefulness of Big-O We can compare algorithms for efficiency, for example: Linear search vs binary search Different sort algorithms Iterative vs recursive solutions (recall Fibonacci sequence!) We can estimate actual run times if we know the time complexity of the algorithm(s) we are analyzing 12-44

Estimating Run Times Assuming a million operations per second on a computer, here are some typical complexity functions and their associated runtimes: f(n) n = 10 3 n = 10 5 n = 10 6 ------------------------------------------------------------------------------ log 2 (n) 10-5 sec. 1.7*10-5 sec. 2*10-5 sec. n 10-3 sec. 0.1 sec. 1 sec. n log 2 (n) 0.01 sec. 1.7 sec. 20 sec. n 2 1 sec. 3 hours 12 days n 3 17 mins. 32 years 317 centuries 2 n 10 285 cent. 10 10000 years 10 100000 years 12-45

Discussion Suppose we want to perform a sort that is O(n 2 ). What happens if the number of items to be sorted is 100000? Compare this to a sort that is O(n log 2 (n)). Now what can we expect? Is an O(n 3 ) algorithm practical for large n? What about an O(2 n ) algorithm, even for small n? e.g. for a Pentium, runtimes are: n = 30 n = 40 n = 50 n = 60 11 sec. 3 hours 130 days 365 years 12-46

Intractable Problems A problem is said to be intractable if solving it by computer is impractical Algorithms with time complexity O(2 n ) take too long to solve even for moderate values of n What are some examples we have seen? 12-47