Objectives Objectives Searching a Simple Searching Problem A
Total Page:16
File Type:pdf, Size:1020Kb
Python Programming: Objectives An Introduction to To understand the basic techniques for Computer Science analyzing the efficiency of algorithms. To know what searching is and understand the algorithms for linear and binary Chapter 13 search. Algorithm Design and Recursion To understand the basic principles of recursive definitions and functions and be able to write simple recursive functions. Python Programming, 1/e 1 Python Programming, 1/e 2 Objectives Searching To understand sorting in depth and Searching is the process of looking for a know the algorithms for selection sort particular value in a collection. and merge sort. For example, a program that maintains To appreciate how the analysis of a membership list for a club might need algorithms can demonstrate that some to look up information for a particular problems are intractable and others are member œ this involves some sort of unsolvable. search process. Python Programming, 1/e 3 Python Programming, 1/e 4 A simple Searching Problem A Simple Searching Problem Here is the specification of a simple In the first example, the function searching function: returns the index where 4 appears in def search(x, nums): the list. # nums is a list of numbers and x is a number # Returns the position in the list where x occurs # or -1 if x is not in the list. In the second example, the return value -1 indicates that 7 is not in the list. Here are some sample interactions: >>> search(4, [3, 1, 4, 2, 5]) 2 Python includes a number of built-in >>> search(7, [3, 1, 4, 2, 5]) -1 search-related methods! Python Programming, 1/e 5 Python Programming, 1/e 6 Python Programming, 1/e 1 A Simple Searching Problem A Simple Searching Problem We can test to see if a value appears in The only difference between our a sequence using in. search function and index is that if x in nums: index raises an exception if the target # do something value does not appear in the list. If we want to know the position of x in We could implement search using a list, the index method can be used. index by simply catching the exception >>> nums = [3, 1, 4, 2, 5] >>> nums.index(4) and returning -1 for that case. 2 Python Programming, 1/e 7 Python Programming, 1/e 8 A Simple Searching Problem Strategy 1: Linear Search def search(x, nums): Pretend you‘re the computer, and you were try: given a page full of randomly ordered return nums.index(x) numbers and were asked whether 13 was in except: return -1 the list. How would you do it? Sure, this will work, but we are really Would you start at the top of the list, interested in the algorithm used to scanning downward, comparing each number actually search the list in Python! to 13? If you saw it, you could tell me it was in the list. If you had scanned the whole list and not seen it, you could tell me it wasn‘t there. Python Programming, 1/e 9 Python Programming, 1/e 10 Strategy 1: Linear Search Strategy 1: Linear Search This strategy is called a linear search, The Python in and index operations where you search through the list of both implement linear searching items one by one until the target value algorithms. is found. def search(x, nums): If the collection of data is very large, it for i in range(len(nums)): if nums[i] == x: # item found, return the index value return i makes sense to organize the data return -1 # loop finished, item was not in list somehow so that each data value This algorithm wasn‘t hard to develop, doesn‘t need to be examined. and works well for modest-sized lists. Python Programming, 1/e 11 Python Programming, 1/e 12 Python Programming, 1/e 2 Strategy 1: Linear Search Strategy 2: Binary Search If the data is sorted in ascending order If the data is sorted, there is an even better (lowest to highest), we can skip checking searching strategy œ one you probably some of the data. already know! As soon as a value is encountered that is Have you ever played the number guessing greater than the target value, the linear game, where I pick a number between 1 and search can be stopped without looking at the 100 and you try to guess it? Each time you rest of the data. guess, I‘ll tell you whether your guess is On average, this will save us about half the correct, too high, or too low. What strategy work. do you use? Python Programming, 1/e 13 Python Programming, 1/e 14 Strategy 2: Binary Search Strategy 2: Binary Search Young children might simply guess Each time we guess the middle of the numbers at random. remaining numbers to try to narrow Older children may be more systematic, down the range. using a linear search of 1, 2, 3, 4, … This strategy is called binary search. until the value is found. Binary means two, and at each step we Most adults will first guess 50. If told the value is higher, it is in the range 51- are diving the remaining group of 100. The next logical guess is 75. numbers into two parts. Python Programming, 1/e 15 Python Programming, 1/e 16 Strategy 2: Binary Search Strategy 2: Binary Search We can use the same approach in our The heart of the algorithm is a loop that looks binary search algorithm! We can use two at the middle element of the range, variables to keep track of the endpoints of comparing it to the value x. the range in the sorted list where the If x is smaller than the middle item, high is number could be. moved so that the search is confined to the lower half. Since the target could be anywhere in the list, initially low is set to the first location If x is larger than the middle item, low is in the list, and high is set to the last. moved to narrow the search to the upper half. Python Programming, 1/e 17 Python Programming, 1/e 18 Python Programming, 1/e 3 Strategy 2: Binary Search Strategy 2: Binary Search def search(x, nums): The loop terminates when either low = 0 high = len(nums) - 1 x is found while low <= high: # There is still a range to search mid = (low + high)/2 # Position of middle item item = nums[mid] There are no more places to look if x == item: # Found it! Return the index ( > ) return mid low high elif x < item: # x is in lower half of range high = mid - 1 # move top marker down else: # x is in upper half of range low = mid + 1 # move bottom marker up return -1 # No range left to search, # x is not there Python Programming, 1/e 19 Python Programming, 1/e 20 Comparing Algorithms Comparing Algorithms Which search algorithm is better, linear or One way to conduct the test would be to binary? code up the algorithms and try them on The linear search is easier to understand and varying sized lists, noting the runtime. implement Linear search is generally faster for lists of length The binary search is more efficient since it doesn‘t need to look at each element in the list 10 or less There was little difference for lists of 10-1000 Intuitively, we might expect the linear search to work better for small lists, and binary Binary search is best for 1000+ (for one million list search for longer lists. But how can we be elements, binary search averaged .0003 seconds sure? while linear search averaged 2.5 second) Python Programming, 1/e 21 Python Programming, 1/e 22 Comparing Algorithms Comparing Algorithms While interesting, can we guarantee that How do we count the number of these empirical results are not dependent on —steps“? the type of computer they were conducted on, the amount of memory in the computer, Computer scientists attack these the speed of the computer, etc.? problems by analyzing the number of We could abstractly reason about the steps that an algorithm will take relative algorithms to determine how efficient they are. We can assume that the algorithm with to the size or difficulty of the specific the fewest number of —steps“ is more problem instance being solved. efficient. Python Programming, 1/e 23 Python Programming, 1/e 24 Python Programming, 1/e 4 Comparing Algorithms Comparing Algorithms For searching, the difficulty is determined by Let‘s consider linear search. the size of the collection œ it takes more steps For a list of 10 items, the most work we might have to do is to look at each item in turn œ looping at most 10 to find a number in a collection of a million times. numbers than it does in a collection of 10 For a list twice as large, we would loop at most 20 numbers. times. For a list three times as large, we would loop at most How many steps are needed to find a value in 30 times! a list of size n? The amount of time required is linearly In particular, what happens as n gets very related to the size of the list, n. This is what large? computer scientists call a linear time algorithm. Python Programming, 1/e 25 Python Programming, 1/e 26 Comparing Algorithms Comparing Algorithms Now, let‘s consider binary search. To determine how many items are Suppose the list has 16 items.