Analysis of Algorithms. CSE Data Structures April 10, 2002

Similar documents
Recall from Last Time: Big-Oh Notation

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

Lecture 5: Running Time Evaluation

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

The growth of functions. (Chapter 3)

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

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

Analysis of Algorithm. Chapter 2

Complexity of Algorithms. Andreas Klappenecker

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Complexity of Algorithms

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

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

UNIT 1 ANALYSIS OF ALGORITHMS

Programming II (CS300)

CS:3330 (22c:31) Algorithms

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

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe

Choice of C++ as Language

Chapter 2: Complexity Analysis

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

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Elementary maths for GMT. Algorithm analysis Part I

Introduction to Computers & Programming

Introduction to Computer Science

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

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe Sandra Batista

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS S-02 Algorithm Analysis 1

Math 501 Solutions to Homework Assignment 10

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

Measuring algorithm efficiency

Classic Data Structures Introduction UNIT I

Analysis of Algorithms

Algorithmic Complexity


EECS 477: Introduction to algorithms. Lecture 6

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

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

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

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

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

Data Structures and Algorithms. Part 2

Algorithmics. Some information. Programming details: Ruby desuka?

Algorithms A Look At Efficiency

Introduction to Computer Science

The Running Time of Programs

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

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

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

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

CS 310: Order Notation (aka Big-O and friends)

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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

Recursion. Example R1

Data Structures Lecture 8

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

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

Computational Complexity: Measuring the Efficiency of Algorithms

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

Data Structures and Algorithms

L6,7: Time & Space Complexity

1. Attempt any three of the following: 15

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

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, )

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

ANALYSIS OF ALGORITHMS

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Topic Number 2 Efficiency Complexity Algorithm Analysis

HOMEWORK FILE SOLUTIONS

A LISP programmer knows the value of everything, but the cost of nothing.

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

Data Structures and Algorithms Key to Homework 1

Outline and Reading. Analysis of Algorithms 1

Computer Science Approach to problem solving

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

BBM 201 DATA STRUCTURES

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

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

Measuring Efficiency

Introduction to the Analysis of Algorithms. Algorithm

Complexity Analysis of an Algorithm

Data Structures and Algorithms Chapter 2

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

Introduction to Analysis of Algorithms

Data Structures and Algorithms

Analytical Modeling of Parallel Programs

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

CSE 373: Data Structures and Algorithms

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

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

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Lecture 2: Algorithm Analysis

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Algorithms and Data Structures

Transcription:

Analysis of Algorithms CSE 373 - Data Structures April 10, 2002

Readings and References Reading Chapter 2, Data Structures and Algorithm Analysis in C, Weiss Other References 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 2

Asymptotic Behavior The asymptotic performance as N, regardless of what happens for small input sizes N, is generally most important Performance for small input sizes may matter in practice, if you are sure that small N will be common forever We will compare algorithms based on how they scale for large values of N 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 3

Big-Oh Notation The growth rate of the time or space required in relation to the size of the input N is generally the critical issue T(N) is said to be O(f(N)) if there are positive constants c and n 0 such that T(N) cf(n) for N n 0. ie, f(n) is an upper bound on T(N) for N n 0 T(N) is big-oh of f(n) or "order" f(n) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 4

T(x) = 2x 2 + x + 1 is O(x 2 ) x = 1:10; y1 = x.*x; y2 = 2*(x.*x)+x+1; y3 = 3*(x.*x); plot(x,y1,'r') hold on plot(x,y2,'g') plot(x,y3,'b')

Big-Oh Notation Suppose T(N) = 50N T(N) = O(N) Take c = 50 and n 0 = 1 Suppose T(N) = 50N+11 T(N) = O(N) T(N) 50N+11N = 61N for N 1. So, c = 61 and n 0 = 1 works 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 6

The common comparisons Increasing cost Name Big-Oh Constant O(1) Log log O(log log N) Logarithmic O(log N) Log squared O((log N) 2 ) Linear O(N) N log N O(N log N) Quadratic O(N 2 ) Cubic O(N 3 ) Exponential O(2 N ) Polynomial time 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 7

n = 1:100; y1 = n-n+1; y2 = log2(n); y3 = n; y4 = n.*log2(n); y5 = n.^2; y6 = 2.^n; from bigo.m Exponential Growth swamps everything else

Bounds Upper bound (O) is not the only bound of interest Big-Oh (O), Little-Oh (o), Omega (Ω), and Theta (Θ): (Fraternities of functions ) Examples of time and space efficiency analysis 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 9

Big-Oh Notation Run Time N 2 T A (N) = 50N T A (N) = O(N 2 ) T A (N) is O(N 2 ) because 50N N 2 for N 50. So N 2 is an upper bound. But it's not a very tight upper bound. Input Size N 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 10

Big-Oh and Omega T(N) = O(f(N)) if there are positive constants c and n 0 such that T(N) cf(n) for N n 0. O(f(N)) is an upper bound for T(N) 100 log N, N 0.9, 0.0001 N, 2 100 N + log N are O(N) T(N) = Ω(f(N)) if there are positive constants c and n 0 such that T(N) cf(n) for N n 0. Ω(f(N)) is a lower bound for T(N) 2 N, N log N, N 1.2, 0.0001 N, N + log N are Ω(N) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 11

