Top Banner
CS221 Week 6 - Friday
29

CS221

Jan 03, 2016

Download

Documents

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 Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS221

CS221Week 6 - Friday

Page 2: CS221

Last time

What did we talk about last time? Recursive running time Fast exponentiation Merge sort Introduced the Master theorem

Page 3: CS221

Questions?

Page 4: CS221

Recursion

Assignment 3

Page 5: CS221

Infix to Postfix Converter

Project 2

Page 6: CS221

Master Theorem

Page 7: CS221

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

Case 1

If for some constant , then

Page 9: CS221

Case 2

If for some constant , then

Page 10: CS221

Case 3

If for some constant , and if

for some constant and sufficiently large , then

Page 11: CS221

Stupid Sort

Page 12: CS221

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

Let's code up Stupid Sort!

Page 14: CS221

But, how long does it take?

Page 15: CS221

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

Binary Search

We know that binary search takes O(log n) time

Can we use the Master Theorem to check that?

Page 17: CS221

Student Lecture: Symbol Tables

Page 18: CS221

Symbol Tables

Page 19: CS221

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

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

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

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

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

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

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

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

Upcoming

Page 28: CS221

Next time…

Binary trees Read section 3.2

Page 29: CS221

Reminders

Finish Assignment 3 Due tonight!

Keep working on Project 2 Due next Friday

No class on Monday!