Lecture 5 1/27/04 16:56 Attributes of Algorithms • First, it must be correct! • Other factors include: Lecture 5 – – – – Efficiency of Algorithms (S&G, ch.3) 1/27/04 CS 100 - Lecture 5 1 1/27/04 Three Values Common to Many Technological Activities CS 100 - Lecture 5 2 Efficiency • Deals with the amount of resources used • The Three E’s: – e.g., computer time, memory space – how to trade off one against another? – Efficiency – Economy – Elegance • Basic criteria are correctness & security • Related to the Three S’s: – correct results – resilient to operator error – error recovery – Scientific – Social – Symbolic 1/27/04 efficiency understandability maintainability usability • Issues are scientific CS 100 - Lecture 5 3 1/27/04 CS 100 - Lecture 5 4 Elegance and the Limitations of Analysis Economy • Deals with social benefit relative to costs • Basic criteria are costs & benefits – not all costs are monetary (e.g. human dissatisfaction, frustration, fear, suffering; environmental impact) – costs change in time & are hard to predict – similarly benefits come in many forms • Tacoma Narrows Bridge, Puget Sound • Basic issues are social • Opened July 1, 1940 – usefulness of software in social context – monetary costs depend on market forces – social expectations affect costs & benefits 1/27/04 CS 100 CS 100 - Lecture 5 5 1/27/04 CS 100 - Lecture 5 6 1 Lecture 5 1/27/04 16:56 Elegance The Result • Incompleteness of analytical approaches • Under-determination of design space • Restrict attention to designs for which correct, efficient, economical designs are obviously so • Aesthetic sense guides design • “When the form is well chosen, its analysis becomes astonishingly simple” — Billington 1/27/04 CS 100 - Lecture 5 7 1/27/04 Learning Elegant Design 8 Multiple Algorithms • For some problems there are several wellknown algorithms • A given algorithm may be chosen because: • A sense of elegance is acquired through experience in design, criticism, revision 1/27/04 CS 100 - Lecture 5 – – – – CS 100 - Lecture 5 9 1/27/04 It works well for small (large) data sets It works well for data sets of a given nature It is easy to program A program for it is available CS 100 - Lecture 5 10 slide courtesy of S. Levy Data Cleanup: Example of Multiple Algorithms • Given: n and N 1, N2, …, Nn • Want: k and M1, M2, …, Mk where M1, …, Mk represent the valid (non-zero) items of the original list • The focus will be on: • Sometimes we have a large list of data, some items of which are not legitimate items for the work at hand – No responses – Invalid values – Not relevant for current analysis 1/27/04 CS 100 - Lecture 5 slide courtesy of S. Levy CS 100 Data Cleanup (2) – the number of copying operations – the extra temporary space required – both relative to problem size 11 1/27/04 CS 100 - Lecture 5 12 slide courtesy of S. Levy 2 Lecture 5 1/27/04 16:56 A New Primitive Operation Data cleanup - Shuffle Left • To examine or change the item at position i in the list M, in addition to Mi we use the expression • Strategy: Move through list from left to right. When we encounter a zero, shuffle everything to the right of the zero to the left one position. M [i] • Examples: • Variables: Set i to 10 Set M [i] to 35 Set M [i] to M [i] + 1 Output M [i] 1/27/04 CS 100 - Lecture 5 Legit - number of legitimate items Left - current item inspected Right - current item to shuffle left 13 1/27/04 CS 100 - Lecture 5 slide courtesy of S. Levy Shuffle Left - The Algorithm Data cleanup - Copy Over Set Legit to n Set Left to 1 Set Right to 2 While Left ≤ Legit do If N [Left] ≠ 0 then Set Left to Left + 1 Set Right to Right + 1 Else Set Legit to Legit – 1 While Right ≤ n do Set N [Right – 1] to N [Right] Set Right to Right + 1 Set Right to Left + 1 End of loop 1/27/04 CS 100 - Lecture 5 • Strategy: Make a new list with the valid items Move through original list from left and copy valid items to new list. • Variables: OldPos - Position in original list NewPos - Position in new list 15 1/27/04 slide courtesy of S. Levy CS 100 - Lecture 5 CS 100 16 Data cleanup - Converging Pointers • Strategy: Have “pointers” marking left and right ends of list still to be processed. Everything to left of left pointer is good data. Everything to right of right pointer is bad. Pointers converge to one another as we proceed Set OldPos to 1 Set NewPos to 0 While OldPos ≤ n do If N [OldPos] ≠ 0 then Set NewPos to NewPos + 1 Set M [NewPos] to N [OldPos] Set OldPos to OldPos + 1 End of loop Stop slide courtesy of S. Levy CS 100 - Lecture 5 slide courtesy of S. Levy Copy Over - The Algorithm 1/27/04 14 slide courtesy of S. Levy • Variables: Legit - number of good items Left - the left hand pointer Right - the right hand pointer 17 1/27/04 CS 100 - Lecture 5 18 slide courtesy of S. Levy 3 Lecture 5 1/27/04 16:56 Comparison of Data Cleanup Algorithms Converging Pointers - The Algorithm Set Legit to n Set Left to 1 Set Right to n While Left < Right do If N [Left] = 0 then Set Legit to Legit – 1 Set N [Left] to N [Right] Set Right to Right – 1 Else Set Left to Left + 1 End of loop If N [Left] = 0 then Set Legit to Legit – 1 Stop 1/27/04 CS 100 - Lecture 5 • Shuffle-Left requires: – fixed working space (4, independent of problem size) – 19 copies for a particular list of 10 elements • Copy-Over requires: – fewer copies (7) – an extra copy of the list • Converging-Pointers requires: – fewest copies (3) – fixed extra space (4) – but it reorders elements (Do we care?) 19 1/27/04 CS 100 - Lecture 5 20 slide courtesy of S. Levy Performance Evaluation • We compared performance of the algorithms on a specific problem instance – Typical? Unusual? We don’t know • Empirical approach: measure performance on representative variety of test cases • Analytical approach: use mathematics to analyze performance on all possible inputs 1/27/04 CS 100 CS 100 - Lecture 5 21 4