Analysis of Algorithms Review

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

EECS 477: Introduction to algorithms. Lecture 5

Data Structures and Algorithms. Asymptotic notation

Cpt S 223. School of EECS, WSU

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

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

Topic 17. Analysis of Algorithms

Running Time Evaluation

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

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

Principles of Algorithm Analysis

Ch 01. Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Growth Functions

Analysis of Algorithms

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

with the size of the input in the limit, as the size of the misused.

CSE 421: Intro Algorithms. 2: Analysis. Winter 2012 Larry Ruzzo

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

Ch01. Analysis of Algorithms

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

Data Structures and Algorithms CSE 465

CSE 417: Algorithms and Computational Complexity

CS 310 Advanced Data Structures and Algorithms

Growth of Functions (CLRS 2.3,3)

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

CISC 235: Topic 1. Complexity of Iterative Algorithms

Algorithms and Their Complexity

Module 1: Analyzing the Efficiency of Algorithms

CSC Design and Analysis of Algorithms. Lecture 1

Introduction to Algorithms

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

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

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

What is Performance Analysis?

CS 380 ALGORITHM DESIGN AND ANALYSIS

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

The Time Complexity of an Algorithm

Algorithms Design & Analysis. Analysis of Algorithm

COMP Analysis of Algorithms & Data Structures

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

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

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

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

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

COMP Analysis of Algorithms & Data Structures

Advanced Algorithmics (6EAP)

Asymptotic Analysis 1

Algorithms. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY. Algorihms, A.Yazici, Fall 2007 CEng 315

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

Comparison of functions. Comparison of functions. Proof for reflexivity. Proof for transitivity. Reflexivity:

Design and Analysis of Algorithms

CSC2100B Data Structures Analysis

Computational Complexity - Pseudocode and Recursions

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

Big O (Asymptotic Upper Bound)

Time Analysis of Sorting and Searching Algorithms

The Time Complexity of an Algorithm

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

3.1 Asymptotic notation

Asymptotic Analysis of Algorithms. Chapter 4

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Growth of Functions. As an example for an estimate of computation time, let us consider the sequential search algorithm.

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

CS Non-recursive and Recursive Algorithm Analysis

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Omega notation. Transitivity etc.

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

Analysis of Algorithms

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

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

csci 210: Data Structures Program Analysis

Computational Complexity

Analysis of Algorithms

Data Structures and Algorithms Chapter 2

An analogy from Calculus: limits

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

csci 210: Data Structures Program Analysis

Asymptotic Algorithm Analysis & Sorting

P, NP, NP-Complete, and NPhard

Copyright 2000, Kevin Wayne 1

How many hours would you estimate that you spent on this assignment?

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

Analysis of Multithreaded Algorithms

COMPUTER ALGORITHMS. Athasit Surarerks.

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

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

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

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

COMP 382: Reasoning about algorithms

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

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

Lecture 2: Asymptotic Notation CSCI Algorithms I

Announcements. CompSci 230 Discrete Math for Computer Science. The Growth of Functions. Section 3.2

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

Data Structures and Algorithms

Divide and Conquer. Recurrence Relations

Transcription:

COMP171 Fall 2005 Analysis of Algorithms Review Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.

Introduction to Analysis of Algorithms / Slide 2 Outline Why Does Growth Rate Matter? Properties of the Big-Oh Notation Logarithmic Algorithms Polynomial and Intractable Algorithms Compare complexity

Introduction to Analysis of Algorithms / Slide 3 Why Does Growth Rate Matter? Complexity 10 20 30 n 0.00001 sec 0.00002 sec 0.00003 sec n 2 n 3 n 5 0.0001 sec 0.0004 sec 0.0009 sec 0.001 sec 0.008 sec 0.027 sec 0.1 sec 3.2 sec 24.3 sec 2 n 0.001 sec 1.0 sec 17.9 min 3 n 0.59 sec 58 min 6.5 years

Introduction to Analysis of Algorithms / Slide 4 Why Does Growth Rate Matter? Complexity 40 50 60 n 0.00004 sec 0.00005 sec 0.00006 sec n 2 n 3 n 5 0.016 sec 0.025 sec 0.036 sec 0.064 sec 0.125 sec 0.216 sec 1.7 min 5.2 min 13.0 min 2 n 12.7 days 35.7 years 366 cent 3 n 3855 cent 2 x 10 8 cent 1.3 x 10 13 cent

Introduction to Analysis of Algorithms / Slide 5

Introduction to Analysis of Algorithms / Slide 6

Introduction to Analysis of Algorithms / Slide 7 Notations Asymptotically less than or equal to O (Big-Oh) Asymptotically greater than or equal to Ω (Big-Omega) Asymptotically equal to θ (Big-Theta) Asymptotically strictly less o (Little-Oh)

