1 / 18

Algorithm Evaluation

Algorithm Evaluation. What’s an algorithm?. a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What are some different search algorithms? What are some different sorting algorithms?. How good is it?.

wilton
Download Presentation

Algorithm Evaluation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithm Evaluation

  2. What’s an algorithm? • a clearly specified set of simple instructions to be followed to solve a problem • a way of doing something • What are some different search algorithms? • What are some different sorting algorithms?

  3. How good is it? • Measure performance by measuring resource usage • time • space • Space should be consistent across different machines, but what about time? • What if you ran an algorithm on both a Intel Pentium II and an Intel Core i7?

  4. How many comparisons? • Assume an array containing n=10 elements: 4, 2, 62, 6, 78, 8, 5, 91, 63, 13 • Using the linear search algorithm: • How many comparisons would we need to find something at the beginning (e.g., 4)? • On average how many comparisons would we need to find something in the middle? • How many comparisons would we need to find something at the end (e.g., 13)?

  5. Linear Search Evaluation • Assuming an array of n elements, linear search yields: • Best case: 1 comparison • Average case: n/2 comparisons • Worst case: n comparisons

  6. Big O! • a mathematical representation of the complexity of an algorithm • “on the order of...” • usually used to estimate the upper bound performance • O( # comparisons ) or O( # operations ) or O( # of times things happen)

  7. Summary of Complexities O(1) : constant time not related to length of data set O(log2n) : log time and is fast O(n) : linear time O(n log2n) : “n log n” time O(n2) : quadratic time, polynomial complexity O(n3) : cubic time, polynomial complexity O(nn) : not computable

  8. A Simple Example k = 0 i = 0 while i < n: k += 1 n += 1 What’s my complexity?!? (Hint: use Big O notation) ...on the order of...

  9. A Simpler Example k = 0 k+=1 What’s my complexity?!? (Hint: use Big O notation) ...on the order of...

  10. Another Example for i in range(n): for j in range(n): for k in range(n): a++; What’s my complexity?!? (Hint: use Big O notation) ...on the order of...

  11. ::sigh:: Another Example k = 0 for i in range(n): for x in range(n): k += 1 What’s my complexity?!? (Hint: use Big O notation) ...on the order of...

  12. Rules of Thumb • one loop  O(n) • two nested loops  O(n2) • three nested loops  O(n3)

  13. Back to linear search... • Best case: 1 comparison O(1) • Worst case: n comparisons  O(n) • Average case: n/2 comparisons ...wait...what if n is really, really, really big? O(n/2) reduces to O(n) • Linear search’s complexity is O(n)

  14. Complexity Reduction • Rules of thumb: • consider LARGE values of n removes constants • larger complexities overshadow smaller ones  removes smaller Os • Examples: O(n/2)  O(n) O(2*n)  O(n) O(n + 5)  O(n) O(n + 209502)  O(n) O(n2) + O(n)  O(n2 + n)  O(n2)

  15. Reduction Example k = 0 for i in range(n/2): k += 1 • Notice the “n/2”! • What’s the complexity?

  16. Binary Search Algorithm • Assume a sorted array containing n=10 elements: 2, 4, 5, 6, 8, 13, 62, 63, 78, 91 • Using the binary search algorithm: • What is the best case performance? • What is the worst case performance? • What is the O complexity?

  17. Summary of Common Complexities linear search : O(n) binary search : O(log2n) bubble sort : O(n2) selection sort : O(n2) quick sort : O(n log2n)

  18. IB Expectations • Evaluate the efficiency of a given algorithm • How many times does it run exactly? • How do you improve the algorithm? • Trace an algorithm (follow its steps) when given input • Analyze an algorithm as pseudocode or flowchart

More Related