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

Similar documents
4.1, 4.2: Analysis of Algorithms

10: Analysis of Algorithms

Lecture 20: Analysis of Algorithms

Analysis of Algorithms

2.1 Computational Tractability. Chapter 2. Basics of Algorithm Analysis. Computational Tractability. Polynomial-Time

Copyright 2000, Kevin Wayne 1

csci 210: Data Structures Program Analysis

csci 210: Data Structures Program Analysis

2. ALGORITHM ANALYSIS

Copyright 2000, Kevin Wayne 1

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity divide-and-conquer ROBERT SEDGEWICK KEVIN WAYNE

Lecture 14: Nov. 11 & 13

2.1 Computational Tractability

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

Topic 17. Analysis of Algorithms

Analysis of Algorithms Fall Basics of Algorithm Analysis Computational Tractability

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

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

Algorithms and Their Complexity

Asymptotic Running Time of Algorithms

Computer Science 4.1 PERFORMANCE. empirical analysis mathematical analysis ROBERT SEDGEWICK KEVIN WAYNE.

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

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

Running Time Evaluation

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

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

CSCE 222 Discrete Structures for Computing

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

Big-O Notation and Complexity Analysis

CS 380 ALGORITHM DESIGN AND ANALYSIS

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

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

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

Analysis of Algorithms

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

COMP 355 Advanced Algorithms

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

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

Introduction. An Introduction to Algorithms and Data Structures

Cpt S 223. School of EECS, WSU

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Mat Week 6. Fall Mat Week 6. Algorithms. Properties. Examples. Searching. Sorting. Time Complexity. Example. Properties.

Student Responsibilities Week 6. Mat Properties of Algorithms. 3.1 Algorithms. Finding the Maximum Value in a Finite Sequence Pseudocode

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

! Insert. ! Remove largest. ! Copy. ! Create. ! Destroy. ! Test if empty. ! Fraud detection: isolate $$ transactions.

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

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

Asymptotic Analysis of Algorithms. Chapter 4

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

Data Structures and Algorithms Chapter 2

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

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

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

Principles of Algorithm Analysis

COMP 382: Reasoning about algorithms

CISC 235: Topic 1. Complexity of Iterative Algorithms

Ch 01. Analysis of Algorithms

data structures and algorithms lecture 2

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

P, NP, NP-Complete, and NPhard

Ch01. Analysis of Algorithms

Copyright 2000, Kevin Wayne 1

Elementary Sorts 1 / 18

Big O (Asymptotic Upper Bound)

5. DIVIDE AND CONQUER I

Algorithm runtime analysis and computational tractability

Analysis of Algorithms

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

Data Structures and Algorithms. Asymptotic notation

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

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

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

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

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Data Structures and Algorithms

CS1210 Lecture 23 March 8, 2019

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 Science. Questions for discussion Part II. Computer Science COMPUTER SCIENCE. Section 4.2.

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Algorithms Design & Analysis. Analysis of Algorithm

Runtime Complexity. CS 331: Data Structures and Algorithms

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

Problem-Solving via Search Lecture 3

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

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Growth of Functions (CLRS 2.3,3)

Dynamic Programming. Weighted Interval Scheduling. Algorithmic Paradigms. Dynamic Programming

CS Data Structures and Algorithm Analysis

Divide and Conquer CPE 349. Theresa Migler-VonDollen

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

What is Performance Analysis?

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

COMPUTER ALGORITHMS. Athasit Surarerks.

Algorithm Design and Analysis

Transcription:

Running Time Analysis of Algorithms As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise - By what course of calculation can these results be arrived at by the machine in the shortest time? - Charles Babbage Charles Babbage (1864) Analytic Engine (schematic) Robert Sedgewick and Kevin Wayne Copyright 2005 http://www.princeton.edu/~cos226 2 Overview Case Study: Sorting Analysis of algorithms: framework for comparing algorithms and predicting performance. Scientific method.! Observe some feature of the universe.! Hypothesize a model that is consistent with observation.! Predict events using the hypothesis.! Verify the predictions by making further observations.! Validate the theory by repeating the previous steps until the hypothesis agrees with the observations. Universe = computer itself. Sorting problem:! Given items, rearrange them in ascending order.! Applications: statistics, databases, data compression, computational biology, computer graphics, scientific computing,... name name Hauser Hanley Hong Haskell Hsu Hauser Hayes Hayes Haskell Hong Hanley Hornet Hornet Hsu 3 4

