Professional Documents
Culture Documents
CH 6 Searching Algorithms and Hashing
CH 6 Searching Algorithms and Hashing
CH 6 Searching Algorithms and Hashing
1
Search
Unit 6: Searching Algorithms and Hashing (5 hrs)
1.Sequential Search
2.Binary Search
3.Hashing
3.1.Hash Function
3.2.Hash Table
3.3.Hashing as a Data Structure and a Search Technique
4.Collision in Hash Table
5.Collision Resolution Techniques
5.1.Open Hashing: Separate Chaining
5.2.Closed Hashing: Linear Probing, Quadratic Probing and Double
Hashing
6.Load Factor and Rehashing
If the data are sorted, the searching becomes very easy and efficient.
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 5
fl
Sequential Search
• Sequential search is also called a Linear search.
• It compares the item (to be searched) or key with every elements in
the array/list starting from the first element, one at a time.
• It keeps comparing until the item (with matching key) is found or all
the elements are examined/compared without success.
• If the item is found, it returns the index (or location) of the item,
else it returns -1.
• Sequential search can be applied on the sorted or unsorted list.
39
1. Search is successful
4 2. Returned location = 4
Sequential Search
https://brilliant.org/wiki/binary-search/
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 14
How does binary search work?
• Example:Consider the following is our sorted array and let us assume
that we need to search the location of value 31 using binary search
Now, find new low and the new mid value again as
low = mid+1 = 4+1 = 5
mid = low + (high - low) / 2 = 5 + (9 - 5 ) / 2 = 7
So, index 7 is the mid of the array and 35 is the new middle element.
Now, find new high and the new mid value again as
high = mid-1 = 7-1 = 6
mid = low + (high - low) / 2 = 5 + (6 - 5 ) / 2 = 5+0.5 = 5.5 = 5
So, index 5 is the mid of the array and 31 is the new middle element.
• Iterative Method
• Worst-case = O(log2n)
• Average-case = O(log2n)
• Best-case = O(1)
Linear
Search
Binary
Search
Linear
Search
Binary
Search
Linear
Search
Binary
Search
Linear
Search
Binary
Search
Linear
Search
Binary
Search
Linear
Search
Binary
Search
5
The answer is YES.
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 32
Can we do better?
26
17 26
77 17 26
77 17 53 26
77 30 17 53 26
77 43 30 17 53 26
77 43 30 17 53 26 62
77 43 30 17 53 26 62
77 43 30 17 53 26 62
30 f(30) = 30 mod 7 2
Value Index Value
Hash Function
(Key)
17 17 mod 7 3 1 43
77 77 mod 7 0 2 30
53 53 mod 7 4 3 17
30 30 mod 7 2 4 53
43 43 mod 7 1 5 26
62 62 mod 7 6 6 62
Hash Table
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 45
Hash System
An item value is a search key that is provided to the hash
function h(x) for hashing to determine the slot no in the
hash table
Hash Table
A hash value is a slot number (index) in the hash table.
An item is inserted or searched in the calculated hash value.
Hash Table
Example: In records of PU students, the Exam_Roll_no of
student can be used as a search key.
0 77
Hash Table
26 26 mod 7 5 30
17 17 mod 7 3 2
x 77
53
77
53
mod
mod
7
7
0
4
h(x) 17
30 30 mod 7 2 3
Search Key Hash Value
43 43 mod 7 1 53
62 62 mod 7 6
4 26
Hash Function
(h(x) = x mod 7) 5 62
Hash Table
6 62
Hash Table
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 50
Hash Table
• A hash table is a data structure that allows the Index Item
very fast retrieval of data. No matter how much
5 26
data there are.
• Thats why, hash table is widely used various 3 17
applications like in database indexing, chasing
0 77
etc.
• Since hashing schemes place records in the table 4 53
in whatever order satisfies the needs of the 2 30
address calculation, records are not ordered by
value. 1 43
6 60
Hash Table
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 51
Hash Function
• The mapping between a search key of a record and the slot in the
hash table where the record is stored in the hash table is called
the hash function.
• A record with key x can be stored in h(x) slot in the hash table.
• To find the record with key value x, the h(x) slot in the hash table is
examined.
30 f(30) = 30 mod 7 2
Value Hash Hash
(Search Key) Function Value
30 f(30) = 30 mod 7 2
Value Hash Hash
(Search Key) Function Value
30 f(30) = 30 mod 7 2
Value Hash Hash
(Search Key) Function Value
77 17 26
77 17 26
77 17 26
59 Collision!
77 17 26
59 Empty slot.
77 17 26
77 17 59 26
77 17 59 26
77 17 59 26
77 17 59 26
77 17 59 26 25
77 17 59 26 25
77 8 17 59 26 25
77 8 17 59 26 25
77 8 17 59 26 25
77 8 17 59 26 25
77 8 17 59 26 25
77 8 17 59 26 25
77 8 47 17 59 26 25
77 8 47 17 59 26 25
77 8 47 17 59 26 25
77 8 47 17 59 26 25
77 8 47 17 59 26 25
77 8 47 17 59 26 25
47 Item matched!
77 8 47 17 59 26 25
25 Collision!
38 17 59 24 31
38 25 17 59 24 31
0 1 2 3 4 5 6 7 8 9
11 Empty slot!
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 91
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
11
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 92
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
21 Collision 1 st
occurred!
11
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 93
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
11
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 94
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
11 21
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 95
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
61 Collision 2 nd
occurred!
11 21
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 96
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
61 Empty slot!
11 21
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 97
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
11 21 61
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 98
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
74 Empty slot!
11 21 61
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 99
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
11 21 74 61
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 100
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
91 Collision 3 rd
occurred!
11 21 74 61
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 101
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
91 Empty slot!
11 21 74 61
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 102
Quadratic Probing- Example
• Let the items: 11, 21, 61, 74, 91
th
• If no collision, h(k) = k mod 10, otherwise, for i
2
collision, hi(k) = [h(k) + i ] mod 10
91 11 21 74 61
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 103
Quadratic Probing- Example
• Observe at the resulted hash table. The items which have
had collisions are not clustered this time.
91 11 21 74 61
0 1 2 3 4 5 6 7 8 9
Quadratic
91 11 21 74 61 Probing
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 105
Class Discussion
• What is the advantage of quadratic probing over linear
probing?
• Ans: No clustering.
• Ans: Causes a long linear search for the items whose keys
are collided and also causes more other collisions.
x=
f(26) = 26 mod 7 = 5
26 X
x=
17 X
f(17) = 17 mod 7 = 3
26 X
x=
17 X
f(77) = 77 mod 7 = 0
26 X
x=
17 59 X
f(59) = 59 mod 7 = 3
26 X
x=
17 59 X
f(25) = 25 mod 7 = 4
25 X
26 X
x=
17 59 X
f(47) = 47 mod 7 = 5
25 X
26 47 X
x=
17 59 X
f(8) = 8 mod 7 = 1
25 X
26 47 X
x=
17 59 52 X
f(52) = 52 mod 7 = 3
25 X
26 47 X
x=
17 59 52 X
f(41) = 41 mod 7 = 6
25 X
26 47 X
41 X
x=
17 59 52 X
f(52) = 52 mod 7 = 3
25 X
Then, we search the linked list
associated with the calculated
26 47 X index using linear search.
41 X
Separate Chaining
0 77 X When we want to search for an
item, we use the hash function
to generate the slot where it
8 X should reside.
Item does not
52 Match!
Hash Table
x=
17 59 52 X
f(52) = 52 mod 7 = 3
25 X
Then, we search the linked list
associated with the calculated
26 47 X index using linear search.
41 X
x=
17 59 52 X
f(52) = 52 mod 7 = 3
25 X
Then, we search the linked list
associated with the calculated
26 47 X index using linear search.
41 X
x=
17 59 52 X
f(52) = 52 mod 7 = 3
25 X
Then, we search the linked list
associated with the calculated
26 47 X index using linear search.
41 X
43 30 26
29 47
36 29 47
36 29 47
7
Fig: New hash table and its Items after rehashing is completed.
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 139
Most student’s Confusion!
• What is a search key in hashing?
• An item can have multiple attributes. For example, a record of a
student can contain attributes:
• Student_ID
• Name
• Address
• …….
• Now come to the point, if you are going to applies to store, retrieve,
delete or search such records of students, you can use student_id as
a search key for hashing to find the hash value (slot no in hash table.
• Alternatively, you can also use the name of student as a search key.
• In some cases, if the item is itself integer value or string value, we
can take the item’s value as a search key for hashing.
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 140
Most student’s Confusion!
}
attribute that can be
manipulated by a hash
function to find a slot
no in a hash table.
Other attributes of person
142