Week 09 Tutorial Notes

Hash Tables

Group Question

  1. 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.

  2. Given the following Hash Table that uses Linear Probing with the hash function \(h(k) = k \% 7\)
  3. Key 0 1 2 3 4 5 6
    Value 6 - - 10 - - 20
    1. Insert 24 and 13.
    2. How many comparisons does it take to search for each of these values: 10, 17, 6.

  4. 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.

    1. 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?

    2. 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.

    3. What is the average time complexity of your implementation?

  5. For a Hash Table that uses Double Hashing for a collision resolution strategy, explain whether the given hash functions are valid or problematic:
    1. \(h_1(k) = (k \% 17)\), \(h_2(k) = (k \% 16) + 1\)
    2. \(h_1(k) = (k \% 10)\), \(h_2(k) = (k \% 4) + 1\)
    3. \(h_1(k) = (k \% 15)\), \(h_2(k) = (k \% 7) + 1\)
    4. \(h_1(k) = (k \% 49)\), \(h_2(k) = (k \% 6) + 1\)
    5. \(h_1(k) = (k \% 64)\), \(h_2(k) = (2k + 1) \% 10\)
    6. \(h_1(k) = (k \% 9)\), \(h_2(k) = 2 ^ {(k \% 3)}\)
    7. \(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 6 13 - 10 24 - 20
  • Searching for 10 starts at index 3, and does 1 comparison before the key is found.
  • Searching for 17 starts at index 3, and does 3 comparisons before stopping the search with no key found.
  • Searching for 6 starts at index 6, and does 2 comparisons before the key is found.

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.

  1. Time Complexity is \(O(2^{n})\)
  2. 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;
    }
    
  3. Time Complexity of the above implementation is average \(O(n)\).

    1. Valid, because both \(h_1(k)\) and \(h_2(k)\) are coprime.
    2. Invalid. \(h_2(k) = (k \% 4) + 1\) can evaluate to \(2\) which is a factor of \(h_1(k) = (k \% 10)\).
    3. Invalid. \(h_2(k) = (k \% 7) + 1\) can evaluate to \(3\) or \(5\) which is a factor of \(h_1(k) = (k \% 15)\).
    4. 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)\).
    5. Valid, because \(h_2(k) = (2k + 1)\% 10\) always evaluates to an odd number, which is coprime with \(h_1(k) = (k \% 64)\).
    6. Valid, because \(h_2(k) = 2 ^ {(k \% 3)}\) evaluates to 1, 2 or 4, which are all coprime with \(h_1(k) = (k \% 9)\).
    7. 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.