Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Announcements You survived midterm 2! No Class / No Office hours Friday.
MATH 224 – Discrete Mathematics
Introduction to Computing Science and Programming I
CPS120: Introduction to Computer Science Searching and Sorting.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chapter 19: Searching and Sorting Algorithms
Introduction to Analysis of Algorithms
Recitation 9 Programming for Engineers in Python.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Quicksort.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Quicksort
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Recursion, Complexity, and Sorting By Andrew Zeng.
Analysis of Algorithms
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
CSC 211 Data Structures Lecture 13
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 19: Friday 9 th March Simon Gay Department of Computing Science University of Glasgow 2006/07.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Searching Topics Sequential Search Binary Search.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Algorithms.
Week 9 - Monday CS 113.
Recitation 13 Searching and Sorting.
Teach A level Computing: Algorithms and Data Structures
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Data Structures Review Session
Coding Concepts (Basics)
Lecture 11 Algorithm Design
Presentation transcript:

Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07

Computing Science 1P Lecture 21 - Simon Gay2 What's coming up? Wed 18 th April (today):lecture Fri 20 th April:lecture Mon 23 rd April, :FPP demo session; PRIZES! Mon 23 rd – Wed 25 th April:labs: exam preparation / lab exam preparation Wed 25 th April:lecture / tutorial Friday 27 th April:lecture: revision / questions with Peter Saffrey Mon 30 th April – Wed 2 nd May:Lab Exam Wed 2 nd May:No lecture Fri 4 th May:No lecture THE END

2006/07Computing Science 1P Lecture 21 - Simon Gay3 Lab Exam 2 As you know, there will be a second lab exam, during the week beginning 30 th April. It is worth 10% of the module mark. The question has been available since Monday afternoon. In the first lab exam, although there were many very good submissions, a worrying number of submissions showed very little evidence of advance preparation. You can prepare for the lab exam in any way you want to, including asking a friend to explain the solution to you. On the day, of course, you are on your own. Trying to solve the problem from scratch in the exam is just making life difficult for yourself.

2006/07Computing Science 1P Lecture 21 - Simon Gay4 Algorithms We have looked at algorithms for sorting; we saw that choosing a better algorithm can have a dramatic effect on the efficiency of a program. Now let's consider searching, another basic computing task. The general problem: find a desired item of data in a collection. Example: in a collection of (name,address) pairs, find a particular person's address.

2006/07Computing Science 1P Lecture 21 - Simon Gay5 Searching in unstructured data Imagine that we have a list of (key,value) pairs and we do not know anything about the order. We can easily define: def find(key,data): for i in data: if i[0] == key: return i[1] raise "KeyNotFound",key

2006/07Computing Science 1P Lecture 21 - Simon Gay6 Searching in unstructured data What can we say about the time taken by find ? Just as for sorting, the relevant measure is the number of comparisons. Clearly it is possible that the key we are looking for is at the end of the list. In that case we have to compare the given key with every key in the list. If we imagine testing find repeatedly with a large number of random lists, on average it will have to search half way along the list. When analysing algorithms, sometimes we talk about the average case and sometimes the worst case. In this situation they are both the same: order n, where n is the length of the list.

2006/07Computing Science 1P Lecture 21 - Simon Gay7 Searching in unstructured data It's obvious that we can't do better than order n for searching in an unstructured list, because we can't avoid the possibility that the desired key is at the end. Remarkably, there is an algorithm for quantum computers which only takes square root of n operations to search in an unstructured list. However, quantum computers of a useful size have not yet been built. To find out more, look up Grover's algorithm. But let's stick to conventional algorithms…

2006/07Computing Science 1P Lecture 21 - Simon Gay8 More efficient search If we can't improve the algorithm for search in an unstructured list, the only alternative is to change the data structure: don't use an unstructured list! The first idea is quite simple: use an ordered list instead. In other words, put the data in the list in such a way that the keys are in order. Often this means alphabetical order or numerical order, but other more complex orders could be defined. Example: in a dictionary, the words are in alphabetical order, and we can take advantage of this to find words quickly.

2006/07Computing Science 1P Lecture 21 - Simon Gay9 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat

2006/07Computing Science 1P Lecture 21 - Simon Gay10 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat It could be anywhere in the list.

