1 / 16

hashing

hashing. Mid-square hashing. public class Hash2{ 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=getDigits(value); System.out.println("hash of id is "+hid);}

britain
Télécharger la présentation

hashing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. hashing

  2. Mid-square hashing public class Hash2{ 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=getDigits(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.charAt(i)-97; return r*r;} public static String getDigits(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;} }

  3. Mid-square hashing output C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash2 midsquare id is midsquare square of id is 1651174047573070276 hash of id is 0010111111 C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash2 long id is long square of id is 157151296 hash of id is 0011111011 C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash2 absval id is absval square of id is 906672321 hash of id is 0111010101

  4. Division (table size should be a prime) C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash1 long 997 id is long value of id is 12536 hash of id is 572 C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash1 division 997 id is division value of id is 40198953 hash of id is 910 C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash1 int 997 id is int value of id is 949 hash of id is 949

  5. Division (table size should be a prime) public class Hash1{ public static void main(String args[]){ String id=args[0]; int tablesize=Integer.parseInt(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.charAt(i)-97; return r;} }

  6. Folding at the boundaries C:\PROGRA~1\Java\JDK16~1.0\bin>java Hash3 123 412 527 892 value is 123reverse value is 321 value is 527reverse value is 725 hash of id 2350

  7. Folding at the boundaries public class Hash3{ 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.parseInt(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;} }

  8. Digit analysis Dig#/ct 0 1 2 3 4 5 6 7 8 9 0 0 1 5 6 8 0 4 1 6 1 • 0 6 1 9 6 2 1 2 1 4 • 1 2 2 6 4 5 5 4 0 3 • 3 3 2 4 3 3 3 3 5 1 5 • 4 5 6 5 2 5 1 2 4 0 2 • 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 0 0

  9. Digit analysis (id values) C:\PROGRA~1\Java\JDK16~1.0\bin>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

  10. Digit Analysis import java.util.*; public class DigitAnalysis{ public static void main(String args[]){ StringTokenizer st=new StringTokenizer("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.nextToken()); 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 getCounts(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.charAt(i)-97; return r;} public static void getCounts(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]++; } }}}

  11. Quadratic probing discussion • Clustering is when blocks/clumps of table become full.

  12. Computing proble address

  13. 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

  14. 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); }

  15. 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.

  16. 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.

More Related