Analysis of Algorithmic Efficiency

Slides:



Advertisements
Similar presentations
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Advertisements

Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Week 2 CS 361: Advanced Data Structures and Algorithms
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
2.1 Computational Tractability. 2 Computational Tractability Charles Babbage (1864) As soon as an Analytic Engine exists, it will necessarily guide the.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Asymptotic Analysis-Ch. 3
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Lecture 3 Analysis of Algorithms, Part II. Plan for today Finish Big Oh, more motivation and examples, do some limit calculations. Little Oh, Theta notation.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
Algorithm Analysis Part of slides are borrowed from UST.
Page 1 adapted after Dr. Menezes Analysis of Algorithms What does it mean to analyze ? Strictly speaking analysis is the separation of an intellectual.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
UNIT-I FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 2:
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Section 1.7 Comparing Algorithms: Big-O Analysis.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
Asymptotic Complexity
Algorithm Analysis 1.
CMPT 438 Algorithms.
Theoretical analysis of time efficiency
Analysis of Algorithms
Analysis of algorithms
Analysis of algorithms
Introduction to Algorithms: Asymptotic Notation
Analysis of Algorithms
Introduction to Algorithms
CS 3343: Analysis of Algorithms
Analysis of algorithms
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
CS 3343: Analysis of Algorithms
Analysis of algorithms
Algorithm Analysis (not included in any exams!)
Asymptotic Notations Algorithms Lecture 9.
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Data Structures Review Session
CS 3343: Analysis of Algorithms
COSC 320 Advanced Data Structures and Algorithm Analysis
CS 201 Fundamental Structures of Computer Science
DS.A.1 Algorithm Analysis Chapter 2 Overview
Analysis of Algorithms
Chapter 2.
CSE 2010: Algorithms and Data Structures Algorithms
Fundamentals of the Analysis of Algorithm Efficiency
At the end of this session, learner will be able to:
Analysis of algorithms
David Kauchak cs161 Summer 2009
Design and Analysis of Algorithms
Fundamentals of the Analysis of Algorithm Efficiency
Estimating Algorithm Performance
Algorithm/Running Time Analysis
Presentation transcript:

Analysis of Algorithmic Efficiency Textbook Section 2.1-2.2 Analysis of Algorithmic Efficiency

Announcements Questions on HW 1? No Wed afternoon office hours

Efficiency Analysis Relative to input size (n) Measured in basic operations not seconds Interested in order of growth How did your data structures in HW 1 compare with only 100 queries?

Orders of Growth What happens when you double the input size (n)? O(1) O(log2n) O(n) O(n2) O(n3) O(2n)

Worst, Best and Average Case What is efficiency of linear search? LinearSearch(A[0…n-1], K) i ← 0 while i < n and A[i] ≠ K do i ← i + 1 if i < n return i else return -1

Average Case Assume: Probability of successful search is p (0 ≤ p ≤ 1) Uniform probability of finding at each position 𝐶 𝑎𝑣𝑔 𝑛 = 1∙ 𝑝 𝑛 +2∙ 𝑝 𝑛 + …+𝑛∙ 𝑝 𝑛 +𝑛∙ 1−𝑝 = 𝑝 𝑛 1+2+ …+𝑛 +𝑛 1−𝑝 = 𝑝 𝑛 𝑛 𝑛+1 2 +𝑛 1−𝑝 = 𝑝 𝑛+1 2 +𝑛 1−𝑝 What if p = 0? p = 1?

Worst, Best and Average Case Worst Case: guarantees runtime will never exceed Cworst(n) Best Case: if (near) best input covers useful instances, can be worth knowing Cbest(n) e.g. Sorting mostly sorted list Avg. Case: hard to obtain, but important because worst case may be overly pessimistic Amortized: single op may be expensive, but less so for each subsequent one.

Asymptotic Notation (Informally) Prove runtime t(n) of an algorithm by proving bounds. O(n) = upper bound O(n2): all fns whose order of growth is no higher than n2. Ω(n) = lower bound Ω(n2): all fns whose order of growth is no lower than n2. Θ(n) e.g. Θ(n2): all fns whose order of growth is same as n2.

Asymptotic Notation (Informally) Which of following belong to O(n2)? Ω(n2)? Θ(n2)? a(x) = 100n + 5 b(x) = ½ n(n-1) c(x) = 0.00001n3 d(x) = n2 + log n e(x) = n + sin n f(x) = 2n

Basic Efficiency Classes Name Comments 1 constant Short of best case efficiency, very few algorithms belong to this class. log n logarithmic Typically result of cutting problem size by constant factor on each iteration (Ch. 4) Doesn’t examine all/most of input. n linear Algorithms that scan all of the input, e.g. linear search. n log n linearithmic log-linear Many divide and conquer algorithms (Ch. 5) belong to this category, e.g. mergesort and quicksort. n2 quadratic Characterizes efficiency of algorithms with 2 embedded loops, e.g. simple sorting algorithms and operations on nxn matrices. n3 cubic Characterizes efficiency of algorithms with 3 embedded loops, e.g. nontrivial algorithms from linear algebra. 2n exponential Typical for algorithms that generate all subsets of n-item set. n! factorial Typical for algorithms that generate all permutations of n-item set.

