Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted.

Similar presentations


Presentation on theme: "CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted."— Presentation transcript:

1 CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted from instructor slides for Schneider & Gerstung

2 2 Overview What makes a good algorithm? Correctness Ease of understanding Elegance Efficiency Computational efficiency – order of magnitude of running time Polynomial – time increases (reasonably) slowly as problem size increases  tractable (solvable in reasonable time) Exponential (or worse!) – time increases explosively as problem size increases  intractable (can’t be solved in practice for big problems) (We are also sometimes interested in memory or space efficiency ) Tue 10/2/12Efficiency of Algorithms

3 3 Introduction Introduction There are many solutions to any given problem How can we judge and compare algorithms? Analogy: Purchasing a car safety ease of handling style fuel efficiency Evaluating an algorithm correctness ease of understanding elegance time/space efficiency Efficiency of Algorithms Tue 10/2/12

4 4 Attributes of Algorithms Attributes of interest: correctness, ease of understanding, elegance, and efficiency Correctness: Is the problem specified correctly? Does the algorithm produce the correct result? Example: pattern matching Problem specification: “Given pattern p and text t, determine the location, if any, of pattern p occurring in text t ” Correctness: does the algorithm always work? If p is in t, will it say so? If the algorithm says p is in t, is it? Efficiency of Algorithms Tue 10/2/12

5 5 Attributes of Algorithms (continued) Ease of understanding, useful for: checking correctness program maintenance Elegance: using a clever or non-obvious approach Example: Gauss’ summing of 1 + 2 + … + 100 Attributes may conflict: Elegance often conflicts with ease of understanding Attributes may reinforce each other: Ease of understanding supports correctness Efficiency of Algorithms Tue 10/2/12

6 6 Attributes of Algorithms (continued) Efficiency: an algorithm’s use of time and space resources We’ll focus on computational efficiency (time) Timing an algorithm with a clock is not always useful Confounding factors: machine speed, size of input Benchmarking: timing an algorithm on standard data sets Testing hardware and operating system, etc. Testing real-world performance limits Analysis of algorithms: the study of the efficiency of algorithms Order of magnitude Θ () or just O() (“big O”): The class of functions that describes how time increases as a function of problem size (more on which later) Efficiency of Algorithms Tue 10/2/12

7 Sequential Search Analysis Tue 10/2/12Efficiency of Algorithms

8 8 Measuring Efficiency Sequential Search Searching: the task of finding a specific value in a list of values, or deciding it is not there Solution #1: Sequential search (from Ch. 2): “Given a target value and a random list of values, find the location of the target in the list, if it occurs, by checking each value in the list in turn” Efficiency of Algorithms Tue 10/2/12

9 9 Sequential Search Algorithm get (NameList, PhoneList, Name) i = 1 N = length(NameList) Found = FALSE while ( (not Found) and (i <= N) ) { if ( Name == NameList[i] ) { print (Name, “’s phone number is ”, PhoneList[i]) Found = TRUE } i = i+1 } if ( not Found ) { print (Name, “’s phone number not found!”) } Tue 10/2/12Efficiency of Algorithms

10 10 Measuring Efficiency Sequential Search (continued) Central unit of work: operations that occur most frequently Central unit of work in sequential search: Comparison of target Name to each name in the list Also add 1 to i Typical iteration: two steps (one comparison, one addition) Given a large input list: Best case: smallest amount of work algorithm must do Worst case: greatest amount of work algorithm must do Average case: depends on likelihood of different scenarios occurring What are the best, worst, and average cases for sequential search? Efficiency of Algorithms Tue 10/2/12

11 11 Measuring Efficiency Sequential Search (continued) Best case: target found with the first comparison ( 1 iteration ) Worst case : target never found or last value ( N iterations) Average case: if each value is equally likely to be searched, work done varies from 1 to N, on average N/2 iterations Most often we will consider the worst case Best case is too lucky – can’t count on it Average case is much harder to compute for many problems (hard to know the distribution of possible solutions) Efficiency of Algorithms Tue 10/2/12

12 12 Measuring Efficiency Order of Magnitude—Order n Sequential search worst case ( N ) grows linearly in the size of the problem 2 N steps (one comparison and one addition per loop) Also some initialization steps... On the last iteration, we may print something... After the loop, we test and maybe print... To simplify analysis, disregard the “negligible” steps (which don’t happen as often), and ignore the coefficient in 2 N Just pay attention to the dominant term (N) Order of magnitude O(N) : the class of all linear functions (any algorithm that takes C 1 N + C 2 steps for any constants C 1 and C 2 ) Efficiency of Algorithms Tue 10/2/12

