Hwk3 Solution
Hwk3 Solution
Homework Set 3
15
13 9
5 12 8 7
4 0 6 2 1
The MAX_HEAP-INSERT (A, 10, 12) will be executed in the following steps (10 is the key value to be
inserted and 12 is the current size of the max-heap):
   1) Expand the max-heap with a new element whose key is -∞ as shown in the following figure. Note that
       nodes are added at the bottom level from left to right and the size of the max-heap will be increased
       from 12 to 13.
                                                    Page 1 of 22
                                                  15
13 9
5 12 8 7
4 0 6 2 1 -∞
2) Call HEAP-INCREASE-KEY on A[13] to set the key of the new node to its correct value (10) and
   maintain the max-heap property as detailed in the following figures.
15
13 9
                5                     12                      8                 7
                                                                        Swap
4 0 6 2 1 10
15
                           13                                              9
                                                             Swap
5 12 10 7
4 0 6 2 1 8
                                             Page 2 of 22
                                                      15
13 10
5 12 9 7
4 0 6 2 1 8
The insert process is now complete and the given key has been moved to its appropriate location in the max-
heap as shown in the above figure.
                                                 Page 3 of 22
Problem 2 (20 points)
Demonstrate what happens when we insert the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a hash table with
collisions resolved by chaining. Let the table have 9 slots, and let the hash function be ℎ(𝑘)=𝑘 𝑚𝑜𝑑 9.
Solution
We will assume that the hash table T was empty before inserting the given keys.
We will compute the hash function for each key as follows:
   h(5) = 5 mod 9 = 5          h(28) = 28 mod 9 = 1
   h(19) = 19 mod 9 = 1        h(15) = 15 mod 9 = 6
   h(20) = 20 mod 9 = 2        h(33) = 33 mod 9 = 6
   h(12) = 12 mod 9 = 3        h(17) = 17 mod 9 = 8
   h(10) = 10 mod 9 = 1
Now, we will insert the given keys into the hash table T based on their hash values calculated above as detailed
in Figure 1. All keys that hash to the same slot will be linked using a linked list (refer to Figure 1).
                     T
               0     /
               1                  /     28                          19                       10     /
               2                  /     20     /
               3                  /     12     /
               4     /
               5                  /      5     /
               6                  /     15                          33    /
               7     /
               8                  /     17     /
Figure 1
                                                     Page 4 of 22
Problem 3 (25 points)
Consider a hash table of size m=1000 and a corresponding hash function
ℎ(𝑘)=⌊𝑚(𝑘 𝐴 𝑚𝑜𝑑 1)⌋ 𝑓𝑜𝑟 𝐴=(√ −1)/2.
Compute the locations to which the keys 61, 62, 63, 64, and 65 are mapped.
Solution
We will compute the hash function for each key as follows:
h(61) = ⌊ 1000[ 61 ((√ −1)/2) - ⌊ 61 ((√ −1)/2) ⌋ ] ⌋ = ⌊ 1000[ 37.7001 - 37 ] ⌋ = ⌊ 700.1 ⌋ = 700
h(62) = ⌊ 1000[ 62 ((√ −1)/2) - ⌊ 62 ((√ −1)/2) ⌋ ] ⌋ = ⌊ 1000[ 38.3181 - 38 ] ⌋ = ⌊ 318.1 ⌋ = 318
h(63) = ⌊ 1000[ 63 ((√ −1)/2) - ⌊ 63 ((√ −1)/2) ⌋ ] ⌋ = ⌊ 1000[ 38.9361 - 38 ] ⌋ = ⌊ 936.1 ⌋ = 936
h(64) = ⌊ 1000[ 64 ((√ −1)/2) - ⌊ 64 ((√ −1)/2) ⌋ ] ⌋ = ⌊ 1000[ 39.5542 - 39 ] ⌋ = ⌊ 554.2 ⌋ = 554
h(65) = ⌊ 1000[ 65 ((√ −1)/2) - ⌊ 65 ((√ −1)/2) ⌋ ] ⌋ = ⌊ 1000[ 40.1722 - 40 ] ⌋ = ⌊ 172.2 ⌋ = 172
Therefore, if T is the hash table then, the given keys will be mapped to the following locations:
T[700] = 61
T[318] = 62
T[936] = 63
T[554] = 64
T[172] = 65
                                                   Page 5 of 22
