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

Similar documents
Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Sorting. Bubble Sort. Selection Sort

Chapter 2: Complexity Analysis

Algorithm Analysis. Performance Factors

Lecture 5 Sorting Arrays

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

Algorithmic Analysis. Go go Big O(h)!

Introduction to the Analysis of Algorithms. Algorithm

Lecture Notes on Sorting

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

UNIT 1 ANALYSIS OF ALGORITHMS

Data Structures and Algorithms Chapter 2

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Lecture 2: Algorithm Analysis

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Assignment 1 (concept): Solutions

Measuring algorithm efficiency

Data structure and algorithm in Python

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

Algorithms A Look At Efficiency

Programming II (CS300)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Lecture 5: Running Time Evaluation

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Algorithms and Data Structures

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Outline and Reading. Analysis of Algorithms 1

Asymptotic Analysis of Algorithms

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS 261 Data Structures. Big-Oh Analysis: A Review

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Computer Algorithms. Introduction to Algorithm

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Algorithm Performance. (the Big-O)

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

IT 4043 Data Structures and Algorithms

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

csci 210: Data Structures Program Analysis

Data Structures and Algorithms

CS:3330 (22c:31) Algorithms

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Scientific Computing. Algorithm Analysis

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

CS126 Final Exam Review

CS S-02 Algorithm Analysis 1

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

What is an algorithm?

Recitation 9. Prelim Review

Data Structures and Algorithms. Part 2

ote that functions need not have a limit (in which case we say that the limit as x approaches c does not exist).

CSCI 261 Computer Science II

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Introduction to Computer Science

ASYMPTOTIC COMPLEXITY

Introduction to Computers & Programming

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Choice of C++ as Language

CS 3410 Ch 5 (DS*), 23 (IJP^)

Introduction to Data Structure

(Refer Slide Time: 1:27)

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Introduction to Analysis of Algorithms

O(n): printing a list of n items to the screen, looking at each item once.

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

ANALYSIS OF ALGORITHMS

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Analysis of Algorithms

Analysis of Algorithm. Chapter 2

Sorting Algorithms part 1

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Data Structures and Algorithms CSE 465

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Classic Data Structures Introduction UNIT I

Complexity, General. Standard approach: count the number of primitive operations executed.

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

4.1 Real-world Measurement Versus Mathematical Models

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Recall from Last Time: Big-Oh Notation

Complexity of Algorithms. Andreas Klappenecker

Analysis of Algorithms. CSE Data Structures April 10, 2002

Transcription:

Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring all resources are basically the same Historically, time dominated 3.1

The efficiency of a program or system is affected by: Programming Language Physical characteristics of the computer Room temperature Amount of data to process (or actual parameters) Algorithms used In the real world all factors are considered, and any one can dominate in any particular situation. When developing algorithms in the abstract, most such factors are ignored because they are: Out of our control, as programmers and algorithm developers. Difficult to predict. For that reason we say they are arbitrary. Consequently, we are interested in algorithm efficiency, not program or system efficiency. 3.2

The efficiency of an algorithm is specified as a running time, sometimes also called complexity. In our abstract world, the input length or parameters have the most influence on running time of an algorithm. The running time of an algorithm measures: Total number of operations executed by an algorithm, or Total number of statements executed by an algorithm Running times can be based on: Worst case (most frequently used) Average case (most difficult) Best case 3.3

