Docsity

Algorithm Design and Recursion - Introduction to Computer Science - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Algorithm Design and Recursion - Introduction to Computer Science - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Python Programming:

An Introduction to

Computer Science

Algorithm Design and Recursion

Objectives

 To understand the basic techniques for analyzing the efficiency of algorithms.  To know what searching is and understand the algorithms for linear and binary search.  To understand the basic principles of recursive definitions and functions and be able to write simple recursive functions.

Searching

 Searching is the process of looking for a

particular value in a collection.  For example, a program that maintains a membership list for a club might need to look up information for a particular member – this involves some sort of search process.

A simple Searching Problem

 Here is the specification of a simple searching function: def search(x, nums):

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.

 Here are some sample interactions: >>> search(4, [3, 1, 4, 2, 5]) 2 >>> search(7, [3, 1, 4, 2, 5])

A Simple Searching Problem

 We can test to see if a value appears in a sequence using in. if x in nums:

do something

 If we want to know the position of x in a list, the index method can be used. >>> nums = [3, 1, 4, 2, 5] >>> nums.index(4) 2

A Simple Searching Problem

 The only difference between our search function and index is that index raises an exception if the target value does not appear in the list.  We could implement search using index by simply catching the exception and returning -1 for that case.

Strategy 1: Linear Search

 Pretend you’re the computer, and you were given a page full of randomly ordered numbers and were asked whether 13 was in the list.  How would you do it?  Would you start at the top of the list, scanning downward, comparing each number 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.

Strategy 1: Linear Search

 This strategy is called a linear search,

where you search through the list of items one by one until the target value is found.  def search(x, nums):for i in range(len(nums)): if nums[i]return == x: # item found, return the index valuei return -1 # loop finished, item was not in list  This algorithm wasn’t hard to develop, and works well for modest-sized lists.

Strategy 1: Linear Search

 If the data is sorted in ascending order (lowest to highest), we can skip checking some of the data.  As soon as a value is encountered that is greater than the target value, the linear search can be stopped without looking at the rest of the data.  On average, this will save us about half the work.

Strategy 2: Binary Search

 If the data is sorted, there is an even better searching strategy – one you probably already know!  Have you ever played the number guessing game, where I pick a number between 1 and 100 and you try to guess it? Each time you guess, I’ll tell you whether your guess is correct, too high, or too low. What strategy do you use?

Strategy 2: Binary Search

 Each time we guess the middle of the remaining numbers to try to narrow down the range.

 This strategy is called binary search.

 Binary means two, and at each step we are diving the remaining group of numbers into two parts.

Strategy 2: Binary Search

 We can use the same approach in our binary search algorithm! We can use two variables to keep track of the endpoints of the range in the sorted list where the number could be.  Since the target could be anywhere in the list, initially low is set to the first location in the list, and high is set to the last.

Strategy 2: Binary Search

 The loop terminates when either  x is found  There are no more places to look (low > high)

Strategy 2: Binary Search

def search(x, nums): low = 0 high = len(nums) - 1 while low <= high: # There is still a range to search mid = (low + high)//2 # Position of middle item item = nums[mid] if x == item: return mid # Found it! Return the index elif x < item: high = mid - 1 # x is in lower# move top marker down half of range else: low = mid + 1 # x is in upper# move bottom marker up half of range return -1 # No range left # x is not there to search,