Pengindeksan Dan Fail Songsang inverted File Penjanaan Fail

  • Slides: 98
Download presentation
Pengindeksan Dan Fail Songsang (inverted File)

Pengindeksan Dan Fail Songsang (inverted File)

Penjanaan Fail Indeks Songsang Word Extraction Word IDs Dokumen Asal W 1: d 1,

Penjanaan Fail Indeks Songsang Word Extraction Word IDs Dokumen Asal W 1: d 1, d 2, d 3 W 2: d 2, d 4, d 7, d 9 Document IDs Wn : di, …dn Inverted Files

Indeks Songsang l l Sistem capaian maklumat membangunkan indeks songsang untuk mencari katakunci dalam

Indeks Songsang l l Sistem capaian maklumat membangunkan indeks songsang untuk mencari katakunci dalam koleksi dokumen dengan berkesan. Indeks songsang mengandungi dua komponen iaitu satu senarai bagi setiap katakunci yang dipanggil indeks dan satu senarai yang dipanggil posting list. Posting List panjang Posting List pendek Terbaik jika indeks disimpan dalam ingatan utama Disebabkan saiznya posting list disiimpan dalam disk

Penjanaan Fail Indeks Songsang l l Map the file names to file IDs Consider

Penjanaan Fail Indeks Songsang l l Map the file names to file IDs Consider the following Original Documents D 1 The Department of Computer Science was established in 1984. D 2 The Department launched its first BSc(Hons) in Computer Studies in 1987. D 3 followed by the MSc in Computer Science which was started in 1991. D 4 The Department also produced its first Ph. D graduate in 1994. D 5 Our staff have contributed intellectually and professionally to the advancements in these fields.

Penjanaan Fail Indeks Songsang green: stop word D 1 The Department of Computer Science

Penjanaan Fail Indeks Songsang green: stop word D 1 The Department of Computer Science was established in 1984. D 2 The Department launched its first BSc(Hons) in Computer Studies in 1987. D 3 followed by the MSc in Computer Science which was started in 1991. D 4 The Department also produced its first Ph. D graduate in 1994. D 5 Our staff have contributed intellectually and professionally to the advancements in these fields.

Penjanaan Fail Indeks Songsang After stemming, make lowercase (option), delete numbers (option) D 1

Penjanaan Fail Indeks Songsang After stemming, make lowercase (option), delete numbers (option) D 1 depart comput scienc establish D 2 depart launch bsc hons comput studi D 3 follow msc comput scienc start D 4 depart produc phd graduat D 5 staff contribut intellectu profession advanc field

Penjanaan Fail Indeks Songsang (belum terisih) Words Documents depart d 1, d 2, d

Penjanaan Fail Indeks Songsang (belum terisih) Words Documents depart d 1, d 2, d 4 produc d 4 comput d 1, d 2, d 3 phd d 4 scienc d 1, d 3 graduat d 4 establish d 1 staff d 5 launch d 2 contribut d 5 bsc d 2 intellectu d 5 hons d 2 profession d 5 studi d 2 advanc d 5 follow d 3 field d 5 msc d 3 start d 3

Penjanaan Fail Indeks Songsang (terisih) Words Documents advanc d 5 msc d 3 bsc

Penjanaan Fail Indeks Songsang (terisih) Words Documents advanc d 5 msc d 3 bsc d 2 phd d 4 comput d 1, d 2, d 3 produc d 4 contribut d 5 profession d 5 depart d 1, d 2, d 4 scienc d 1, d 3 establish d 1 staff d 5 field d 5 start d 3 follow d 3 studi d 2 graduat d 4 intellectu d 5 launch d 2

Pembinaan indeks l Setiap dokumen diwakilkan dalam bentuk vektor • <term 1, term 2,

Pembinaan indeks l Setiap dokumen diwakilkan dalam bentuk vektor • <term 1, term 2, term 3, …, termn> • Setiap kemasukkan data menggambarkan bilangan sesuatu term itu ujud pada satu-satu dokumen Terms

Indeks Songsang l l Secara konsep, ianya telah dipelajari dalam model ruang vektor dimana

