HASHING AND SET PART-A 1. Define hash function. The mapping

advertisement
HASHING AND SET
PART-A
1. Define hash function.
The mapping of each key into some number ranged from 0 to tablesize-1.A hash function is simple to
compute and should ensure that any two distinct keys get different cells. Ideally , the hash function is used
to determine the location of any record given its key value. If the input keys are integers ,then simple mod
function which takes the form
Hash value=key % table size
2. What is mean by hashing?
The implementation of hash tables is frequently called hashing. Hashing is a technique used for
performing insertions, deletions, and finds in constant average time.
Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that
represents the original string.
Hashing is used to index and retrieve items in a database because it is faster to find the item using the
short hashed key than to find it using the original value.
3. State the importance of hashing?
Maps key with the corresponding value using hash function.
Hash tables support the efficient addition of new entries, and the time spent on searching for the required
data is independent of the number of items stored that is O(1).
4. Give a simple hash function when the input keys are integers?
if the input keys are integers then the hash function is
Hash value=key % table size
When the key happens to have undesirable properties such that for table size 10, all key result in zero,
then the standard hash function is a bad choice.To avoid such a bad situation ensure that the table size be
a prime.
5. What does collision in hashing? How does it avoided?
We should choose a hash function such that it gives us distinct values for different values of primary key.
Collision is the situation in which two different values may hash to the same address. Collision may be
avoided by applying some special techniques.
a. Separate chaining
b. Open Addressing.
6. What is mean by collision in hashing?
When an element is inserted, it hashes to the same values as an already inserted element then it
produce collision
7.What is meant by hash address? And in what way it is generated.
The value that the hash function returns for a given key is its hash address. A perfect hash function maps
every key into a different table location and have high degree of collision avoidance.
The address may be generated using different methods such as:
1. Truncation method 2. Folding method
3. Mid-square method 4. Division method.
8.What is an open addressing (or) closed hashing? What are the various techniques used in it?
The technique of finding the availability of another suitable empty location in the hash table, when the
calculated hash address is already occupied is known as open addressing. This technique is generally used
where storage space is large. Arrays are used here as hash tables. There are various open addressing
methods are available. They are,
1. Linear probing
2. Quadratic probing
3. Double hashing
9. What is meant by double hashing?
In double hashing method, we apply a second hash function to X and probe at a distance hash2(X),
2 hash2, (X),…, and so on. An effective hash function in double hashing is,
Hash2 (X) = R – (X mod R) with R a prime number where R < Table size
10. Define linear probing.
Linear probing is a scheme for resolving hash collision of values of hash function by sequentially
searching the hash table for a free location.
In a linear probing, F is a linear function of i , typically F( i ) = i.
11. Define quadratic probing.
Quadratic probing is a collision resolution method that eliminates the primary clustering problem of linear
probing. The popular choice is F ( i) = i2
In the case of quadratic probing we are still looking for an empty location. However, instead of
incrementing offset by 1 every time, as in linear probing, we will increment the offset by 1, 3, 5, 7, ... We
explore a sequence of location until an empty one is found as follows:
index, index + 1, index + 4, index + 9, index + 16, ...
12. Define extendible hashing.
Extendible Hashing is a mechanism for altering the size of the hash table to accommodate new entries
when buckets overflow. Common strategy in internal hashing is to double the hash table and rehash each
entry. However, this technique is slow, because writing all pages to disk is too expensive.
Therefore, instead of doubling the whole hash table, we use a directory of pointers to buckets, and double
the number of buckets by doubling the directory, splitting just the bucket that overflows.
13. Define set.
a set is an abstract data structure that can store certain values, without any particular order
and no repeated values. Some set data structures are designed for static sets that do not change
with time, and allow only query operations — such as checking whether a given value is in the
set, or enumerating the values in some arbitrary order. Other variants, called dynamic or
mutable sets, allow also the insertion and/or deletion of elements from the set.
14. Define basic Disjoint set class operations .
The two basic disjoint set class operations are:
find – return the name of the set (i.e., equivalence class containing a given element).
union – adds relations to a set.
15. Define equivalence relation.


A relation R is defined on a set S if for every pair of elements (a, b), a, b  S , a R b is
either true or false. If a R b is true, we say that a is related to b.
An equivalence relation is a relation R that satisfied three properties:
o Reflexive: a R a is true for all a  S .
o Symmetric: a R b if and only if b R a.
o Transitive: a R b and b R c implies that a R c.
16. Write an application of set.
A network of computers and list of bidirectional connections ,each of these connections allows a file
transfer from one computer to another. Two computers can transfer files if and only if they are in the
same set. when we read some connections say (u,v) ,we test to see whether u and v in the same set and do
nothing if they are. If they are in different sets, we merge their sets.
17. What is meant by path compression in the set?
A path compression is performed during a Find operation and is independent of the strategy used to
perform Unions. Suppose the operation is Find(X). Then the effect of path compression is that every node
on the path from X to the root has its parent changed to the root. The following figure shows the effect of
path compression after Find(15).
Path compression is compatible with union-by-size.
It is not compatible with union-by-height as there is no efficient way to change the height of the
tree.
18. Explain union by size in smart union algorithm.
A simple improvement to the union- find algorithm is to make the smaller tree (in terms of number of
nodes) a subtree of the larger, breaking ties by any method .This is called union-by-size.
19. Explain union by height in smart union algorithm.
union-by-height is an another implementation of smart union algorithm, in which we keep track of
the height of the trees and perform union operations by making a shallow tree a subtree of the deeper
tree.
20.Define the set operations with example.




union(S,T): returns the union of sets S and T.
intersection(S,T): returns the intersection of sets S and T.
difference(S,T): returns the difference of sets S and T.
subset(S,T): a predicate that tests whether the set S is a subset
of set T.
21.What is separate chaining (or) external hashing? What are its advantages?
Separate changing is a collision resolution method in which a chain or link is allocated to store the key
element when the hash address calculated cannot be allocate(already occupied).
Advantages
1. Helps to get a uniform and perfect collision resolution hashing.
2. It allows an unlimited number of collisions to the same hash address.
3. Doesn’t require a prior knowledge of the number of elements that are stored in the hash table.
4. The elements having the same memory address will be in the same chain and hence leads to faster
searching.
PART-B
1. Explain in detail the linear probing technique. (8)
2. Write routine to find and to insert an element in a separate chaining hash table.(8)
3. Explain in detail about hashing. (8)
4. Given input {4371, 1323, 6173, 4199, 4344, 9679, 1989} and a hash function
h(X)=X(mod 10), show the resulting
Separate chaining table
Open addressing hash table using linear probing
Open addressing hash table using quadratic probing
Open addressing hash table with second hash function
H2(X)=7-(X mod 7)
5 . How will you resolve the collisions, while inserting elements into the hash table using separate
chaining and linear probing? Write the routines for inserting, searching and removing elements from the
hash table using above mentioned techniques.
6. What is meant by collision resolution in hashing? Explain in detail any one strategy for detailing with
it?
7.(i). Explain the various hashing technique with suitable example. (10)
(ii). When will collision arise? Discuss? (6)
8.Discuss in detail about the working of different hashing function? (10)
9.Describe open hashing and closed hashing technique in detail?
10.Explain the dynamic equivalence problem with an example?
11.Explain the Find-union algorithm of the set representation?
12.Explain the path compression of the set representation?
Download