2006/07Computing Science 1P Lecture 21 - Simon Gay11 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat It could be anywhere in the list. The list has length 12. Divide it by 2 and look at position 6.

2006/07Computing Science 1P Lecture 21 - Simon Gay12 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat It could be anywhere in the list. The list has length 12. Divide it by 2 and look at position 6. cat < garage

2006/07Computing Science 1P Lecture 21 - Simon Gay13 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Because the list is ordered, we now know that cat must be before garage, i.e. it is in the first half of the list.

2006/07Computing Science 1P Lecture 21 - Simon Gay14 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Because the list is ordered, we now know that cat must be before garage, i.e. it is in the first half of the list.

2006/07Computing Science 1P Lecture 21 - Simon Gay15 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Now repeat, searching in a list of length 6.

2006/07Computing Science 1P Lecture 21 - Simon Gay16 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Now repeat, searching in a list of length 6. Divide by 2 and look at position 3.

2006/07Computing Science 1P Lecture 21 - Simon Gay17 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Now repeat, searching in a list of length 6. Divide by 2 and look at position 3. cat < door

2006/07Computing Science 1P Lecture 21 - Simon Gay18 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat We now know that cat must be before door.

2006/07Computing Science 1P Lecture 21 - Simon Gay19 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat We now know that cat must be before door.

2006/07Computing Science 1P Lecture 21 - Simon Gay20 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Now repeat, searching in a list of length 3.

2006/07Computing Science 1P Lecture 21 - Simon Gay21 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Now repeat, searching in a list of length 3. Divide by 2 and look at position 1.

2006/07Computing Science 1P Lecture 21 - Simon Gay22 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat Now repeat, searching in a list of length 3. Divide by 2 and look at position 1. cat > badger

2006/07Computing Science 1P Lecture 21 - Simon Gay23 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat We now know that cat must be after badger.

2006/07Computing Science 1P Lecture 21 - Simon Gay24 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat We now know that cat must be after badger. We have narrowed down the possible position of cat to just one place. And in fact cat is there, so we have found it.

2006/07Computing Science 1P Lecture 21 - Simon Gay25 Binary search android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: cat We now know that cat must be after badger. We have narrowed down the possible position of cat to just one place. And in fact cat is there, so we have found it. If a different word is there, then cat is not in the list.

2006/07Computing Science 1P Lecture 21 - Simon Gay26 Binary search The idea of binary search is very simple, but implementing it correctly requires care: there are many possibilities for "off by one" errors. Searching in a dictionary is often used as an example of binary search, but we don't really use dictionaries in exactly this way. Usually we flick through the pages quickly to find the right letter, then do something similar to binary search. A typical dictionary has extra structure to support this process (e.g. words in the page headers; thumbholes for indexing).

2006/07Computing Science 1P Lecture 21 - Simon Gay27 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay28 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay29 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay30 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay31 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay32 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle We could stop here, but if we follow the algorithm strictly, we continue dividing the region in two

2006/07Computing Science 1P Lecture 21 - Simon Gay33 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay34 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle

2006/07Computing Science 1P Lecture 21 - Simon Gay35 Another example android badger cat door ending fireman garage handle iguana jumper kestrel lemon Search for the key: handle Now we can certainly stop

2006/07Computing Science 1P Lecture 21 - Simon Gay36 Analysing binary search Remember that we are interested in the number of comparisons. Suppose that we are searching in a list of length n. We compare the middle item with the search key. The result might tell us we have found the key, but in general it narrows down the region of the list in which we are searching. The possible region of the list is now half the size it was.

2006/07Computing Science 1P Lecture 21 - Simon Gay37 Analysing binary search We keep halving the size of the region, until we narrow it down to a single position in which the key should be found. How many times do we have to halve the size? n = 16: 8, 4, 2, 1 4 comparisons n = 64: 32, 16, 8, 4, 2, 1 6 comparisons It is the logarithm of n to base 2, i.e. the power of 2 which gives n. Binary search is an order log n algorithm.

2006/07Computing Science 1P Lecture 21 - Simon Gay38 Analysing binary search We can compare the efficiency of an order n algorithm with that of an order log n algorithm: nlog ntimen microsec millisec microsec millisec microsec millisec microsec sec microsec sec