Insertion Sort Insertion Sort: Observation Insertion sort.! Brute-force sorting solution.! Move left-to-right through array.! Exchange next element with larger elements to its left, one-by-one. Observe and tabulate running time for various values of.! Data source: random numbers between 0 and 1. public static void insertionsort(double[] a) { int = a.length; for (int i = 0; i < ; i++) { for (int j = i; j > 0; j--) { if (less(a[j], a[j-1])) exch(a, j, j-1); else break; 5,000 10,000 20,000 80,000 6.2 million 25 million 99 million 400 million 16 million 5 6 Insertion Sort: Experimental Hypothesis Insertion Sort: Prediction and Verification Data analysis. Plot # comparisons vs. input size on log-log scale. Experimental hypothesis. # comparisons! 2 /4. Prediction. 400 million comparisons for =. Observations. 401.3 million 399.7 million 401.6 million 400.0 million Agrees. Prediction. 10 billion comparisons for = 200,000. slope Observation. Regression. Fit line through data points! a b. Hypothesis. # comparisons grows quadratically with input size! 2 /4. 200,000 9.997 billion Agrees. 7 8

Insertion Sort: Theoretical Hypothesis Insertion Sort: Theoretical Hypothesis Experimental hypothesis.! Measure running times, plot, and fit curve.! Model useful for predicting, but not for explaining. Worst case. (descending)! Iteration i requires i comparisons.! Total = 0 + 1 + 2 +... + -2 + -1 = (-1) / 2. Theoretical hypothesis.! Analyze algorithm to estimate # comparisons as a function of: number of elements to sort average or worst case input! Model useful for predicting and explaining.! Model is independent of a particular machine or compiler. E F G H I J D C B A i Average case. (random)! Iteration i requires i/2 comparisons on average.! Total = 0 + 1/2 + 2/2 +... + (-1)/2 = (-1) / 4. Difference. Theoretical model can apply to machines not yet built. A C D F H J E B I G i 9 10 Insertion Sort: Theoretical Hypothesis Insertion Sort: Observation Theoretical hypothesis. Observe and tabulate running time for various values of. Analysis Worst Input Descending 2 / 2 Stddev -! Data source: random numbers between 0 and 1.! Machine: Apple G5 1.8GHz with 1.5GB memory running OS X. Average Random 2 / 4 1/6 3/2 Best Ascending - Time Validation. Theory agrees with observations. 5,000 6.2 million 0.13 seconds 10,000 25 million 0.43 seconds 20,000 99 million 1.5 seconds 400 million 5.6 seconds 80,000 16 million 23 seconds 200,000 10 billion 145 seconds 11 12

Insertion Sort: Experimental Hypothesis Timing in Java Data analysis. Plot time vs. input size on log-log scale. Wall clock. Measure time between beginning and end of computation.! Manual: Skagen wristwatch.! Automatic: Stopwatch.java library. Stopwatch.tic();... double elapsed = StopWatch.toc(); Regression. Fit line through data points! a b. Hypothesis. Running time grows quadratically with input size. public class Stopwatch { private static long start; public static void tic() { start = System.currentTimeMillis(); public static double toc() { long stop = System.currentTimeMillis(); return (stop - start) / 1000.0; 13 14 Measuring Running Time Summary Factors that affect running time.! Machine.! Compiler.! Algorithm.! Input data. More factors.! Caching.! Garbage collection.! Just-in-time compilation.! CPU used by other processes. Analysis of algorithms: framework for comparing algorithms and predicting performance. Scientific method.! Observe some feature of the universe.! Hypothesize a model that is consistent with observation.! Predict events using the hypothesis.! Verify the predictions by making further observations.! Validate the theory by repeating the previous steps until the hypothesis agrees with the observations. Bottom line. Often hard to get precise measurements. Remaining question. How to formulate a hypothesis? 15 16

Types of Hypotheses How To Formulate a Hypothesis Worst case running time. Obtain bound on largest possible running time of algorithm on input of a given size.! Generally captures efficiency in practice.! Draconian view, but hard to find effective alternative. Average case running time. Obtain bound on running time of algorithm on random input as a function of input size.! Hard to accurately model real instances by random distributions.! Algorithm tuned for a certain distribution may perform poorly on other inputs. Amortized running time. Obtain bound on running time of sequence of operations as a function of the number of operations. Robert Sedgewick and Kevin Wayne Copyright 2005 http://www.princeton.edu/~cos226 18 Estimating the Running Time Asymptotic Running Time Total running time: sum of cost " frequency for all of the basic ops.! Cost depends on machine, compiler.! Frequency depends on algorithm, input. Cost for sorting.! A = # exchanges.! B = # comparisons.! Cost on a typical machine = 11A + 4B. Frequency of sorting ops.! = # elements to sort.! Selection sort: A = -1, B = (-1)/2. Donald Knuth 1974 Turing Award An easier alternative. (i) Analyze asymptotic growth as a function of input size. (ii) For medium, run and measure time. (iii) For large, use (i) and (ii) to predict time. Asymptotic growth rates.! Estimate as a function of input size., log, 2, 3, 2,!! Ignore lower order terms and leading coefficients. Ex. 6 3 + 17 2 + 56 is asymptotically proportional to 3 19 20

Big Oh otation Why It Matters Big Theta, Oh, and Omega notation.! #( 2 ) means { 2, 17 2, 2 + 17 1.5 + 3,... ignore lower order terms and leading coefficients! O( 2 ) means { 2, 17 2, 2 + 17 1.5 + 3, 1.5, 100,... #( 2 ) and smaller Run time in nanoseconds --> Time to solve a problem of size 1000 10,000 100,000 million 1.3 3 1.3 seconds 22 minutes 15 days 41 years 10 2 10 msec 1 second 1.7 minutes 2.8 hours 47 log 2 0.4 msec 6 msec 78 msec 0.94 seconds 48 0.048 msec 0.48 msec 4.8 msec 48 msec use for upper bounds 10 million 41 millennia 1.7 weeks 11 seconds 0.48 seconds! $( 2 ) means { 2, 17 2, 2 + 17 1.5 + 3, 3, 100 5,... #( 2 ) and larger use for lower bounds Max size problem solved in one second minute hour day 920 3,600 14,000 41,000 10,000 77,000 600,000 2.9 million 1 million 49 million 2.4 trillion 50 trillion 21 million 1.3 billion 76 trillion 1,800 trillion multiplied by 10, time multiplied by 1,000 100 10+ 10 ever say: insertion sort makes at least O( 2 ) comparisons. Reference: More Programming Pearls by Jon Bentley 21 22 Orders of Magnitude Constant Time Seconds 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 Equivalent 1 second 10 10 seconds 1.7 minutes 17 minutes 2.8 hours 1.1 days 1.6 weeks 3.8 months 3.1 years 3.1 decades 3.1 centuries Meters Per Second 10-10 10-8 10-6 10-4 10-2 1 10 2 10 4 10 6 10 8 Imperial Units 1.2 in / decade 1 ft / year 3.4 in / day 1.2 ft / hour 2 ft / minute 2.2 mi / hour 220 mi / hour 370 mi / min 620 mi / sec 62,000 mi / sec Example Continental drift Hair growing Glacier Gastro-intestinal tract Ant Human walk Propeller airplane Space shuttle Earth in galactic orbit 1/3 speed of light Linear time. Running time is O(1). Elementary operations.! Function call.! Boolean operation.! Arithmetic operation.! Assignment statement.! Access array element by index.... forever 10 17 age of universe Powers of 2 2 10 thousand 2 20 million 2 30 billion Reference: More Programming Pearls by Jon Bentley 23 24

Logarithmic Time Linear Time Logarithmic time. Running time is O(log ). Searching in a sorted list. Given a sorted array of items, find index of query item. Linear time. Running time is O(). Find the maximum. Find the maximum value of items in an array. O(log ) solution. Binary search. public static int binarysearch(string[] a, String key) { int left = 0; int right = a.length - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = key.compareto(a[mid]); if (cmp < 0) right = mid - 1; else if (cmp > 0) left = mid + 1; else return mid; return -1; double max = Integer.EGATIVE_IFIITY; for (int i = 0; i < ; i++) { if (a[i] > max) max = a[i]; 25 26 Linearithmic Time Quadratic Time Linearithmic time. Running time is O( log ). Sorting. Given an array of elements, rearrange in ascending order. O( log ) solution. Mergesort. [stay tuned] Remark. $( log ) comparisons required. [stay tuned] Quadratic time. Running time is O( 2 ). Closest pair of points. Given a list of points in the plane, find the pair that is closest. O( 2 ) solution. Enumerate all pairs of points. double min = Math.POSITIVE_IFIITY; for (int i = 0; i < ; i++){ for (int j = i+1; j < ; j++) { double dx = (x[i] - x[j]); double dy = (y[i] - y[j]); if (dx*dx + dy*dy < min) min = dx*dx + dy*dy; Remark. $( 2 ) seems inevitable, but this is just an illusion. 27 28

Exponential Time Summary of Common Hypotheses Exponential time. Running time is O(a ) for some constant a > 1. Finbonacci sequence: 1 1 2 3 5 8 13 21 34 55 Complexity Description When doubles, running time O(% ) solution. Spectacularly inefficient! " = 1 2 ( 1+ 5 ) = 1.618034... 1 log Constant algorithm is independent of input size. Logarithmic algorithm gets slightly slower as grows. does not change increases by a constant public static int F(int ) { if (n == 0 n == 1) return n; else return F(n-1) + F(n-2); log 2 Linear algorithm is optimal if you need to process inputs. Linearithmic algorithm scales to huge problems. Quadratic algorithm practical for use only on relatively small problems. doubles slightly more than doubles quadruples Efficient solution. F() = # " % $ 5 & (. ' nearest integer function 2 Exponential algorithm is not usually practical. squares! 29 30