Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 171 Introduction to Computer Science Efficiency of Algorithms.

Similar presentations


Presentation on theme: "CPSC 171 Introduction to Computer Science Efficiency of Algorithms."— Presentation transcript:

1 CPSC 171 Introduction to Computer Science Efficiency of Algorithms

2 Announcements Read Chapter 3 Lab 2, 3 due this Thursday (Hand in electronically)

3 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

4 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

5 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

6 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?

7

8 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

9 Figure 3.1 Sequential Search Algorithm

10 How Efficient is the Algorithm? Best Case: Worst Case: Average Case: 1 Comparison N Comparisons N/2 Comparisons

11 Order of Magnitude Total number of operations performed is some constant number of operations times N 2N 3N ½*N Linear  (n)

12 Figure 3.4 Work = cn for Various Values of c

13 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.

14 Figure 3.18 Binary Search Algorithm (list must be sorted)

15 How Efficient is the Algorithm? Best Worst Average 1 Comparison Log N Comparisons  (Log n) About Log N Comparisons  (Log n)

16 Figure 3.21 A Comparison of n and lg n

17 Data Cleanup Algorithms Given a collection of numbers, find and remove all zeros Possible algorithms Shuffle-left Copy-over Converging-pointers

18 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

19 Figure 3.14 The Shuffle-Left Algorithm for Data Cleanup

20 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 )

21 The Shuffle-Left Algorithm (continued) Space efficiency n slots for n values, plus a few local variables  (n)

22 The Copy-Over Algorithm Use a second list Copy over each nonzero element in turn

23 Figure 3.15 The Copy-Over Algorithm for Data Cleanup

24 The Copy-Over Algorithm Time efficiency Count examinations and copies Best case  All zeros  n examinations and 0 copies   (n)

25 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

26 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

27 The Converging-Pointers Algorithm Swap zero values from left with values from right until pointers converge in the middle

28 Figure 3.16 The Converging-Pointers Algorithm for Data Cleanup

29 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)

30 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

31 Figure 3.17 Analysis of Three Data Cleanup Algorithms


Download ppt "CPSC 171 Introduction to Computer Science Efficiency of Algorithms."

Similar presentations


Ads by Google