2006/07Computing Science 1P Lecture 21 - Simon Gay39 Implementing binary search def find(key,data): lower = 0 upper = len(data)-1 length = upper - lower + 1 while length > 1: midpoint = lower + length/2 if key < data[midpoint]: upper = midpoint - 1 else: lower = midpoint length = upper - lower + 1 if key == data[lower]: return lower else: raise "KeyNotFound",key

2006/07Computing Science 1P Lecture 21 - Simon Gay40 Implementing binary search def find(key,data): lower = 0 upper = len(data)-1 length = upper - lower + 1 while length > 1: midpoint = lower + length/2 if key < data[midpoint]: upper = midpoint - 1 else: lower = midpoint length = upper - lower + 1 if key == data[lower]: return lower else: raise "KeyNotFound",key android badger cat door ending fireman garage handle iguana jumper kestrel lemon lower upper midpoint

2006/07Computing Science 1P Lecture 21 - Simon Gay41 Termination When writing programs with loops, we have to be sure that they terminate, i.e. eventually stop. In almost all of our previous programs, it has been obvious that loops terminate. In a for loop, the number of iterations is known before we start, e.g. for x in range(10) In a while loop, the condition can be anything, but we have always used a simple structure: i = 0 while i < 10: # code inside the loop, not changing i i = i + 1

2006/07Computing Science 1P Lecture 21 - Simon Gay42 Termination Binary search uses a while loop with a more complex structure: length = upper - lower + 1 while length > 1: midpoint = lower + length/2 if key < data[midpoint]: upper = midpoint - 1 else: lower = midpoint length = upper - lower + 1 Let's prove that eventually length <= 1, so that the loop terminates.

2006/07Computing Science 1P Lecture 21 - Simon Gay43 Proving termination Consider one iteration of the loop. At the beginning we have values lower, upper and length. At the end of the body of the loop we have new values lower', upper' and length'. We will prove that length' < length. Therefore as we go round the loop repeatedly, length gets smaller and smaller. It is always an integer value, so eventually it must reach 1 or less.

2006/07Computing Science 1P Lecture 21 - Simon Gay44 Proving termination At the top of the loop we have values lower, upper. We have length = upper – lower + 1 Assume that length > 1, so that we go into the loop. At the bottom of the loop we have new values lower', upper' and we have a new value length' = upper' – lower' + 1 we also have midpoint = lower + length/2 Now we consider the possible ways of calculating lower', upper'

2006/07Computing Science 1P Lecture 21 - Simon Gay45 Proving termination Case 1: we take the first branch of the if statement. upper' = midpoint – 1 lower' = lower length' = upper' – lower' + 1 = midpoint – 1 – lower + 1 = midpoint – lower = lower + length/2 – lower = length/2 1

2006/07Computing Science 1P Lecture 21 - Simon Gay46 Proving termination Case 2: we take the second branch of the if statement. upper' = upper lower' = midpoint length' = upper' – lower' + 1 = upper – midpoint + 1 = upper – (lower + length/2) + 1 = upper – lower – length/2 + 1 = length – length/2 1

2006/07Computing Science 1P Lecture 21 - Simon Gay47 Proving termination We have proved that whichever path we take through the body of the while loop, length decreases. Therefore the loop must terminate. With further calculation of a similar kind (exercise!) we can prove that when the loop terminates, length = 1 (not < 1), meaning that we really have identified one location in the list where the key should be found if it is present at all.

2006/07Computing Science 1P Lecture 21 - Simon Gay48 Refining binary search It might turn out that when we look at the midpoint of the list, the key we want happens to be there. We might as well take advantage of that case: while length > 1: midpoint = lower + length/2 if key < data[midpoint]: upper = midpoint - 1 elif key > data[midpoint]: lower = midpoint else: return midpoint but notice that we have introduced an extra comparison. What can we do about this?

2006/07Computing Science 1P Lecture 21 - Simon Gay49 The function cmp The problem is that when we compare two values, there are three possible results: equal, first one smaller, second one smaller The comparisons >= == return a boolean result, so they only tell us one of two possible results. To solve this problem, Python provides the function cmp. cmp(a,b) returns 0 if a == b -1 if a < b 1 if a > b

2006/07Computing Science 1P Lecture 21 - Simon Gay50 Using cmp while length > 1: midpoint = lower + length/2 r = cmp(key,data[midpoint]) if r == -1: upper = midpoint - 1 elif r == 1: lower = midpoint else: return midpoint