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

Similar documents
CPS 616 DIVIDE-AND-CONQUER 6-1

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

COMP 355 Advanced Algorithms

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

COMP 9024, Class notes, 11s2, Class 1

Data Structures and Algorithms. Asymptotic notation

2. ALGORITHM ANALYSIS

data structures and algorithms lecture 2

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

The Time Complexity of an Algorithm

Principles of Algorithm Analysis

Growth of Functions (CLRS 2.3,3)

ITEC2620 Introduction to Data Structures

Analysis of Algorithms

Divide-and-conquer: Order Statistics. Curs: Fall 2017

The Time Complexity of an Algorithm

Review Of Topics. Review: Induction

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Divide and Conquer CPE 349. Theresa Migler-VonDollen

COMP 382: Reasoning about algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

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

CS 310 Advanced Data Structures and Algorithms

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

Data Structures and Algorithms CSE 465

Analysis of Algorithms

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

Analysis of Algorithms

Ch01. Analysis of Algorithms

COMP Analysis of Algorithms & Data Structures

Design and Analysis of Algorithms

Computational Complexity

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

COMP Analysis of Algorithms & Data Structures

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

Data Structures and Algorithms

CS173 Running Time and Big-O. Tandy Warnow

CSC Design and Analysis of Algorithms. Lecture 1

Algorithms Design & Analysis. Analysis of Algorithm

Algorithms And Programming I. Lecture 5 Quicksort

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

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

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

Design and Analysis of Algorithms

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Data Structures and Algorithms

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

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

Analysis of Algorithms

Ch 01. Analysis of Algorithms

Divide and Conquer Algorithms

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

3.1 Asymptotic notation

Asymptotic Analysis of Algorithms. Chapter 4

Lecture 4. Quicksort

Asymptotic Algorithm Analysis & Sorting

Divide-and-conquer. Curs 2015

Asymptotic Analysis and Recurrences

Divide and Conquer. Recurrence Relations

Notes for Recitation 14

Algorithm Design and Analysis

CSC2100B Data Structures Analysis

Data Structures and Algorithms Chapter 3

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

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

Divide and Conquer Algorithms

Divide-Conquer-Glue Algorithms

Advanced Algorithmics (6EAP)

EECS 477: Introduction to algorithms. Lecture 5

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

Asymptotic Analysis 1

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

A point p is said to be dominated by point q if p.x=q.x&p.y=q.y 2 true. In RAM computation model, RAM stands for Random Access Model.

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

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

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Divide and Conquer. Arash Rafiey. 27 October, 2016

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

Data Structures and Algorithms CSE 465

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

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

1 Divide and Conquer (September 3)

CS473 - Algorithms I

COMPUTER ALGORITHMS. Athasit Surarerks.

CS 4407 Algorithms Lecture 2: Growth Functions

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

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

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Transcription:

Types of formulas for basic operation count Exact formula e.g., C(n) = n(n-1)/2 Algorithms, Design and Analysis Big-Oh analysis, Brute Force, Divide and conquer intro Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n 2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn 2 v1.2 1 v1.2 2 Order of growth Table 2.1 Most important: Order of growth within a constant multiple as n? 8 Example: How much faster will algorithm run on computer that is twice as fast? How much longer does it take to solve problem of double input size? See table 2.1 v1.2 3 v1.2 4 Asymptotic growth rate Big-oh A way of comparing functions that ignores constant factors and small input sizes O(g(n)): class of functions f(n) that grow no faster than g(n) T (g(n)): class of functions f(n) that grow at same rate as g(n) O(g(n)): class of functions f(n) that grow at least as fast as g(n) v1.2 5 v1.2 6 1

Big-omega Big-theta v1.2 7 v1.2 8 Establishing rate of growth: Method 1 using limits L Hôpital s rule lim n? 8 T(n)/g(n) = 0 order of growth of T(n) < order of growth of g(n) c>0 order of growth of T(n) = order of growth of g(n) 8 order of growth of T(n) > order of growth of g(n) If lim n? 8 f(n) = lim n? 8 g(n) = 8 and the derivatives f, g exist, Examples: 10n vs. 2n 2 Then lim n??8 f(n) g(n) = lim n??8 f (n) g (n) n(n+1)/2 vs. n 2 log b n vs. log c n Example: logn vs. n v1.2 9 v1.2 10 Establishing rate of growth: Method 2 using definition f(n) is O(g(n)) if order of growth of f(n) = order of growth of g(n) (within constant multiple) There exist positive constant c and nonnegative integer n 0 such that f(n) = c g(n) for every n = n 0 Examples: 10n is O(2n 2 ) Basic Asymptotic Efficiency classes cubic 1 constant log n logarithmic n linear n log n n log n n 2 quadratic n 3 2 n exponential n! factorial 5n+20 is O(10n) v1.2 11 v1.2 12 2

