Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

5/1/20151 Analysis of Algorithms Introduction. 5/1/20152 Are you want to be a computer scientist?
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 3 Growth of Functions
Introduction to Analysis of Algorithms
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Data Structures Types of Data Structures Algorithms
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Insertion Sort CSE 331 Section 2 James Daly. Insertion Sort Basic idea: keep a sublist sorted, then add new items into the correct place to keep it sorted.
Analysis of Performance
Instructor Neelima Gupta
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
Design and Analysis Algorithm Drs. Achmad Ridok M.Kom Fitra A. Bachtiar, S.T., M. Eng Imam Cholissodin, S.Si., M.Kom Aryo Pinandito, MT Pertemuan 04.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
MS 101: Algorithms Instructor Neelima Gupta
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
Asymptotic Analysis-Ch. 3
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
DR. Gatot F. Hertono, MSc. Design and Analysis of ALGORITHM (Session 2)
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
MS 101: Algorithms Instructor Neelima Gupta
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Time Complexity of Algorithms (Asymptotic Notations)
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Analysis of Algorithms Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Data Structures & Algorithm CS-102 Lecture 12 Asymptotic Analysis Lecturer: Syeda Nazia Ashraf 1.
Mathematical Foundations (Growth Functions) Neelima Gupta Department of Computer Science University of Delhi people.du.ac.in/~ngupta.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Analysis of Algorithms
Analysis of Algorithms
Advanced Analysis of Algorithms
Analysis of Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
Performance Evaluation
Introduction To Algorithms
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

Asymptotic Performance

Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running time Memory/storage requirements Remember that we use the RAM model: All memory equally expensive to access No concurrent operations All reasonable instructions take unit time Except, of course, function calls Constant word size Unless we are explicitly manipulating bits

Review: Running Time Number of primitive steps that are executed Except for time of executing a function call most statements roughly require the same amount of time We can be more exact if need be Worst case vs. average case

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i =  j =  key =  A[j] =  A[j+1] = 

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 2j = 1key = 10 A[j] = 30 A[j+1] = 10

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 2j = 1key = 10 A[j] = 30 A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 2j = 1key = 10 A[j] = 30 A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 2j = 0key = 10 A[j] =  A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 2j = 0key = 10 A[j] =  A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 2j = 0key = 10 A[j] =  A[j+1] = 10

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 3j = 0key = 10 A[j] =  A[j+1] = 10

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 3j = 0key = 40 A[j] =  A[j+1] = 10

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 3j = 0key = 40 A[j] =  A[j+1] = 10

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 3j = 2key = 40 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 3j = 2key = 40 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 3j = 2key = 40 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 40 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 20 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 20 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 3key = 20 A[j] = 40 A[j+1] = 20

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 3key = 20 A[j] = 40 A[j+1] = 20

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 3key = 20 A[j] = 40 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 3key = 20 A[j] = 40 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 3key = 20 A[j] = 40 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 20 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 20 A[j] = 30 A[j+1] = 40

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 20 A[j] = 30 A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 2key = 20 A[j] = 30 A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 1key = 20 A[j] = 10 A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 1key = 20 A[j] = 10 A[j+1] = 30

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 1key = 20 A[j] = 10 A[j+1] = 20

An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } i = 4j = 1key = 20 A[j] = 10 A[j+1] = 20 Done!

Animating Insertion Sort Check out the Animator, a java applet at: r.html r.html Try it out with random, ascending, and descending inputs

Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } What is the precondition for this loop?

Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } How many times will this loop execute?

Insertion Sort Statement Effort InsertionSort(A, n) { for i = 2 to n { c 1 n key = A[i] c 2 (n-1) j = i - 1; c 3 (n-1) while (j > 0) and (A[j] > key) { c 4 T A[j+1] = A[j] c 5 (T-(n-1)) j = j - 1 c 6 (T-(n-1)) } 0 A[j+1] = key c 7 (n-1) } 0 } T = t 2 + t 3 + … + t n where t i is number of while expression evaluations for the i th for loop iteration

