cs221
DESCRIPTION
Week 6 - Friday. CS221. Last time. What did we talk about last time? Exam 1 Merge sort. Questions?. Assignment 3. Recursion. Project 2. Infix to Postfix Converter. Master Theorem. Master Theorem. Has a great name… - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/1.jpg)
CS221Week 6 - Friday
![Page 2: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Recursive running time Fast exponentiation Merge sort Introduced the Master theorem
![Page 3: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/3.jpg)
Questions?
![Page 4: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/4.jpg)
Recursion
Assignment 3
![Page 5: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/5.jpg)
Infix to Postfix Converter
Project 2
![Page 6: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/6.jpg)
Master Theorem
![Page 7: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/7.jpg)
Basic form that recursion must take
where
a is the number of recursive calls made b is how much the quantity of data is
divided by each recursive call f(n) is the non-recursive work done at
each step
![Page 8: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/8.jpg)
Case 1
If for some constant , then
![Page 9: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/9.jpg)
Case 2
If for some constant , then
![Page 10: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/10.jpg)
Case 3
If for some constant , and if
for some constant and sufficiently large , then
![Page 11: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/11.jpg)
Stupid Sort
![Page 12: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/12.jpg)
Stupid Sort algorithm (recursive)
Base case: List has size less than 3 Recursive case:
Recursively sort the first 2/3 of the list Recursively sort the second 2/3 of the
list Recursively sort the first 2/3 of the list
again
![Page 13: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/13.jpg)
Let's code up Stupid Sort!
![Page 14: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/14.jpg)
But, how long does it take?
![Page 15: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/15.jpg)
Stupid Sort
We need to know logb aa = 3b = 3/2 = 1.5 Because I’m a nice guy, I’ll tell you
that the log1.5 3 is about 2.7
![Page 16: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/16.jpg)
Binary Search
We know that binary search takes O(log n) time
Can we use the Master Theorem to check that?
![Page 17: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/17.jpg)
Student Lecture: Symbol Tables
![Page 18: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/18.jpg)
Symbol Tables
![Page 19: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/19.jpg)
Symbol tables
A symbol table goes by many names: Map Lookup table Dictionary
The idea is a table that has a two columns, a key and a value
You can store, lookup, and change the value based on the key
![Page 20: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/20.jpg)
Example
A symbol table can be applied to almost anything:
The key doesn't have to be a String
But it should be unique
Key Value
Spiderman Climbing and webs
Wolverine Super healing
Professor X Telepathy
Human Torch
Flames and flying
Deadpool Super healing
Mr. Fantastic
Stretchiness
Key Value
121 Programming I
122 Programming II
221 Data Structures and Algorithms
322 Formal methods
361 Computer Graphics
363 Computer Security
![Page 21: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/21.jpg)
Symbol table ADT
We can define a symbol table ADT with a few essential operations: put(Key key, Value value)▪ Put the key-value pair into the table
get(Key key):▪ Retrieve the value associated with key
delete(Key key)▪ Remove the value associated with key
contains(Key key)▪ See if the table contains a key
isEmpty() size()
It's also useful to be able to iterate over all keys
![Page 22: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/22.jpg)
Ordered symbol tables
The idea of order in a symbol table is reasonable: You want to iterate over all the keys in
some natural order Ordering can give certain kinds of data
structures (like a binary search tree) a way to organize
![Page 23: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/23.jpg)
Ordered symbol table ADT An ordered symbol table ADT adds the following
operations to a regular symbol table ADT: Key min()▪ Get the smallest key
Key max()▪ Get the biggest key
void deleteMin()▪ Remove the smallest key
void deleteMax()▪ Remove the largest key
Other operations might be useful, like finding keys closest in value to a given key or counting the number of keys in a range between two keys
![Page 24: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/24.jpg)
Implementations
Like other ADTs, a symbol table can be implemented in a number of different ways: Linked list Sorted array Binary search tree Balanced binary search tree Hash table
Note that a hash table cannot be used to implement an ordered symbol table And it's inefficient to use a linked list for ordered
![Page 25: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/25.jpg)
Sorted array
We know how to make a sorted array symbol table
A search is Θ(log n) time, which is great!
The trouble is that doing an insert takes Θ(n) time, because we have to move everything in the array around
A sorted array is a reasonable model for a symbol table where you don't have to add or remove items
![Page 26: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/26.jpg)
Trees
Trees will allow us to make a sorted symbol table with the following miraculous properties: Θ(log n) get Θ(log n) put Θ(log n) delete Θ(n) traversal (iterating over everything)
Unfortunately, only balanced binary search trees will give us this property
We'll start next week with binary search trees and then built up to balanced ones
![Page 27: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/27.jpg)
Upcoming
![Page 28: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/28.jpg)
Next time…
Binary trees Read section 3.2
![Page 29: CS221](https://reader035.vdocument.in/reader035/viewer/2022081603/56813515550346895d9c6a3e/html5/thumbnails/29.jpg)
Reminders
Finish Assignment 3 Due tonight!
Keep working on Project 2 Due next Friday
No class on Monday!