Problem 4 (35 points)
Consider inserting the keys 10, 22, 31, 4, 15, 28, 17, 88, 59 into a hash table of length m = 11 using open
addressing with the auxiliary hash function ℎ(𝑘)=𝑘. Illustrate the result of inserting these keys using linear
probing, using quadratic probing with 𝑐1=1, and 𝑐2=3, and using double hashing with ℎ1(𝑘)=𝑘 and
ℎ2(𝑘)=1+(𝑘 𝑚𝑜𝑑 (𝑚−1)).
Solution
Scenario No 1 (Linear Probing)
We will assume that the hash table T was empty before inserting the given keys.
The hash function that is going to be used to map key values to the appropriate locations in T is defined as
follows:
       h(k , i) = [ ℎ(𝑘) + i ] mod m [ where k is the key value and i is the probe number ]
Then, h(k , i) = [ k + i ] mod 11
Now, we will compute the newly defined hash function for each key and that computation could be repeated
using sequential probe numbers (as applicable) until we find a free slot in T or until T is full. The hash function
computations are detailed in the following text.
h(10,0) = 10 mod 11 = 10
Therefore, 10 will be inserted into T[10] as shown in Figure 2 below.
                    T                               T
              0                               0
              1                               1
              2                               2
              3                               3
              4                               4
              5                               5
              6                               6
              7                               7
              8                               8
              9                               9
10           10                               10    10
Figure 2
                                                   Page 6 of 22
h(22,0) = 22 mod 11 = 0
Therefore, 22 will be inserted into T[0] as shown in Figure 3 below.
                   T                              T
22           0                               0    22
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
             9                               9
             10    10                       10    10
Figure 3
h(31,0) = 31 mod 11 = 9
Therefore, 31 will be inserted into T[9] as shown in Figure 4 below.
                    T                             T
             0     22                        0    22
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
31           9                               9    31
             10    10                       10    10
                              Figure 4
                                                  Page 7 of 22
h(4,0) = 4 mod 11 = 4
Therefore, 4 will be inserted into T[4] as shown in Figure 5 below.
                   T                              T
              0    22                        0    22
              1                              1
              2                              2
              3                              3
 4            4                              4     4
              5                              5
              6                              6
              7                              7
              8                              8
              9    31                        9    31
             10    10                       10    10
Figure 5
                   T                                                       T
              0    22                                                 0    22
              1                                                       1
              2                                                       2
              3                                                       3
15            4     4         Collision                               4    4
              5                                                       5    15
              6                                                       6
              7                                                       7
              8                                                       8
              9    31                                                 9    31
             10    10                                                 10   10
                                      Figure 6
                                                  Page 8 of 22
h(28,0) = 28 mod 11 = 6 (28 will be inserted into T[6] as shown in Figure 7 below)
                   T                              T
             0     22                        0    22
             1                               1
             2                               2
             3                               3
             4     4                         4    4
             5     15                        5   15
28           6                               6   28
             7                               7
             8                               8
             9     31                        9    31
             10    10                       10    10
Figure 7
                   T                                                     T
             0     22                                              0     22
             1                                                     1
             2                                                     2
             3                                                     3
             4     4                                               4     4
             5     15                                              5     15
17           6     28         Collision                            6     28
             7                                                     7     17
             8                                                     8
             9     31                                              9     31
             10    10                                             10     10
Figure 8
                                                 Page 9 of 22
h(88,0) = 88 mod 11 = 0 (collision with k = 22 and we will hash again)
h(88,1) = 89 mod 11 = 1 (88 will be inserted into T[1] as detailed in Figure 9 below)
                   T                                                     T
88            0    22         Collision                             0    22
              1                                                     1    88
              2                                                     2
              3                                                     3
              4     4                                               4     4
              5    15                                               5    15
              6    28                                               6    28
              7    17                                               7    17
              8                                                     8
              9    31                                               9    31
             10    10                                              10    10
