Advanced Algorithmics (6EAP)

Similar documents
Principles of Algorithm Analysis

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

data structures and algorithms lecture 2

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Computational Complexity

Algorithms Design & Analysis. Analysis of Algorithm

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

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CISC 235: Topic 1. Complexity of Iterative Algorithms

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

Growth of Functions (CLRS 2.3,3)

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

csci 210: Data Structures Program Analysis

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

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

CSE 417: Algorithms and Computational Complexity

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

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

CS 4407 Algorithms Lecture 2: Growth Functions

csci 210: Data Structures Program Analysis

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Asymptotic Analysis of Algorithms. Chapter 4

Reading 10 : Asymptotic Analysis

Assignment 5 Bounding Complexities KEY

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Data Structures and Algorithms

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

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

Analysis of Algorithms

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

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

Data Structures and Algorithms. Asymptotic notation

Cpt S 223. School of EECS, WSU

The Time Complexity of an Algorithm

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

Asymptotic Running Time of Algorithms

CS 310 Advanced Data Structures and Algorithms

COMP 355 Advanced Algorithms

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Big-O Notation and Complexity Analysis

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Algorithms and Their Complexity

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Review Of Topics. Review: Induction

Divide and Conquer CPE 349. Theresa Migler-VonDollen

COMP 9024, Class notes, 11s2, Class 1

CS 380 ALGORITHM DESIGN AND ANALYSIS

Ch01. Analysis of Algorithms

The Time Complexity of an Algorithm

Omega notation. Transitivity etc.

Mat Week 6. Fall Mat Week 6. Algorithms. Properties. Examples. Searching. Sorting. Time Complexity. Example. Properties.

Ch 01. Analysis of Algorithms

Student Responsibilities Week 6. Mat Properties of Algorithms. 3.1 Algorithms. Finding the Maximum Value in a Finite Sequence Pseudocode

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

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

EECS 477: Introduction to algorithms. Lecture 5

Module 1: Analyzing the Efficiency of Algorithms

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

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

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

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

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

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

CS173 Running Time and Big-O. Tandy Warnow

Topic 17. Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 1

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

COMP 382: Reasoning about algorithms

Analysis of Algorithms

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

Asymptotic Analysis 1

Data Structures and Algorithms Chapter 2

Design and Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

Asymptotic Algorithm Analysis & Sorting

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

Time complexity analysis

Analysis of Algorithms Review

Data Structures and Algorithms CSE 465

Running Time Evaluation

2. ALGORITHM ANALYSIS

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.

What is Performance Analysis?

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

Reminder of Asymptotic Notation. Inf 2B: Asymptotic notation and Algorithms. Asymptotic notation for Running-time

Introduction to Algorithms: Asymptotic Notation

CS Data Structures and Algorithm Analysis

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

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,

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

Analysis of Algorithms CMPSC 565

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

Transcription:

Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth maths Jaak Vilo 2017 fall Jaak Vilo 1

Program execution on input of size n How many steps/cycles a processor would need to do How to relate algorithm execution to nr of steps on input of size n? f(n) e.g. f(n) = n + n*(n-1)/2 + 17 n + n*log(n)

What happens in infinity? Faster computer, larger input? Bad algorithm on fast computer will be outcompeted by good algorithm on slow

Big-Oh notation classes Class Informal Intuition Analogy f(n) ο ( g(n) ) f is dominated by g Strictly below < f(n) O( g(n) ) Bounded from above Upper bound f(n) Θ( g(n) ) Bounded from above and below equal to = f(n) Ω( g(n) ) Bounded from below Lower bound f(n) ω( g(n) ) f dominates g Strictly above >

Mathematical Background Justification As engineers, you will not be paid to say: or Method A is better than Method B Algorithm A is faster than Algorithm B Such descriptions are said to be qualitative in nature; from the OED: qualitative, a. a Relating to, connected or concerned with, quality or qualities. Now usually in implied or expressed opposition to quantitative.

Mathematical Background Justification Business decisions cannot be based on qualitative statements: Algorithm A could be better than Algorithm B, but Algorithm A would require three person weeks to implement, test, and integrate while Algorithm B has already been implemented and has been used for the past year there are circumstances where it may beneficial to use Algorithm A, but not based on the word better

Mathematical Background Justification Thus, we will look at a quantitative means of describing data structures and algorithms From the OED: quantitative, a. Relating to or concerned with quantity or its measurement; that assesses or expresses quantity. In later use freq. contrasted with qualitative.

Program time and space complexity Time: count nr of elementary calculations/operations during program execution Space: count amount of memory (RAM, disk, tape, flash memory, ) usually we do not differentiate between cache, RAM, In practice, for example, random access on tape impossible

Program 1.17 float Sum(float *a, const int n) { float s = 0; for (int i=0; i<n; i++) s += a[i]; return s; } The instance characteristic is n. Since a is actually the address of the first element of a[], and n is passed by value, the space needed by Sum() is constant (S sum (n)=1).

Program 1.18 float RSum(float *a, const int n) { if (n <= 0) return 0; else return (RSum(a, n-1) + a[n-1]); } n=997 n=998 n=999 n=1000 Each call requires at least 4 words The values of n, a, return value and return address. The depth of the recursion is n+1. The stack space needed is 4(n+1).

Input size = n Input size usually denoted by n Time complexity function = f(n) array[1..n] e.g. 4*n + 3 Graph: n vertices, m edges f(n,m)

So, we may be able to count a nr of operations needed What do we do with this knowledge?

n 2 100 n log n

n 2 100 n log n n = 1000

0.00001*n 2 100 n log n n = 10,000,000

0.00001*n 2 100 n log n n = 2,000,000,000

Logscale y

logscale x logscale y