O-Notation (formal) Function t n ∈ O g n if ∃ some postive constant c and some nonnegative integer n 0 𝑠.𝑡. 𝑡 𝑛 ≤𝑐∙𝑔 𝑛 ∀𝑛 ≥ 𝑛 0 e.g., prove that 100𝑛+5 ∈𝑂( 𝑛 2 )

O-Notation (formal) Function t n ∈ O g n if ∃ some postive constant c and some nonnegative integer n 0 𝑠.𝑡. 𝑡 𝑛 ≤𝑐∙𝑔 𝑛 ∀𝑛 ≥ 𝑛 0 e.g., prove that 100𝑛+5 ∈𝑂( 𝑛 2 ) 100𝑛+5 ≤100𝑛+𝑛 ∀𝑛 ≥5 ≤101𝑛 ≤101 𝑛 2 𝑡ℎ𝑢𝑠 𝑐=101 𝑎𝑛𝑑 𝑛 0 =5

Many possible values satisfy. O-Notation (formal) Function t n ∈ O g n if ∃ some postive constant c and some nonnegative integer n 0 𝑠.𝑡. 𝑡 𝑛 ≤𝑐∙𝑔 𝑛 ∀𝑛 ≥ 𝑛 0 e.g., prove that 100𝑛+5 ∈𝑂( 𝑛 2 ) 100𝑛+5 ≤100𝑛+5𝑛 ∀𝑛 ≥1 ≤105𝑛 ≤105 𝑛 2 𝑡ℎ𝑢𝑠 𝑐=105 𝑎𝑛𝑑 𝑛 0 =1 Many possible values satisfy.

Example Prove that 3 𝑛 3 +6 𝑛 2 +3𝑛 3𝑛 is in O(n2). 𝑐 0 ∙ 𝑛 2

Ω-Notation (formal) Function t n ∈ Ω g n if ∃ some postive constant c and some nonnegative integer n 0 𝑠.𝑡. 𝑡 𝑛 ≥𝑐∙𝑔 𝑛 ∀𝑛 ≥ 𝑛 0 e.g., prove that 100 𝑛 3 +5 𝑛 2 ∈Ω( 𝑛 2 )

Ω-Notation (formal) Function t n ∈ Ω g n if ∃ some postive constant c and some nonnegative integer n 0 𝑠.𝑡. 𝑡 𝑛 ≥𝑐∙𝑔 𝑛 ∀𝑛 ≥ 𝑛 0 e.g., prove that 100 𝑛 3 +5 𝑛 2 ∈Ω( 𝑛 2 ) 100 𝑛 3 +5 𝑛 2 ≥ 5𝑛 2 ≥ 𝑛 2 ∀𝑛 ≥0 𝑡ℎ𝑢𝑠 𝑐=1 𝑎𝑛𝑑 𝑛 0 =0

Θ-Notation (formal) Function t n ∈ Θ g n if ∃ some postive constants c1 and c2 and some nonnegative integer n 0 𝑠.𝑡. 𝑐1∙𝑔 𝑛 ≤𝑡 𝑛 ≤𝑐2∙𝑔 𝑛 ∀𝑛 ≥ 𝑛 0 Your turn: prove that 1 2 𝑛 𝑛−1 ∈Θ 𝑛 2

Visually…

Useful Theorem THEOREM: 𝑖𝑓 𝑡 1 𝑛 ∈𝑂 𝑔 1 𝑛 𝑎𝑛𝑑 𝑡 2 𝑛 ∈𝑂 𝑔 2 𝑛 , 𝑡ℎ𝑒𝑛 𝑡 1 𝑛 + 𝑡 2 𝑛 ∈𝑂( max 𝑔 1 𝑛 , 𝑔 2 𝑛 ) Efficiency of an algorithm with 2 consecutively executed parts determined by part with higher order of growth.

Limit Ratios Alternative for proving order of growth: Which bounds are implied by the 3 parts?

Limit Ratios Alternative for proving order of growth: Which bounds are implied by the 3 parts? Can use calculus techniques for computing limits, e.g. L’Hôpital’s rule: lim 𝑛→∞ 𝑡(𝑛) 𝑔(𝑛) = lim 𝑛→∞ 𝑡 ′ (𝑛) 𝑔 ′ (𝑛) and, Stirling’s formula: 𝑛! ≈ 2𝜋𝑛 𝑛 𝑒 𝑛

Limit Ratio Examples lim 𝑛→∞ 1 2 𝑛(𝑛−1) 𝑛 2 lim 𝑛→∞ log 2 𝑛 𝑛

Lab Lab 3 More examples available in textbook