hashing Midsquare hashing public class Hash 2 public
hashing
Mid-square hashing public class Hash 2{ public static void main(String args[]){ String id=args[0]; long value=square(id); System. out. println("id is "+id); System. out. println("square of id is "+value); String hid=get. Digits(value); System. out. println("hash of id is "+hid); } public static long square(String v){ long r=0; for(int i=0; i<v. length(); i++) r=r*10+v. char. At(i)-97; return r*r; } public static String get. Digits(long v){ //return middle 10 bits String s=""; for(int i=0; i<30; i++) { long b=v%2; if(i>9&&i<20)s+=b; v/=2; } return s; } }
Mid-square hashing output C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 2 midsquare id is midsquare of id is 1651174047573070276 hash of id is 0010111111 C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 2 long id is long square of id is 157151296 hash of id is 0011111011 C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 2 absval id is absval square of id is 906672321 hash of id is 0111010101
Division (table size should be a prime) C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 1 long 997 id is long value of id is 12536 hash of id is 572 C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 1 division 997 id is division value of id is 40198953 hash of id is 910 C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 1 int 997 id is int value of id is 949 hash of id is 949
Division (table size should be a prime) public class Hash 1{ public static void main(String args[]){ String id=args[0]; int tablesize=Integer. parse. Int(args[1]); long value=numeric(id); System. out. println("id is "+id); System. out. println("value of id is "+value); long table=(long) tablesize; long val=value%table; System. out. println("hash of id is "+val); } public static long numeric(String v){ long r=0; for(int i=0; i<v. length(); i++) r=r*10+v. char. At(i)-97; return r; } }
Folding at the boundaries C: PROGRA~1JavaJDK 16~1. 0bin>java Hash 3 123 412 527 892 value is 123 reverse value is 321 value is 527 reverse value is 725 hash of id 2350
Folding at the boundaries public class Hash 3{ public static void main(String args[]){ int value=shiftfolding(args); System. out. println("hash of id "+value); } public static int shiftfolding(String v[]){ int address=0; for(int i=0; i<v. length; i++){ int r=Integer. parse. Int(v[i]); if(i%2==0)r=reverse(r); address+=r; } return address; } public static int reverse(int x){ int ans=0; System. out. print("value is "+x); while(x!=0){ ans=ans*10+x%10; x/=10; } System. out. println("reverse value is "+ans); return ans; } }
Digit analysis Dig#/ct 0 1 2 3 4 5 6 7 0123456789 0156804161 0619621214 1226455403 33243333515 45652512402 5 13 5 5 1 2 2 0 1 1 2 23 2 2 2 0 1 1 1 0 0 29 2 0 0 0 1 0 0
Digit analysis (id values) C: PROGRA~1JavaJDK 16~1. 0bin>j 19798 17412234 12536 2007936 319 1741234 657 19524 16847712 197574 236113 3549093 60343 3459343 155571348 25553 26664 1999998 244442 177776 133332 12344 16788 24432 273411 225914 162133 908512 173638 2325498 44636 207644
Digit Analysis import java. util. *; public class Digit. Analysis{ public static void main(String args[]){ String. Tokenizer st=new String. Tokenizer("this reallly long string but really for some program there would contain fixed defined operands xxxx yyyy ssssss wwwww qqqqq mmmmm lmno pqrs wwwm zxcvb werty plkmn htrokm rdews xcfdss eeewq trewy"); int table[]=new int[32]; for(int j=0; j<32; j++){ table[j]=value(st. next. Token()); System. out. println(table[j]); } int digitcts[][]=new int[8][10]; for(int i=0; i<8; i++) for(int j=0; j<10; j++)digitcts[i][j]=0; //zero counts get. Counts(digitcts, table); for(int i=0; i<8; i++){ String s=""; for(int j=0; j<10; j++)s=s+" " +digitcts[i][j]; System. out. println(s); }} public static int value(String v){ int r=0; for(int i=0; i<v. length(); i++) r=r*10+v. char. At(i)-97; return r; } public static void get. Counts(int [][]digitcts, int []table){ for(int j=0; j<8; j++) { for(int i=0; i<32; i++){ int m=table[i]%10; table[i]/=10; digitcts[j][m]++; } }}}
Quadratic probing discussion • Clustering is when blocks/clumps of table become full.
Computing proble address
insertion 1. Get the key k 2. Set counter j = 0 3. Compute hash function h[k] = k % SIZE 4. If hashtable[h[k]] is empty (4. 1) Insert key k at hashtable[h[k]] (4. 2) Stop Else (4. 3) The key space at hashtable[h[k]] is occupied, so we need to find the next available key space (4. 4) Increment j (4. 5) Compute new hash function h[k] = ( k + j * j ) % SIZE (4. 6) Repeat Step 4 till j is equal to the SIZE of hash table 5. The hash table is full 6. Stop
In C int quadratic_probing_insert(int *hashtable, int key, int *empty) { int j = 0, hk; hk = key % SIZE; while(j < SIZE) { if(empty[hk] == 1) { hashtable[hk] = key; empty[hk] = 0; return (hk); } j++; hk = (key + j * j) % SIZE; } return (-1); }
problems • For linear probing it is a bad idea to let the hash table get nearly full, because performance is degraded as the hash table gets filled. In the case of quadratic probing, the situation is even more drastic. With the exception of the triangular number case for a power-of-two-sized hash table, there is no guarantee of finding an empty cell once the table gets more than half full, or even before the table gets half full if the table size is not prime. This is because at most half of the table can be used as alternative locations to resolve collisions.
• If the hash table size is b (a prime greater than 3), it can be proven that the first b / 2 alternative locations including the initial location h(k) are all distinct and unique. • Theoretically, an empty key space can always be found as long as at most (b / 2) locations are filled, i. e. , the hash table is not more than half full.
- Slides: 16