CPSC 171 Introduction to Computer Science Efficiency of Algorithms
Announcements Read Chapter 3 Lab 2, 3 due this Thursday (Hand in electronically)
An Algorithm is A well-ordered collection of Unambiguous and Effectively computable operations that, when executed Produces a result and Halts in a finite amount of time Textbook definition
Properties of Algorithms Correctness Does the algorithm always work Clarity Is the algorithm easy to understand Elegance (Style) Example: Gauss asked to add numbers from 1 to 100 Efficiency Speed of algorithm Space required to run algorithm
Efficiency of Algorithms How long does it take an algorithm to run? Depends on computer Depends on input data Benchmarking Run same algorithm and same input on difference machines Runs same algorithm on same machine using difference input Need a method of measuring inherent efficiency of algorithm, independent of machine or input Count how many times “work unit” is executed
Game Time Guess My Number 20 Questions Guess Who? For Each of these games understand different strategies for finding the answer. What is the best strategy?
Telephone Book Example Task Find a particular person’s number from a list of telephone subscribers, given the name Sequential Search Algorithm outline Start with the first entry and check its name, then repeat the process for all entries
Figure 3.1 Sequential Search Algorithm
How Efficient is the Algorithm? Best Case: Worst Case: Average Case: 1 Comparison N Comparisons N/2 Comparisons
Order of Magnitude Total number of operations performed is some constant number of operations times N 2N 3N ½*N Linear (n)
Figure 3.4 Work = cn for Various Values of c
Telephone Book Example 2 Task Find a particular person’s number from a sorted list of telephone subscribers, given the name Binary Search Algorithm outline Always select the middle name. If the persons name is greater than selected name then search top half, otherwise search bottom half of remaining names.
Figure 3.18 Binary Search Algorithm (list must be sorted)
How Efficient is the Algorithm? Best Worst Average 1 Comparison Log N Comparisons (Log n) About Log N Comparisons (Log n)
Figure 3.21 A Comparison of n and lg n
Data Cleanup Algorithms Given a collection of numbers, find and remove all zeros Possible algorithms Shuffle-left Copy-over Converging-pointers
The Shuffle-Left Algorithm Scan list from left to right When a zero is found, shift all values to its right one slot to the left
Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup
The Shuffle-Left Algorithm (continued) Time efficiency Count examinations of list values and shifts Best case No shifts, n examinations (n) Worst case Shift at each pass, n passes n 2 shifts plus n examinations (n 2 )
The Shuffle-Left Algorithm (continued) Space efficiency n slots for n values, plus a few local variables (n)
The Copy-Over Algorithm Use a second list Copy over each nonzero element in turn
Figure 3.15 The Copy-Over Algorithm for Data Cleanup
The Copy-Over Algorithm Time efficiency Count examinations and copies Best case All zeros n examinations and 0 copies (n)
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
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
The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle
Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup
The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle Time efficiency Count examinations and swaps Best case n examinations, no swaps (n)
The Converging-Pointers Algorithm (continued) Time efficiency (continued) Worst case n examinations, n swaps (n) Space efficiency n slots for the values, plus a few extra variables
Figure 3.17 Analysis of Three Data Cleanup Algorithms