Analyzing Insertion Sort T(n) = c 1 n + c 2 (n-1) + c 3 (n-1) + c 4 T + c 5 (T - (n-1)) + c 6 (T - (n-1)) + c 7 (n-1) = c 8 T + c 9 n + c 10 What can T be? Best case -- inner loop body never executed t i = 1  T(n) is a linear function Worst case -- inner loop body executed for all previous elements t i = i  T(n) is a quadratic function Average case ???

Analysis Simplifications Ignore actual and abstract statement costs Order of growth is the interesting measure: Highest-order term is what counts Remember, we are doing asymptotic analysis As the input size grows larger it is the high order term that dominates

Growth of Functions Asymptotic: Growth of running time of algorithm is relevant to growth of input size Asymptotic Notations: These are the functions with domains of natural numbers Normally worst case running time is considered These notations are abused but not misused

Asymptotic Notations Following are various asymptotic notations Big O notation: i.e. O(g(n 2 )) Theta notation: i.e.  (g(n)) Omega Notation: i.e.  (n) Small o notation: i.e. o(g(n 2 ))

Big O Notation (Asymptotic Upper Bound) Asymptotic Upper Bound In general a function f(n) is O(g(n)) if there exist positive constants c and n 0 such that f(n)  c  g(n) for all n  n 0 Formally O(g(n)) = { f(n):  positive constants c and n 0 such that f(n)  c  g(n)  n  n 0

Upper Bound Notation We say Insertion Sort’s run time is O(n 2 ) Properly we should say run time is in O(n 2 ) Read O as “Big-O” (you’ll also hear it as “order”)

Insertion Sort Is O(n 2 ) Proof Suppose runtime is an 2 + bn + c If any of a, b, and c are less than 0 replace the constant with its absolute value an 2 + bn + c  (a + b + c)n 2 + (a + b + c)n + (a + b + c)  3(a + b + c)n 2 for n  1 Let c’ = 3(a + b + c) and let n 0 = 1 Question Is InsertionSort O(n 3 )? Is InsertionSort O(n)?

Big O Fact A polynomial of degree k is O(n k ) Proof: Suppose f(n) = b k n k + b k-1 n k-1 + … + b 1 n + b 0 Let a i = | b i | f(n)  a k n k + a k-1 n k-1 + … + a 1 n + a 0

Big O Notation  (g(n)) ≤ O (g(n)) Any quadratic function in  (n 2 ) is also in O(n 2) Any linear function an+b is also in O(n 2) It is normal to distinguish the asymptotic upper bound with asymptomatic tight bound. O(g(n)) is applicable for every inputs whereas it is not the case for  (g(n))

Big O Notation

Asymptotic Tight Bound: Theta Notation A function f(n) is  (g(n)) if  positive constants c 1, c 2, and n 0 such that c 1 g(n)  f(n)  c 2 g(n)  n  n 0 f(n) is asymptomatically tight bound for f(n) Theorem f(n) is  (g(n)) iff f(n) is both O(g(n)) and  (g(n)) Proof: someday

Theta Notation Every member f(n) E g(n) must be non negative Example: f(n)=(½)n 2 – 3n =  (n 2 ) Find c 1, c 2 and n 0 C 1 (n 2 )  f(n)  C 2 (n 2 ) After calculation C 1  1/14, C 2  ½ Other values of constants may be found depending upon the function

Theta Notation The function is sandwiched

Lower Bound Notation We say InsertionSort’s run time is  (n) In general a function f(n) is  (g(n)) if  positive constants c and n 0 such that 0  c  g(n)  f(n)  n  n 0 Proof: Suppose run time is an + b Assume a and b are positive (what if b is negative?) an  an + b It purpose is to show that running time will always remain upper than given limit

Lower Bound Notation

Theorem For any functions f(n) and g(n), we have f(n)=  (g(n)) if and only if f(n)=O(g(n)) and f(n)=  (g(n)) Proof: An 2 +bn+c=  (g(n 2 )) for any constant a,b,c where a>0

Practical Complexity

Other Asymptotic Notations A function f(n) is o(g(n)) if  positive constants c and n 0 such that f(n) < c g(n)  n  n 0 A function f(n) is  (g(n)) if  positive constants c and n 0 such that c g(n) < f(n)  n  n 0 Intuitively,  o() is like <  O() is like    () is like >   () is like    () is like =

Up Next Solving recurrences Substitution method Master theorem