890 likes | 984 Views
Invitation to Computer Science 6th Edition. Chapter 3 The Efficiency of Algorithms. Objectives. In this chapter, you will learn about: Attributes of algorithms Measuring efficiency Analysis of algorithms When things get out of hand. Introduction.
E N D
Invitation to Computer Science 6thEdition Chapter 3 The Efficiency of Algorithms
Objectives In this chapter, you will learn about: • Attributes of algorithms • Measuring efficiency • Analysis of algorithms • When things get out of hand Invitation to Computer Science, 6th Edition
Introduction • An important part of computer science • Finding algorithms to solve problems of interest • Are some algorithms better than others? Invitation to Computer Science, 6th Edition
Attributes of Algorithms • We expect correctness from our algorithms • Program maintenance • Fixes errors uncovered through repeated use • Extends program to meet new requirements • Ease of understanding • Desirable characteristic of an algorithm • Elegance • Algorithmic equivalent of style Invitation to Computer Science, 6th Edition
Attributes of Algorithms (continued) • Efficiency • Term used to describe an algorithm’s careful use of resources • Benchmarks • Useful for rating one machine against another and for rating how sensitive a particular algorithm is with respect to variations in input on one particular machine Invitation to Computer Science, 6th Edition
Measuring Efficiency • Analysis of algorithms • The study of the efficiency of algorithms • An important part of computer science • Efficiency measured as a function relating size of input to time or space used • For one input size, best case, worst case, and average case behavior must be considered • The notation captures the order of magnitude of the efficiency function Invitation to Computer Science, 6th Edition
Sequential Search • Search for NAME among a list of n names • Start at the beginning and compare NAME to each entry until a match is found Invitation to Computer Science, 6th Edition
Figure 3.1 Sequential Search Algorithm Invitation to Computer Science, 6th Edition
Figure 3.2 Number of Comparisons to Find NAME in a List of n Names Using Sequential Search Invitation to Computer Science, 6th Edition
Order of Magnitude - Order n • Order of magnitude n • Anything that varies as a constant times n (and whose graph follows the basic shape of n) • Sequential search • An Θ(n) algorithm in both the worst case and the average case Invitation to Computer Science, 6th Edition
Figure 3.3 Work = 2n Invitation to Computer Science, 6th Edition
Figure 3.4Work = cn for Various Values of c Invitation to Computer Science, 6th Edition
Figure 3.5 Growth of Work = cn for Various Values of c Invitation to Computer Science, 6th Edition
Selection Sort • Selection sort algorithm • Sorts in ascending order • Subtask within selection sort • Task of finding the largest number in a list • When selection sort algorithm begins • The largest-so-far value must be compared to all the other numbers in the list • If there are n numbers in the list, n – 1 comparisons must be done Invitation to Computer Science, 6th Edition
Figure 3.6 Selection Sort Algorithm Invitation to Computer Science, 6th Edition
Figure 3.7 Comparisons Required by Selection Sort Invitation to Computer Science, 6th Edition
Selection Sort (continued) • Selection sort algorithm • Does n exchanges, one for each position in the list to put the correct value in that position • Space efficiency of the selection sort • Original list occupies n memory locations • Storage is needed for: • The marker between the unsorted and sorted sections • Keeping track of the largest-so-far value and its location in the list Invitation to Computer Science, 6th Edition
Figure 3.8An Attempt to Exchange the Values at X and Y Invitation to Computer Science, 6th Edition
Figure 3.9 Exchanging the Values at X and Y Invitation to Computer Science, 6th Edition
Selection Sort (continued) Count comparisons of largest so far against other values Find Largest, given m values, does m-1 comparisons Selection sort calls Find Largest n times, Each time with a smaller list of values Cost = n-1 + (n-2) + … + 2 + 1 = n(n-1)/2 Invitation to Computer Science, 6th Edition
Selection Sort (continued) Time efficiency Comparisons: n(n-1)/2 Exchanges: n (swapping largest into place) Overall: (n2), best and worst cases Space efficiency Space for the input sequence, plus a constant number of local variables Invitation to Computer Science, 6th Edition
Order of Magnitude – Order n2 All functions with highest-order term cn2 have similar shape An algorithm that does cn2 work for any constant c is order of magnitude n2, or (n2) Invitation to Computer Science, 6th Edition
Order of Magnitude – Order n2 Anything that is (n2) will eventually have larger values than anything that is (n), no matter what the constants are An algorithm that runs in time (n) will outperform one that runs in (n2) Invitation to Computer Science, 6th Edition
Order of Magnitude - Order n2 (continued) • Order of magnitude n2, or Θ(n2) • An algorithm that does cn2 work for any constant c • Selection sort • An Θ(n2) algorithm (in all cases) • Sequential search • An Θ(n) algorithm (in the worst case) Invitation to Computer Science, 6th Edition
Figure 3.10 Work 5 cn2 for Various Values of c Invitation to Computer Science, 6th Edition
Figure 3.11 A Comparison of n and n2 Invitation to Computer Science, 6th Edition
Figure 3.12 For Large Enough n, 0.25n2 Has Larger Values Than 10n Invitation to Computer Science, 6th Edition
Order of Magnitude - Order n2 (continued) • Part of the job of program documentation • To make clear any assumptions or restrictions about the input size the program was designed to handle • Comparing algorithm efficiency • Only makes sense if there is a choice of algorithms for the task at hand Invitation to Computer Science, 6th Edition
Figure 3.13 A Comparison of Two Extreme Q(n2) and Q(n) Algorithms Invitation to Computer Science, 6th Edition
Analysis of Algorithms Multiple algorithms for one task may be compared for efficiency and other desirable attributes Data cleanup problem Search problem Pattern matching Invitation to Computer Science, 6th Edition
Analysis of Algorithms • Given a collection of numbers, find and remove all zeros • Possible algorithms • Shuffle-left • Copy-over • Converging-pointers Invitation to Computer Science, 6th Edition
The Shuffle-Left Algorithm • Scans list from left to right • When a zero is found, copy each remaining data item in the list one cell to the left • Value of legit • Originally set to the length of the list • Is reduced by 1 every time a 0 is encountered Invitation to Computer Science, 6th Edition
Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup Invitation to Computer Science, 6th Edition
The Shuffle-Left Algorithm (continued) • To analyze time efficiency: • Identify the fundamental units of work the algorithm performs • Examinations • Copying numbers • Best case occurs when the list has no 0 values because no copying is required • Worst case occurs when the list has all 0 values Invitation to Computer Science, 6th Edition
The Shuffle-Left Algorithm (continued) Time efficiency Count examinations of list values and copies Best case No copies, n examinations (n) Worst case Shift at each pass, n passes n2 copies plus n examinations (n2) Invitation to Computer Science, 6th Edition
The Shuffle-Left Algorithm (continued) • Space-efficient • n slots for n values, plus a few local variables • Only requires four memory locations to store the quantities n, legit, left,and right Invitation to Computer Science, 6th Edition
The Copy-Over Algorithm • Scans the list from left to right, copying every legitimate (non-zero) value into a new list that it creates • Every list entry is examined to see whether it is 0 • Every non-zero list entry is copied once Invitation to Computer Science, 6th Edition
Figure 3.15 The Copy-Over Algorithm for Data Cleanup Invitation to Computer Science, 6th Edition
The Copy-Over Algorithm Use a second list Copy over each nonzero element in turn Time efficiency Count examinations and copies Best case All zeros n examinations and 0 copies (n) Invitation to Computer Science, 6th Edition
The Copy-Over Algorithm (continued) Time efficiency (continued) Worst case No zeros n examinations and n copies (n) Space efficiency 2n slots for n values, plus a few extraneous variables Invitation to Computer Science, 6th Edition
The Copy-Over Algorithm (continued) Time/space tradeoff Algorithms that solve the same problem offer a tradeoff One algorithm uses more time and less memory Its alternative uses less time and more memory Invitation to Computer Science, 6th Edition