CSCE5150 Analysis of Computer Algorithms Dr. Xuan Guo 1 Content • Asymptotic notation • • • • • Big Oh Big Omega Big Theta little Oh little Omega • Asymptotic Dominance • Recursive algorithms • Merge Sort • Resolving Recurrences • Substitution method • Recursion trees • Master method 2 1 Problem Solving: Main Steps 1. 2. 3. 4. 5. 6. Problem definition Algorithm design / Algorithm specification Algorithm analysis Implementation Testing [Maintenance] 3 Algorithm Analysis • Goal • Predict the resources that an algorithm requires • What kind of resources? • • • • Memory Communication bandwidth Hardware requirements Running time • Two approaches • Empirical tests • Mathematical analysis 4 2 Algorithm Analysis – Empirical Tests • Steps: • Implement algorithm in a given programming language • Measure runtime with several inputs • Infer running time for any input • Pros: • No math, straightforward method • Cons: • Not reliable, heavily dependent on • the sample inputs • programming language and environment • We want to analyze algorithms to decide whether they are worth implementing 5 Algorithm Analysis – Mathematical Analysis • Use math to estimate the running time of an algorithm • almost always dependent on the size of the input • Algorithm1 running time is ๐2 for an input of size ๐ • Algorithm2 running time is ๐ × log ๐ for an input of size ๐ • Pros: • formal, rigorous • no need to implement algorithms • machine-independent • Cons: • math knowledge 6 3 Algorithm Analysis • Best case analysis • shortest running time for any input of size ๐ • often meaningless, one can easily cheat • Worst case analysis • • • • longest running time for any input of size ๐ it guarantees that the algorithm will not take any longer provides an upper bound on the running time worst case occurs often search for an item that does not exist • Average case analysis • running time averaged for all possible inputs • it is hard to estimate the probability of all possible inputs 7 Random-Access Machine (RAM) In order to predict running time, we need a (simple) computational model: the Random-Access Machine (RAM) • Instructions are executed sequentially • No concurrent operations • Each basic instruction takes a constant amount of time • • • • arithmetic: add, subtract, multiply, divide, remainder, floor, ceiling, shift left/shift right data movement: load, store, copy control: conditional/unconditional branch, subroutine call and return Loops and subroutine calls are not simple operations. They depend upon the size of the data and the contents of a subroutine. “Sort” is not a single step operation. • Each memory access takes exactly 1 step. We measure the run time of an algorithm by counting the number of steps. RAM model is useful and accurate in the same sense as the flat-earth model (which is useful)! 8 4 The RAM Model of Computation • The worst-case (time) complexity of an algorithm is the function defined by the maximum number of steps taken on any instance of size ๐. • The best-case complexity of an algorithm is the function defined by the minimum number of steps taken on any instance of size ๐. • The average-case complexity of the algorithm is the function defined by an average number of steps taken on any instance of size ๐. • Each of these complexities defines a numerical function: time vs. size! 9 Asymptotic Notation – Big Oh, ๐ • In plain English: ๐(๐(๐)) are all functions ๐(๐) for which there exists two positive constants ๐ and ๐0 such that for all ๐ ≥ ๐0 , 0 ≤ ๐(๐) ≤ ๐๐(๐) • ๐(๐) is an asymptotic upper bound for ๐(๐) • Intuitively, you can think of ๐ as “≤” for functions • If ๐(๐) ∈ ๐(๐(๐)), we write ๐(๐) = ๐(๐(๐)) • The definition implies a constant ๐0 beyond which they are satisfied. We do not care about small values of ๐. 10 5 Examples • Is 2๐+1 = ๐ 2๐ ? Is 22๐ = ๐ 2๐ ? 11 Examples • Is 2๐+1 = ๐ 2๐ ? Is 22๐ = ๐ 2๐ ? 12 6 Asymptotic Notation – Big Omega, Ω • In plain English: โฆ(๐(๐)) are all function ๐(๐) for which there exists two positive constants ๐ and ๐0 such that for all ๐ ≥ ๐0 , 0 ≤ ๐๐ ๐ ≤ ๐ ๐ • ๐(๐) is an asymptotic lower bound for ๐ ๐ • Intuitively, you can think of โฆ as “≥” for functions 13 Asymptotic Notation – Theta, Θ • In plain English: Θ(๐(๐)) are all function ๐(๐) for which there exists three positive constants ๐1 , ๐2 , and ๐0 such that for all ๐ ≥ ๐0 , 0 ≤ ๐1 ๐(๐) ≤ ๐(๐) ≤ ๐2 ๐(๐) • In other words, all functions that grow at the same rate as ๐(๐) • ๐(๐) is an asymptotically tight bound for ๐(๐) • Intuitively, you can think of Θ as “=” for functions 14 7 Asymptotic Notation – Theta, Θ 15 Asymptotic notation in equations and inequalities • On the right-hand side alone of an equation (or inequality) ≡ a set of functions • ex., ๐ = ๐ ๐2 ↔ ๐ ∈ ๐ ๐2 • In general, in a formula, stands for some anonymous function that we do not care to name • ex., 2๐2 + 3๐ + 1 = 2๐2 + Θ ๐ ↔ 2๐2 + 3๐ + 1 = 2๐2 + ๐ ๐ , where ๐ ๐ =Θ ๐ • help eliminate inessential detail and clutter in a formula • ex., ๐ ๐ = 2๐ ๐ 2 +Θ ๐ 16 8 Asymptotic Notation – little oh, ๐ • In plain English: ๐(๐(๐)) are all function ๐(๐) for which for all constant ๐ > 0, there exists a constant ๐0 > 0 such that for all ๐ ≥ ๐0 , 0 ≤ ๐(๐) < ๐๐(๐) • ๐(๐(๐)) is an asymptotic upper bound for ๐(๐), but not tight • ๐(๐) becomes insignificantly relative to ๐(๐) as ๐ grows • ๐(๐) grows asymptotically slower than ๐(๐) • Similar to ๐(๐(๐)), intuitively, you can think of ๐ as “<” for functions 17 Asymptotic Notation – little oh, ๐ Examples • ๐1.999 = ๐ ๐2 • ๐2 log ๐ 2 = ๐ ๐2 • ๐ ≠ ๐ ๐2 • ๐2 1000 ≠ ๐ ๐2 18 9 Asymptotic Notation – little Omega, ๐ • In plain English: ๐(๐(๐)) are all function ๐(๐) for which for all constant ๐ > 0, there exists a constant ๐0 > 0 such that for all ๐ ≥ ๐0 , 0 ≤ ๐๐(๐) < ๐(๐) • • • • ๐(๐(๐)) is an asymptotic lower bound for ๐(๐), but not tight ๐(๐) becomes arbitrarily large relative to ๐(๐) as ๐ grows ๐(๐) grows asymptotically faster than ๐(๐) Similar to โฆ(๐(๐)) • Intuitively, you can think of ๐ as “>” for functions 19 Asymptotic Notation – little Omega, ๐ Examples: • ๐2.0001 = ๐ ๐2 • ๐2 log ๐ = ๐ ๐2 • ๐2 ≠ ๐ ๐2 20 10 Asymptotic Notation • Asymptotic notation is a way to compare functions • • • • • ๐ ≈≤ โฆ ≈≥ Θ ≈= ๐ ≈< ๐ ≈> • When using asymptotic notations, sometimes, • drop lower-order terms • ignore constant coefficient in the leading term 21 Asymptotic Notation Multiplication by Constant • Multiplication by a constant does not change the asymptotic: ๐(๐ · ๐(๐)) → ๐(๐(๐)) โฆ(๐ · ๐(๐)) → โฆ(๐(๐)) Θ(๐ · ๐(๐)) → Θ(๐(๐)) • The “old constant” ๐ถ from the Big Oh becomes ๐ · ๐ถ. 22 11 Asymptotic Notation Multiplication by Function • But when both functions in a product are increasing, both are important: ๐(๐(๐)) · ๐(๐(๐)) → ๐(๐(๐) · ๐(๐)) โฆ(๐(๐)) · โฆ(๐(๐)) → โฆ(๐(๐) · ๐(๐)) Θ(๐(๐)) · Θ(๐(๐)) → Θ(๐(๐) · ๐(๐)) This is why the running time of two nested loops is ๐(๐2 ). 23 Testing Dominance ๐ ๐ • ๐(๐) dominates ๐(๐) if lim = 0, which is the same as saying ๐→∞ ๐ ๐ ๐(๐) = ๐(๐(๐)). • Note the little-oh – it means “grows strictly slower than”. 24 12 Dominance Rankings • You must come to accept the dominance ranking of the basic functions: • ๐! โซ 2๐ โซ ๐3 โซ ๐2 โซ ๐ log ๐ โซ ๐ โซ log ๐ โซ 1 25 Advanced Dominance Rankings • Additional functions arise in more sophisticated analysis: • ๐! โซ ๐ ๐ โซ ๐3 โซ ๐2 โซ ๐1+๐ โซ ๐ log ๐ โซ ๐ โซ ๐ โซ log ๐ log ๐ โซ log ๐เตlog log ๐ โซ log log ๐ โซ 1 2 โซ 26 13 Logarithms • It is important to understand deep in your bones what logarithms are and where they come from. • A logarithm is simply an inverse exponential function. • Saying ๐ ๐ฅ = ๐ฆ is equivalent to saying that ๐ฅ = log ๐ ๐ฆ. • Logarithms reflect how many times we can double something until we get to ๐ or halve something until we get to 1. 27 The Base is not Asymptotically Important • Recall the definition, ๐ log๐ ๐ฅ = ๐ฅ and that • log ๐ ๐ = log๐ ๐ log๐ ๐ 1 • Thus, log 2 ๐ = (1/ log100 2) × log100 ๐. Since = 6.643 is just log100 2 a constant, it does not matter in the Big Oh. 28 14 Analyzing Merge Sort 29 Analyzing Merge Sort – Merge Procedure 30 15 Analyzing Merge Sort Input: {10,5,7,6,1,4,8,3} Output: {1,3,4,5,6,7,9,10} 31 Analyzing Algorithms – recursive algorithms • Divide-and-conquer paradigm • solve a problem for a given input of size ๐ • You don’t know how to solve it for any ๐, but • you can divide the problem into smaller subproblems (input sizes ๐′ < ๐) • you can conquer (solve) the subproblems recursively – you need a base case: for a small enough input solving the problem is straightforward • you can combine the solutions to the subproblems to solve the original problem with input size ๐ • Recursive algorithms call themselves with a different input • we cannot just count the number of instructions 32 16 Analyzing Merge Sort – running time • In plain English, the cost of merge sort, i.e., ๐(๐) is • a constant if ๐ <= 1; Θ(1) • the cost of dividing, solving the subproblems and combining the subproblems, if ๐ > 1 • dividing: Θ(๐) • conquering (solving) subproblems: 2 × ๐(๐/2) • combining subproblems: Θ(๐) 33 Analyzing Merge Sort – running time • Formally Θ(1), if ๐= 1 ๐(๐) = แ 2T(๐/2) + Θ(๐), if ๐> 1 How do we solve a recurrence? (Solving a recurrence relation means obtaining a closedform solution: a non-recursive function of ๐.) 34 17 Resolving Recurrences • Substitution method • Guess a solution and check if it is correct • Pros: rigorous, formal solution • Cons: requires induction, how do you guess? • Recursion trees • Build a tree representing all recursive calls, infer the solution • Pros: intuitive, visual, can be used to guess • Cons: not too formal • Master method • Check the cookbook and apply the appropriate case • Pros: formal, useful for most recurrences • Cons: have to memorize, useful for most recurrence 35 Resolving Recurrences – Substitution Method • Steps • Guess the solution • Use induction to show that the solution works • What is induction? • A method to prove that a given statement is true for all-natural numbers ๐ • Two steps • Base case: show that the statement is true for the smallest value of ๐ (typically 0) • Induction step: assuming that the statement is true for any ๐, show that it must also hold for ๐ + 1 36 18 Resolving Recurrences – Substitution Method, induction • A more serious example •๐ ๐ =แ 2๐ 1 ๐ 2 ๐๐ ๐ = 1 +๐ ๐๐ ๐ > 1 • guess ๐ ๐ = ๐ log ๐ + ๐ • ๐ ๐ = Θ ๐ log ๐ 37 38 19 Resolving Recurrences – Substitution Method, induction • When we want an asymptotic solution to a recurrence, we don’t worry about the base cases in our proof. • Since we are ultimately interested in an asymptotic solution to a recurrence, it will always be possible to choose base cases that work. • When we want an exact solution, then we have to deal with base case. 39 Resolving Recurrences – Substitution Method, induction • When solving recurrences with asymptotic notation • assume ๐(๐) = Θ(1) for small enough ๐, don’t worry about base case • show upper and lower bounds separately (๐, โฆ) • Example • ๐(๐) = 2๐(๐/2) + Θ(๐) 40 20 Resolving Recurrences – Recursion Trees • Draw a “recursion tree” for the recurrence • Nodes are the cost of a single subproblem • Nodes have as their children the subproblems they are decomposed into • Example • ๐(๐) = 2๐(๐/2) + ๐ 41 Example -- Recursion Tree Method • Example • ๐ ๐ =๐ ๐ 4 +๐ ๐ 2 + Θ(๐2 ) • For upper bound, rewrite as ๐ ๐ ≤ ๐ • For lower bound, as ๐ ๐ = Ω(๐2 ) ๐ 4 +๐ ๐ 2 + ๐๐2 42 21 Asymptotic Behavior of a Geometric Series • Sum a geometric series 3 3 2 4 4 • ๐ = ๐2 + ๐2 + • σ๐๐=1 ๐๐ = ๐ 1−๐ ๐ 1−๐ ๐2 + โฏ + , σ∞ ๐=1 ๐๐ = ๐ 3 ๐ 4 1 1−๐ ๐2 if ๐ < 1 • A decreasing geometric series behaves asymptotically just like its 1st term: ๐ = Θ ๐2 • By symmetry, if S were increasing, it would behave asymptotically like its final term: ๐ = ๐2 + 2๐2 + 22 ๐2 + โฏ + 2๐ ๐2 = Θ 2๐ ๐2 43 Resolving Recurrences – Master Method • “Cookbook” for many divide-and-conquer recurrences of the form ๐(๐) = ๐๐(๐/๐) + ๐(๐), where • ๐≥1 • ๐≥1 • ๐(๐) is an asymptotically positive function • What are ๐, ๐, and ๐(๐)? • ๐ is a constant corresponding to ... • ๐ is a constant corresponding to ... • ๐(๐) is a function corresponding to ... 44 22 Resolving Recurrences – Master Method • Problem: You want to solve a recurrence of the form ๐(๐) = ๐๐(๐/๐) + ๐(๐), where • ๐ ≥ 1, ๐ ≥ 1 and ๐(๐) is asymptotically positive function • Solution: • Compare ๐log๐ ๐ vs. ๐(๐), and apply the Master Theorem 45 Resolving Recurrences – Master Method • Intuition: the larger of the two functions determines the solution • The three cases compare ๐(๐) with ๐log๐ ๐ • Case 1: ๐log๐ ๐ is larger – ๐ definition • Case 2: ๐(๐) and ๐log๐ ๐ grow at the same rate – Θ definition • Case 3: ๐(๐) is larger – โฆ definition • The three cases do not cover all possibilities • but they cover most cases we are interested in 46 23 The Master Theorem • Case 1: If ๐ ๐ = ๐ ๐ then ๐ ๐ = Θ ๐log๐ ๐ log๐ ๐ −๐ for some ๐ > 0 • Case 2: If ๐ ๐ = Θ ๐log๐ ๐ then ๐ ๐ = Θ ๐log๐ ๐ log ๐ • Case 3: If ๐ ๐ = Ω ๐ log๐ ๐ +๐ for some ๐ > 0 and if ๐๐ ๐Τ๐ ≤ ๐๐ ๐ for some ๐ < 1 and all sufficiently large ๐ (regularity condition), then ๐ ๐ = Θ ๐ ๐ A function ๐ ๐ is polynomially bounded if ๐ ๐ = ๐ ๐๐ for some constant ๐. You need to memorize these rules and be able to use them 47 The Master Theorem Case 2: If ๐ ๐ = Θ ๐log๐ ๐ then ๐ ๐ = Θ ๐log๐ log ๐ ๐ ๐ log ๐ for some constant ๐ ≥ 0 ๐+1 48 24 Resolving Recurrences – Master Method, examples ๐(๐) = ๐๐(๐/๐) + ๐(๐), ๐log๐ ๐ vs. ๐(๐) • Examples: • ๐ ๐ = 4๐ ๐ • ๐ ๐ = 4๐ ๐ • ๐ ๐ = 4๐ ๐ 2 2 2 +n + ๐2 + ๐3 49 Resolving Recurrences – Master Method, examples • Examples: • ๐ ๐ = 5๐ ๐ 2 + Θ ๐3 50 25 Resolving Recurrences – Master Method, examples • Examples: • ๐ ๐ = 27๐ ๐ 3 +Θ ๐3เต log ๐ 51 Idea of master theorem 52 26 Resolving Recurrences – Master Method • Case 3: If ๐ ๐ = Ω ๐ log๐ ๐ +๐ for some ๐ > 0 and if ๐๐ ๐Τ๐ ≤ ๐๐ ๐ for some ๐ < 1 and all sufficiently large ๐, then ๐ ๐ = Θ ๐ ๐ The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. Intuition: cost is dominated by root, we can discard the rest 53 Resolving Recurrences – Master Method • Case 1: If ๐ ๐ = ๐ ๐ log๐ ๐ −๐ for some ๐ > 0 then ๐ ๐ = Θ ๐log๐ ๐ ๐(๐) is polynomially smaller than ๐log๐ ๐ Intuition: cost is dominated by the leaves 54 27 Resolving Recurrences – Master Method • Case 2: If ๐ ๐ = Θ ๐log๐ ๐ then ๐ ๐ = Θ ๐log๐ ๐ log ๐ ๐(๐) grows at the same rate of ๐log๐ ๐ Intuition: cost ๐log๐ ๐ at each level, there are log ๐ levels 55 Resolving Recurrences – Master Method Case 3: If ๐ ๐ = Ω ๐ log๐ ๐ +๐ for some ๐ > 0 and if ๐๐ ๐Τ๐ ≤ ๐๐ ๐ for some ๐ < 1 and all sufficiently large ๐, then ๐ ๐ = Θ ๐ ๐ What’s with the Case 3 regularity condition? Generally, not a problem. It always holds whenever ๐ ๐ = ๐๐ and ๐ ๐ = Ω ๐ log๐ ๐ +๐ for constant ๐ > 0. So, you don’t need to check it when ๐ ๐ is a polynomial. 56 28