Figure 9
                                                  Page 10 of 22
                    T                                                      T
              0     22                                               0     22
              1     88                                               1     88
              2                                                      2
              3                                                      3
59            4     4          Collision                             4      4
              5     15         Collision                             5     15
              6     28         Collision                             6     28
              7     17         Collision                             7     17
              8                                                      8     59
              9     31                                               9     31
             10     10                                               10    10
Figure 10
                                                   Page 11 of 22
                   T                              T
             0                               0
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
             9                               9
10           10                             10    10
Figure 11
h(22,0) = 22 mod 11 = 0
Therefore, 22 will be inserted into T[0] as shown in Figure 12 below.
                   T                              T
22           0                               0    22
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
             9                               9
             10    10                       10    10
Figure 12
                                                 Page 12 of 22
h(31,0) = 31 mod 11 = 9
Therefore, 31 will be inserted into T[9] as shown in Figure 13 below.
                    T                             T
             0     22                        0    22
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
31           9                               9    31
             10    10                       10    10
Figure 13
h(4,0) = 4 mod 11 = 4
Therefore, 4 will be inserted into T[4] as shown in Figure 14 below.
                   T                              T
             0     22                        0    22
             1                               1
             2                               2
             3                               3
 4           4                               4     4
             5                               5
             6                               6
             7                               7
             8                               8
             9     31                        9    31
             10    10                       10    10
Figure 14
                                                 Page 13 of 22
h(15,0) = 15 mod 11 = 4 (collision with k = 4 and we will hash again)
h(15,1) = (15 + 1 + 3) mod 11 = 19 mod 11 = 8 (15 will be inserted into T[8] as detailed in Figure 15)
                   T                                                    T
             0     22                                              0    22
             1                                                     1
             2                                                     2
             3                                                     3
15           4      4          Collision                           4     4
             5                                                     5
             6                                                     6
             7                                                     7
             8                                                     8    15
             9     31                                              9    31
             10    10                                              10   10
Figure 15
h(28,0) = 28 mod 11 = 6 (28 will be inserted into T[6] as shown in Figure 16 below)
                   T                                T
             0     22                         0     22
             1                                1
             2                                2
             3                                3
             4     4                          4     4
             5                                5
28           6                                6    28
             7                                7
             8     15                         8    15
             9     31                         9     31
             10    10                        10     10
Figure 16
                                                   Page 14 of 22
h(17,0) = 17 mod 11 = 6 (collision with k = 28 and we will hash again)
h(17,1) = (17 + 1 + 3) mod 11 = 21 mod 11 = 10 (collision with k = 10 and we will hash again)
h(17,2) = (17 + 2 + 12) mod 11 = 31 mod 11 = 9 (collision with k = 31 and we will hash again)
h(17,3) = (17 + 3 + 27) mod 11 = 47 mod 11 = 3 (17 will be inserted into T[3] as detailed in Figure 17 below)
                   T                                                           T
             0     22                                                     0    22
             1                                                            1
             2                                                            2
             3                                                            3    17
             4     4                                                      4     4
             5                                                            5
17           6    28         Collision                                    6    28
             7                                                            7
             8    15                                                      8    15
             9     31                    Collision                        9    31
             10    10                    Collision                       10    10
Figure 17
                                                     Page 15 of 22
                   T                                                           T
      88     0     22         Collision                                  0     22
             1                                                           1
             2                                                           2
             3     17                                             88     3     17    Collision
             4      4                     Collision                      4     4
             5                                                           5
             6     28                                                    6     28
             7                                                           7
             8     15                                                    8     15    Collision
             9     31                                                    9     31
             10    10                                                   10     10
                   T                                                           T
             0     22         Collision                                  0     22
             1                                                           1
             2                                                           2     88
      88     3     17         Collision                                  3     17
             4      4         Collision                                  4     4
             5                                                           5
             6     28                                                    6     28
             7                                                           7
             8     15                                                    8     15
             9     31                                                    9     31
             10    10                                                   10     10