Example Inputting n integers: public static void inputints(int n) { int i; i = 1; while (i <= n) { x = kb.nextint(); i = i + 1; Total number of statements executed is approximately T(n) = 3n+2 Running time is therefore O(n) How many operations are executed by the above algorithm? 3.4

Another Example: public static void inputints(int n) { for (int i = 0; i < n; i++) { System.out.println(i); for (int i = 0; i < 2*n; i++) { System.out.println(2*n-i+1); Total number of statements executed is approximately T(n) = 9n+4 Running time is therefore O(n) 3.5

Another Example: public static void inputints(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.println(i); System.out.println(i+j); System.out.println(j); System.out.println( All done! ); Total number of statements executed is approximately T(n) = 4n 2 +5n+2 Running time is therefore O(n 2 ) 3.6

Another Example: public static void inputints(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { System.out.println(i); System.out.println(j); System.out.println(k); System.out.println( All done inner loop! ); System.out.println( All done middle loop! ); System.out.println( All done outer loop! ); Total number of statements executed is approximately good luck with that!!! Running time is therefore O(n 3 ) 3.7

Another Example: public static void inputints(int n) { int x, y; x = n; y = 10 * n; x = x + y; System.out.println(x); Total number of statements executed is T(n) = 4 Running time is therefore O(1) 3.8

Big Oh Notation Note that it is tempting to think that O(f(n)) means approximately f(n). Although used in this manner frequently, this interpretation or use of big Oh is absolutely incorrect. Most frequently, O(f(n)) is pronounced on the order of f(n) or order f(n), but keep in mind that this also does not mean approximately f(n). 3.9

Definitions: Let f(n) and g(n) be functions: f(n) g(n) n 3.10

f(n) is said to be less than g(n) if f(n) <= g(n) for all n. g(n) f(n) n For example, n 2 is less than n 4 + 1. 3.11

To within a constant factor, f(n) is said to be less than g(n) if there exists a positive constant c such that f(n) <= cg(n) for all n. cg(n) f(n) g(n) n 3.12

Example: g(n) = 3n 2 + 2 f(n) = 6n 2 + 3 Note: 6n 2 + 3 is not less than 3n 2 + 2 However: To within a constant factor 6n 2 + 3 is less than 3n 2 + 2 Proof: Let c=9, then we see that: 6n 2 + 3 <= 9(3n 2 + 2) = 27n 2 + 18 3.13

By the way, for these two functions it is also the case that: To within a constant factor 3n 2 + 2 is less than 6n 2 + 3 Proof: Let c=1, then we see that: 3n 2 + 2 <= 1(6n 2 + 3) = 6n 2 + 3 In fact 3n 2 + 2 is actually less than 6n 2 + 3 In other words, asymptotically, there ain t much difference in the growth of the two functions, as n goes to infinite. (always enjoy using that word in class) 3.14

Question: g(n) = n 2 f(n) = 2 n Is f(n), to within a constant factor, less than g(n)? In other words, is there a constant c such that: f(n) <= cg(n)? No! Not even if we let c = 1,000,000, or larger! 3.15

Definition: f(n) is said to be O(g(n)) if there exists two positive constants c and n 0 such that f(n) <= cg(n) for all n >= n 0. cg(n) f(n) n n 0 Intuitively, we say that, as n goes to infinity, f(n) is, to within a constant factor, less than g(n). Stated another way, as n goes to infinity, f(n) is, to within a constant factor, bounded from above by g(n). 3.16

From the definition of big-o, it should now be clear that saying T(n) is O(g(n)) means that g(n) is an upper bound on T(n), and does not mean approximately. An analogy: Consider two integers x and y, where x <= y. Is y approximately equal to x? Of course not! Similarly for the definition of big-o. 3.17

Example: g(n) = n 2 f(n) = n 2 + 1 cg(n) f(n) Claim: n 2 + 1 is O(n 2 ) n 0 Proof: Let c=2 and n 0 = 1, then we see that: n 2 + 1 <= 2n 2 for all n >= n 0 = 1. Note that in this case f(n) is not less than g(n), even to within a constant. 3.18

The original procedure is still correct (choose the highest order term, drop the constant). More specifically, this procedure is consistent with the formal definition of big-o. f(n) = n 2 + 1 f(n) is O(n 2 ) g(n) = n + 6 g(n) is O(n) h(n) = n 35 + n 16 + n 4 + 3n 2 + 1025 h(n) is O(n 35 ) r(n) = (1/2)n + 300 r(n) is O(n) s(n) = 4n 2 + 2n + 25 s(n) is O(n 2 ) And yes, the depth of loop nesting, at least on simple programs, does indicate the running time. 3.19

More generally: If: f(n) = a m-1 n m-1 + a m-2 n m-2 + + a 1 n 1 + a 0 Then: f(n) is O(n m-1 ). Explanation: Let: c = a m-1 + a m-2 + a 1 + a 0 n 0 = 1 Then it will be the case that: f(n) <= cn m-1 for all n>= n 0 3.20

Why is f(n) <= cn m-1 for all n>=n 0 =1? cn m-1 = a m-1 n m-1 + a m-2 n m-1 + + a 1 n m-1 + a 0 n m-1 f(n) = a m-1 n m-1 + a m-2 n m-2 + + a 1 n 1 + a 0 3.21

Another thing worth noting: If, for example, f(n) = 5n 3 + 35n 2 + 3n + 1025 Then f(n) is O(n 3 ), but also O(n 4 ), O(n 5 ), etc. This may seem counter-intuitive, but it is exactly what we want big-oh to be, i.e., an upper bound. 3.22

Common Running Times: Polynomial O(1) O(logn) O(n) O(nlogn) O(n 2 ) O(n 2 logn) O(n 3 ) O(n 4 ) Exponential O(2 n ) O(3 n ) constant logarithmic linear quadratic 3.23

Example: Sequential Search Let n be the length of the array A. What is the (worst case) running time of sequential/linear search? What are the worst case scenario? We will focus on the == operation (not that its special, or anything ) public boolean inlist(int x; int[] A) { int i; i=0; while (i<=a.length-1) { if (x == A[i])) return true; i++; // notice for -> while return false; 3.24

Example: Selection Sort What is the (worst case) running time of selection sort? What are the worst case scenario? Again, we will focus on the < operator. // Selection sort public static void sort(int[] a) { int i, j, minpos, temp; for (i=0; i<a.length-1; i++) { // Find the position of the value that belongs in position i minpos = i; for (j=i+1; j<=a.length-1; j++) if (a[j] < a[minpos]) minpos = j; // Swap the values in positions i and min temp = a[i]; a[i] = a[minpos]; a[minpos] = temp; 3.25

Selection Sort Consider the number of comparisons made in the worst case: n 1 iterations performed by the outer loop First iteration: how many comparisons? Second iteration: how many comparisons? : (N 1) + (N 2) + + 2 + 1 = N(N-1)/2 = (N 2 N)/2 Which is O(n 2 ) Question: What about the other operations shouldn t they be counted also? We could count those also, but the end result would be the same, at least asymptotically it would. 3.26

Selection Sort Other Questions: Should we distinguish between different types of operations, such as comparisons vs. assignments? In some cases, yes, but here the distinction is not important; all operations are in main memory, and operate on a small, fixed number of bytes. File I/O or network message operations would be more time consuming. What about best and average cases? Best case: The assignment to minpos never takes place, would appear more efficient, but the < operator is still executed the same number of times. Consequently, the end result is the same - O(n 2 ) Average case: Same thing - O(n 2 ) 3.27

Insertion Sort What is the (worst case) running time of insertion sort? public static void insertionsort(int[] a) { int j; for (int i=1; i<=a.length-1; i++) { j=i; v = a[j]; while ((j>0) && (v<a[j-1])) { a[j]=a[j-1]; j=j-1; a[j] = v; 3.28

Insertion Sort Consider the number of times the condition in the while-loop is evaluated in the worst case: N 1 iterations performed by the outer loop. First iteration: how many evaluations? Second iteration: how many evaluations? 2 + + (N 2) + (N 1) + N = (1 + 2 +. + N) -1 = N(N+1)/2 1 = (1/2)N 2 + (1/2)N - 1 : Which is O(n 2 ) How about best case? How about average case? 3.29

Bubble Sort What is the worst-case running time of bubble sort? public static void bubblesort1(int[] a) { int temp; for (int i=1; i<a.length; i++) { for (int j=0; j<a.length-i; j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; Question: Is there a distinction between worst, best and average cases? 3.30

BubbleSort Consider the number of comparisons made by the if-statement in the worst case: n 1 iterations performed by the outer loop First iteration: how many comparisons? Second iteration: how many comparisons? : (N 1) + (N 2) + + 2 + 1 = N(N-1)/2 = (N 2 N)/2 Which is O(n 2 ) What about the other versions of bubble sort? 3.31

Bubble Sort Second version: (fewer bubbles) // This version stops when a pass occurs with no swaps. public static void bubblesort1(int[] a) { int i, temp; boolean domore; i = 1; domore = true; while ((i<a.length) && (domore)) { domore = false; for (int j=0; j<a.length-i; j++) if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; domore = true; i = i + 1; 3.32

Analysis of Binary Search Recall binary search: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 4 5 7 10 12 14 18 21 23 25 26 29 30 32 35 45 51 56 62 64 66 79 80 83 Suppose we are searching for: 45 23 29 31 3.33

Analysis of Binary Search Summary of binary search: At each step there is a segment of the array being searched, which is indicated by a low position and a high position. Look at the middle element between the low and high. If the middle element is the one you are looking for, stop. If not, repeat the search on the upper or lower half of the current segment of the array being searched. See the program at: http://cs.fit.edu/~pbernhar/teaching/cse1001/binarysearch.txt 3.34

Logarithms Fear not the logarithm, for it is your friend! The logarithm of a number to a given base is the exponent to which the base must be raised in order to produce that number. In other words, log a (b) is the power to which a must be raised in order to produce b. What is: log 10 (1000) =? log 2 (16) =? log 3 (9) =? log 2 (32) =? log 5 (1) =? log 2 (1024) =? 3.35

Base-2 Logarithms Of particular interest in computer science are base-2 logarithms. log 2 (1) = 0 log 2 (64) = 6 log 2 (2) = 1 log 2 (128) = 7 log 2 (4) = 2 log 2 (256) = 8 log 2 (8) = 3 log 2 (512) = 9 log 2 (16) = 4 log 2 (1024) = 10 log 2 (32) = 5 log 2 (2048) = 11 Notice that the logarithm is the inverse of exponentiation. log 2 (2 k ) = k The logarithm function is therefore a very slowly growing function. Exponentiation is a very fast growing function. See the wikipedia page on logarithms for the graph. 3.36

Base-2 Logarithms As stated previously, log 2 (n) is the power to which 2 must be raised in order to produce n. Another way to state this is: log 2 (n) is the number of times you can divide n by 2 and still get a number >= 1. log 2 (8) = 3 log 2 (16) = 4 log 2 (32) = 5 log 2 (2 k ) = k log 2 (9) = 3 log 2 (30) = 4 log 2 (2000) = 10 Why is this important? Recall binary search http://cs.fit.edu/~pbernhar/teaching/cse1001/binarysearch.txt 3.37

Running Time of Binary Search Each iteration of the main loop, performs one comparison, and divides the array segment to be searched in half. At the start of the first iteration, the segment has length n. At the start of the second iteration, the segment has length n/2. At the start of the second iteration, the segment has length n/4. : In the worst-case, the loop will continue until the array has length < 1. How many times does the loop iterate, i.e., how many times can the length of the array be reduced by ½ and still result in an array of length >= 1? log 2 (n) Since each iteration performs a constant number of operations, the running time of binary search is therefore O(logn) 3.38