Algorithm Analysis Techniques and Concepts in CSE 332 Lecture 2

cse 332 autumn 2023 lecture 2 algorithm analysis n.w
1 / 25
Embed
Share

Learn about algorithm analysis, including end-of-yarn finding, running time analysis, resource analysis importance, algorithm analysis goals, worst-case running time analysis, and an example illustrating these concepts. Understand how to compare algorithms based on resource usage before implementation for optimal performance.

  • Algorithm Analysis
  • CSE 332
  • End-of-Yarn
  • Running Time
  • Resource 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 2023 Lecture 2: Algorithm Analysis pt.2 Nathan Brunelle http://www.cs.uw.edu/332

  2. End-of-Yarn Finding 1. Set aside the already-obtained beginning 2. If you see the end of the yarn, you re done! 3. Separate the pile of yarn into 2 piles, note which connects to the beginning (call it pile A, the other pile B) A B Repeat on pile with end 4. Count the number of strands crossing the piles 5. If the count is even, pile A contains the end, else pile B does 2

  3. Running Time Analysis Units of time How do we express running time? 3

  4. Why Do resource Analysis? Allows us to compare algorithms, not implementations Using observations necessarily couples the algorithm with its implementation If my implementation on my computer takes more time than your implementation on your computer, we cannot conclude your algorithm is better We can predict an algorithm s running time before implementing Understand where the bottlenecks are in our algorithm

  5. Goals for Algorithm Analysis Identify a functionwhich maps the algorithm s input size to a measure of resources used Domain of the function: sizes of the input Number of characters in a string, number of items in a list, number of pixels in an image Codomain of the function: counts of resources used Number of times the algorithm adds two numbers together, number times the algorithm does a > or < comparison, maximum number of bytes of memory the algorithm uses at any time Important note: Make sure you know the units of your domain and codomain!

  6. 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 I.e. ?(?) gives the maximum operation count from among all inputs of size ?

  7. Worst Case Running Time - Example myFunction(List n){ b = 55 + 5; c = b / 3; b = c + 100; for (i = 0; i < n.size(); i++) { b++; } if (b % 2 == 0) { c++; } else { for (i = 0; i < n.size(); i++) { c++; } } return c; } Questions to ask: What are the units of the input size? What are the operations we re counting? For each line: How many times will it run? How long does it take to run? Does this change with the input size?

  8. Worst Case Running Time Example 2 beAnnoying(List n){ List m = []; for (i=0; i < n.size(); i++){ m.add(n[i]); for (j=0; j< n.size(); j++){ print ( Hi, I m annoying ); } } return; } Questions to ask: What are the units of the input size? What are the operations we re counting? For each line: How many times will it run? How long does it take to run? Does this change with the input size?

  9. 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

  10. 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 boolean linearSearch(array a, integer k){ for(i=0; i< a.length; i++){ if (a[i] == k){ return true; } } return false; }

  11. 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?

  12. Comparing

  13. 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

  14. ?(?) = ?(? ? ) ?(?) = (? ? ) ?(?) = (? ? )

  15. 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 ? ? ? ?(? ? )

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

  17. 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

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

  19. 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.

  20. Asymptotic Notation Example Show: ?2 ? ?

  21. 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 ? ? .

  22. 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)

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

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

  25. 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).

Related


More Related Content