Theta and Little-Oh T(N) = Θ(f(N)) iff T(N) = O(f(N)) and T(N) = Ω(f(N)) Θ(f(N)) is a tight bound, upper and lower 0.0001 N, 2 100 N + log N are all = Θ(N) T(N) = o(f(n)) iff T(N) = O(f(N)) and T(N) Θ(f(N)) f(n) grows faster than T(N) 100 log N, N 0.9, sqrt(n), 17 are all = o(n) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 12

For large N and ignoring constant factors T(N) = O(f(N)) means T(N) is less than or equal to f(n) Upper bound T(N) = Ω(f(N)) means T(N) is greater than or equal to f(n) Lower bound T(N) = Θ(f(N)) means T(N) is equal to f(n) Tight bound, same growth rate T(N) = o(f(n)) means T(N) is strictly less than f(n) Strict upper bound: f(n) grows faster than T(N) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 13

Big-Oh Analysis of iterative sum function Find the sum of the first num integers stored in array v. Assume num size of v. int sum ( int v[ ], int num) { int temp_sum = 0, i; //1 for ( i = 0; i < num; i++ ) //2 temp_sum = temp_sum + v[i] ; //3 return temp_sum; //4 } lines 1, 3, and 4 take fixed (constant) amount of time line 2: i goes from 0 to num-1= num iterations Running time = constant + (num)*constant = O(num) Actually, Θ(num) because there are no fast cases 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 14

Big-Oh Analysis of recursive sum function Recursive function to find the sum of first num integers in v: int sum ( int v[ ], int num){ if (num == 0) return 0; else return v[num-1] + sum(v,num-1); } // constant time here // constant + T(num-1) Let T(num) be the running time of sum Then, T(num) = constant + T(num-1) = 2*constant + T(num-2) = = num*constant + constant = Θ(num) (same as iterative algorithm!) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 15

Common Recurrence Relations Common recurrence relations in analysis of algorithms: T(N) = T(N-1) + Θ(1) T(N) = O(N) T(N) = T(N-1) + Θ(N) T(N) = O(N 2 ) T(N) = T(N/2) + Θ(1) T(N) = O(log N) T(N) = 2T(N/2) + Θ(N) T(N) = O(N log N) T(N) = 4T(N/2) + Θ(N) T(N) = O(N 2 ) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 16

Big-Oh Analysis of Recursive Algorithms To derive, expand the right side and count Note: Multiplicative constants matter in recurrence relations: T(N) = 4T(N/2) + Θ(N) is O(N 2 ), not O(N log N)! You will see these again later you will only need to know a few specific relations and their big-oh answers 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 17

Recursion Recall the example using Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34, First two are defined to be 1 Rest are sum of preceding two F n = F n-1 + F n-2 (n > 1) Leonardo Pisano Fibonacci (1170-1250) 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 18

Recursive Fibonacci Function int fib(int N) { if (N < 0) return 0; //invalid input if (N == 0 N == 1) return 1; //base cases else return fib(n-1)+fib(n-2); } Running time T(N) =? 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 19

Recursive Fibonacci Analysis int fib(int N) { if (N < 0) return 0; // time = 1 for (N < 0) if (N == 0 N == 1) return 1; // time = 3 else return fib(n-1)+fib(n-2); //T(N-1)+T(N-2)+1 } Running time T(N) = T(N-1) + T(N-2) + 5 Using F n = F n-1 + F n-2 we can show by induction that T(N) F N. We can also show by induction that F N (3/2) N Therefore, T(N) (3/2) N Exponential running time! 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 20

Iterative Fibonacci Function int fib_iter(int N) { int fib0 = 1, fib1 = 1, fibj = 1; if (N < 0) return 0; //invalid input for (int j = 2; j <= N; j++) { //all fib nos. up to N fibj = fib0 + fib1; fib0 = fib1; fib1 = fibj; } return fibj; } Running time =? 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 21

Iterative Fibonacci Analysis int fib_iter(int N) { int fib0 = 1, fib1 = 1, fibj = 1; if (N < 0) return 0; //invalid input for (int j = 2; j <= N; j++) { //all fib nos. up to N fibj = fib0 + fib1; fib0 = fib1; fib1 = fibj; } return fibj; } Running time T(N) = constant+(n-1) constant T(N) = Θ(N) Exponentially faster than recursive Fibonacci 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 22

Appendix 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 23

Matlab - bigo.m % bigo functions % calculate and plot several functions used in comparing growth rates figure n = 1:100; y1 = n-n+1; y2 = log2(n); y3 = n; y4 = n.*log2(n); y5 = n.^2; y6 = 2.^n; % the x axis % O(1) % O(log2(n)) % O(n) % O(nlog2(n)) % O(n^2) % O(2^n) plot(n,y1,'y','linewidth',2) hold on plot(n,y2,'r','linewidth',2) plot(n,y3,'g','linewidth',2) plot(n,y4,'b','linewidth',2) plot(n,y5,'m','linewidth',2) plot(n,y6,'c','linewidth',2) legend('o(1)','o(log_2 n)','o(n)','o(n log_2 n)','o(n^2)','o(2^n)',2) hold off 10-Apr-02 CSE 373 - Data Structures - 5 - Analysis of Algorithms 24