Indeks Songsang l l Secara konsep, ianya telah dipelajari dalam model ruang vektor dimana ianya dijanakan dalam bentuk vektor di antara term vs dokumen. Fail songsang merupakan “songsangan” dari fail vektor dimana lajur menjadi baris dan baris menjadi lajur.

Pembinaan Fail Songsang l Dokumen dihuraikan bagi menghasilkan token dan ia disimpan bersama dengan

Pembinaan Fail Songsang l Dokumen dihuraikan bagi menghasilkan token dan ia disimpan bersama dengan ID dokumen Doc 1 Doc 2 Now is the time for all good men to come to the aid of their country It was a dark and stormy night in the country manor. The time was past midnight

Pembinaan Fail Songsang l Setelah selesai semua dokumen dihuraikan, maka fail songsang diisih dalam

Pembinaan Fail Songsang l Setelah selesai semua dokumen dihuraikan, maka fail songsang diisih dalam bentuk tersusun.

Pembinaan Fail Songsang l Term yang berulang pada sesuatu dokumen akan dicantumkan (tambah nilai

Pembinaan Fail Songsang l Term yang berulang pada sesuatu dokumen akan dicantumkan (tambah nilai kekerapan)

Pembinaan Fail Songsang l Kemudian fail boleh dipecahkan kepada dua iaitu • Fail Dictionary

Pembinaan Fail Songsang l Kemudian fail boleh dipecahkan kepada dua iaitu • Fail Dictionary dan • Fail Postings

Pembinaan Fail Songsang Dictionary Postings

Pembinaan Fail Songsang Dictionary Postings

Fail Songsang Kelebihan l Meningkatkan keberkesanan penggelintaran. Kelemahan l Keperluan menyimpan struktur data yang

Fail Songsang Kelebihan l Meningkatkan keberkesanan penggelintaran. Kelemahan l Keperluan menyimpan struktur data yang saiznya 10 – 100% lebih besar daripada saiz teks dan keperluan untuk menukar indeks jika terdapat penukaran data. l Proses pengemaskinian indeks adalah mahal tetapi tatasusunan yang tersisih mudah dijanakan dan cepat.

Struktur Data yang digunakan pada Fail Songsang l l Tatasusunan Terisih (Sorted Arrays) Pohon

Struktur Data yang digunakan pada Fail Songsang l l Tatasusunan Terisih (Sorted Arrays) Pohon B Struktur Cincangan (Hashing Structures) Tries (digital search trees)

Tatasusunan Terisih l l Fail songsang yang menggunakan metod ini menyimpan katakunci dalam bentuk

Tatasusunan Terisih l l Fail songsang yang menggunakan metod ini menyimpan katakunci dalam bentuk tatasusunan terisih, berserta dengan bilangan dokumen yang mengandungi katakunci tersebut dan hubungan yang menghubungkan ke dokumen-dokumen tersebut. Penggelintaran dalam tatasusunan ini ialah berdasarkan penggelintaran binari. Kebaikan : senang nak diimplementasi Keburukan : pengemaskinian indeks agak mahal

Tatasusunan Terisih Penghasilan tatasusunan fail songsang terisih boleh dibahagi kepada 2 atau 3 langkah:

Tatasusunan Terisih Penghasilan tatasusunan fail songsang terisih boleh dibahagi kepada 2 atau 3 langkah: 1. Teks yang digunakan sebagai input dihuraikan menjadi senarai perkataan-perkataan berserta dengan lokasinya dalam teks (tentukan penggunaan katahenti dan cantasan sama ada perlu dimasukkan atau tidak. Ini bergantung kepada kekangan penggunaan masa dan storan dalam operasi pengindeksan). 2. Senarai perkataan di songsangkan dari senarai perkataan dalam susunan lokasi ke senarai perkataan terisih bagi kegunaan carian. Pengisihan dibuat dalam susunan tertentu beserta semua lokasi yang dikaitkan bagi setiap term/perkataan.

Tatasusunan Terisih 3. Proses lanjutan terhadap fail songsang yang terhasil seperti meletakkan pemberat sebutan

Tatasusunan Terisih 3. Proses lanjutan terhadap fail songsang yang terhasil seperti meletakkan pemberat sebutan atau penyusunan semula atau penggunaan pemadatan (compression) bagi fail. (proses ini adalah opsional)

Pohon B Pohon-B biasanya digunakan untuk tujuan gelintaran data. Ia mesti mempunyai nombor kunci

Pohon B Pohon-B biasanya digunakan untuk tujuan gelintaran data. Ia mesti mempunyai nombor kunci dan anak. Pohon pada order m nerupakan pohon dimana setiap nod mempunyai sebanyak-banyaknya m anak. Bagi setiap nod, jika k merupakan bilangan sebenar anak pada nod, maka k-1 merupakan bilangan kunci pada nod Rujuk rajah dibawah dimana baris pertama menunjukkan nod bagi setiap kunci manakala baris kedua menunjukkan penunjuk ke kunci anak.

Pohon B l Jika pohon gelintar dalam order 4 maka ia harus memenuhi syarat

Pohon B l Jika pohon gelintar dalam order 4 maka ia harus memenuhi syarat berikut l The keys in each node are in ascending order. l Bagi setiap nod jika berikut adalah benar. • Sub pokok bermula dari rekod Node. Branch[0] hanya ada kunci yang kurang dari Node. key[0] • Sub pokok bermula dari rekod Node. Branch[1] hanya ada kunci yang lebih dari Node. key[0] dan pada masa yang sama kurang dari Node. Key[1] • Sub pokok bermula dari rekod Node. Branch[2] hanya ada kunci yang lebih dari Node. key[1] dan pada masa yang sama kurang dari Node. Key[2] • Sub pokok bermula dari rekod Node. Branch[3] hanya ada kunci yang lebih dari Node. key[2]

Pohon B l Berikut merupakan contoh bagi pohon-B dengan order 5. Ini bermaksud semua

Pohon B l Berikut merupakan contoh bagi pohon-B dengan order 5. Ini bermaksud semua nod luar mempunyai sekurang-kurangnya ceil(5/2) = 3 anak. Bilangan maksimum anak bagi nod adalah 5 (4 adalah bilangan maksimum kunci). Setiap nod daun mesti mengandungi sekurangnya 2 kunci.

Pohon B (Kemasukkan Data Baru) l l Katakan kemasukkan data baru akan dibuat ke

Pohon B (Kemasukkan Data Baru) l l Katakan kemasukkan data baru akan dibuat ke atas pohon-B yang kosong di mana ia menggunakan order 5. Diberi huruf-huruf berikut : C N G A H E K Q M F W L T Z D P R X Y S. Ini bermaksud nod boleh mempunyai maksima 5 anak dan 4 kunci. Semua nod selain akar mesti mempunyai minimum 2 kunci. 4 huruf dimasukkan pada nod seperti rajah disebelah

Pohon B (Kemasukkan Data Baru) Masukkan H, Masukkan E, K, dan Q Masukkan M

Pohon B (Kemasukkan Data Baru) Masukkan H, Masukkan E, K, dan Q Masukkan M

Pohon B (Kemasukkan Data Baru) Huruf F, W, L, dan T masuk Z

Pohon B (Kemasukkan Data Baru) Huruf F, W, L, dan T masuk Z

Pohon B (Kemasukkan Data Baru) Masukkan D Masuk S

Pohon B (Kemasukkan Data Baru) Masukkan D Masuk S

Pohon B (Penghapusan Data) Penghapusan huruf H

Pohon B (Penghapusan Data) Penghapusan huruf H

Pohon B (Penghapusan Data) Hapuskan huruf T.

Pohon B (Penghapusan Data) Hapuskan huruf T.

Cincangan Apa itu Cincangan ? l Teknik untuk menentukan indeks atau lokasi untuk menyimpan

Cincangan Apa itu Cincangan ? l Teknik untuk menentukan indeks atau lokasi untuk menyimpan data pada struktur data. l Fungsi cincangan : • Untuk menghantar kunci carian/gelintar. • Merupakan satu transformasi kepada bentuk kunci • Kebiasaannya dalam bentuk formula matematik • Memulangkan indeks dimana akan disimpan dan untuk capaian data pada jadual.

Konsep Asas We can think of hashing as a key-to -address transformation the keys

Konsep Asas We can think of hashing as a key-to -address transformation the keys map to addresses in a list.

Cincangan l l l Fungsi cincang ialah fungsi h(k) yang menukarkan data kepada kunci

Cincangan l l l Fungsi cincang ialah fungsi h(k) yang menukarkan data kepada kunci iaitu suatu alamat bagi suatu julat 0 Saiz. Jadual-1 Fungsi cincang digunakan untuk memetakan kekunci ke dalam slot di dalam Jadual cincangan. Contoh : • Katakan kita menentukan untuk menggunakan 1000 alamat maka jika U merupakan semua kemungkinan set kekunci, maka fungsi hash adalah dari U ke {0, 1, 2, …. . 999} k Kod ASCII untuk 2 huruf pertama Hasil darab (d) h(k)= d mod 1000 BALL 66, 65 66. 65 = 4290 LOWELL 76, 79 76. 79 = 6004 TREE 84, 82 84. 82 = 6888 000 001. . 004 LOWELL. . 290 BALL … 888 TREE … 999

Contoh Hash(const char *Key, const int Table. Size) { int Hash. Val = 0;

Contoh Hash(const char *Key, const int Table. Size) { int Hash. Val = 0; while (*key != ‘’) Hash. Val += *key++; return Hash. Val % Table. Size } U T (universe of keys) k K 1 k k 5 4 (actual k 7 keys) k 6 k 2 k 8 k 3 —— —— ——

Fungsi cincangan yang baik for (hash=len; len--; ) { hash = ((hash<<5)^(hash>>27))^*key++; } hash

Fungsi cincangan yang baik for (hash=len; len--; ) { hash = ((hash<<5)^(hash>>27))^*key++; } hash = hash % prime;

Cincangan Namun begitu, terdapat kekunci yang berbeza tetapi dihantar alamat yang sama maka akan

Cincangan Namun begitu, terdapat kekunci yang berbeza tetapi dihantar alamat yang sama maka akan berlaku perlanggaran (collision) Seperti contoh sebelum, di mana terdapat dua atau lebih yang bermula dengan 2 huruf pertama yang sama. Maka satu proses yang dinamakan cincangan semula (rehashing) perlu dilakukan T U (universe of keys) k K 1 k k 5 4 (actual k 7 keys) k 6 k 2 k 8 k 3 —— —— ——

Cincangan Semula (Rehashing) Fungsi Cincangan semula Fungsi kedua yang boleh digunakan untuk memilih lokasi

Cincangan Semula (Rehashing) Fungsi Cincangan semula Fungsi kedua yang boleh digunakan untuk memilih lokasi jadual bagi item baru yang akan dimasukkan. Jika lokasi tersebut juga telah digunakan maka fungsi rehash boleh digunakan bagi mendapat lokasi ketiga dan seterusnya. Contoh mudah fungsi rehash : rehash(k) = (k + 1) % prime

Cincangan Semula (Rehashing) Kaedah untuk mengurangkan perlanggaran l Cuba dapatkan fungsi cincangan yang terbaik

Cincangan Semula (Rehashing) Kaedah untuk mengurangkan perlanggaran l Cuba dapatkan fungsi cincangan yang terbaik untuk penaburan rekod l Penggunakan ruang ingatan yang lebih besar. Meningkatkan ruang pengalamatan, contohnya jika keperluan ialah 1000 maka lebihkan sehingga 2000 ruang tambahan. l Letakkan lebih dari satu rekod pada satu alamat (penggunaan buckets)

Rantaian (Chaining) l Chaining puts elements that hash to the same slot in a

Rantaian (Chaining) l Chaining puts elements that hash to the same slot in a linked list: T U (universe of keys) k 1 K k 4 k 5 (actual k 7 keys) k 6 k 2 k 8 k 3 —— —— —— k 1 k 4 —— k 5 k 2 k 3 —— k 8 k 6 —— k 7 ——

Hashing (Abu Ata) l l Memudahkan sesuatu alamat disimpan dicapai secara terus serta cepat

Hashing (Abu Ata) l l Memudahkan sesuatu alamat disimpan dicapai secara terus serta cepat dan betul. Dikira berdasarkan Kod ASCII bagi sesuatu huruf dan dijadi penghubung antara huruf-huruf perkataan yang diindeks Hubungan dikira berdasarkan susunan huruf antara set huruf dan untuk huruf berikutnya berdasarkan susunan huruf yang bersebelahan. Mungkin berlaku perlanggaran. Cincangan semula dilakukan dan satu alamat baru akan dijanakan bagi mendapatkan satu rekod yang kosong.

Hashing (Abu Ata) 1. 2. 3. 4. Semua huruf ditukar kepada huruf kecil Set

Hashing (Abu Ata) 1. 2. 3. 4. Semua huruf ditukar kepada huruf kecil Set 26 huruf abjad diberi nilai berdasarkan susunan jujukan dalam set abjad contoh : a=1, b=2, c=3 ……. . , y=25, z=26 Huruf bagi suatu perkataan dan huruf yang berikutnya dan pengiraan adalah seperti berikut i. Kedudukan huruf pertama dalam set abjad (peraturan 2) ii. Kedudukan huruf kedua dalam set abjad (peraturan 2) iii. Keputusan pada (i) di darab dengan 26 iv. Campur keputusan pada (ii) dan (iii) Campur keputusan bagi peraturan di 2 dengan peraturan di 3

Basic Concepts l l In this case, we must use the collision resolution algorithm

Basic Concepts l l In this case, we must use the collision resolution algorithm to determine the next possible location for the data and continue until we find the correct data. Each calculation of an address and test for success is known as a probe. Sumber : http: //www. ee. udel. edu/~durbano/teaching/CISC 220/slides/38

Hashing Methods l There are several hashing methods that we will discuss: • •

Hashing Methods l There are several hashing methods that we will discuss: • • Direct Subtraction Modulo-Division Digit Extraction Midsquare Folding Rotation Pseudorandom Generation

Direct Method l In direct hashing, the key is the address without any algorithmic

Direct Method l In direct hashing, the key is the address without any algorithmic manipulation.

Direct Method l l In this case, the hash table must contain an element

Direct Method l l In this case, the hash table must contain an element for every possible key. Although it has a limited use, it is powerful in the sense that it is easy to code and there are no synonyms.

Direct Method l l l As an example, consider a small company with less

Direct Method l l l As an example, consider a small company with less than 100 employees. Each employee is assigned an employee number (from 0 to 99). By storing the employees in an array of size 100, we can reference an employee simply by using the employee number as the index into the array.

Direct Method l l l Obviously, the direct method has limited uses. Namely, it

Direct Method l l l Obviously, the direct method has limited uses. Namely, it can only be used on small data sets. For example, it would be impractical to use direct hashing via the SSN of our employees. If we did, we would have a 9 digit number as the index into our array (i. e. , we would need an array of size 1 billion but would use less than 100 entries!)

Subtraction Method l l l Sometimes, keys may be consecutive, but may not start

Subtraction Method l l l Sometimes, keys may be consecutive, but may not start from ‘ 1’. Consider our small company – what if we assigned employee numbers from 1000 to 1099? In this case, our hashing function would simply subtract 1000 from the key value to produce the address (0 to 99).

Subtraction Method l Algorithm: address = key – subtraction. Constant l As with the

Subtraction Method l Algorithm: address = key – subtraction. Constant l As with the direct method, the subtraction method is easy to implement, guarantees no collisions, and has limited uses.

Modulo-Division Method l l Also known as the division remainder method, the modulo-division method

Modulo-Division Method l l Also known as the division remainder method, the modulo-division method divides the key by the array size and uses the remainder for the address. Algorithm address = key % list. Size

Modulo-Division Method l l Although this algorithm will work with any size list, we

Modulo-Division Method l l Although this algorithm will work with any size list, we typically choose a list size that is a prime number. This has the effect of reducing the number of collisions.

Modulo-Division Method l l To continue with our small company example, let’s say we

Modulo-Division Method l l To continue with our small company example, let’s say we are planning on expanding our company. In our new system, employees will receive employee numbers from 0 to 999, 999 and we will provide space in our data structure for up to 300 employees.

Modulo-Division Method l l l We start by choosing a list size of 307

Modulo-Division Method l l l We start by choosing a list size of 307 (the first prime number above 300). Therefore, our available address space is 0 to 306 (key%307=[0, 306]). As an example, let’s say we want to hash Bryan’s employee number 121267: 121267/307 = 395 remainder 2 Therefore, hash(121267)=2

Modulo-Division Method

Modulo-Division Method

Modulo-Division Method l Note: in a test situation, I expect you to be able

Modulo-Division Method l Note: in a test situation, I expect you to be able to perform the modulus operation on small numbers.

Digit-Extraction Method l l Using digit extraction, selected digits are extracted from the key

Digit-Extraction Method l l Using digit extraction, selected digits are extracted from the key and used as an address. For example, using our 6 -digit employee number from before, if we wanted to realize a 3 -digit address, we could select the 1 st, 2 nd, and last digits to create our address 379452 372 121267 127

Midsquare Method l l In midsquare hashing, the key is squared and the address

Midsquare Method l l In midsquare hashing, the key is squared and the address is selected from the middle of the result. For example, if our key value were 9452: 9452*9452=89340304 address = 3403

Midsquare Method l l A limitation to the use of this method is the

Midsquare Method l l A limitation to the use of this method is the size of key. Because squaring a key produces a number twice the length of the key, this method will only work for small key values.

Midsquare Method l l However, if we wish to apply the midsquare method to

Midsquare Method l l However, if we wish to apply the midsquare method to large key values, we can simply choose a subset of the digits of the key to square (sort of like digit extraction). address For example: 379452 379*379 = 143641 = 364 key

Folding Methods l Two folding methods are used: • Fold shift • Fold boundary

Folding Methods l Two folding methods are used: • Fold shift • Fold boundary

Fold Shift l l l In fold shift, the key value is divided into

Fold Shift l l l In fold shift, the key value is divided into parts whose size matches the size of the required address. Then, the left and right parts are shifted and added with the middle part. Should the addition result in a carry digit, that digit is simply dropped.

Fold Boundary l l l In fold boundary, the left and right numbers are

Fold Boundary l l l In fold boundary, the left and right numbers are folded on a fixed boundary between them and the center number. The two outside values are thus reversed. Should the addition result in a carry digit, that digit is simply dropped.

Folding Methods

Folding Methods

Rotation Method l l In the rotation method, we rotate a digit to the

Rotation Method l l In the rotation method, we rotate a digit to the front or back of the key. This has the effect of spreading the keys more evenly over the key space. Rotation hashing is usually used in conjunction with other hashing methods, which results in a more effective hash. Example: imagine selecting only the first 3 digits of the following keys.

Rotation Method

Rotation Method

Pseudorandom Method l l Here, we use the key as the seed in a

Pseudorandom Method l l Here, we use the key as the seed in a pseudorandom number generator. The resulting random number is then scaled into the possible address range using modulo division.

Pseudorandom Method l l One example of a random number generator is y =

Pseudorandom Method l l One example of a random number generator is y = ax + c where x = key a = scaling coefficient c = constant address = y%list. Size For maximum efficiency, a and c should be prime numbers.

Collision Resolution l l With the exception of direct hashing and subtraction hashing, none

Collision Resolution l l With the exception of direct hashing and subtraction hashing, none of the hashing methods we discussed result in a one-to-one mapping. Therefore, as discussed before, a collision may occur. Fortunately, there are many methods of dealing with collisions (all of which are independent of the hashing method used). That is, any collision resolution algorithm can be used with any hashing algorithm.

Collision Resolution l Generally, there are two different approaches to resolving collisions: • Open

Collision Resolution l Generally, there are two different approaches to resolving collisions: • Open addressing • Linked Lists l A third concept, buckets, defers collisions, but does not fully resolve them.

Open Addressing l l Open addressing resolves collisions in the prime area (the area

Open Addressing l l Open addressing resolves collisions in the prime area (the area that contains all of the home addresses). This technique is contrasted with linked list resolution, in which the collisions are resolved by placing the data in a separate overflow area.

Open Addressing l l When a collision occurs, the prime area addresses are searched

Open Addressing l l When a collision occurs, the prime area addresses are searched for an open element where the new data can be placed. We will discuss 4 methods of open addressing: • • Linear probe Quadratic probe Double hashing Key offset

Linear Probe When data cannot be stored at the home address, we resolve the

Linear Probe When data cannot be stored at the home address, we resolve the collision by adding 1 to the current address. Here, we get a collision at address 1. To resolve this, we try to insert the data at 2. However, this location is occupied, so we try address 3.

Linear Probe l l l As an alternative to a simple linear probe, we

Linear Probe l l l As an alternative to a simple linear probe, we can add 1, subtract 2, add 3, subtract 4, etc. until we locate an empty element. Note: the code that does the collision resolution must verify that the new address is within the address space. For example, if we are at the last element of the list, when we add 1, we must start back at the beginning of the list.

Linear Probe l Linear probes have 2 advantages: • Easy to implement • Data

Linear Probe l Linear probes have 2 advantages: • Easy to implement • Data tends to remain near their home addresses (good for caching) l However, linear probes tend to produce primary clustering.

Linear Probe l l After the collision has been resolved, hashing continues as it

Linear Probe l l After the collision has been resolved, hashing continues as it did before the collision. The next time a collision occurs, we restart our resolution algorithm by adding 1 to the address and then continue as before.

Quadratic Probe l l We can eliminate the primary clustering phenomenon in the linear

Quadratic Probe l l We can eliminate the primary clustering phenomenon in the linear probe by adding a number other than 1 to the address. One example of this is the quadratic probe. Here, the increment is the collision probe number squared. Thus, for the first collision we add 12, the second collision we add 22, the third collision 32, etc.

Quadratic Probe l l Again, we have to make sure that we don’t run

Quadratic Probe l l Again, we have to make sure that we don’t run off the end of the address list. To do this, we use the modulus of the new address and the list size. new address = (last address tried + probe 2)%list. Size

Quadratic Probe l l l Disadvantages: • • • The time it takes to

Quadratic Probe l l l Disadvantages: • • • The time it takes to perform the ‘square’ operation Produces secondary clustering It is not possible to generate a new address for every element in the list To help alleviate the last disadvantage, we choose a list size that is a prime number. This will allow at least half of the list to be reachable (a reasonable number).

Quadratic Probe l l After the collision has been resolved, hashing continues as it

Quadratic Probe l l After the collision has been resolved, hashing continues as it did before the collision. The next time a collision occurs, we start resolution again with 12 and continue as before.

Double Hashing l l l The next two open addressing methods are collectively known

Double Hashing l l l The next two open addressing methods are collectively known as double hashing. In double hashing, rather than use an arithmetic probe function, as in the linear and quadratic probes, we rehash the address. This prevents primary clustering.

Double Hashing l l The probe sequences used by both linear and quadratic probing

Double Hashing l l The probe sequences used by both linear and quadratic probing are key independent. For example, linear probing inspects the table locations sequentially, no matter what the value of the key is. In contrast, double hashing defines keydependent probe sequences. In this scheme, the probe sequence still searches the table in a linear order, but a second hash determines the size of the steps taken.

Pseudorandom Collision Resolution l l l The first method uses a pseudorandom number to

Pseudorandom Collision Resolution l l l The first method uses a pseudorandom number to resolve the collision. This is basically the same process as the pseudorandom hashing function. In this case, however, instead of using the key as the seed to the pseudorandom number generator, we use the collision address as the seed.

Pseudorandom Collision Resolution Here, we have a collision at address 1. To resolve this

Pseudorandom Collision Resolution Here, we have a collision at address 1. To resolve this collision, we use the collision address (1) in our pseudorandom number generator y=3(1)+5=8 Therefore, we try address 8 as our new address.

Pseudorandom Collision Resolution l l Disadvantage: all of the keys will follow only 1

Pseudorandom Collision Resolution l l Disadvantage: all of the keys will follow only 1 collision resolution path through the list. Therefore, this method will lead to secondary clustering.

Key Offset l l l The second double hashing method is key offset. This

Key Offset l l l The second double hashing method is key offset. This method will produce different collision paths for different keys. Whereas the pseudorandom number generator produces a new address as a function of only the collision address, the key offset method uses both the original key and the collision address to calculate the new address.

Key Offset l Here is one of the simplest implementations: offset = key/list. Size;

Key Offset l Here is one of the simplest implementations: offset = key/list. Size; // integer arithmetic address = (old address + offset)%list. Size; l l Here, we calculate an offset value based on the key and add this value to the collision address. Does this method lead to primary or secondary clustering?

Linked List Resolution l l l In open addressing, we resolve collisions by placing

Linked List Resolution l l l In open addressing, we resolve collisions by placing the data in the same memory area as the rest of the data (the prime area). One problem with this approach is that each resolved collision increases the probability of future collisions. This disadvantage can be eliminated by using a linked list resolution approach rather than an open addressing approach.

Linked List Resolution l l Linked list resolution uses a separate area (the overflow

Linked List Resolution l l Linked list resolution uses a separate area (the overflow area) to store the collisions and chains all of the synonyms together in a linked list. When a collision occurs, one element is stored in the prime area and the other element is stored in the overflow area.

Linked List Resolution Here, we have had 2 collisions at address 1. The collision

Linked List Resolution Here, we have had 2 collisions at address 1. The collision is resolved by placing the synonyms in a linked list with the head element in the prime area.

Linked List Resolution l l Items are usually inserted in a last-in, first-out (LIFO)

Linked List Resolution l l Items are usually inserted in a last-in, first-out (LIFO) order. This allows for fast insertions as the list need not be scanned … the element is simply inserted in the prime area. Another possible ordering is a key sequenced list where the data with the smallest key value is stored in the prime area, allowing for fast retrievals.

Bucket Hashing l l l Another approach to handling the collision problem is bucket

Bucket Hashing l l l Another approach to handling the collision problem is bucket hashing. A bucket is a node that can accommodate multiple data occurrences. Because the bucket can hold multiple data values, collisions can be postponed until the bucket is full.

Bucket Hashing Here, we see an implementation with a bucket size of 3. This

Bucket Hashing Here, we see an implementation with a bucket size of 3. This structure can accommodate up to 3 synonyms before a collision will occur.

Bucket Hashing l Disadvantages: • More space is used (many buckets will be •

Bucket Hashing l Disadvantages: • More space is used (many buckets will be • l l empty or partially empty) It does not completely resolve collisions When a collision does occur, a typical resolution is to use a linear probe. Here, we assume that the adjacent bucket will have an empty space.

Bucket Hashing l l Question: Why not just increase the size of the hash

Bucket Hashing l l Question: Why not just increase the size of the hash table instead of using buckets? Answer: Entire bucket will probably be cached (its contents are adjacent in memory). Thus, multiple “probes” will likely “hit” in cache.

Combination Approaches l l Typically, we often use multiple steps to resolve collisions. For

Combination Approaches l l Typically, we often use multiple steps to resolve collisions. For example, we might use bucket hashing. Should a collision occur, we will perform up to, say, 3 linear probes to try to resolve the collision. Then, we may resort to a linked list resolution.

What Makes A Good Hashing Function? 1) 2) A hashing function should be fast

What Makes A Good Hashing Function? 1) 2) A hashing function should be fast and easy to compute. A hashing function should scatter the data evenly throughout the hash table. • How well does the hash function scatter random data? Nonrandom data?

Tips For Developing Good Hashing Functions l l The calculation of the hashing function

Tips For Developing Good Hashing Functions l l The calculation of the hashing function should involve the entire search key. Thus, for example, computing the modulus of the entire ID number is much safer than using only its first 2 digits.

Tips For Developing Good Hashing Functions l l l If a hashing function uses

Tips For Developing Good Hashing Functions l l l If a hashing function uses modulo arithmetic, the base should be a prime number. That is, if h is of the form: h(x) = x mod list. Size then list. Size should be a prime number. This is a safeguard against many subtle kinds of patterns in the data (for example, search keys whose digits are likely to be multiples of one another).

Disadvantage of Hashing l l l For all of its advantages, one of the

Disadvantage of Hashing l l l For all of its advantages, one of the major disadvantages of hashing is trying to traverse the data in sorted order. Traversals are inefficient because a good hashing function scatters items as randomly as possible throughout the array. Hence, in order to traverse the table in sorted order, you would first have to sort the items.