Hashing Exercises Hash Function v A good hash

  • Slides: 57
Download presentation
Hashing Exercises

Hashing Exercises

Hash Function v A good hash function should be: v Repeatable v Fast to

Hash Function v A good hash function should be: v Repeatable v Fast to compute (don’t want to add complexity) v Minimizes Collisions v Utilizes the whole range of the table

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing A# want to add complexity) v Minimizes Collisions v Utilizes the whole range of the table int hash=A[3];

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing A# want to add complexity) v Minimizes Collisions int hash=0; v Utilizes the whole for(int i=1; i<9; i++) range of the table hash+=A[i];

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing A# want to add complexity) v Minimizes Collisions v Utilizes the whole range of the table int hash=rand()%100, 000;

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing A# want to add complexity) v Minimizes Collisions int hash=0; v Utilizes the whole for(int i=1; i<9; i++) range of the table hash=hash*128+A[i];

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing

Hash functions v Repeatable Table Size: 100, 000 v Fast to compute (don’t Hashing A# want to add complexity) v Minimizes Collisions int hash=0; v Utilizes the whole for(int i=1; i<9; i++) range of the table hash=hash<<7+A[i];

Exercises v Table Size 15 1) Linear Probe v Hash- mod 15 2) Quadratic

Exercises v Table Size 15 1) Linear Probe v Hash- mod 15 2) Quadratic Probe v Insert 16, 7, 28, 31, 3) Chaining 67, 28, 29, 73, 99, 43, 218 4) Double hash – %13 + 1

Exercise 1 0 v Linear probe v Insert 16, 7, 28, 31, 67, 28,

Exercise 1 0 v Linear probe v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Exercise 1 v Linear probe v Insert 16, 7, 28, 31, 67, 28, 29,

Exercise 1 v Linear probe v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? v 22 0 73 1 16 2 31 3 43 4 5 6 7 7 8 67 9 99 10 218 11 12 13 28 14 29

Exercise 2 0 v Quadratic probe v Insert 16, 7, 28, 31, 67, 28,

Exercise 2 0 v Quadratic probe v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Exercise 2 0 v Quadratic probe v Insert 16, 7, 28, 31, 67, 28,

Exercise 2 0 v Quadratic probe v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 1 16 2 31 3 245 260 275 4 v How many probes? v 18 v 43 couldn’t insert (I went to 40) 73 5 6 7 7 8 67 9 99 10 11 12 218 13 28

Exercise 3 0 v Chaining v Insert 16, 7, 28, 31, 67, 28, 29,

Exercise 3 0 v Chaining v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Exercise 3 0 v Chaining v Insert 16, 7, 28, 31, 67, 28, 29,

Exercise 3 0 v Chaining v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? v 16 1 16 ->31 2 3 4 5 6 7 7 ->67 8 8 9 99 10 11 12 13 28 ->73 ->43 14 29

Exercise 4 0 v Double hash %13 + 1 v Insert 16, 7, 28,

Exercise 4 0 v Double hash %13 + 1 v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Exercise 4 0 v Double hash %13 + 1 v Insert 16, 7, 28,

Exercise 4 0 v Double hash %13 + 1 v Insert 16, 7, 28, 31, 67, 28, 29, 73, 99, 43, 218 v How many probes? 1 16 2 3 43 4 31 5 6 v 15 7 7 v Cannot insert 73 8 218 9 99 10 67 because 9*5 = 15*3 11 12 13 28 14 29

Clustering v Primary Clustering- Didn’t initially hash to same location, but had to compete

Clustering v Primary Clustering- Didn’t initially hash to same location, but had to compete for successive v Secondary Clustering- Hashed to the same place, and continued competing v Non-Clustering

Complete Heap v Insert 10, 105, 18, 9

Complete Heap v Insert 10, 105, 18, 9

Complete Heap 0 v Put it in the array 1 2 3 4 5

Complete Heap 0 v Put it in the array 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Heap v Insert 10, 105, 18, 9 0 100 1 19 2 36

Complete Heap v Insert 10, 105, 18, 9 0 100 1 19 2 36 3 17 4 3 5 25 6 1 7 2 8 7 9 10 11 12 13 14

Complete Heap v Delete largest, then delete again for next largest

Complete Heap v Delete largest, then delete again for next largest

Complete Heap v Delete largest, then delete again for next largest 0 100 1

Complete Heap v Delete largest, then delete again for next largest 0 100 1 19 2 36 3 17 4 3 5 25 6 1 7 2 8 7 9 10 11 12 13 14

Leftist Heap v Are these leftist heaps?

Leftist Heap v Are these leftist heaps?

Leftist Heap v Are these leftist heaps?

Leftist Heap v Are these leftist heaps?

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Insert 3

Leftist Heap v Insert 3

Leftist Heap v Insert 4

Leftist Heap v Insert 4

Leftist Heap v Delete

Leftist Heap v Delete

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Insert 4

Skew Heap v Insert 4

Skew Heap v Delete

Skew Heap v Delete

Binomial Queue v Merge

Binomial Queue v Merge

Binomial Queue v Insert 12, 11, 28, 33

Binomial Queue v Insert 12, 11, 28, 33

Binomial Queue v Delete

Binomial Queue v Delete

Binomial Queue v Delete

Binomial Queue v Delete

Binomial Queue v Delete

Binomial Queue v Delete

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Leftist Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Skew Heap v Merge

Complete Heap v Is this a complete max heap? 0 100 1 89 2

Complete Heap v Is this a complete max heap? 0 100 1 89 2 36 3 17 4 67 5 35 6 19 7 4 8 7 9 43 10 59 11 28 12 23 13 21 14