Algorithm Analysis: Understanding Running Time

cse 332 autumn 2024 lecture 3 algorithm analysis n.w
1 / 29
Embed
Share

Learn about analyzing algorithms by understanding running time, worst-case scenarios, general guides, and defining complexity functions. Explore concepts such as worst-case running time analysis, units of time operations, and amortized complexity in this comprehensive guide.

  • Algorithms
  • Running Time
  • Worst Case
  • Complexity
  • Analysis

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSE 332 Autumn 2024 Lecture 3: Algorithm Analysis pt.2 Nathan Brunelle http://www.cs.uw.edu/332

  2. Running Time Analysis Units of time Operations Whichever operations we pick How do we express running time? Function Domain (input): size of the input Range: count of operations 2

  3. Worst Case Running Time Analysis If an algorithm has a worst case running time of ?(?) Among all possible size-?inputs, the worst one will do ?(?) operations ?(?) gives the maximum count of operations needed from among all inputs of size ?

  4. Analysis Process From 123/143 Count the number of primitive chosenoperations +, -, compare, arr[i], arr.length, etc. Select the operation(s) which: Is/are done the most Is/are the most expensive Is/are the most important Write that count as an expression using ? (the input size) Put that expression into a bucket by ignoring constants and non- dominant terms, then put a ?( ) around it. 4?2+ 8? 10 ends up as ? ?2 2? + 80 ends up as ? ? 1

  5. Worst Case Running Time General Guide Add together the time of consecutive statements Loops: Sum up the time required through each iteration of the loop If each takes the same time, then [time per loop number of iterations] Conditionals: Sum together the time to check the condition and time of the slowest branch Function Calls: Time of the function s body Recursion: Solve a recurrence relation

  6. Defining your running time function Worst-case complexity: max number of steps algorithm takes on most challenging input Best-case complexity: min number of steps algorithm takes on easiest input Average/expected complexity: avg number of steps algorithm takes on random inputs (context-dependent) Amortized complexity: max total number of steps algorithm takes on M most challenging consecutive inputs, divided by M (i.e., divide the max total sum by M).

  7. Amortized Complexity Example - ArrayList What is the worst case running time of add? Input size: size of this Operations counted: indexing ? ? public void add(T value){ if(data.length == size) resize(); data[size] = value; size++; } private void resize(){ T[] oldData = data; data = (T[]) new Object[data.length*2]; for(int i = 0; i < oldData.length; i++) data[i] = oldData[i]; }

  8. Amortized Complexity Example - ArrayList Amortized Analysis Idea: Suppose we have a program that in total does ? adds. How much time was spent on average across all ?? Let ? be the initial size of data The first ? adds take: ? + ? = 2? The next 2? adds: 2? + 2? = 4? The next 4? adds: 4? + 4? = 8? Overall: 14? 7?= 2? public void add(T value){ if(data.length == size) resize(); Every time we resize, we earn data.length more adds guaranteed to not resize! data[size] = value; size++; } private void resize(){ T[] oldData = data; data = (T[]) new Object[data.length*2]; for(int i = 0; i < oldData.length; i++) data[i] = oldData[i]; }

  9. Searching in a Sorted List 5 8 13 42 75 79 88 90 95 99 0 1 2 3 4 5 6 7 8 9 public static boolean contains(List<Integer> a, int k){ for(int i=0; i< a.size(); i++){ if (a.get(i) == k) return true; } return false; }

  10. Faster way? 5 8 13 42 75 79 88 90 95 99 0 1 2 3 4 5 6 7 8 9 Can you think of a faster algorithm to solve this problem?

  11. Binary Search 5 8 13 42 75 79 88 90 95 99 0 1 2 3 4 5 6 7 8 9 public static boolean contains(List<Integer> a, int k){ int start = 0; int end = a.size(); while(start < end){ int mid = start + (end-start)/2; if(a.get(mid) == k) return true; else if(a.get(mid) < k) start = mid+1; else end = mid; } return false; }

  12. Why is this log2?? In the beginning: end-start=? After 1 iteration: end-start=? mid-start = (start+(end-start)/2)-start end-mid = end-(start+(end-start)/2) Each iteration cuts the gap in half! We stop when the gap is 1 2

  13. Comparing

  14. Comparing Running Times Suppose I have these algorithms, all of which have the same input/output behavior: Algorithm A s worst case running time is 10? + 900 Algorithm B s worst case running time is 100? 50 Algorithm C s worst case running time is ?2 Which algorithm is best? 100

  15. ?(?) = ?(? ? ) ?(?) = (? ? ) ?(?) = (? ? )

  16. Asymptotic Notation ? ? ? The set of functions with asymptotic behavior less than or equal to ? ? Upper-bounded by a constant times ? for large enough values ? ? ? ? ? ? > 0. ?0> 0. ? ?0.? ? ? ? ? (? ? ) the set of functions with asymptotic behavior greater than or equal to ? ? Lower-bounded by a constant times ? for large enough values ? ? ? ? ? > 0. ?0> 0. ? ?0.? ? ? ? ? ? ? Tightly within constant of ? for large ? ? ? ?(? ? )

  17. Idea of ? = ? ? ? ? ?

  18. Asymptotic Notation Example Show: 10? + 100 ? ?2 Technique: find values ? > 0 and ?0> 0 such that ? > ?0.10? + 100 ? ?2 Proof:

  19. Asymptotic Notation Example Show: 10? + 100 ? ?2 Technique: find values ? > 0 and ?0> 0 such that ? ?0.10? + 100 ? ?2 Proof: Let ? = 10 and ?0= 6. Show ? 6.10? + 100 10?2 10? + 100 10?2 ? + 10 ?2 10 ?2 ? 10 ? ? 1 This is True because ?(? 1) is strictly increasing and 6 6 1 > 10

  20. Asymptotic Notation Example Show: 13n2 50n ?2 Technique: find values ? > 0 and ?0> 0 such that ? ?0.13?2 50? ? ?2 Proof: ? = ?0=

  21. Asymptotic Notation Example Show: 13n2 50n ?2 Technique: find values ? > 0 and ?0> 0 such that ? ?0.13?2 50? ? ?2 Proof: let ? = 12 and ?0= 50. Show ? 50.13?2 50? 12?2 13?2 50? 12?2 ?2 50? 0 ?2 50? ? 50 This is certainly true ? 50.

  22. Asymptotic Notation Example Show: ?2 ? ? Want to show that there does not exist a pair of ? and ?0 such that ?0> ?.?2 ? ?

  23. Asymptotic Notation Example Proof by Contradiction! To Show: ?2 ? ? Technique: Contradiction Proof: Assume ?2 ? ? . Then ?,?0> 0 s.t. ? > ?0,?2 ?? Let us derive constant ?. For all ? > ?0> 0, we know: ?? ?2, ? ?. Since ? is lower bounded by ?, ? cannot be a constant and make this True. Contradiction. Therefore ?2 ? ? .

  24. Gaining Intuition When doing asymptotic analysis of functions: If multiple expressions are added together, ignore all but the biggest If ?(?) grows asymptotically faster than ?(?), then ? ? + ? ? ? ? Ignore all multiplicative constants ? ? + ? ? ? for any constant ? Ignore bases of logarithms Do NOT ignore: Non-multiplicative and non-additive constants (e.g. in exponents, bases of exponents) Logarithms themselves Examples: 4? + 5 0.5?log? + 2? + 7 ?3+ 2?+ 3? ?log(10?2)

  25. More Examples Is each of the following True or False? 4 + 3? ?(?) ? + 2log? ?(log?) log? + 2 ?(1) ?50 ?(1.1?) 3? (2?)

  26. Common Categories ?(1) ? log? logarithmic ? ? linear ? ?log? log-linear ? ?2 quadratic ? ?3 cubic ? ?? polynomial ? ?? exponential constant

  27. Defining your running time function Worst-case complexity: max number of steps algorithm takes on most challenging input Best-case complexity: min number of steps algorithm takes on easiest input Average/expected complexity: avg number of steps algorithm takes on random inputs (context-dependent) Amortized complexity: max total number of steps algorithm takes on M most challenging consecutive inputs, divided by M (i.e., divide the max total sum by M).

  28. Beware! Worst case, Best case, amortized are ways to select a function ?, , are ways to compare functions You can mix and match! The following statements totally make sense! The worst case running time of my algorithm is ?3 The best case running time of my algorithm is ? ? The best case running time of my algorithm is 2?

Related


More Related Content