PowerPoint Presentation - University Of Illinois...std data structures...

24
CS 225 Data Structures October 29 – Hashing Analysis Wade Fagen-Ulmschneider

Transcript of PowerPoint Presentation - University Of Illinois...std data structures...

Page 1: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

CS 225Data Structures

October 29 – Hashing AnalysisWade Fagen-Ulmschneider

Page 2: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Running Times

Linear Probing:• Successful: ½(1 + 1/(1-α))• Unsuccessful: ½(1 + 1/(1-α))2

Double Hashing:• Successful: 1/α * ln(1/(1-α))• Unsuccessful: 1/(1-α)

The expected number of probes for find(key) under SUHA

Page 3: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

ReHashingWhat if the array fills?

Page 4: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Which collision resolution strategy is better?• Big Records:

• Structure Speed:

What structure do hash tables replace?

What constraint exists on hashing that doesn’t exist with BSTs?

Why talk about BSTs at all?

Page 5: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Running Times

Hash Table AVL Linked List

Find

SUHA:

Worst Case:

Insert

SUHA:

Worst Case:

Storage Space

Page 6: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

std data structures

std::map

Page 7: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

std data structures

std::map::operator[]::insert::erase

::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator to first element > key

Page 8: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

std data structures

std::unordered_map::operator[]::insert::erase

::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator to first element > key

Page 9: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

std data structures

std::unordered_map::operator[]::insert::erase

::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator to first element > key

::load_factor()::max_load_factor(ml) Sets the max load factor

Page 10: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Mattox Monday

Page 11: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

CS 225 Final Exam

Exam Details:CBTF Exam, 3 Hours LongFormat: 1 Theory Exam + 1 Programming Exam

When you finish your exam, you’re done with CS 225! :)

Signup Process:CS 225 Exam will run Thursday, Dec. 13 - Tuesday, Dec. 18

(including both Saturday and Sunday)

You can sign up for your slot right now!

Page 12: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Secret, Mystery Data Structure

ADT:insertremoveisEmpty

Page 13: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Priority Queue Implementationinsert removeMin

O(n) O(n)

O(1) O(n)

O( lg(n) ) O(1)

O( lg(n) ) O(1)

unsorted

sorted

Page 14: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Another possibly structure…

5

15 9

25

4

6

7 20

1116 1214

Page 15: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

(min)Heap

5

15 9

25

4

6

7 20

1116 1214

A complete binary tree T is a min-heap if:• T = {} or• T = {r, TL, TR}, where r is

less than the roots of {TL, TR} and {TL, TR} are min-heaps.

Page 16: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

(min)Heap

5

15 9

25

4

6

7 20

1116 1214

4 5 6 15 9 7 20 16 25 14 12 11

Page 17: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

insert

5

15 9

25

4

6

7 20

1116 1214

4 5 6 15 9 7 20 16 25 14 12 11

Page 18: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

template <class T>

void Heap<T>::_insert(const T & key) {

// Check to ensure there’s space to insert an element

// ...if not, grow the array

if ( size_ == capacity_ ) { _growArray(); }

// Insert the new element at the end of the array

item_[++size] = key;

// Restore the heap property

_heapifyUp(size);

}

12

3

4

5

6

7

8

9

10

11

12

insert

5

15 9

25

4

6

7 20

1116 1214

4 5 6 15 9 7 20 16 25 14 12 11

Page 19: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

growArray

5

15 9

25

4

6

7 20

1116 1214

Page 20: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

template <class T>

void Heap<T>::_heapifyUp( _________________ ) {

if ( index > _________ ) {

if ( item_[index] < item_[ parent(index) ] ) {

std::swap( item_[index], item_[ parent(index) ] );

_heapifyUp( ________________ );

}

}

}

12

3

4

5

6

7

8

9

insert - heapifyUptemplate <class T>

void Heap<T>::_insert(const T & key) {

// Check to ensure there’s space to insert an element

// ...if not, grow the array

if ( size_ == capacity_ ) { _growArray(); }

// Insert the new element at the end of the array

item_[++size] = key;

// Restore the heap property

_heapifyUp(size);

}

12

3

4

5

6

7

8

9

10

11

12

Page 21: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

removeMin

5

15 9

25

4

6

7 20

1116 1214

4 5 6 15 9 7 20 16 25 14 12 11

Page 22: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

template <class T>

void Heap<T>::_removeMin() {

// Swap with the last value

T minValue = item_[1];

item_[1] = item_[size_];

size--;

// Restore the heap property

heapifyDown();

// Return the minimum value

return minValue;

}

12

3

4

5

6

7

8

9

10

11

12

13

removeMin

5

15 9

25

4

6

7 20

1116 1214

4 5 6 15 9 7 20 16 25 14 12 11

Page 23: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

template <class T>

void Heap<T>::_removeMin() {

// Swap with the last value

T minValue = item_[1];

item_[1] = item_[size_];

size--;

// Restore the heap property

_heapifyDown();

// Return the minimum value

return minValue;

}template <class T>

void Heap<T>::_heapifyDown(int index) {

if ( !_isLeaf(index) ) {

T minChildIndex = _minChild(index);

if ( item_[index] ___ item_[minChildIndex] ) {

std::swap( item_[index], item_[minChildIndex] );

_heapifyDown( ________________ );

}

}

}

12

3

4

5

6

7

8

9

10

removeMin - heapifyDown12

3

4

5

6

7

8

9

10

11

12

13

Page 24: PowerPoint Presentation - University Of Illinois...std data structures std::map::operator[]::insert::erase::lower_bound(key) Iterator to first element ≤ key::upper_bound(key) Iterator

Array Abstractions