Group Question
Given the hash function \(h(k) = k \% 6\) and a Hash Table that uses Separate Chaining, insert the following values:
7, 19, 35, 36, 10, 2, 26.
Given the following Hash Table that uses Linear Probing with the hash function \(h(k) = k \% 7\)
Key
0
1
2
3
4
5
6
Value
6
-
-
10
-
-
20
Insert 24 and 13.
How many comparisons does it take to search for each of these values: 10, 17, 6.
Given a Hash Table that uses Double Hashing, where the primary hash is \(h_1(k) = k \% 7\) and the secondary hash is \(h_2(k) = (k \% 4) + 1\). Insert the following values: 11, 6, 29, 50, 25, 7.
The following function computes the \(n\)th fibonacci number.
int recursive_fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 ) return 1 ;
int result = recursive_fib ( n -1 ) + recursive_fib ( n -2 );
return result ;
}
What is the time complexity?
We would like to keep the recursive approach but improve the time complexity of this function by using a Hash Table.
int recursive_fib ( int n , HashTable ht )
Write psuedocode to achieves this.
What is the average time complexity of your implementation?
For a Hash Table that uses Double Hashing for a collision resolution strategy, explain whether the given hash functions are valid or problematic:
\(h_1(k) = (k \% 17)\), \(h_2(k) = (k \% 16) + 1\)
\(h_1(k) = (k \% 10)\), \(h_2(k) = (k \% 4) + 1\)
\(h_1(k) = (k \% 15)\), \(h_2(k) = (k \% 7) + 1\)
\(h_1(k) = (k \% 49)\), \(h_2(k) = (k \% 6) + 1\)
\(h_1(k) = (k \% 64)\), \(h_2(k) = (2k + 1) \% 10\)
\(h_1(k) = (k \% 9)\), \(h_2(k) = 2 ^ {(k \% 3)}\)
\(h_1(k) = (k \% 7)\), \(h_2(k) = k \% 3\)
Group Work Answers:
Click each section to reveal.
Key
0
1
2
3
4
5
6
Value
50
29
-
25
11
7
6
Inserting 11: \(h_1(k) = 4\) and is inserted at index 4.
Inserting 6: \(h_1(k) = 6\) and is inserted at index 6.
Inserting 29: \(h_1(k) = 1\) and is inserted at index 1.
Inserting 50: \(h_1(k) = 1\) which clashes. \(h_2(k) = 3\) and is inserted at index 0.
Inserting 25: \(h_1(k) = 4\) which clashes. \(h_2(k) = 2\) and is inserted at index 3.
Inserting 7: \(h_1(k) = 0\) which clashes. \(h_2(k) = 4\) and is inserted at index 5.
Time Complexity is \(O(2^{n})\)
int recursive_fib ( int n , HashTable ht ) {
if ( n == 0 ) return 0 ;
if ( n == 1 ) return 1 ;
if ( HashTableContains ( ht , n ) == true ) {
return HashTableGet ( ht , n );
}
int result = recursive_fib ( n -1 , ht ) + recursive_fib ( n -2 , ht );
HashTableInsert ( ht , n , result );
return result ;
}
Time Complexity of the above implementation is average \(O(n)\).
Valid, because both \(h_1(k)\) and \(h_2(k)\) are coprime.
Invalid. \(h_2(k) = (k \% 4) + 1\) can evaluate to \(2\) which is a factor of \(h_1(k) = (k \% 10)\).
Invalid. \(h_2(k) = (k \% 7) + 1\) can evaluate to \(3\) or \(5\) which is a factor of \(h_1(k) = (k \% 15)\).
Valid, because \(h_2(k) = (k \% 6) + 1\) evaluates to 1, 2, 3, 4, 5 or 6, which are all coprime with \(h_1(k) = (k \% 49)\).
Valid, because \(h_2(k) = (2k + 1)\% 10\) always evaluates to an odd number, which is coprime with \(h_1(k) = (k \% 64)\).
Valid, because \(h_2(k) = 2 ^ {(k \% 3)}\) evaluates to 1, 2 or 4, which are all coprime with \(h_1(k) = (k \% 9)\).
Invalid, because \(h_2(k) = k \% 3\) can evaluate to 0 which will not cycle through every slot in the Hash Table when there is a collision.
COMP2521 26T3: Data Structures and Algorithms
is brought to you by
the School of Computer Science and Engineering
at the University of New South Wales , Sydney.
For all enquiries, please email the class account at
cs2521@cse.unsw.edu.au
CRICOS Provider 00098G