1 / 96

The Efficiency of Algorithms

The Efficiency of Algorithms. Chapter 3 CS10051. OUR NEXT QUESTION IS: "How do we know we have a good algorithm?". In the lab session, you will explore algorithms that are related as they all solve the same problem:.

brosh
Télécharger la présentation

The Efficiency of Algorithms

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. The Efficiency of Algorithms Chapter 3 CS10051

  2. OUR NEXT QUESTION IS: "How do we know we have a good algorithm?" In the lab session, you will explore algorithms that are related as they all solve the same problem: Problem: We are given a list of numbers which include good data (represented by nonzero whole numbers) and bad data (represented by zero entries). We want to "clean-up" the data by moving all the good data to the left, preferably keeping it in the same order, and setting a value legit that will equal the number of good items. For example, 0 24 16 0 0 0 5 27 becomes 24 16 5 27 ? ? ? ? with legit being 4. The ? means we don't care what is in that old position.

  3. WE'LL LOOK AT 3 DIFFERENT ALGORITHMS • Shuffle-Left Algorithm • The Copy-Over Algorithm • The Converging-Pointers Algorithm All solve the problem, but differently.

  4. These three algorithms will enable us to investigate the notion of the complexity of an algorithm. Algorithms consume resources of a computing agent: TIME: How much time is consumed during the execution of the algorithm? SPACE: How much additional storage (space), other than that used to hold the input and a few extra variables, is needed to execute the algorithm?

  5. HOW WILL WE MEASURE THE TIME FOR AN ALGORITHM? • Code the algorithm and run it on a computer? • What machine? • What language? • Who codes? • What data? Doing this (which is called benchmarking) can be useful, but not for comparing operations.

  6. Instead, we determine the time complexity of an algorithm and use it to compare that algorithm with others for which we also have their time complexity. What we want to do is relate 1. the amount of work performed by an algorithm 2. and the algorithm's input size by a fairly simple formula. You will do experiments and other work in the lab to reinforce these concepts.

  7. STEPS FOR DETERMING THE TIME COMPLEXITY OF AN ALGORITHM • 1. Determine how you will measure input size. Ex: • N items in a list • N x M table (with N rows and M columns) • Two numbers of length N • 2. Choose an operation (or perhaps two operations) to count as a gauge of the amount of work performed. Ex: • Comparisons • Swaps • Copies • Additions Normally we don't count operations in input/output.

  8. STEPS FOR DETERMING THE TIME COMPLEXITY OF AN ALGORITHM • 3. Decide whether you wish to count operations in the • Best case? - the fewest possible operations • Worst case? - the most possible operations • Average case? • This is harder as it is not always clear what is meant by an "average case". Normally calculating this case requires some higher mathematics such as probability theory. • 4. For the algorithm and the chosen case (best, worst, average), express the count as a function of the input size of the problem. For example, we determine by counting, statements such as ...

  9. EXAMPLES: • For n items in a list, counting the operation swap, we find the algorithm performs 10n + 5 swaps in the worst case. • For an n X m table, counting additions, we find the algorithm perform nm additions in the best case. • For two numbers of length n, there are 3n + 20 multiplications in the best case.

  10. STEPS FOR DETERMING THE TIME COMPLEXITY OF AN ALGORITHM 5. Given the formula that you have determined, decide the complexity class of the algorithm. What is the complexity class of an algorithm? Question: Is there really much difference between 3n 5n + 20 and 6n -3 especially when n is large?

  11. But, there is a huge difference, for n large, between n n2 and n3 So we try to classify algorithm into classes, based on their counts and simple formulas such as n, n2, n3, and others. Why does this matter? It is the complexity of an algorithm that most affects its running time--- not the machine or its speed

  12. ORDER WINS OUT The TRS-80 Main language support: BASIC - typically a slow running language For more details on TRS-80 see: http://mate.kjsl.com/trs80/ The CRAY-YMP Language used in example: FORTRAN- a fast running language For more details on CRAY-YMP see: http://ds.dial.pipex.com/town/park/abm64/CrayWWWStuff/Cfaqp1.html#TOC3

  13. CRAY YMP TRS-80with FORTRAN with BASICcomplexity is 3n3 complexity is 19,500,000n n is: 10 100 1000 2500 10000 1000000 3 microsec 200 millisec 2 sec 3 millisec 20 sec 3 sec 50 sec 50 sec 49 min 3.2 min 95 years 5.4 hours

  14. Trying to maintain an exact count for an operation isn't too useful. Thus, we group algorithms that have counts such as n 3n + 20 1000n - 12 0.00001n +2 together. We say algorithms with these type of counts are in the class (n) - read as the class of theta-of-n or all algorithms of magnitude n or all order-n algorithms

  15. Similarly, algorithms with counts such as n2 + 3n 1/2n2 + 4n - 5 1000n2 + 2.54n +11 are in the class (n2). Other typical classes are those with easy formulas in n such as 1 n3 2n lg n k = lg n if and only if 2k = n

  16. lg n k = lg n if and only if 2k = n lg 4 = ? lg 8 = ? lg 16 = ? lg 10 = ? Note that all of these are base 2 logarithms. You don't use any logarithm table as we don't need exact values (except on integer powers of 2). Look at the curves showing the growth for algorithms in (1), (n), (n2), (n3), (lg n), (n lg n), (2n) These are the major ones we'll use.

  17. ANOTHER COMPARISON n = order 10 50 100 1,000 lg n 0.0003 sec 0.0006 sec 0.0007 sec 0.001 sec n 0.001 sec 0.005 sec 0.01 sec 0.1 sec n2 0.01 sec 0.25 sec 1 sec 1.67 min 2n 0.1024 sec 3570 years 4 x 1016 why centuries? bother? Does order make a difference? You bet it does, but not on tiny problems. On large problems, it makes a major difference and can even predict whether or not you can execute the algorithm.

  18. Why not just build a faster computing agent? Why not use parallel computing agents? No matter what we do, the complexity (i.e. the order) of the algorithm has a major impact!!! So, can we compare two algorithms and say which is the better one with respect to time? Yes, provided we do several things:

  19. COMPARING TWO ALGORITHMS WITH RESPECT TO TIME • 1. Count the same operation for both. • 2. Decide whether this is a best, worst, or average case. • 3. Determine the complexity class for both, say (f) and (g) for the chosen case. • 4. Then, for large problems, data that is for the case you analyzed, and no further information: • If (f) = (g), they are essentially the same. • If (f) <(g), , choose the (f) algorithm. • Otherwise, choose the (g) algorithm.

  20. A MORE PRECISE DEFINITION OF (only for those with calculus backgrounds) Definition: Let f and g be functions defined on the positive real numbers with real values. We say g is in O(f) if and only if lim g(n)/f(n) = c n ->  for some nonnegative real number c--- i.e. the limit exists and is not infinite. We say f is in (g) if and only if f is in O(g) and g is in O(f) Note: Often to calculate these limits you need L'Hopital's Rule.

  21. CHAPTER 3Section 3.4 Three Important Algorithms That Will Serve as Examples

  22. 3 EXAMPLES ILLUSTRATE OUR COMPLEXITY ANALYSIS Problem: We are given a list of numbers which include good data (represented by nonzero whole numbers) and bad data (represented by zero entries). We want to "clean-up" the data by moving all the good data to the left, keeping it in the same order, and setting a value legit that will equal the number of good items. For example, 0 24 16 0 0 0 5 27 becomes 24 16 5 27 ? ? ? ? with legit being 4. The ? means we don't care what is in that old position.

  23. WE'LL LOOK AT 3 DIFFERENT ALGORITHMS • Shuffle-Left Algorithm • Copy-Over Algorithm • The Converging-Pointers Algorithm All solve the problem, but differently.

  24. THE SHUFFLE LEFT ALGORITHM FOR DATA CEANUP 0 24 16 0 36 42 23 21 0 27 legit = 10 . . . Detect a 0 at left finger so reduce legit and copy values under a right finger that moves: legit = 9 36 42 23 21 0 24 16 0 27 27 didn't move ------------------end of round 1 ----------------

  25. Reset the right finger: 24 16 0 36 42 23 21 0 27 27 legit = 9 No 0 is detected, so march the fingers along until a 0 is under the left finger: 24 16 0 36 42 23 21 0 27 27 legit = 9 24 16 0 36 42 23 21 0 27 27 legit = 9

  26. Now decrement legit again and shuffle the values left as before: Starting with: 24 16 0 36 42 23 21 0 27 27 legit = 9 After the shuffle and reset we have: 24 16 36 42 23 21 0 27 27 27 legit = 8 ------------------end of round 2 ----------------

  27. Now decrement legit again and shuffle the values left as before: Starting with: 24 16 36 42 23 21 0 27 27 27 legit = 8 After the shuffle and reset we have: 24 16 36 42 23 21 27 27 27 27 legit = 7 ------------------end of round 3 ----------------

  28. Now we try again: Starting with: 24 16 36 42 23 21 27 27 27 27 legit = 7 We move the fingers once: 24 16 36 42 23 21 27 27 27 27 legit = 7 But, now the location of the left finger is greater than legit, so we are done! -----------end of the algorithm execution ----------------

  29. Here's the pseudocode version of the algorithm: The textbook uses numbered steps which I don't. I have added some comments in red that provide additional information to the reader. Input the necessary values: Get values for n and the n data items. Initialize variables: Set the value of legit to n. Legit is the number of good items. Set the value of left to 1. Left is the position of the left finger. Set the value of right to 2. Right is the position of the right finger.

  30. While left is less than or equal to legit If the item at position left is not 0 Increase left by 1 moving the left finger Increase right by 1 moving the right finger Else in this case the item at position left is 0 Reduce legit by 1 While right is less than or equal to n Copy item at position right to right-1 Increase right by 1 End loop Set the value of right to left + 1 End loop end of shuffle left algorithm for data cleanup

  31. ANOTHER ALGORITHM FOR DATA CLEANUP - COPY-OVER 0 24 16 0 36 42 23 21 0 27 ... The idea here is that we write a new list by copying only those values that are nonzero and using the position of n moved item to be the count of the number of good data items: 24 16 36 42 23 21 27 At the end, newposition (i.e. legit) is 7.

  32. COPY-OVER ALGORITHM PSEUDOCODE Input the necessary values and initialize variables: Get the values for n and the n data items. Set the value of left to 1. Left is an index in the original list. Set the value of newposition to 1. This is an index in a new list. Copy good items to the new list indexed by newposition While left is less than or equal to n If the item at position left is not 0 then Copy the position left item into position newposition Increase left by 1 Increase newposition by 1 Else the item at position left is zero Increase left by 1 End loop

  33. OUR LAST DATA CLEANUP ALGORITHM- CONVERGING-POINTERS 0 24 16 0 36 42 23 21 0 27 legit = 10 We again use fingers (or pointers). But, now we start at the far right and the far left. Since a 0 is encountered at left, we copy the item at right to left, and decrement both legit and right: 27 24 16 0 36 42 23 21 0 27 legit = 9 ------------------end of round 1 ----------------

  34. Starting with: 27 24 16 0 36 42 23 21 0 27 legit = 9 Move the left pointer until a zero is encountered or until it meets the right pointer: 27 24 16 0 36 42 23 21 0 27 legit = 9 Since a 0 is encountered at left, we copy the item at right to left, and decrement both legit and right: 27 24 16 0 36 42 23 21 0 27 legit = 8 Because a 0 was copied to a 0 it doesn't look as if the data changed, but it did! This is the end of round 2.

  35. Starting with: 27 24 16 0 36 42 23 21 0 27 legit = 8 We again encountered a 0 at left, so we copy the item at right to left, and decrement both legit and right to end round 3: 27 24 16 21 36 42 23 21 0 27 legit = 7 On the last round, the left moves to the right pointer 27 24 16 21 36 42 23 21 0 27 legit = 7 NOTE: If the item is 0 at this point, we would need to decrement legit by 1. This ends the algorithm execution.

  36. CONVERGING-POINTERS ALGORITHM PSEUDOCODE Input the necessary values: Get values for n and the n data items. Initialize the variables: Set the value of legit to n. Set the value of left to 1. Set the value of right to n.

  37. While left is less than right If the item at position left is not 0 then Increase left by 1 Else the item at position left is 0 Reduce legit by 1 Copy the item at position right into position left Reduce right by 1 End loop. If the item at position left is 0 then Reduce legit by 1. End of algorithm.

  38. NOW LET US COMPARE THESE THREE ALGORITHMS BY ANALYZING THEIR ORDERS OF MAGNITUDE • All 3 algorithms must measure the input size the same. What should we use? • The length of the list is an obvious measure of the size of the data set.

  39. All 3 algorithms must count the same operation (or operations) for a time analysis. What should we use? • All examine each element in the list once. So all do at least (n) work if we count examinations. • All use copying, but the amount of copying done by each algorithm differs. So this is a nice operation to count. • So we will analyze with respect to both of these operations.

  40. Which case (best, worst, or average) should we consider? • We'll analyze the best and worst case for each algorithm. • The average case will not be analyzed, but final result will just stated. Remember, this case is often much harder to determine.

  41. With respect to space, it should be clear that • The Shuffle-Left Algorithm and the Converging Pointers use no extra space beyond the original input space and space for variables such as counting variables, etc. • But, the Copy-Over Algorithm does use more space, although the amount used depends upon which case we are considering.

  42. THE COPY-OVER ALGORITHM IS THE EASIEST TO ANALYZE With respect to copies, for what kind of data will the algorithm do the most work? Try to design a set of data for an arbitrary length, n, that does the most copying---i.e. a worst case data set? Example: For n = 4: 12 13 2 5 We could characterize worst case data as data with no zeroes. Note: There are lots of examples of worst case data.

  43. THE COPY-OVER ALGORITHMWORST CASE ANALYSIS Data set of size n contains no zeroes. Number of examinations is n. Number of copies is n. Amount of extra space is n. So the time complexity in the worst case counting both of these operations is (n), and the space complexity in the worst case is 2n (input size of n plus an additional n). Note: With space complexity, we often keep the formula rather than use the  class.

  44. THE COPY-OVER ALGORITHMBEST CASE ANALYSIS Data set of size n contains all zeroes. Number of examinations is n. Number of copies is 0. Amount of extra space is 0. So the time complexity in the best case counting both of these operations is (n). If only copies are being counted, the amount of work is (1) [but this seems to not be "fair" ;-) ] The space complexity in the best case is n.

  45. THE COPY-OVER ALGORITHMWHAT IF YOU WANTED TO DO AN AVERAGE CASE ANALYSIS? The difficulty lies in first defining "average". Then you would need to consider the probability of an average set being available out of all possible sets of data. These questions can be answered, but they are beyond the scope of this course. For this algorithm, (n) is the amount of work done in the average case. Computer scientists who analyze at this level usually have strong mathematical backgrounds.

  46. Space complexity is easy to analyze for the other two algorithms: Neither use extra space in any case so for Shuffle-Left and Converging-Pointers, the space complexity is n. If we are concerned only about space, then the Copy-Over Algorithm should not be used.

  47. THE SHUFFLE-LEFT ALGORITHMWORST CASE ANALYSIS Data set of size n contains all zeroes. Note: This data was the best case for the copy-over algorithm! Number of copies is ? Element 1 is 0, so we copy n-1 items in the first round. Again, element 1 is 0, so we copy n-1 items in the second round. Continuing, we do this n times (until legit becomes 0). How much work? n (n-1) = n2 - n n  n = n2 Number of examinations is

  48. So, the time complexity in the worst case for the shuffle- left algorithm, counting both of these operations, is n2 + n(n-1) = 2n2 -n i.e. the algorithm is (n2). The amount of extra space needed in the worst case for the shuffle-left algorithm is 0 so the space complexity is n.

  49. THE SHUFFLE-LEFT ALGORITHMBEST CASE ANALYSIS Data set of size n contains no zeroes. Note: This data was the worst case for the copy-over algorithm! Number of examinations is n. Number of copies is ? With no zeroes, there are no copies. So, the complexity of both operations is (n). The amount of extra space needed in the worst case for the shuffle-left algorithm is 0 so the space complexity is n.

  50. THE CONVERGING-POINTERS ALGORITHMWORST CASE ANALYSIS Data set of size n contains all zeroes. Note: This data was the best case for the copy-over algorithm! Number of examinations is n. Number of copies is n - 1 There is 1 copy for each decrement of right from n to 1 -- for a total of n Thus, the time complexity in this case is (n). No extra space is needed, so the space complexity is n.

More Related