Data Structures 1 Flashcards
What is the goal of Hashing?
Do faster than O(LogN) time complexity for: lookup, insert, and remove operations. To achieve O(1)
What kind of Collection is Hashing?
value-orientated.
How does Hashing work?
- you have a key for the item.2. the item’s key gets churned within the hash function to form the Hash index.3. The hash index can be applied to the data array, and so, the specific data is found.
hash Table:
An array that stores a collection of items.
Table Size(TS)
The Array’s Length
Load Factor (LF)
Number of items/Table size. For instance, a load factor of 1 = 100% of the items are used.
Key
Information in items that is used to determine where the item goes into the table.
Hash Function
A function that takes in the key to compute a specific Hash Index.
What would the Perfect Hash Function be?
Each Key maps to an unique Hash Index.
How do you delete a value within the hash table?
You just set Table[hash(Key)] = null
How do you look up a value within the hash table?
return Table[Hash(key)];
How do you insert a value within the hash table?
Table[Hash(key)]=data;
What is the worst case time complexity for: Insert, lookup, and delete, for hash functions?
O(1)
If we have UW-Madison student ID’s, and we wanted the ideal hash functions, how would we do it, and why would there be a problem
-> We’d simply count each one as an index-> Hash table would be huge.
Collisions:
When the Hash Function returns the same index for different keys.
Good Hash Function qualities:
- Must be deterministic:-> Key must ALWAYS generate the same Hash Index (excluding rehashing).2. Must achieve uniformity-> Keys should be distributed evenly across hash table.3. FAST/EASY to compute-> only use parts of the key that DISTINGUISH THE ITEMS FROM EACH OTHER4. Minimize collisions:
Extraction:
Breaking keys into parts and using the parts that uniquely identify with the item. 379452 = 394121267 = 112
Folding:
Combining parts of the key using operations like + and bitwise operations such as exclusive or.Key: 123456789 123 456 789 — 1368 ( 1 is discarded)
Weighting:
Emphasizing some parts of the key over another.
Compressing:
Ensuring the hash code is a valid index for the table size.
The more items a table can hold, the () likely a collision will happen.
less
Load Factor
Approximately how it’s full… 0.7-0.8.
Why do we use prime numbers for table size?
We mod often, and prime numbers give us the most unique numbers. (2*ts+1)
Steps to resizing:
- Double table size to nearest prime number2. Re-hash items from old table into the new table.
Rehashing Complexity:
O(N)– costly. Carefully select initial TS to avoid re-hashing.
Collesion handeling:
How you handle the collisions so each element in the hittable stores only one item.
Idea of probing:
If you have a collision, search somewhere else on the table.
Linear Probing:
Step size is 1. Find the index, and keep incrementing by one until you find a free space.
Quadratic Probing:
Probe Sequence is (Hk+1)^2.Minimizes clusteringbetter at distinguishing items across table.
Probe Hashing:
-> Hash it, and if it leads to a collision, use a separate equation to determine the step size and use that step size to find a new site.
Collision Hashing using Buckets
-Each element can solre than one item.-throw collisions into a bucket.-buckets aren’t sorted.
Array Bucket
– a bucket of arrays. -Fixed in size.-size of about 3 work usually well.
Chained bucket:
+–easy to implement+– buckets can’t overfill+– buckets won’t waste time.+– buckets are dynamically sized.
Tree Buckets
+–WC = O(logN)+–no wasting space+–dynamically sized– more complicated than what’s needed. –> insert with dups= O(1)–> W/o dups = O(N)
HashCode Method:
-method of OBJECT class-Returns an int-default hash code is BAD– computed from Object’s memory address. –> must override
HashTable & HashMap class
java.utilimplements map interfaceK– type paramater for key and v– type parameter for associated valueOperations: lookup, insert, delete.Constructor lets you set init capacity and load factorhandles collisions with chained bucketshash map only allows null for keys and values