210 likes | 219 Views
This lecture discusses the efficiency of algorithms, including factors such as correctness, understandability, maintainability, and usability. It also explores the three values common to technological activities: efficiency, economy, and elegance. The lecture covers various strategies for improving algorithm efficiency, such as data cleanup and different algorithms for solving the same problem.
E N D
Lecture 5 Efficiency of Algorithms (S&G, ch.3) CS 100 - Lecture 5
Attributes of Algorithms • First, it must be correct! • Other factors include: • efficiency • understandability • maintainability • usability CS 100 - Lecture 5
Three Values Common to Many Technological Activities • The Three E’s: • Efficiency • Economy • Elegance • Related to the Three S’s: • Scientific • Social • Symbolic CS 100 - Lecture 5
Efficiency • Deals with the amount of resources used • e.g., computer time, memory space • how to trade off one against another? • Basic criteria are correctness & security • correct results • resilient to operator error • error recovery • Issues are scientific CS 100 - Lecture 5
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 • Basic issues are social • usefulness of software in social context • monetary costs depend on market forces • social expectations affect costs & benefits CS 100 - Lecture 5
Elegance and theLimitations of Analysis • Tacoma Narrows Bridge, Puget Sound • Opened July 1, 1940 CS 100 - Lecture 5
The Result CS 100 - Lecture 5
Elegance • 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 CS 100 - Lecture 5
Learning Elegant Design • A sense of elegance is acquired through experience in design, criticism, revision CS 100 - Lecture 5
Multiple Algorithms • For some problems there are several well-known algorithms • A given algorithm may be chosen because: • 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 slide courtesy of S. Levy
Data Cleanup:Example of Multiple Algorithms • 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 CS 100 - Lecture 5 slide courtesy of S. Levy
Data Cleanup (2) • Given: n and N1, 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: • the number of copying operations • the extra temporary space required • both relative to problem size CS 100 - Lecture 5 slide courtesy of S. Levy
A New Primitive Operation • To examine or change the item at position i in the list M, in addition to Mi we use the expression M [i] • Examples: Set i to 10 Set M [i] to 35 Set M [i] to M [i] + 1 Output M [i] CS 100 - Lecture 5 slide courtesy of S. Levy
Data cleanup - Shuffle Left • 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. • Variables: • Legit - number of legitimate items • Left - current item inspected • Right - current item to shuffle left CS 100 - Lecture 5 slide courtesy of S. Levy
Shuffle Left - The Algorithm 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 CS 100 - Lecture 5 slide courtesy of S. Levy
Data cleanup - Copy Over • 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 CS 100 - Lecture 5 slide courtesy of S. Levy
Copy Over - The Algorithm 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 CS 100 - Lecture 5 slide courtesy of S. Levy
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 • Variables: • Legit - number of good items • Left - the left hand pointer • Right - the right hand pointer CS 100 - Lecture 5 slide courtesy of S. Levy
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 CS 100 - Lecture 5 slide courtesy of S. Levy
Comparison ofData Cleanup Algorithms • 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?) CS 100 - Lecture 5
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 CS 100 - Lecture 5