Download presentation
Presentation is loading. Please wait.
1
Lecture 5 Efficiency of Algorithms (S&G, ch.3)
2/16/2019 9:43 AM Lecture 5 Efficiency of Algorithms (S&G, ch.3) 2/16/2019 CS Lecture 5 CS 100
2
Attributes of Algorithms
Lecture 5 2/16/2019 9:43 AM Attributes of Algorithms First, it must be correct! Other factors include: efficiency understandability maintainability usability 2/16/2019 CS Lecture 5 CS 100
3
Three Values Common to Many Technological Activities
Lecture 5 2/16/2019 9:43 AM Three Values Common to Many Technological Activities The Three E’s: Efficiency Economy Elegance Related to the Three S’s: Scientific Social Symbolic Call the S’s three dimensions 2/16/2019 CS Lecture 5 CS 100
4
Efficiency Deals with the amount of resources used
Lecture 5 2/16/2019 9:43 AM 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 In other words, what resources are required to get correct results Also, what resources are required to deal with errors, provide recovery, etc. 2/16/2019 CS Lecture 5 CS 100
5
Economy Deals with social benefit relative to costs
Lecture 5 2/16/2019 9:43 AM 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 Note program maintenance cost Monetary costs may depend on litigation & its expected outcome Note that many people don’t expect their PC software to be reliable 2/16/2019 CS Lecture 5 CS 100
6
Elegance and the Limitations of Analysis
Lecture 5 2/16/2019 9:43 AM Elegance and the Limitations of Analysis Tacoma Narrows Bridge, Puget Sound Opened July 1, 1940 Opened July 1, 1940 Took 2 years to construct Almost 6000 ft long (5939) 42 MPH wind Collapsed Nov 7, 1940 "Just as I drove past the towers, the bridge began to sway violently from side to side. Before I realized it, the tilt became so violent that I lost control of the car... I jammed on the brakes and got out, only to be thrown onto my face against the curb. "Around me I could hear concrete cracking. I started to get my dog Tubby, but was thrown again before I could reach the car. The car itself began to slide from side to side of the roadway. "On hands and knees most of the time, I crawled 500 yards or more to the towers... My breath was coming in gasps; my knees were raw and bleeding, my hands bruised and swollen from gripping the concrete curb... Toward the last, I risked rising to my feet and running a few yards at a time... Safely back at the toll plaza, I saw the bridge in its final collapse and saw my car plunge into the Narrows." 2/16/2019 CS Lecture 5 CS 100
7
The Result 2/16/2019 CS Lecture 5
8
Elegance Incompleteness of analytical approaches
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 CS 100
9
Learning Elegant Design
Lecture 5 2/16/2019 9:43 AM Learning Elegant Design A sense of elegance is acquired through experience in design, criticism, revision 2/16/2019 CS Lecture 5 CS 100
10
slide courtesy of S. Levy
Lecture 5 2/16/2019 9:43 AM 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 E.g. working well for given data set: sorting almost sorted list Dealing with English text (with its statistical properties) Note that some of these reasons pertain to different of the Three E’s 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
11
Data Cleanup: Example of Multiple Algorithms
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
12
slide courtesy of S. Levy
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
13
A New Primitive Operation
Lecture 5 2/16/2019 9:43 AM 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] 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
14
Data cleanup - Shuffle Left
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
15
Shuffle Left - The Algorithm
Lecture 5 2/16/2019 9:43 AM 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 Left + 1 End of loop 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
16
Data cleanup - Copy Over
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
17
Copy Over - The Algorithm
Lecture 5 2/16/2019 9:43 AM 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 Set M [NewPos] to N [OldPos] Set OldPos to OldPos + 1 End of loop Stop Note that this algorithm is much easier to understand 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
18
Data cleanup - Converging Pointers
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
19
Converging Pointers - The Algorithm
Lecture 5 2/16/2019 9:43 AM 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 Stop Note that we do not advance Left on a zero item, because we don’t know if N[Right] = 0 Will this algorithm terminate? Yes, because either Left is incremented or Right is decremented, so will eventually converge The final If handles the case when Left = Right We know that everything to the left of Left is good, but we do not know about what’s under Left 2/16/2019 CS Lecture 5 slide courtesy of S. Levy CS 100
20
Comparison of Data Cleanup Algorithms
Lecture 5 2/16/2019 9:43 AM Comparison of Data 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?) Shuffle-Left uses n, Legit, Left, Right Converging-Pointers uses n, Legit, Left, Right 2/16/2019 CS Lecture 5 CS 100
21
Performance Evaluation
Lecture 5 2/16/2019 9:43 AM 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 2/16/2019 CS Lecture 5 CS 100
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.