Introduction to Analysis of Algorithms / Slide 8 Why is the big Oh a Big Deal? Suppose I find two algorithms, one of which does twice as many operations in solving the same problem. I could get the same job done as fast with the slower algorithm if I buy a machine which is twice as fast. But if my algorithm is faster by a big Oh factor - No matter how much faster you make the machine running the slow algorithm the fastalgorithm, slow machine combination will eventually beat the slow algorithm, fast machine combination.

Introduction to Analysis of Algorithms / Slide 9 Properties of the Big-Oh Notation (I) Constant factors may be ignored: For all k > 0, k*f is O(f ). e.g. a*n 2 and b*n 2 are both O(n 2 ) Higher powers of n grow faster than lower powers: n r is O(n s ) if 0 < r < s. The growth rate of a sum of terms is the growth rate of its fastest growing term: If f is O(g), then f + g is O(g). e.g. a*n 3 + b*n 2 is O(n 3 ).

Introduction to Analysis of Algorithms / Slide 10 Properties of the Big-Oh Notation (II) The growth rate of a polynomial is given by the growth rate of its leading term If f is a polynomial of degree d, then f is O(n d ). If f grows faster than g, which grows faster than h, then f grows faster than h The product of upper bounds of functions gives an upper bound for the product of the functions If f is O(g) and h is O(r), then f*h is O(g*r) e.g. if f is O(n 2 ) and g is O(log n), then f*g is O(n 2 log n).

Introduction to Analysis of Algorithms / Slide 11 Properties of the Big-Oh Notation (III) Exponential functions grow faster than powers: n k is O(b n ), for all b > 1, k > 0, e.g. n 4 is O(2 n ) and n 4 is O(exp(n)). Logarithms grow more slowly than powers: log b n is O(n k ) for all b > 1, k > 0 e.g. log 2 n is O(n 0:5 ). All logarithms grow at the same rate: log b n is θ(log d n) for all b, d > 1.

Introduction to Analysis of Algorithms / Slide 12 Properties of the Big-Oh Notation (IV) The sum of the first n r th powers grows as the (r + 1) th power: 1 + 2 + 3 +. N = N(N+1)/2 (arithmetic series) 1 + 2 2 + 3 2 + N 2 = N(N + 1)(2N + 1)/6

Introduction to Analysis of Algorithms / Slide 13 Logarithms A logarithm is an inverse exponential function - Exponential functions grow distressingly fast - Logarithm functions grow refreshingly show Binary search is an example of an O(logn) algorithm - Anything is halved on each iteration, then you usually get O(logn) If you have an algorithm which runs in O(logn) time, take it because it will be very fast

Introduction to Analysis of Algorithms / Slide 14 Properties of Logarithms Asymptotically, the base of the log does not matter log log A B = log c c B A log 2 n = (1/log 100 2) x log 100 n 1/log 100 2 = 6.643 is just a constant Asymptotically, any polynomial function of n does not matter log(n 475 + n 2 + n + 96) = O(logn) since n 475 + n 2 + n + 96 = O(n 475 ) and log(n 475) = 475*logn

Introduction to Analysis of Algorithms / Slide 15 Binary Search You have a sorted list of numbers You need to search the list for the number If the number exists find its position. If the number does not exist you need to detect that

Introduction to Analysis of Algorithms / Slide 16 Binary Search with Recursion // Searches an ordered array of integers using recursion int bsearchr(const int data[], // input: array int first, // input: lower bound int last, // input: upper bound int value // input: value to find )// output: index if found, otherwise return 1 { int middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last) return -1; else if (value < data[middle]) return bsearchr(data, first, middle-1, value); else return bsearchr(data, middle+1, last, value); }

Introduction to Analysis of Algorithms / Slide 17 Complexity Analysis T(n) = T(n/2) + c O(?) complexity

Introduction to Analysis of Algorithms / Slide 18 Polynomial and Intractable Algorithms Polynomial time complexity An algorithm is said to have polynomial time complexity iff it is O(n d ) for some integer d. Intractable Algorithms A problem is said to be intractable if no algorithm with polynomial time complexity is known for it

Introduction to Analysis of Algorithms / Slide 19 Compare Complexity Method 1: A function f(n) is O(g(n)) if there exists a number n0 and a nonnegative c such that for all n n0, f(n) cg(n). Method 2: If lim n f(n)/g(n) exists and is finite, then f(n) is O(g(n))!

Introduction to Analysis of Algorithms / Slide 20 kf(n) is O(f(n)) for any positive constant k kf(n) kf(n), for all n, k > 0 f(n) is O(g(n)), g(n) is O(h(n)), Is f(n) O(h(n))? f(n) cg(n), for some c > 0, n m g(n) dh(n), for some d > 0, n p f(n) (cd)h(n), for some cd > 0, n max(p,m) n r is O(n p ) if r p since lim n n r /n p = 0, if r < p = 1 if r = p n r is O(exp(n)) for any r > 0 since lim n n r /exp(n) = 0,

