CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
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!
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,
Introduction. 2COMPSCI Computer Science Fundamentals.
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.
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.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
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.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson Prof. Jeremy Dixon.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 03 – Variables
CMSC201 Computer Science I for Majors Lecture 25 – Classes
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
UMBC CMSC 104 – Section 01, Fall 2016
Outline lecture Revise arrays Entering into an array
CMPT 120 Topic: Searching – Part 2
Now with a speaking professor! (hopefully...)
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
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 21 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Intro to Computer Science II
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Sorting by Tammy Bailey
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Announcements Final Exam on August 17th Wednesday at 16:00.
And now for something completely different . . .
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
Computer Science 2 Hashing
Introduction to Programming
CS190/295 Programming in Python for Life Sciences: Lecture 6
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Last Class We Covered Sorting algorithms Searching algorithms
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Coding Concepts (Basics)
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
Last Class We Covered Asymptotic Analysis Run “time” Big O
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Basics of Recursion Programming with Recursion
Prof. Katherine Gibson Prof. Jeremy Dixon
Last Class We Covered File I/O How to open a file
CMSC201 Computer Science I for Majors Final Exam Information
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Last Class We Covered Recursion Stacks Parts of a recursive function:
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Last Class We Covered Sorting algorithms Searching algorithms
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations of the course

Last Class We Covered Tuples Dictionaries – Creating – Accessing – Manipulating Dictionaries vs Lists 2

Any Questions from Last Time?

Review: Tuples Create five tuples about you – (e.g., your major is CMSC, your age is 19) Create a tuple with all of the courses you’re taking this semester Create a tuple with a single element Create an empty tuple Create a tuple by casting a list 4

Review: Dictionaries Create a dictionary that contains four different (key, value) pairs, similar to “a is for apple” – Add one additional (key, value) pair – Update one of your (key, value) pairs – Remove one of your (key, value) pairs Why must dictionary keys be unique? Do values need to be unique? 5

Review: Matching Symbols Match the following data types to the symbols needed to create them (may be more than one) 6 String List Dictionary Tuple " ( ) { } [ ] '

Review: Matching Symbols Match the following data types to the symbols needed to create them (may be more than one) 7 String List Dictionary Tuple " ( ) { } [ ] '

Review: Mutability Which of the following are mutable data types? 8 String Boolean List Integer Float Dictionary Tuple ???

Review: Mutability Which of the following are mutable data types? 9 String Boolean List Integer Float Dictionary Tuple ??? Immutable Mutable Immutable Mutable Immutable

Review: Implementation You are given a dictionary of the NATO phonetic alphabet, in the form: alpha = {"A" : "Alpha", "B" : "Bravo", "C" : "Charlie",... etc. } Write a function to convert a string from the user into its phonetic code words – You need only handle letters (upper and lowercase) 10

Review: Implementation Example Here is an example of how it should work: Please enter a word: EXAMPLE The word "EXAMPLE" becomes "Echo X-ray Alpha Mike Papa Lima Echo" Please enter a word: dogmeat The word "dogmeat" becomes "Delta Oscar Golf Mike Echo Alpha Tango" 11

Any Questions about the Material we Just Reviewed?

Today’s Objectives To learn more about searching algorithms – Linear search – Binary search To understand why certain algorithms are “better” than others To learn about asymptotic performance – To examine how fast an algorithm “runs” 13

Search

Searching 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 be some significance to this order – That means sometimes we want to find where in the list something is! 15

Exercise: Search Write a function that takes a list and a variable and returns the first location of the variable in the list – If it’s not found, return -1 def find(myList, myVar): 16

Exercise Solution def find(myList, myVar): for i in range(0, len(myList)): if myList[i] == myVar: return i # we didn't find the variable return -1 17

Linear Search This is called linear search! It’s a pretty common, simple operation It’s especially useful when our information isn’t in a sorted order 18

Searching Sorted Information 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 earlier) What is a good algorithm for locating their phone number? Think about how you would do this. 19

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 This is very hard on phone books, but you’ll find the name! 20

Binary Search

Binary Search We can use this to search 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 22

Binary Search Binary search is a problem that can be broken down into – Something simple (breaking a list in half) – A smaller version of the original problem (searching that half of the list) That means we can use recursion!

Exercise: Recursive Binary Search Write a recursive binary search! Remember to ask yourself: – What is our base case(s)? – What is the recursive step? 24

Exercise: Recursive Binary Search Write a recursive binary search! Remember to ask yourself: – What is our base case(s)? – 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] 25

Exercise Solution 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) 26

Algorithm Run Time

Run Time for Search 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? 28

Run Time for Search Say we have a list that does not contain what we’re looking for. What about for binary search? – How many things does it have to look at to figure out the item’s not there for a list of 8 things? – 16 things? – 32 things? Notice anything different? 29

Different Run Times These algorithms scale differently! – Linear search does work equal to the number of items in the list – Binary search does work equal to the log 2 of the numbers in the list! A log 2 (x) is basically asking “2 to what power equals x?” – This is the same as saying, “how many times must we divide x in half before we hit 1?” 30

Different Run Times As our list gets bigger and bigger, which of the search algorithms is faster? – Linear or binary search? How much faster is binary search? 31

Another Example

Sum of All Products Say we have a list, and we want find the sum of everything in that list multiplied by everything else in that list – 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): 33

Exercise Solution def sumOfAllProducts(myList): result = 0 for item in myList: for item2 in myList: result += item * item2 return result 34

Run Time for Sum of All Products How many multiplications does this have to do for a list of 8 things? For 8 things, it does 64 multiplications – 16 things? For 16 things, it does 256 multiplications – 32 things? 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! 35

Asymptotic Analysis

Asymptotic Analysis For a list of size N, linear search does N operations. So we say it is O(N) (pronounced “big 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 37

Example 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: print(i*j*k) This will be O(N 3 ) 38

Any Other Questions?

General Announcements Lab 12 this week – last lab of the semester! Project 2 is out – Due by Tuesday, December 8th at 8:59:59 PM – Do NOT procrastinate! Next Class: Sorting 40

Announcements: Final Exam Final Exam will held be on Friday, December 11 th from 3:30 to 5:30 PM Being held in three separate rooms Section 1 (Gibson, 1) – CHEM 030 Section 7 (Dixon, 5:30) – CHEM 030 Section 13 (Dixon, 10) – CHEM 030 Section 19 (Morawski, 4) – PAHB 132 Section 25 (Gibson, 4) – PHYS 101 Make sure you go to the correct room! 41

Announcements: Surveys Next class, we will be doing the in-class SCEQ (Student Course Evaluation Questionnaire) – This is an important metric for assessment The second survey will be released and announced on Blackboard shortly – This is 1% of your grade, and is your chance to give feedback on your experience with the course 42