13 13 Binary Search Solution #2: Binary search Assume list is sorted Split the list in half on each iteration On each iteration: If we’ve run out of things to look at, quit Is the centerpoint of the list the name we’re looking for? If so, we’re done! If not, check whether the name is alphabetically after or before the centerpoint of the list If it’s after, take the second half of the list and continue looping If it’s before, take the first half of the list and continue looping Tue 10/2/12Efficiency of Algorithms

14 14 Binary Search: Algorithm get (NameList, PhoneList, Name) N = length(NameList) upper = N lower = 1 Found = FALSE while ( (not Found) and (lower NameList[(lower+upper)/2] ) { lower = ((lower+upper)/2) + 1 // keep looking BEFORE center } if ( not Found ) { print (Name, “’s phone number not found!”) } Tue 10/2/12Efficiency of Algorithms

15 15 Measuring Efficiency Binary Search Best case: target found with the first comparison ( 1 iteration ) Worst case : target never found or last value (split the list in half repeatedly until only one item to examine) For a list of length 2, test twice For a list of length 4, test three times (split twice) For a list of length 8, only test four times! (split three times) For a list of length 2 k, how many times to test? For a list of length N, how many times to test? Average case: harder than you would think to analyze... and surprisingly, the average case is only one step better than the worst case Why? Hint: Try drawing a tree of all of the cases (left side, right side after each time the list is split in half) Efficiency of Algorithms Tue 10/2/12

16 16 Orders of Magnitude: log N Binary search has order of magnitude O(log 2 N) : grows very slowly Here: log 2 (base 2) but other bases behave similarly Tue 10/2/12Efficiency of Algorithms

17 Sorting Algorithms and Analysis http://www.youtube.com/watch?v=k4RRi_ntQc8 http://www.youtube.com/watch?v=2HjspVV0jK4 Tue 10/2/12Efficiency of Algorithms

18 18 Measuring Efficiency Selection Sort Sorting: The task of putting a list of values into numeric or alphabetical order Selection sort : Pass repeatedly over the unsorted portion of the list On each pass, select the largest remaining value Move that value immediately after the other unsorted values Accumulate the largest values, in reverse order, at the end of the list After each iteration, the number of sorted values grows by one and the number of unsorted values shrinks by one Efficiency of Algorithms Tue 10/2/12

19 19 Efficiency of Algorithms Tue 10/2/12 How long does this step take? (Hint: do we use sequential search or binary search?

20 20 Measuring Efficiency Selection Sort (continued) Central unit of work: hidden in “find largest” step Work done to find largest changes as unsorted portion shrinks (N-1) + (N-2) + … + 2 + 1 = N (N-1) / 2 Efficiency of Algorithms Tue 10/2/12

21 21 Measuring Efficiency Order of Magnitude—Order N 2 Selection sort takes N(N- 1)/2 steps = ½ N 2 – N/2 Order of magnitude: ignore all but the dominant (highest-order) term; ignore the coefficient Order O( N 2 ): the set of functions whose growth is on the order of N 2 Efficiency of Algorithms Tue 10/2/12

22 22 Measuring Efficiency Order of Magnitude—Order N 2 (continued) Eventually, every function with order N 2 has greater values than any function with order N Tue 10/2/12Efficiency of Algorithms

23 23 Efficiency of Algorithms Tue 10/2/12

24 24 Quicksort Quicksort is a faster searching algorithm Divide and conquer approach: Pick a point in the list (the pivot ) Toss everything smaller than the pivot to the left and everything larger to the right Separately sort those two sublists (using quicksort! This is an example of a recursive algorithm, which we’ll talk about more later in the semester...) Quicksort is O (N log N) on average (but can be O( N 2 ) in the worst case...) O (N log N) is slower than linear but faster than quadratic Tue 10/2/12Efficiency of Algorithms

25 25 Getting Out of Control Polynomially bounded: an algorithm that does work on the order of O( N k ) (or less) linear, log, quadratic,... degree k polynomial Most common problems are polynomially bounded (in “P”) Hamiltonian circuit (“traveling salesman problem”): no known polynomial solution—it is in “NP” Given a graph, find a path that passes through each vertex exactly once and returns to its starting point Efficiency of Algorithms Tue 10/2/12

26 26 Efficiency of Algorithms Tue 10/2/12 # possible circuits grows exponentially in the number of cities  takes a long time to find the best one!

27 27 Efficiency of Algorithms Tue 10/2/12

28 28 Summary We must evaluate the quality of algorithms, and compare competing algorithms to each other Attributes: correctness, efficiency, elegance, and ease of understanding Compare competing algorithms for time and space efficiency (time/space tradeoffs are common) Orders of magnitude capture work as a function of input size: O( log N ), O( N ), O( N 2 ), O( 2 N ) Problems with only exponential algorithms are intractable Efficiency of Algorithms Tue 10/2/12

29 29 Summary of Complexity Classes Tue 10/2/12Efficiency of Algorithms


Download ppt "CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted."

Similar presentations


Ads by Google