More Big-Oh Examples Big-Oh Rules 7n-2 7n-2 is O(n) need c > 0 and n 0 1 such that 7n-2 c n for n n 0 this is true for c = 7 and n 0 = 1 3n 3 + 20n 2 + 5 3n 3 + 20n 2 + 5 is O(n 3 ) need c > 0 and n 0 1 such that 3n 3 + 20n 2 + 5 c n 3 for n n 0 this is true for c = 4 and n 0 = 21 3 log n + log log n 3 log n + log log n is O(log n) need c > 0 and n 0 1 such that 3 log n + log log n c log n for n n 0 this is true for c = 4 and n 0 = 2 v1.2 13 If is f(n) a polynomial of degree d, then f(n) is O(n d ), i.e., 1. Drop lower-order terms 2. Drop constant factors Use the smallest possible class of functions Say 2n is O(n) instead of 2n is O(n 2 ) Use the simplest expression of the class Say 3n + 5 is O(n) instead of 3n + 5 is O(3n) v1.2 14 Big-Oh Intuition for Asymptotic Notation f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n) big-omega f(n) is Ω(g(n)) if f(n) is asymptotically greater than or equal to g(n) big-theta f(n) is Θ(g(n)) if f(n) is asymptotically equal to g(n) little-oh f(n) is o(g(n)) if f(n) is asymptotically strictly less than g(n) little-omega f(n) is ω(g(n)) if is asymptotically strictly greater than g(n) Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing a n (a > 0, n a nonnegative integer) 2. Computing n! 3. Selection sort v1.2 15 4. Sequential search v1.2 16 More brute force algorithm examples: Closest pair Problem: find closest among n points in k- dimensional space Algorithm: Compute distance between each pair of points Efficiency : Convex hull Problem: find smallest convex polygon enclosing n points on the plane Algorithm: For each pair of points p 1 and p 2 determine whether all other points lie to the same side of the straight line through p 1 and p 2 v1.2 17 Efficiency : Brute force strengths and weaknesses Strengths: wide applicability simplicity yields reasonable algorithms for some important problems searching string matching matrix multiplication yields standard algorithms for simple computational tasks sum/product of n numbers finding max/min in a list Weaknesses: rarely yields efficient algorithms some brute force algorithms unacceptably slow not as constructive/creative as some other design techniques v1.2 18 3

Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances Divide-and-conquer technique subproblem 1 of size n/2 a problem of size n subproblem 2 of size n/2 2. Solve smaller instances recursively a solution to subproblem 1 a solution to subproblem 2 3. Obtain solution to original (larger) instance by combining these solutions v1.2 19 a solution to the original problem v1.2 20 Divide and Conquer Examples Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplication-strassen s algorithm Convex hull-quickhull algorithm General Divide and Conquer recurrence: T(n) = at(n/b) + f (n) where f (n)? T(n k ) 1. a < b k T(n)? T(n k ) 2. a = b k T(n)? T(n k lg n ) 3. a > b k T(n)? T(n log b a ) Note: the same results hold with O instead of T. v1.2 21 v1.2 22 Mergesort Mergesort Example Algorithm: Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] Sort arrays B and C Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: compare the first elements in the remaining unprocessed portions of the arrays copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A. v1.2 23 7 2 1 6 4 v1.2 24 4

Efficiency of mergesort All cases have same efficiency: T( n log n) Number of comparisons is close to theoretical minimum for comparison-based sorting: log n! n lg n - 1.44 n Space requirement: T( n ) (NOT in-place) Can be implemented without recursion (bottom-up) v1.2 25 Quicksort Select a pivot (partitioning element) Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than the pivot (See algorithm Partition in section 4.2) Exchange the pivot with the last element in the first (i.e., = sublist) the pivot is now in its final position Sort the two sublists p A[i]=p A[i]>p v1.2 26 The partition algorithm Quicksort Example 15 22 13 27 12 10 20 25 v1.2 27 v1.2 28 Efficiency of quicksort Best case: split in the middle T ( n log n) Worst case: sorted array! T ( n 2 ) Average case: random arrays T ( n log n) Improvements: better pivot selection: median of three partitioning avoids worst case in sorted files switch to insertion sort on small subfiles elimination of recursion these combine to 20-25% improvement QuickHull Algorithm Inspired by Quicksort compute Convex Hull: Assume points are sorted by x-coordinate values Identify extreme points P 1 and P 2 (part of hull) Compute upper hull: find point P max that is farthest away from line P 1 P 2 compute the hull of the points to the left of line P 1 P max compute the hull of the points to the left of line P max P 2 Compute lower hull in a similar manner P max Considered the method of choice for internal sorting for large files (n = 10000) v1.2 29 P 1 P 2 v1.2 30 5

Efficiency of QuickHull algorithm Finding point farthest away from line P 1 P 2 can be done in linear time This gives same efficiency as quicksort: Worst case: T( n 2 ) Average case: T( n log n) If points are not initially sorted by x-coordinate value, this can be accomplished in T ( n log n) no increase in asymptotic efficiency class Other algorithms for convex hull: Graham s scan DCHull also in T( n log n) v1.2 31 6