CS S-02 Algorithm Analysis 1

Similar documents
Data Structures and Algorithms

Introduction to Computers & Programming

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Chapter 2: Complexity Analysis

Lecture 2: Algorithm Analysis

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

Recall from Last Time: Big-Oh Notation

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

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Math 501 Solutions to Homework Assignment 10

CS:3330 (22c:31) Algorithms

Algorithms A Look At Efficiency

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

Complexity of Algorithms. Andreas Klappenecker

csci 210: Data Structures Program Analysis

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Complexity of Algorithms

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

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

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Lecture 5: Running Time Evaluation

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

Introduction to the Analysis of Algorithms. Algorithm

Introduction to Analysis of Algorithms

4.4 Algorithm Design Technique: Randomization

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

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

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

Data Structures and Algorithms

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

UNIT 1 ANALYSIS OF ALGORITHMS

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

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

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

Complexity Analysis of an Algorithm

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Analysis of Algorithms. CSE Data Structures April 10, 2002

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

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe Sandra Batista

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

Time Complexity for Loops

Quiz 1 Solutions. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) Solution: True. Θ is transitive.

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

Data Structures and Algorithms. Part 2

ENGI 4892: Data Structures Assignment 2 SOLUTIONS

CS240 Fall Mike Lam, Professor. Algorithm Analysis

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

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

Outline and Reading. Analysis of Algorithms 1

1.4 Analysis of Algorithms

Data Structures and Algorithms Key to Homework 1

Introduction to Data Structure

Quiz 1 Practice Problems

Choice of C++ as Language

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Quiz 1 Practice Problems

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

Data Structures Lecture 8

Comparison of x with an entry in the array

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

Elementary maths for GMT. Algorithm analysis Part I

CS 303 Design and Analysis of Algorithms

1. Answer the following questions about each code fragment below. [8 points]

CS 360 Exam 1 Fall 2014 Solution. 1. Answer the following questions about each code fragment below. [8 points]

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

1. Attempt any three of the following: 15

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

1. Answer the following questions about each code fragment below. [8 points]

The growth of functions. (Chapter 3)

Internal Sorting by Comparison

Algorithm Analysis. Performance Factors

HOMEWORK FILE SOLUTIONS

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Data Structure and Algorithm Homework #1 Due: 1:20pm, Tuesday, March 21, 2017 TA === Homework submission instructions ===

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

EECS 477: Introduction to algorithms. Lecture 6

Algorithmic Analysis. Go go Big O(h)!

CS 360 Exam 1 Fall 2014 Name. 1. Answer the following questions about each code fragment below. [8 points]

Data Structures and Algorithms

CS2 Practical 6 CS2Bh 24 January 2005

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

Assignment 1 (concept): Solutions

Analysis of Algorithms

An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

Test 1 Review Questions with Solutions

Data Structures and Algorithms Chapter 2

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

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Algorithms and Data Structures

Algorithm Analysis. Big Oh

Classic Data Structures Introduction UNIT I

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

CS240 Fall Mike Lam, Professor. Algorithm Analysis

DATA STRUCTURES AND ALGORITHMS

Transcription:

