|
| 1 | +// C++ program to demonstrate working of Cuckoo |
| 2 | +// hashing. |
| 3 | +#include<bits/stdc++.h> |
| 4 | + |
| 5 | +// upper bound on number of elements in our set |
| 6 | +#define MAXN 11 |
| 7 | + |
| 8 | +// choices for position |
| 9 | +#define ver 2 |
| 10 | + |
| 11 | +// Auxiliary space bounded by a small multiple |
| 12 | +// of MAXN, minimizing wastage |
| 13 | +int hashtable[ver][MAXN]; |
| 14 | + |
| 15 | +// Array to store possible positions for a key |
| 16 | +int pos[ver]; |
| 17 | + |
| 18 | +/* function to fill hash table with dummy value |
| 19 | +* dummy value: INT_MIN |
| 20 | +* number of hashtables: ver */ |
| 21 | +void initTable() |
| 22 | +{ |
| 23 | +for (int j=0; j<MAXN; j++) |
| 24 | +for (int i=0; i<ver; i++) |
| 25 | +hashtable[i][j] = INT_MIN; |
| 26 | +} |
| 27 | + |
| 28 | +/* return hashed value for a key |
| 29 | +* function: ID of hash function according to which |
| 30 | +key has to hashed |
| 31 | +* key: item to be hashed */ |
| 32 | +int hash(int function, int key) |
| 33 | +{ |
| 34 | +switch (function) |
| 35 | +{ |
| 36 | +case 1: return key%MAXN; |
| 37 | +case 2: return (key/MAXN)%MAXN; |
| 38 | +} |
| 39 | +} |
| 40 | + |
| 41 | +/* function to place a key in one of its possible positions |
| 42 | +* tableID: table in which key has to be placed, also equal |
| 43 | +to function according to which key must be hashed |
| 44 | +* cnt: number of times function has already been called |
| 45 | +in order to place the first input key |
| 46 | +* n: maximum number of times function can be recursively |
| 47 | +called before stopping and declaring presence of cycle */ |
| 48 | +void place(int key, int tableID, int cnt, int n) |
| 49 | +{ |
| 50 | +/* if function has been recursively called max number |
| 51 | +of times, stop and declare cycle. Rehash. */ |
| 52 | +if (cnt==n) |
| 53 | +{ |
| 54 | +printf("%d unpositioned\n", key); |
| 55 | +printf("Cycle present. REHASH.\n"); |
| 56 | +return; |
| 57 | +} |
| 58 | + |
| 59 | +/* calculate and store possible positions for the key. |
| 60 | +* check if key already present at any of the positions. |
| 61 | +If YES, return. */ |
| 62 | +for (int i=0; i<ver; i++) |
| 63 | +{ |
| 64 | +pos[i] = hash(i+1, key); |
| 65 | +if (hashtable[i][pos[i]] == key) |
| 66 | +return; |
| 67 | +} |
| 68 | + |
| 69 | +/* check if another key is already present at the |
| 70 | +position for the new key in the table |
| 71 | +* If YES: place the new key in its position |
| 72 | +* and place the older key in an alternate position |
| 73 | +for it in the next table */ |
| 74 | +if (hashtable[tableID][pos[tableID]]!=INT_MIN) |
| 75 | +{ |
| 76 | +int dis = hashtable[tableID][pos[tableID]]; |
| 77 | +hashtable[tableID][pos[tableID]] = key; |
| 78 | +place(dis, (tableID+1)%ver, cnt+1, n); |
| 79 | +} |
| 80 | +else //else: place the new key in its position |
| 81 | +hashtable[tableID][pos[tableID]] = key; |
| 82 | +} |
| 83 | + |
| 84 | +/* function to print hash table contents */ |
| 85 | +void printTable() |
| 86 | +{ |
| 87 | +printf("Final hash tables:\n"); |
| 88 | + |
| 89 | +for (int i=0; i<ver; i++, printf("\n")) |
| 90 | +for (int j=0; j<MAXN; j++) |
| 91 | +(hashtable[i][j]==INT_MIN)? printf("- "): |
| 92 | +printf("%d ", hashtable[i][j]); |
| 93 | + |
| 94 | +printf("\n"); |
| 95 | +} |
| 96 | + |
| 97 | +/* function for Cuckoo-hashing keys |
| 98 | +* keys[]: input array of keys |
| 99 | +* n: size of input array */ |
| 100 | +void cuckoo(int keys[], int n) |
| 101 | +{ |
| 102 | +// initialize hash tables to a dummy value (INT-MIN) |
| 103 | +// indicating empty position |
| 104 | +initTable(); |
| 105 | + |
| 106 | +// start with placing every key at its position in |
| 107 | +// the first hash table according to first hash |
| 108 | +// function |
| 109 | +for (int i=0, cnt=0; i<n; i++, cnt=0) |
| 110 | +place(keys[i], 0, cnt, n); |
| 111 | + |
| 112 | +//print the final hash tables |
| 113 | +printTable(); |
| 114 | +} |
| 115 | + |
| 116 | +/* driver function */ |
| 117 | +int main() |
| 118 | +{ |
| 119 | +/* following array doesn't have any cycles and |
| 120 | +hence all keys will be inserted without any |
| 121 | +rehashing */ |
| 122 | +int keys_1[] = {20, 50, 53, 75, 100, 67, 105, |
| 123 | +3, 36, 39}; |
| 124 | + |
| 125 | +int n = sizeof(keys_1)/sizeof(int); |
| 126 | + |
| 127 | +cuckoo(keys_1, n); |
| 128 | + |
| 129 | +/* following array has a cycle and hence we will |
| 130 | +have to rehash to position every key */ |
| 131 | +int keys_2[] = {20, 50, 53, 75, 100, 67, 105, |
| 132 | +3, 36, 39, 6}; |
| 133 | + |
| 134 | +int m = sizeof(keys_2)/sizeof(int); |
| 135 | + |
| 136 | +cuckoo(keys_2, m); |
| 137 | + |
| 138 | +return 0; |
| 139 | +} |
0 commit comments