Introduction to Analysis of Algorithms / Slide 21 log n is O (n r ) if r > 0, since lim n log(n)/ n r = 0, Is kn O(n 2 )? kn is O(n) n is O(n 2 ) f(n) + g(n) is O(h(n)) if f(n), g(n) are O(h(n)) f(n) ch(n), for some c > 0, n m g(n) dh(n), for some d > 0, n p f(n) + g(n) ch(n) + dh(n), n max(m,p) (c+d)h(n), c + d > 0, n max(m,p)

Introduction to Analysis of Algorithms / Slide 22 T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) T 2 (n) is O(f(n)g(n)) T 1 (n) cf(n), for some c > 0, n m T 2 (n) dg(n), for some d > 0, n p T 1 (n) T 2 (n) (cd)f(n)g(n), for some cd > 0, n max(p,m) T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) + T 2 (n) is O(max(f(n),g(n))) Let h(n) = max(f(n),g(n)), T 1 (n) is O(f(n)), f(n) is O(h(n)), so T 1 (n) is O(h(n)), T 2 (n) is O(g(n)), g(n) is O(h(n) ), so T 2 (n) is O(h(n)), Thus T 1 (n) + T 2 (n) is O(h(n))

Introduction to Analysis of Algorithms / Slide 23 Maximum Subsequence Problem There is an array of N elements Need to find i, j such that the sum of all elements between the ith and jth position is maximum for all such sums Algorithm 1: Maxsum = 0; For (i=0; i < N; i++) For (j=i; j < N; j++) { Thissum = sum of all elements between ith and jth positions; Maxsum = max(thissum, Maxsum);}

Introduction to Analysis of Algorithms / Slide 24 Analysis Inner loop: j=i N-1 (j-i + 1) = (N i + 1)(N-i)/2 Outer Loop: N-1 i=0 (N i + 1)(N-i)/2 = (N 3 + 3N 2 + 2N)/6 Overall: O(N^3 )

Introduction to Analysis of Algorithms / Slide 25 Algorithm 2 Maxsum = 0; For (i=0; i < N; i++) For (Thissum=0;j=i; j < N; j++) { Thissum = Thissum + A[j]; Maxsum = max(thissum, Maxsum);} Complexity? i=0 N-1 (N-i) = N 2 N(N+1)/2 = (N 2 N)/2 O(N 2 )

Introduction to Analysis of Algorithms / Slide 26 Algorithm 3: Divide and Conquer Step 1: Break a big problem into two small sub-problems Step 2: Solve each of them efficiently. Step 3: Combine the sub solutions

Introduction to Analysis of Algorithms / Slide 27 Maximum subsequence sum by divide and conquer Step 1: Divide the array into two parts: left part, right part Max. subsequence lies completely in left, or completely in right or spans the middle. If it spans the middle, then it includes the max subsequence in the left ending at the center and the max subsequence in the right starting from the center!

Introduction to Analysis of Algorithms / Slide 28 Example: 8 numbers in a sequence, 4 3 5 2-1 2 6-2 Max subsequence sum for first half =6 ( 4, -3, 5 ) second half =8 ( 2, 6 ) Max subsequence sum for first half ending at the last element is 4 ( 4, -3, 5, -2 ) Max subsequence sum for sum second half starting at the first element is 7 ( -1, 2, 6 ) Max subsequence sum spanning the middle is 11? Max subsequence spans the middle 4, -3, 5, -2, -1, 2, 6

Introduction to Analysis of Algorithms / Slide 29 Maxsubsum(A[], left, right) { if left = right, maxsum = max(a[left], 0); Center = (left + right)/2 maxleftsum = Maxsubsum(A[],left, center); maxrightsum = Maxsubsum(A[],center+1,right); maxleftbordersum = 0; leftbordersum = 0; for (i=center; i>=left; i--) leftbordersum+=a[i]; Maxleftbordersum=max(maxleftbordersum, leftbordersum);

Introduction to Analysis of Algorithms / Slide 30 Find maxrightbordersum.. return(max(maxleftsum, maxrightsum, maxrightbordersum + maxleftbordersum);

Introduction to Analysis of Algorithms / Slide 31 Complexity Analysis T(1)=1 T(n) = 2T(n/2) + cn = 2(2T(n/4)+cn/2)+cn=2^2T(n/2^2)+2cn =2^2(2T(n/2^3)+cn/2^2)+2cn = 2^3T(n/2^3)+3cn = = (2^k)T(n/2^k) + k*cn (let n=2^k, then k=log n) T(n)= n*t(1)+k*cn = n*1+c*n*log n = O(n log n)

Introduction to Analysis of Algorithms / Slide 32 Algorithm 4 Maxsum = 0; Thissum = 0; For (j=0; j<n; j++) { Thissum = Thissum + A[j]; If (Thissum < 0), Thissum = 0; If (Maxsum < Thissum), Maxsum = Thissum; } O(?)