plot [1:10] 0.01*x*x, 5*log(x), x*log(x)/3

Algorithm analysis goal What happens in the long run, increasing n Compare f(n) to reference g(n) (or f and g ) At some n 0 > 0, if n > n 0, always f(n) < g(n)

Θ, O, and Ω Figure 2.1 Graphic examples of the Θ, O, and Ω notations. In each part, the value of n 0 shown is the minimum possible value; any greater value would also work.

Dominant terms only Essentially, we are interested in the largest (dominant) term only When this grows large enough, it will overshadow all smaller terms

Theorem 1.2 ). ( ) ( Thes,. for, ) ( we have 1,, let Therefore, 1. for, ) ( : Proof ). ( ) ( then,... ) ( If 0 0 0 0 0 0 0 0 1 m m m i i m i i m m i m i i m m i i i m i i i m m m n O n f n n cn n f n a c n a n n a n n a a n n f n O n f a a n n a n f = ³ = = ³ = = + + + = å å å å å = = = - = =

Asymptotic Analysis Given any two functions f(n) and g(n), we will restrict ourselves to: polynomials with positive leading coefficient exponential and logarithmic functions These functions as We will consider the limit of the ratio: n lim n f( n) g( n )

Asymptotic Analysis If the two function f(n) and g(n) describe the run times of two algorithms, and 0 < lim f( n) n g( n) < that is, the limit is a constant, then we can always run the slower algorithm on a faster computer to get similar results

Asymptotic Analysis To formally describe equivalent run times, we will say that f(n) = Q(g(n)) if f( n) 0 < lim n g( n) < Note: this is not equality it would have been better if it said f(n) Q(g(n)) however, someone picked =

Asymptotic Analysis We are also interested if one algorithm runs either asymptotically slower or faster than another lim f( n) n n g( ) < If this is true, we will say that f(n) = O(g(n))

Asymptotic Analysis If the limit is zero, i.e., f( n) lim = g( n) n 0 then we will say that f(n) = o(g(n)) t This is the case if f(n) and g(n) are polynomials where f has a lower degree

Asymptotic Analysis To summarize: f( n) = Ω(g( n)) f( n) lim > g( n) n 0 f( n) = Θ(g( n)) 0 < lim f( n) n g( n) < f( n) = O(g( n)) lim f( n) n n g( ) <

Asymptotic Analysis We have one final case: f( n) = ω(g( n)) f( n) = Θ(g( n)) f( n) = o(g( n)) 0 f( n) lim = n g( n ) f( n) < lim < n g( n) f( n) lim = g( n) n 0

Asymptotic Analysis Graphically, we can summarize these as follows: We say if

Asymptotic Analysis All of n 2 100000 n 2 4 n + 19 n 2 + 1000000 323 n 2 4 n ln(n) + 43 n + 10 42n 2 + 32 n 2 + 61 n ln 2 (n) + 7n + 14 ln 3 (n) + ln(n) are big-q of each other E.g., 42n 2 + 32 = Q( 323 n 2 4 n ln(n) + 43 n + 10 )

Asymptotic Analysis We will focus on these Q(1) Q(ln(n)) Q(n) Q(n ln(n)) Q(n 2 ) Q(n 3 ) constant logarithmic linear n log n quadratic cubic 2 n, e n, 4 n,... exponential O(1)<O(logn)<O(n)<O(nlogn)<O(n 2 )<O(n 3 )<O(2 n )<O(n!)

Growth of functions See Chapter Growth of Functions (CLRS)

Logarithms Algebra cheat sheet: http://tutorial.math.lamar.edu/pdf/algebra_cheat_sheet.pdf

Change of base a b 1 log x = b log a b log a x log b x = Q(log a x)

Big-Oh notation classes Class Informal Intuition Analogy f(n) ο ( g(n) ) f is dominated by g Strictly below < f(n) O( g(n) ) Bounded from above Upper bound f(n) Θ( g(n) ) Bounded from above and below equal to = f(n) Ω( g(n) ) Bounded from below Lower bound f(n) ω( g(n) ) f dominates g Strictly above >

Family of Bachmann Landau notations Notation Name Intuition As, eventually... Definition Big Omicron; Big O; Big Oh f is bounded above by g(up to constant factor) asymptotically for some k or (Note that, since the beginning of the 20th century, papers in number theory have Big Omega been increasingly and widely using this notation in the weaker sense that f = o(g) is false) f is bounded below by g(up to constant factor) asymptotically for some positivek Big Theta f is bounded both above and below bygasymptotically for some positive k 1, k 2 Small Omicron; Small O; Small Oh Small Omega f is dominated by gasymptotically f dominates gasympt otically for every ε for every k on the order of; "twiddles" f is equal to gasymptotically

googol

How much time does sorting take? Comparison-based sort: A[i] <= A[j] Upper bound current best-known algorithm Lower bound theoretical at least estimate If they are equal, we have theoretically optimal solution

Simple sort for i=2..n for j=i ; j>1 ; j-- if A[j] < A[j-1] then swap( A[j], A[j-1] ) else next i

The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions.

Merge sort Merge-Sort(A,p,r) if p<r then q = (p+r)/2 Merge-Sort( A, p, q ) Merge-Sort( A, q+1,r) Merge( A, p, q, r ) It was invented by John von Neumann in 1945.

Example Applying the merge sort algorithm:

Wikipedia / viz. Value Pos in array

Quicksort

Wikipedia / video

Conclusions Algorithm complexity deals with the behavior in the long-term worst case -- typical average case -- quite hard best case -- bogus, cheating In practice, long-term sometimes not necessary E.g. for sorting 20 elements, you don t need fancy algorithms

n 2.3727 n 2.376