Hash pre


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Hash pre

  1. 1. Tafelia Technical university TTU
  2. 2. *Hash table or hash map is a datastructure that uses a hash function tomap identifying values, known askeys(e.g., a persons name), to theirassociated values (e.g., their telephonenumber).*The hash function is used to transform hash)the key into the index (the hash) of anarray element (the slot or bucket) where bucket)the corresponding value is to be sought.
  3. 3. Ideally, the hash function should map eachpossible key to a unique slot index, but this idealis rarely achievable in practice (unless the hashkeys are fixed .
  4. 4. HASH TABLE TYPES There are two types of Hash Tables: Open-addressed Hash Tables and Separate- Open- Separate- Tables. Chained Hash Tables An Open-addressed Hash Table is a one-dimensional array indexed by Open- integer values that are computed by an index function called a hash function function. A Separate-Chained Hash Table is a one-dimensional array of linked lists indexed by Separate- integer values that are computed by an index function called a hash function function. Hash tables are sometimes referred to as scatter tables tables.”chain”. Typical hash table operations are: Initialization. — Initialization. — Insertion. Insertion. — Searching Deletion. — Deletion.
  5. 5. Separate chained hash table• Linked list at each table location• The table entries are pointers to listheads
  6. 6. There are two types of hashing :1. Static hashing: In static hashing, the hash functionmaps search-key values to a fixed set of locations. 2. Dynamic hashing: In dynamic hashing a hash tablecan grow to handle more items. The associated hashfunction must change as the table grows.The load factor of a hash table is the ratio of the number ofkeys in the table to the size of the hash table. The higher the load factor, the slower the retrieval. With open addressing, the load factor cannot exceed 1. With chaining, the load factor often exceeds 1.
  7. 7. hash function f:index = f(key, arrayLength) The hash function calculates an index within the array from the data key. arrayLength is the size of the array. For assembly language or other low-level programs, a trivial hash function can often create an index with just one or two inline machine instructions
  8. 8. A hash function h, is a function which transforms a function,key from a set, K, into an index in a table of size n: h: K -> {0, 1, ..., n-2, n-1} {0 n- n-A key can be a number, a string, a record etc.The size of the set of keys, |K| to be relatively very |K|,large.It is possible for different keys to hash to the samearray location.This situation is called collision and the colliding keysare called synonyms.
  9. 9. A good hash function should: — Minimize collisions. — Be easy and quick to compute. — Distribute key values evenly in the hash table. — Use all the information provided in the key.
  10. 10. 1. Division Remainder (using the table size as the divisor)Computes hash value from key using the % operator.Table size that is a power of 2 like 32 and 1024 should be avoided,for it leads to more collisions.Prime numbers not close to powers of 2 are better table size values.h(key) = key mod size(remainder of key divided by size)where size is the table size m
  11. 11. 2. FoldingIt involves splitting keys into two or more parts and thencombining the parts to form the hash addresses.To map the key 25936715 to a range between 0 and 9999, wecan: — split the number into two as 2593 and 6715 and — add these two to obtain 9308 as the hash value.Very useful if we have keys that are very large.A great advantage is ability to transform non-integer keys intointeger values.
  12. 12. 2. Truncation or Digit/Character ExtractionWorks based on the distribution of digits orcharacters in the key.If search key is string we first convert it tointeger”ASCLL” then use hash tableH(“note”)=78+79+84+69
  13. 13. AdvantagesThe main advantage of hash tablesover other table data structures isspeed. This advantage is moreapparent when the number of entriesis large (thousands or more.
  14. 14. HASH TABLE APPLICATIONSDatabase systemsSymbol tables The tables used by compilers to maintain information tables:about symbols from a program.Data dictionaries Data structures that support adding, deleting, and dictionaries:searching for data. Although the operations of a hash table and a datadictionary are similar, other data structures may be used to implementdata dictionaries. Using a hash table is particularly efficient.Network processing algorithms Hash tables are fundamental algorithms:components of several network processing algorithms and applications,including route lookup, packet classification, and network monitoring.
  15. 15. EXAMPLESuppose that we want to store 10,000 students records(each with a 5-digit ID) in a given containerThe idfield in this class can be used as a search key forrecords in the container. class StudentRecord { String name; // Student name double height; // Student height long id; // Unique id }
  16. 16. Example 1: Introduction to HashingName ID h(r) = id % 13Al-Otaibi, Ziyad 985926 6Al-Turki, Musab Ahmad Bakeer 970876 10Al-Saegh, Radha Mahdi 980962 8Al-Shahrani, Adel Saad 986074 11Al-Awami, Louai Adnan Muhammad 970728 5Al-Amer, Yousuf Jauwad 994593 2Al-Helal, Husain Ali AbdulMohsen 996321 10 1 2 3 4 5 6 7 8 9 10 11 12