


































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!



































Algorísmica
Jordi Vitrià Grau d'Enginyeria Informà3ca Facultat de Matemà3ques
Funcions Hash i Cerca Imaginem que volem assignar un nom “curt” (el més curt possible) a cada una de les possibles 2 32 adreces IP. Imaginem que en tenim prou amb 250 noms. En el cas d’assignar el mateix nom a dues adreces, o col·∙lisió , associem a aquell nom una llista d’IPs amb aquell nom. Aquest esquema té sen3t per fer cerques molt ràpides, atès que les llistes de col·∙lisió seran pe3tes. Inevitablement, en general, moltes adreces 3ndrien el mateix nom! Però pot tenir sen3t si treballem amb les adreces d’una sola empresa. Aquest és el nombre esperat de dades.
The designers of TCP/IP defined an IP address as a 32-bit number and this system, known as Internet Protocol Version 4 (IPv4), is still in use today. However, due to the enormous growth of the Internet and the predicted depletion of available addresses, a new addressing system (IPv6), using 128 bits for the address, was developed in 1995, standardized by RFC 2460 in 1998, and is in world-wide production deployment.
Espai de totes les 2^32 adreces IP 250 IPs [x,y] [z]
x z (^) y
Funcions Hash i Cerca Les funcions hash han de ser:
Funcions Hash i Cerca Atès que fem una correspondència entre 2 32 adreces i una llista de 250 noms hi ha d’haver una col·∙lecció de 2 32 /250 = 16.000.000 d’adreces que col·∙lisionaran. Però podem seguir el següent esquema per minimitzar les col·∙lisions: escollim de forma aleatòria una funció per alguna classe de funcions hash (per una funció hash haurem de poder demostrar que, sigui quin sigui el conjunt d’adreces, la majoria de funcions escollides generaran poques col·∙lisions!)
Funcions Hash i Cerca Anem a definir una classe de funcions que puguem escollir aleatòriament:
Funcions Hash i Cerca Es pot demostrar que: Donada qualsevol parella d’adreces IP x = (x 1 ,x 2 ,x 3 ,x 4
i y = (y 1 ,y 2 ,y 3 ,y 4 ), si els coeficients (a 1 ,a 2 ,a 3 ,a 4
s’escullen aleatòriament de {0,1,...,n-‐1}, llavors: És a dir, la mateixa Pr que si les adreces haguessin estat escollides de forma aleatòria. Sigui quin sigui el conjunt d’adreces, la majoria de funcions escollides generaran poques col·∙lisions!
Funcions Hash i Cerca
Funcions Hash i Cerca Recapitulem el que hem fet:
Famílies La natura de les famílies depèn de les dades! Trivial hash funcon:** If the datum to be hashed is small enough, one can use the datum itself ( **reinterpreted as an integer in binary notaon ) as the hashed value. The cost of compu3ng this "trivial" (iden3ty) hash func3on is effec3vely zero. The meaning of "small enough" depends on how much memory is available for the hash table. A typical PC might have 4 gigabytes of available memory, meaning that hash values of up to 32 bits could be accommodated. However, there are many applica3ons that can get by with much less. For example, when mapping character strings between upper and lower case , one can use the binary encoding of each character, interpreted as an integer, to index a table that gives the alterna3ve form of that character ("A" for "a", "8" for "8", etc.). If each character is stored in 8 bits (as in ASCII or ISO La3n 1), the table has only 2^8 = 256 entries. The same technique can be used to map two-‐lexer country codes like "us" or "za" to country names (26^2 =676 table entries), 5-‐digit zip codes like 13083 to city names (100000 entries), etc. Invalid data values (such as the country code "xx" or the zip code 00000) may be lez undefined in the table, or mapped to some appropriate "null" value.
Families La natura de les famílies depèn de les dades! Hashing uniformly distributed data: If the inputs are bounded-‐length strings (such as telephone numbers, car license plates, invoice numbers, etc.), and each input may independently occur with uniform probability, then a hash func3on need only map roughly the same number of inputs to each hash value. For instance, suppose that each input is an integer z in the range 0 to N −1, and the output must be an integer h in the range 0 to n −1, where N is much larger than n. Then the hash func3on could be h = z mod n (the remainder of z divided by n ), or h = ( z × n ) ÷ N (the value z scaled down by n / N and truncated to an integer), or many other formulas.