Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,

Slides:



Advertisements
Similar presentations
Announcements You survived midterm 2! No Class / No Office hours Friday.
Advertisements

Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Recursion CMSC 201. Recursion Vs. Iteration Recursion and iteration are both methods of solving problems. An iterative solution to a problem is anything.
HST 952 Computing for Biomedical Scientists Lecture 9.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Searching. 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.
Searching Arrays Linear search Binary search small arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
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.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
February 4, 2005 Searching and Sorting Arrays. Searching.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
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.
CSC 211 Data Structures Lecture 13
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills.
COMPSCI 102 Discrete Mathematics for Computer Science.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Data Structures Arrays and Lists Part 2 More List Operations.
 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)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
Searching Arrays Linear search Binary search small arrays
16 Searching and Sorting.
Chapter 15 Recursion.
CMPT 120 Topic: Searching – Part 2
Week 13: Searching and Sorting
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Last Class We Covered Sorting algorithms Searching algorithms
Last Class We Covered Sorting algorithms “Run” time Selection Sort
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Algorithm Analysis The case of recursion.
Chapter 15 Recursion.
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
And now for something completely different . . .
Searching CSCE 121 J. Michael Moore.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Last Class We Covered Sorting algorithms Searching algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Prof. Katherine Gibson Prof. Jeremy Dixon
Data Structures Introduction
Last Class We Covered Sorting algorithms Searching algorithms
Presentation transcript:

Analysis And Algorithms CMSC 201

Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4, 5, 2, 3], there may (should) be some significance to this order. That means sometimes we want to find where in the list something is!

Exercise Write a function that takes a list and a variable and finds and returns the first location of the variable in the list. def find(myList, myVar):

Exercise Write a function that takes a list and a variable and finds and returns the first location of the variable in the list. If it’s not found, return -1. def find(myList, myVar): for iin range(0, len(myList)): if myList[i] == myVar: return i return -1

Search This is called linear search! It’s a fairly common, if simple operation. Now, imagine we’re looking for information in something sorted, like a phone book. We know someone’s name, and want to find their entry in the book (just like we knew the variable we were trying to locate in the search above). What is a good algorithm for locating their phone number? Think about how you would do this.

Search Algorithm in English: Open the book midway through. If the person’s name is on the page you opened to, you’re done! If the person’s name is after the page you opened to, tear the book in half, throw the first half away and repeat this process on the second half. If the person’s name is before the page you opened to, tear the book in half, throw the second half away and repeat this process on the first half.

Search Open the book midway through. If the person’s name is on the page you opened to, you’re done! If the person’s name is after the page you opened to, tear the book in half, throw the first half away and repeat this process on the second half. If the person’s name is before the page you opened to, tear the book in half, throw the second half away and repeat this process on the first half. This is very hard on phone books, but you’ll find the name!

Search We can use the same idea for searching sorted lists! To make our problem slightly easier, let’s make it the problem of “checking to see if something is in a sorted list”. For purposes of our example, if there’s no “middle” of the list, we’ll just look at the lower of the two possible indices. So if the list has 11 elements, the fifth one would be our middle.

Search

This algorithm is called binary search. Binary search is a problem that can be broken down into something simple (breaking a list in half) and a smaller version of the original problem (searching that half of the list). That means we can use recursion!

Exercise Write a recursive binary search! Remember to ask yourself: What is our base cases? What is the recursive step? def binarySearch(myList, item): A hint: in order to get the number at the middle of the list, use this line: myList[len(myList) // 2] Anyone remember what the // 2 does?

Exercise Write a recursive binary search! def binarySearch(myList, item): if(len(myList) == 0): return False middle = len(myList)//2 if(myList[middle] == item): return True elif(myList[middle] < item): return binarySearch(myList[middle+1:], item) else: return binarySearch(myList[:middle], item)

Compare! Say we have a list that does not contain what we’re looking for. How many things in the list does linear search have to look at for it to figure out the item’s not there for a list of 8 things? 16 things? 32 things? How about binary search looking through a list of 8, 16, and 32 things? Notice anything different?

Compare! These algorithms scale differently! Linear search does work equal to the number of items in the list while binary search does work equal to the log 2 of the numbers in the list! (Remember, a log 2 (x) is basically asking “2 to what power equals x?” This is basically the same as saying “how many times can I divide x in half before I hit 1?” Which should make sense in this context.)

Compare! This means as our lists gets bigger, linear search becomes worse way faster than binary search.

Another Example Consider another example. Say we have a list, and we want find the sum of everything in that list multiplied by everything else in that list (this is a completely arbitrary example). So if the list is [1, 2, 3], we want to find the value of: 1*1 + 1*2 + 1*3 + 2*1 + 2*2 + 2*3 + 3*1 + 3*2 + 3*3 As an exercise, try writing this function! def sumOfAllProducts(myList):

Another Example def sumOfAllProducts(myList): result = 0 for item in myList: for item2 in myList: result += item * item2 return result

Another Example Now the big question: How many multiplications does this have to do for a list of 8 things? 16 things? 32 things?

Another Example For 8 things, it does 64 multiplications. For 16 things, it does 256 multiplications. For 32 things, you do 1024 multiplications. In general, if you give it a list of size N, you’ll have to do N 2 multiplications!

Formalizing For a list of size N, linear search does N operations. So we say it is O(N) (pronounced “oh of n”). For a list of size N, binary search does lg(N) operations, so we say it is O(lg(N)) For a list of size N, our sum of products function does N 2 operations, which means it is O(N 2 ). The function in the parentheses indicates how fast the algorithm scales.

Exercise Sort from best to worst! O(N 3 ) O(lg(N)) O(1) O(N 2 ) O(N!)

Exercise What is the big O of the following, given a list of size N: for i in myList: for j in myList: for k in myList: foo()

Exercise What is the big O of the following, given a list of size N: for i in myList: for j in myList: for k in myList: foo() This will be O(N 3 )

Exercise What about this? for i in myList: for j in myList: for k in myList: foo() for i in myList: foo()

Exercise For a list of size 5, the first loop will be 125 and the second loop will be 5, so it will execute 130 times. List of size 10: List of size 100: As you can see, the second part of the code isn’t having much of an effect on the overall number of operations. This code snippet would be O(N 3 +N). However, as N approaches infinity, the smaller terms stop mattering. So we would drop the N and simply call this O(N 3 )