Data Structures and Algorithms Ā· Unit 7
Searching and Hashing
Exam-focused notes for Searching and Hashing (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Searching and Hashing in short
Searching means finding an item in a collection. Hashing is a method that finds items in close to constant time. This chapter explains linear and binary search, and the idea of hashing with ways to handle collisions.
What this unit covers
- Sequential search algorithm
- Binary search algorithm
- Comparison between sequential and binary search
- Hashing definition and advantages
- Hash collision definition
- Quadratic probing collision resolution
- Double hashing collision resolution
- Hashing versus binary search comparison
Quadratic probing collision resolution
2080 Ā· 5 marks
Why do we need hashing? Explain quadratic probing. [5]
2078 Ā· 5 marks
Define hashing. Explain quadratic probing with example. [5]
Show solved answerHide answer
Hashing and Quadratic Probing
Definition of Hashing
Hashing is a technique used to map a key to a specific location (index) in a hash table using a hash function. The hash function computes an index from the key, allowing for O(1) average-case time complexity for insertion, deletion, and search operations.
Hash Function:
$$h(k) = k \mod m$$
where k is the key and m is the size of the hash table.
A collision occurs when two different keys map to the same index. Various collision resolution techniques are used to handle this.
Quadratic Probing
Quadratic Probing is an open addressing collision resolution technique. When a collision occurs at index h(k), instead of checking the next sequential slot (as in linear probing), it probes slots at quadratic intervals.
Probe Sequence Formula:
$$h_i(k) = (h(k) + i^2) \mod m$$
where:
h(k)= initial hash valuei= probe number (i = 0, 1, 2, 3, ...)m= size of the hash table
Advantage over Linear Probing: Quadratic probing reduces primary clustering (long chains of consecutive filled slots).
Example
Insert the keys: 18, 26, 35, 9, 64 into a hash table of size m = 7
Hash function: h(k) = k mod 7
| Key | h(k) = k mod 7 | Probe | Final Index |
|---|---|---|---|
| 18 | 18 mod 7 = 4 | i=0, slot 4 is empty | 4 |
| 26 | 26 mod 7 = 5 | i=0, slot 5 is empty | 5 |
| 35 | 35 mod 7 = 0 | i=0, slot 0 is empty | 0 |
| 9 | 9 mod 7 = 2 | i=0, slot 2 is empty | 2 |
| 64 | 64 mod 7 = 1 | i=0, slot 1 is empty | 1 |
Now insert key = 26 again (to show collision), suppose we insert key = 19:
h(19) = 19 mod 7 = 5ā slot 5 is occupied (collision!)- Probe i=1:
(5 + 1²) mod 7 = 6ā slot 6 is empty ā insert at 6
Final Hash Table:
| Index | Key |
|---|---|
| 0 | 35 |
| 1 | 64 |
| 2 | 9 |
| 3 | -- |
| 4 | 18 |
| 5 | 26 |
| 6 | 19 |
Summary
| Feature | Detail |
|---|---|
| Probe formula | h(k, i) = (h(k) + i²) mod m |
| Collision handling | Quadratic jumps |
| Avoids | Primary clustering |
| Drawback | May cause secondary clustering; may not probe all slots if m is not prime |
Double hashing collision resolution
2079 Ā· 5 marks
Explain collision and collision resolution in hashing. What is double hashing? [5]
Sequential search algorithm
2079 Ā· 5 marks
Write short notes on: a) Linear Search Write short notes on: b) Minimum Spanning Tree [2.5+2.5]
Show solved answerHide answer
Short Notes
a) Linear Search
Definition: Linear search (also called sequential search) is the simplest searching algorithm that checks each element of a list one by one, from the beginning to the end, until the desired element (key) is found or the list is exhausted.
Algorithm:
LinearSearch(A[], n, key):
1. for i = 0 to n-1 do
2. if A[i] == key then
3. return i // element found at index i
4. return -1 // element not found
Working:
- Start from the first element.
- Compare each element with the search key.
- If a match is found, return the position.
- If the end of the array is reached without a match, return -1 (not found).
Example:
Array: [5, 3, 8, 1, 9], Key = 8
- Compare 5 != 8, Compare 3 != 8, Compare 8 == 8 ā Found at index 2.
Complexity:
| Case | Time Complexity |
|---|---|
| Best Case | O(1) |
| Worst Case | O(n) |
| Average Case | O(n) |
Space Complexity: O(1)
Advantages:
- Simple to implement.
- Works on both sorted and unsorted arrays.
- No preprocessing required.
Disadvantage:
- Inefficient for large datasets compared to binary search.
b) Minimum Spanning Tree (MST)
Definition: A Minimum Spanning Tree of a connected, undirected, weighted graph G = (V, E) is a spanning tree that connects all vertices with the minimum possible total edge weight, containing exactly |V| - 1 edges and no cycles.
Key Properties:
- It spans all vertices (all n vertices are included).
- It is acyclic (no cycles).
- It has exactly n - 1 edges for n vertices.
- The sum of edge weights is minimum among all possible spanning trees.
Example:
Consider a graph with 4 vertices and edges:
A --1-- B
| |
4 2
| |
D --3-- C
MST selects edges: A-B (1), B-C (2), C-D (3) ā Total weight = 6
Algorithms to Find MST:
| Algorithm | Approach | Time Complexity |
|---|---|---|
| Kruskal's | Sort edges by weight; add edge if no cycle (greedy) | O(E log E) |
| Prim's | Grow tree from a starting vertex by adding minimum weight edge | O(E log V) |
Applications:
- Network design (telephone, electrical, road networks).
- Cluster analysis.
- Approximation algorithms for NP-hard problems (e.g., Travelling Salesman Problem).
- Designing least-cost communication networks.
Significance: MST provides the most cost-effective way to connect all nodes in a network, making it fundamental in graph theory and network optimization.
Comparison between sequential and binary search
2078 Ā· 5 marks
Explain sequential search. How is it different from binary search? [5]
Hashing versus binary search comparison
0 Ā· 5 marks
Is hashing better than binary search algorithm? Give reasons. Define any two collision resolution techniques. [5]
Make Unit 7 stick
Practice BIT201 with flashcards & quizzes