Data Structures Notes Unit-2
Data Structures Notes Unit-2
Data Structures Notes Unit-2
Page1of 38
Unit-I
Dictionarie
s
SET:-A set is a collection of welldefinedelements.The members ofasetare alldifferent.
A set is a group of “objects”
People in a class: { Alice, Bob, Chris }
Classes offered by a department: { CS 101, CS 202, … }
Colors of a rainbow: { red, orange, yellow, green, blue, purple }
States of matter { solid, liquid, gas, plasma }
States in the US: { Alabama, Alaska, Virginia, … }
Sets can contain non-related elements: { 3, a, red, Virginia }
• Although a set can contain (almost) anything, we will most often use sets of numbers
All positive numbers less than or equal to 5: {1, 2, 3, 4, 5}
A few selected real numbers: { 2.1, π, 0, -6.32, e }
For example:
(i) The set of odd numbers less than 7 is written as: {odd numbers less than 7}.
For example:
(i) Let N denote the set of first five natural numbers.
Therefore, N = {1, 2, 3, 4, 5} → Roster Form
In this form of representation of a set, the element of the set is described by using a symbol ‘x’ or any other variable
followed by a colon The symbol ‘:‘ or ‘|‘ is used to denote such that and then we write the property possessed by the
elements of the set and enclose the whole description in braces. In this, the colon stands for ‘such that’ and braces
stand for ‘set of all’.
bphanikrishnawordpress.com Page10of38
Page1
Selecting an implementation
AdvanceDataStructures-Unit-1(Dictionaries)
Hash Table is a data structure in which keys are mapped to array positions by a hash function.
A Hash Table is a data structure for storing key/value pairs
This table can be searched for an item in O(1) time using a hash function to form an address from the key.
Hash Function: Hash function is any well-defined procedure or mathematical function which converts a large,
possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index into an
array
· Hash function is a function which maps key values to array indices. (OR)
· Hash Function is a function which, when applied to the key, produces an integer which can be used as an address in a
hash table.
· We will use h(k) for representing the hashing function
Hash Values: The values returned by a hash function are called hash values or hash codes or hash sums or simply hashes
Hashing is the process of mapping large amount of data item to a smaller table with the help of a hashing function.
• Hash table is an extremely effective and practical way of implementing dictionaries.
• It takes O(1) time for search, insert, and delete operations in the average case. And O(n) time in the worst case.
AdvanceDataStructures-Unit-1(Dictionaries)
Collisions: If x1 and x2 are two different keys, but the hash values of x1 and x2 are equal (i.e., h(x1) = h(x2))
AdvanceDataStructures-Unit-1(Dictionaries)
then it is called as a collision.
Ex: Assume a hash function = h(k) = k mod 10
h(19)=19 mod 10=9
h(39)=39 mod 10=9
here h(19)=h(39) this is called collision.
Collision resolution is the most important issue in hash table implementations. To resolve the collisions two techniques are there.
1. Open Hashing 2. Closed Hashing
Perfect Hash Function is a function which, when applied to all the members of the set of items to be stored in a hash table,
produces a unique set of integers within some suitable range. Such function produces no collisions.
Good Hash Function: minimizes collisions by spreading the elements uniformly throughout the array.
Page20of38
AdvanceDataStructures-Unit-1(Dictionaries)
Page2
AdvanceDataStructures-Unit-1(Dictionaries)
AdvanceDataStructures-Unit-1(Dictionaries)
AdvanceDataStructures-Unit-1(Dictionaries)
AdvanceDataStructures-Unit-1(Dictionaries)
Page26of38
Quadratic probing:Insert
AdvanceDataStructures-Unit-1(Dictionaries)
AdvanceDataStructures-Unit-1(Dictionaries)
Page2
AdvanceDataStructures@Uint-1(Dictionaries)
Page30of38
AdvanceDataStructures@Uint-1(Dictionaries)
AdvanceDataStructures@Unit-1(Dictionaries)
Page31of38
AdvanceDataStructures@Unit-1(Dictionaries)
Division Method
• Idea:
Map a key k into one of the m slots by taking the remainder of k divided by m
h(k) = k mod m
• Advantage:
fast, requires only one operation
• Disadvantage:
Certain values of m are bad, e.g.,
• power of 2,
• non-prime numbers
Multiplicative method
Idea:
• Multiply key k by a constant A, where 0 < A < 1
• Extract the fractional part of kA
• Multiply the fractional part by m
• Take the floor of the result
h(k) = = m (k A mod 1)
• Disadvantage: Slower than division method Page33
• Advantage: Value of m is not critical, e.g., typically 2p
Universal Hashing
Page3
Runtime of hashing
the load factor λ is the fraction of the table that is full
λ = 0 (empty) λ = 0.5 (half full) λ = 1 (full table)
Linear probing:
If hash function is fair and λ < 0.5 - 0.6, then hashtable
operations are all O(1)
Double hashing:
If hash function is fair and λ < 0.9 - 0.95, then hashtable
operations are all O(1)
Rehashing
Hash Table may get full
No more insertions possible
Solution: Rehash
Build another table that is twice as big and has a new
hash function
Move all elements from smaller table to bigger table