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

Algorithm Design and Recursion, Computer Science, Basic Techniques, Efficiency of Algorithms, Process of Looking, Searching Problem, Search Function, Linear Search, Collection of Data, Binary Search are the important key points of lecture slides of Introduction to Computer Science.

Typology: Slides

2012/2013

Uploaded on 01/02/2013

netii
netii 🇮🇳

4.4

(7)

91 documents

1 / 133

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Programming:
An Introduction to
Computer Science
Algorithm Design and Recursion
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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,