1 / 21

Övning 5 nada.kth.se/~mhj/tilda

Övning 5 www.nada.kth.se/~mhj/tilda. Hashning Textsökning KMP-automat Hemtal Bilarna. Hashning. Iden med hashning. first. Björn. Agnetha. Benny. Anni-Frid. firstA. Agneta. Anni-Frid. firstB. Björn. Benny. table [ ]. Agneta. 0. hash betyder: hackmat, röra. Björn. 1.

espen
Download Presentation

Övning 5 nada.kth.se/~mhj/tilda

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. Övning 5 www.nada.kth.se/~mhj/tilda

  2. Hashning Textsökning KMP-automat Hemtal Bilarna

  3. Hashning

  4. Iden med hashning first Björn Agnetha Benny Anni-Frid firstA Agneta Anni-Frid firstB Björn Benny table [ ] Agneta 0 hash betyder: hackmat, röra Björn 1 Benny 2 Anni-Frid 3

  5. Insättning (put) hashfunktion tabellstorlek f( ) = 2 + 10 + 29 + 18 + 14 = 73 mod 4 Björn 1 mod 4 3 f( ) = 1 + 7 + 14 + 5 + 20 + 7 + 1 = 55 Agnetha mod 4 0 f( ) = 2 + 5 + 14 + 14 + 25 = 60 Benny mod 4 f( ) = 1 + 14 + 14 + 9 + 30 + 6 + 18 + 9 + 4 = 105 Anni-Frid 1 table [ ] Benny 0 Björn Björn Anni-Frid 1 2 Agnetha 3

  6. Sökning (get) f( ) = 2 + 5 + 14 + 14 + 25 = 60 Benny mod 4 0 mod 4 f( ) = 2 + 10 + 29 + 18 + 14 = 73 Björn 1 mod 4 f( ) = 13 + 1 + 18 + 9 + 5 = 46 Marie 2 mod 4 f( ) = 2 + 15 = 17 Bo 1 table [ ] Benny Benny 0 Björn Björn Björn Anni-Frid Anni-Frid 1 2 Agnetha 3

  7. Tabellstorlek table [ ] 0 ….. Agnetha 55 mod 10000 ….. 73 f(Björn) = 73 Benny mod 10000 60 55 f(Agnetha) = 55 ….. mod 10000 60 f(Benny) = 60 Björn 73 mod 10000 105 f(Anni-Frid) = 105 ….. Anni-Frid 105 ….. 9999

  8. Tabellstorlek table [ ] 0 Björn Anni-Frid 1 2 mod 8 1 3 f(Björn) = 73 Benny mod 8 4 7 f(Agnetha) = 55 5 mod 8 4 f(Benny) = 60 6 mod 8 1 f(Anni-Frid) = 105 Agnetha 7 Tumregel: 50% luft

  9. Tabellstorlek table [ ] 0 ….. Agnetha 55 ….. Benny 60 ….. Björn 73 ….. Anni-Frid 105 ….. 9999

  10. Hashfunktion • god spridning • snabb att beräkna Hashning av strängar i Java: public static void main(String [] args) { String s1 = ”hej”; int code = s1.hashCode(); System.out.println(code); } 103181 f(s) = s[0] * 31(n-1) + s[1] * 31(n-2) + ... + s[n-1] f(s1) = 104 * 312 + 101 * 31 + 106 = 103181

  11. Krockhantering med ”linear probing” table [ ] 0 Björn 1 kluster Anni-Frid 2 mod 8 1 3 f(Björn) = 73 Benny mod 8 4 7 f(Agnetha) = 55 5 mod 8 4 f(Benny) = 60 6 mod 8 1 Krock! f(Anni-Frid) = 105 Agnetha 7 2

  12. Krockhantering med ”quadratic probing” table [ ] Anni-Frid 0 Björn 1 2 mod 8 1 3 f(Björn) = 73 Benny mod 8 4 7 f(Agnetha) = 55 5 mod 8 4 f(Benny) = 60 6 mod 8 1 Krock! f(Anni-Frid) = 105 Agnetha 7 22 = 4 Krock! mod 8 32 = 9 1 Krock! mod 8 42 = 16 0

  13. Perfekt hashfunktion false 0 Spara kort som dragits ur en kortlek. false 1 ….. false true 7 klöver = 0, ruter = 1, hjärter = 2, spader = 3 ess = 1, knekt = 11, dam = 12, kung = 13 ….. false true 23 f(färg, nr) = 13 * färg + nr-1 ….. false true 32 klöver 8: f(klöver, 8) = 13 * 0 + 8-1 = 7 f(hjärter, 7) = 13 * 2 + 7-1 = 32 hjärter 7: ….. ruter knekt: false f(ruter, knekt) = 13 * 1 + 11-1 = 23 50 false 51

  14. Bloomfilter false true 0 Stavningskontrollprogram: true false 1 mod 60000 ….. f(”hej”) = 103181 43181 70012 false true fortsätt med samtliga ord… …………… 50 % att ett felstavat ord, t.ex. ”heej”, godkänns. ….. 43181 103181 false true mod 180000 f1(”hej”) = 103181 103181 ….. mod 180000 f2(”hej”) = 430012 70012 143229 false true …………… mod 180000 f3(”hej”) = 1043229 143229 ….. fortsätt med samtliga ord… 59999 179999 false 12.5 % att ett felstavat ord, t.ex. ”heej”, godkänns. 180000 true false 60000 I Viggos Stava används 14 hashfunktioner 0.006 %

  15. Textsökning KMP-automat

  16. n F F Y Y R R T T I I O O T T V V Å Å , S A D J U P A T A N K E N T T T T T T T T T T V V Å T T Å V V V Å T V Å T T V Å Å V V T Å T Å V T T V Å Å V Å m Komplexitet: O(nm)

  17. A A G A A T A A C A T C A A A A G C T T T A A G A A A A A C A A C C A A C A A C A A C A A A A A A C C

  18. KMP automat: O(n) Morris Pratt Knuth

  19. F Y R T I O T V Å , S A D J U P A T A N K E N T T V V Å Å 0 1 2 3 i next[i] 0 1 1

  20. A D A D A M A A D D A A M M 3 0 1 2 4 i next[i] 0 0 1 2

  21. 1. next[1] = 0 2. Om ingen upprepning: next[i] = 1 Optimering: Men om bokstav i är samma som första bokstaven: next[i]=0 3. Om j>0 bokstäver är upprepade: next[i] = j+1 Optimering: Men om dessutom bokstav i och j+1 är samma: next[i]=next[j+1] A A D D A A M M 3 0 1 2 4 i next[i] 0 0 1 2

More Related