CS245-2008S-02 Algorithm Analysis 1 02-0: Algorithm Analysis When is algorithm A better than algorithm B? 02-1: Algorithm Analysis When is algorithm A better than algorithm B? Algorithm A runs faster 02-2: Algorithm Analysis When is algorithm A better than algorithm B? Algorithm A runs faster Algorithm A requires less space to run 02-3: Algorithm Analysis When is algorithm A better than algorithm B? Algorithm A runs faster Algorithm A requires less space to run Space / Time Trade-off Can often create an algorithm that runs faster, by using more space For now, we will concentrate on time efficiency 02-4: Best Case vs. Worst Case How long does the following function take to run: boolean find(int A[], int element) { for (i=0; i<a.length; i++) { if (A[i] == elem) return true; return false; 02-5: Best Case vs. Worst Case How long does the following function take to run: boolean find(int A[], int element) { for (i=0; i<a.length; i++) { if (A[i] == elem) return true; return false; It depends on if and where the element is in the list 02-6: Best Case vs. Worst Case Best Case What is the fastest that the algorithm can run Worst Case What is the slowest that the algorithm can run Average Case How long, on average, does the algorithm take to run

CS245-2008S-02 Algorithm Analysis 2 Worst Case performance is almost always important. Usually, Best Case performance is unimportant (why?) Usually, Average Case = Worst Case (but not always!) 02-7: Measuring Time Efficiency How long does an algorithm take to run? 02-8: Measuring Time Efficiency How long does an algorithm take to run? Implement on a computer, time using a stopwatch. 02-9: Measuring Time Efficiency How long does an algorithm take to run? Implement on a computer, time using a stopwatch. Problems: Not just testing algorithm testing implementation of algorithm Implementation details (cache performance, other programs running in the background, etc) can affect results Hard to compare algorithms that are not tested under exactly the same conditions 02-10: Measuring Time Efficiency How long does an algorithm take to run? Implement on a computer, time using a stopwatch. Problems: Not just testing algorithm testing implementation of algorithm Implementation details (cache performance, other programs running in the background, etc) can affect results Hard to compare algorithms that are not tested under exactly the same conditions Better Method: Build a mathematical model of the running time, use model to compare algorithms 02-11: Competing Algorithms Linear Search for (i=low; i <= high; i++) if (A[i] == elem) return true; return false; Binary Search int BinarySearch(int low, int high, elem) { if (low > high) return false; mid = (high + low) / 2; if (A[mid] == elem) return true; if (A[mid] < elem) return BinarySearch(mid+1, high, elem); else return BinarySearch(low, mid-1, elem);

CS245-2008S-02 Algorithm Analysis 3 02-12: Linear vs Binary Linear Search for (i=low; i <= high; i++) if (A[i] == elem) return true; return false; Time Required, for a problem of size n (worst case): 02-13: Linear vs Binary Linear Search for (i=low; i <= high; i++) if (A[i] == elem) return true; return false; Time Required, for a problem of size n (worst case): c 1 n for some constant c 1 02-14: Linear vs Binary Binary Search int BinarySearch(int low, int high, elem) { if (low > high) return false; mid = (high + low) / 2; if (A[mid] == elem) return true; if (A[mid] < elem) return BinarySearch(mid+1, high, elem); else return BinarySearch(low, mid-1, elem); Time Required, for a problem of size n (worst case): 02-15: Linear vs Binary Binary Search int BinarySearch(int low, int high, elem) { if (low > high) return false; mid = (high + low) / 2; if (A[mid] == elem) return true; if (A[mid] < elem) return BinarySearch(mid+1, high, elem); else return BinarySearch(low, mid-1, elem);

CS245-2008S-02 Algorithm Analysis 4 Time Required, for a problem of size n (worst case): c 2 lg(n) for some constant c 2 02-16: Do Constants Matter? Linear Search requires time c 1 n, for some c 1 Binary Search requires time c 2 lg(n), for some c 2 What if there is a very high overhead cost for function calls? What if c 2 is 1000 times larger than c 1? 02-17: Constants Do Not Matter! Length of list Time Required for Time Required for Linear Search Binary Search 10 0.001 seconds 0.3 seconds 100 0.01 seconds 0.66 seconds 1000 0.1 seconds 1.0 seconds 10000 1 second 1.3 seconds 100000 10 seconds 1.7 seconds 1000000 2 minutes 2.0 seconds 10000000 17 minutes 2.3 seconds 10 10 11 days 3.3 seconds 10 15 30 centuries 5.0 seconds 10 20 300 million years 6.6 seconds 02-18: Growth Rate We care about the Growth Rate of a function how much more we can do if we add more processing power Faster Computers Solving Problems Faster Faster Computers = Solving Larger Problems Modeling more variables Handling bigger databases Pushing more polygons 02-19: Growth Rate Examples Size of problem that can be solved time 10n 5n n lg n n 2 n 3 2 n 1 s 1000 2000 1003 100 21 13 2 s 2000 4000 1843 141 27 14 20 s 20000 40000 14470 447 58 17 1 m 60000 120000 39311 774 84 19 1 hr 3600000 7200000 1736782 18973 331 25 02-20: Constants and Running Times When calculating a formula for the running time of an algorithm: Constants aren t as important as the growth rate of the function Lower order terms don t have much of an impact on the growth rate x 3 + x vs x 3 We d like a formal method for describing what is important when analyzing running time, and what is not.

CS245-2008S-02 Algorithm Analysis 5 02-21: Big-Oh Notation O(f(n)) is the set of all functions that are bound from above by f(n) T(n) O(f(n)) if c, n 0 such that T(n) c f(n) when n > n 0 02-22: Big-Oh Examples n O(n)? 10n O(n)? n O(10n)? n O(n 2 )? n 2 O(n)? 10n 2 O(n 2 )? n lg n O(n 2 )? lnn O(2n)? lg n O(n)? 3n + 4 O(n)? 5n 2 + 10n 2 O(n 3 )? O(n 2 )? O(n)? 02-23: Big-Oh Examples n O(n) 10n O(n) n O(10n) n O(n 2 ) n 2 O(n) 10n 2 O(n 2 ) n lg n O(n 2 ) ln n O(2n) lg n O(n) 3n + 4 O(n) 5n 2 + 10n 2 O(n 3 ), O(n 2 ), O(n)? 02-24: Big-Oh Examples II n O(n)? lg n O(2 n )? lg n O(n)? n lg n O(n)? n lg n O(n 2 )? n O(lg n)? lg n O( n)? n lg n O(n 3 2)? n 3 + n lg n + n n O(n lg n)? n 3 + n lg n + n n O(n 3 )? n 3 + n lg n + n n O(n 4 )? 02-25: Big-Oh Examples II

CS245-2008S-02 Algorithm Analysis 6 n O(n) lg n O(2 n ) lg n O(n) n lg n O(n) n lg n O(n 2 ) n O(lg n) lg n O( n) n lg n O(n 3 2) n 3 + n lg n + n n O(n lg n) n 3 + n lg n + n n O(n 3 ) n 3 + n lg n + n n O(n 4 ) 02-26: Big-Oh { Examples III n for n odd f(n) = n 3 for n even g(n) = n 2 f(n) O(g(n))? g(n) O(f(n))? n O(f(n))? f(n) O(n 3 )? 02-27: Big-Oh { Examples III n for n odd f(n) = n 3 for n even g(n) = n 2 f(n) O(g(n)) g(n) O(f(n)) n O(f(n)) f(n) O(n 3 ) 02-28: Big-Ω Notation Ω(f(n)) is the set of all functions that are bound from below by f(n) T(n) Ω(f(n)) if c, n 0 such that T(n) c f(n) when n > n 0 02-29: Big-Ω Notation Ω(f(n)) is the set of all functions that are bound from below by f(n) T(n) Ω(f(n)) if c, n 0 such that T(n) c f(n) when n > n 0 f(n) O(g(n)) g(n) Ω(f(n)) 02-30: Big-Θ Notation Θ(f(n)) is the set of all functions that are bound both above and below by f(n). Θ is a tight bound T(n) Ω(f(n)) if

CS245-2008S-02 Algorithm Analysis 7 T(n) O(f(n)) and T(n) Ω(f(n)) 02-31: Big-Oh Rules 1. If f(n) O(g(n)) and g(n) O(h(n)), then f(n) O(h(n)) 2. If f(n) O(kg(n) for any constant k > 0, then f(n) O(g(n)) 3. If f 1 (n) O(g 1 (n)) and f 2 (n) O(g 2 (n)), then f 1 (n) + f 2 (n) O(max(g 1 (n), g 2 (n))) 4. If f 1 (n) O(g 1 (n)) and f 2 (n) O(g 2 (n)), then f 1 (n) f 2 (n) O(g 1 (n) g 2 (n)) (Also work for Ω, and hence Θ) 02-32: Big-Oh Guidelines Don t include constants/low order terms in Big-Oh Simple statements: Θ(1) Loops: Θ(inside) * # of iterations Nested loops work the same way Consecutive statements: Longest Statement Conditional (if) statements: O(Test + longest branch) 02-33: Calculating Big-Oh for (i=1; i<n; i++) 02-34: Calculating Big-Oh for (i=1; i<n; i++) Running time: O(n), Ω(n), Θ(n) 02-35: Calculating Big-Oh for (i=1; i<n; i=i+2) 02-36: Calculating Big-Oh for (i=1; i<n; i=i+2) Executed n/2 times Running time: O(n), Ω(n), Θ(n) 02-37: Calculating Big-Oh for (i=1; i<n; i++) for (j=1; j < n/2; j++)

CS245-2008S-02 Algorithm Analysis 8 02-38: Calculating Big-Oh for (i=1; i<n; i++) for (j=1; j < n/2; j++) Executed n/2 times Running time: O(n 2 ), Ω(n 2 ), Θ(n 2 ) 02-39: Calculating Big-Oh 02-40: Calculating Big-Oh Executed lg n times Running Time: O(lg n), Ω(lg n), Θ(lg n) 02-41: Calculating Big-Oh for (i=0; i<n; i++) for (j = 0; j<i; j++) 02-42: Calculating Big-Oh for (i=0; i<n; i++) for (j = 0; j<i; j++) Executed <= n times Running Time: O(n 2 ). Also Ω(n 2 )? 02-43: Calculating Big-Oh for (i=0; i<n; i++) for (j = 0; j<i; j++) Exact # of times sum++ is executed: i = i=1 n(n + 1) 2 Θ(n 2 ) 02-44: Calculating Big-Oh

CS245-2008S-02 Algorithm Analysis 9 for (i=0; i<n; i++) 02-45: Calculating Big-Oh for (i=0; i<n; i++) Executed lg n times Running Time: O(n), Ω(n), Θ(n) 02-46: Calculating Big-Oh for (i=0; i<n; i=i+2) for (i=0; i<n/2; i=i+5) 02-47: Calculating Big-Oh for (i=0; i<n; i=i+2) for (i=0; i<n/2; i=i+5) Executed n/2 times Executed n/10 times Running Time: O(n), Ω(n), Θ(n) 02-48: Calculating Big-Oh for (i=0; i<n;i++) for (j=1; j<n; j=j*2) for (k=1; k<n; k=k+2) 02-49: Calculating Big-Oh for (i=0; i<n;i++) for (j=1; j<n; j=j*2) for (k=1; k<n; k=k+2) Executed lg n times Executed n/2 times Running Time: O(n 2 lg n), Ω(n 2 lg n), Θ(n 2 lg n) 02-50: Calculating Big-Oh

CS245-2008S-02 Algorithm Analysis 10 for (j=0; j<n; j++) 02-51: Calculating Big-Oh for (j=0; j<n; j++) Executed lg n times Running Time: O(n lg n), Ω(n lg n), Θ(n lg n) 02-52: Calculating Big-Oh for (j=0; j<i; j++) 02-53: Calculating Big-Oh for (j=0; j<i; j++) Executed lg n times Executed <= n times Running Time: O(n lg n). Also Ω(n lg n)? 02-54: Calculating Big-Oh for (j=0; j<i; j++) # of times sum++ is executed: lg n 2 i = 2 lg n+1 1 i=0 02-55: Calculating Big-Oh Of course, a little change can mess things up a bit... for (i=1; i<=n; i=i+1) for (j=1; j<=i; j=j*2) = 2n 1 Θ(n)

CS245-2008S-02 Algorithm Analysis 11 02-56: Calculating Big-Oh Of course, a little change can mess things up a bit... for (i=1; i<=n; i=i+1) for (j=1; j<=i; j=j*2) Executed <= lg n times So, this is code is O(n lg n) but is it also Ω(n lg n)? 02-57: Calculating Big-Oh Of course, a little change can mess things up a bit... for (i=1; i<=n; i=i+1) for (j=1; j<=i; j=j*2) Executed <= lg n times Total time sum++ is executed: lg i i 1 This can be tricky to evaluate, but we only need a bound... 02-58: Calculating Big-Oh Total # of times sum++ is executed: lg i = i=1 n/2 1 i=1 i=n/2 i=n/2 lg i + lg i lg n/2 = n/2 lgn/2 Ω(n lg n) i=n/2 lg i