For the input 40, 60, 37, 83, 42, 18 and hash function h(K) = K mod 11, a. Construct both the open and closed hash tables. b. Find the largest number of key comparisons in a succes... For the input 40, 60, 37, 83, 42, 18 and hash function h(K) = K mod 11, a. Construct both the open and closed hash tables. b. Find the largest number of key comparisons in a successful search in both tables. c. Find the average number of key comparisons in a successful search in both tables.
Understand the Problem
The question asks to construct open and closed hash tables using a specific hash function, h(K) = K mod 11, with the provided key inputs. It also requests to determine the largest and average number of key comparisons during successful searches in both hash table implementations.
Answer
Largest comparisons closed: 1; Average closed: 1; Largest comparisons open: 2; Average open: $\approx 1.14$
Answer for screen readers
Largest number of comparisons in closed: 1
Average number of comparisons in closed: 1
Largest number of comparisons in open: 2
Average number of comparisons in open: $\approx 1.14$
Steps to Solve
- Identify the keys and hash function
The keys provided for the hash table construction are: 15, 28, 33, 10, 17, 3, 5
. The hash function is defined as $h(K) = K \mod 11$.
- Hash the keys into the closed hash table
Using the hash function, calculate the index for each key:
- For key 15: $h(15) = 15 \mod 11 = 4$
- For key 28: $h(28) = 28 \mod 11 = 6$
- For key 33: $h(33) = 33 \mod 11 = 0$
- For key 10: $h(10) = 10 \mod 11 = 10$
- For key 17: $h(17) = 17 \mod 11 = 5$
- For key 3: $h(3) = 3 \mod 11 = 3$
- For key 5: $h(5) = 5 \mod 11 = 5$
The closed hash table will look like this, where each bucket can only store one key:
- Index 0: 33
- Index 1:
- Index 2:
- Index 3: 3
- Index 4: 15
- Index 5: 5 (collision, will replace with 17)
- Index 6: 28
- Index 7:
- Index 8:
- Index 9:
- Index 10: 10
- Hash the keys into the open hash table
To manage collisions, the open hash table uses a probing method:
- Index 0: 33
- Index 1:
- Index 2:
- Index 3: 3
- Index 4: 15
- Index 5: 5 (insert 17 to the next free space)
- Index 6: 28
- Index 7: (insert 17 to the next free space after 5)
- Index 8:
- Index 9:
- Index 10: 10
- Determine searches in closed hash table
Now we need to find the largest and average number of key comparisons:
- For index 4 (key 15): 1 comparison
- For index 6 (key 28): 1 comparison
- For index 5 (key 17): 1 comparison
- For index 10 (key 10): 1 comparison
- Index 0 (key 33): 1 comparison
- Index 3 (key 3): 1 comparison
- The largest number of comparisons: 1
- Average number is: $\frac{1 + 1 + 1 + 1 + 1 + 1 + 1}{7} = 1$
- Determine searches in open hash table
In the open hash table, we may have more comparisons due to the probing:
- Keys 15, 28, and 33: 1 comparison each
- Key 3: 1 comparison
- Key 5: 1 comparison, must check for 17 next
- For key 10: 1 comparison
- Key 17: 2 comparisons (due to probing)
- Average number of comparisons: $\frac{6 + 2}{7} = \frac{8}{7} \approx 1.14$, Largest: 2
Largest number of comparisons in closed: 1
Average number of comparisons in closed: 1
Largest number of comparisons in open: 2
Average number of comparisons in open: $\approx 1.14$
More Information
In hash tables, "closed" refers to a table that does not allow collisions by replacing any collided entry, while "open" allows probing to find the next available slot. The concept of probing is helpful in understanding how to handle collisions effectively.
Tips
- Forgetting to rehash or handle collisions properly when they occur.
- Miscalculating the position using the hash function could lead to improper placements of keys.
AI-generated content may contain errors. Please verify critical information