Figure 18
                                                      Page 16 of 22
                    T                                                         T
              0     22                                                  0     22
              1                                                         1
              2     88                                                  2     88
              3     17                                                  3     17
59            4     4          Collision                                4      4
              5                                                         5
              6     28                                                  6     28
              7                                                         7     59
              8     15                     Collision                    8     15
              9     31                                                  9     31
             10     10                                                  10    10
Figure 19
                                                       Page 17 of 22
                   T                              T
             0                               0
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
             9                               9
10           10                             10    10
Figure 20
h(22,0) = 22 mod 11 = 0
Therefore, 22 will be inserted into T[0] as shown in Figure 21 below.
                   T                              T
22           0                               0    22
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
             9                               9
             10    10                       10    10
Figure 21
                                                 Page 18 of 22
h(31,0) = 31 mod 11 = 9
Therefore, 31 will be inserted into T[9] as shown in Figure 22 below.
                    T                             T
             0     22                        0    22
             1                               1
             2                               2
             3                               3
             4                               4
             5                               5
             6                               6
             7                               7
             8                               8
31           9                               9    31
             10    10                       10    10
Figure 22
h(4,0) = 4 mod 11 = 4
Therefore, 4 will be inserted into T[4] as shown in Figure 23 below.
                   T                              T
             0     22                        0    22
             1                               1
             2                               2
             3                               3
 4           4                               4     4
             5                               5
             6                               6
             7                               7
             8                               8
             9     31                        9    31
             10    10                       10    10
                        Figure 23
                                                 Page 19 of 22
h(15,0) = 15 mod 11 = 4 (collision with k = 4 and we will hash again)
h(15,1) = [15 + 1 + 15 mod 10] mod 11 = 21 mod 11 = 10 (collision with k = 10 and we will hash again)
h(15,2) = [15 + 2(1 + 15 mod 10)] mod 11
       = 27 mod 11 = 5 (15 will be inserted into T[5] as detailed in Figure 24)
                   T                                                        T
             0     22                                                   0   22
             1                                                          1
             2                                                          2
             3                                                          3
15           4     4         Collision                                  4    4
             5                                                          5   15
             6                                                          6
             7                                                          7
             8                                                          8
             9     31                                                   9   31
             10    10                    Collision                    10    10
Figure 24
h(28,0) = 28 mod 11 = 6 (28 will be inserted into T[6] as shown in Figure 25 below)
                   T                                  T
             0     22                        0        22
             1                               1
             2                               2
             3                               3
             4     4                         4        4
             5     15                        5        15
28           6                               6       28
             7                               7
             8                               8
             9     31                        9        31
             10    10                        10       10
                             Figure 25
                                                     Page 20 of 22
h(17,0) = 17 mod 11 = 6 (collision with k = 28 and we will hash again)
h(17,1) = [ 17 + 1 + 17 mod 10 ] mod 11
       = 25 mod 11 = 3 (17 will be inserted into T[3] as detailed in Figure 26 below)
                  T                                                        T
             0    22                                                  0    22
             1                                                        1
             2                                                        2
             3                                                        3     17
             4     4                                                  4     4
             5    15                                                  5    15
17           6    28         Collision                                6    28
             7                                                        7
             8                                                        8
             9    31                                                  9    31
             10   10                                                 10    10
Figure 26
                                                 Page 21 of 22
                   T                                                           T
88            0    22         Collision                                 0      22
              1                                                         1
              2                                                         2
              3    17                                                   3      17
              4     4                                                   4      4
              5    15                                                   5      15
              6    28                                                   6      28
              7                                                         7      88
              8                                                         8
              9    31                     Collision                     9      31
             10    10                                                  10      10
Figure 27
                   T                                                           T
              0    22                                                   0      22
              1                                                         1
              2                                                         2      59
              3    17         Collision                                 3      17
59            4     4         Collision                                 4      4
              5    15                                                   5      15
              6    28                                                   6      28
              7    88                                                   7      88
              8                                                         8
              9    31                                                   9      31
             10    10                                                  10      10
                                      Figure 28
                                                      Page 22 of 22