Вы находитесь на странице: 1из 14

Hashing and Hash Tables

EECS 215

Hash Tables
Another kind of Table O(1) on average for insert, lookup, and remove use an array named T of size `capacity` define a hash function that returns an integer int hash(string key, int N) must return an integer between 0 and N-1 store the key and info at T[hash(key, capacity)] hash() must always return the same integer for a given key

Table Size
Table size is usually prime to avoid bias Overly large table size means wasted space Overly small table size means more collisions What happens as table size approaches 1?

Hash Functions
a good hash function has the following characteristics avoids collisions spreads keys evenly in the array inexpensive to compute - must be O(1)

Hash Functions for Signed Integers


remainder after division by table length if keys are positive, you can eliminate the abs int hash(int key, int N) { return abs(key) % N; }

Hash Functions for Strings


Must be careful to cover range from 0 through capacity-1 Some poor choices summing all the ASCII codes multiplying the ASCII codes Important insight letters and digits fall in range 0101 and 0172 octal so all useful information is in lowest 6 bits Key length has a constant limit, so hash(k,N) is O(1)

Hash Functions for Strings


int hash(string key, int N) { const int shift = 6; const int mask = ~0 >> (32-shift); // lower 6 bits on int result = 0; for (int i = 0; i < key.length(); i++) result = (result << shift) | (key[i] & mask); return abs(result) % N; }

Dealing with Collisions


Open addressing key/value pairs are stored in array slots but what about deletions? Linear probing hash(k, i) = (hash1(k) + i) mod N increment hashvalue by a constant, 1, until free slot is found simplest to implement leads to `primary clustering` Quadratic probing hash(k, i) = (hash1(k) + c1*i + c2*i*i) mod N leads to `secondary clustering`

Dealing with Collisions


Double hashing hash(k, i) = (hash1(k) + i*hash2(k)) mod N avoids clustering Separate chaining each array slot is a SearchList avoids clustering never gets 'full' deletions are not a problem they are for other methods

Dealing with A Full Table


allocate a larger hash table rehash each from the smaller into the larger delete the smaller

Linked List Implementation


struct ListNode { string key; Object info; ListNode * next; ListNode(string k, Object ob, ListNode * n) { key = k; info = ob; next = n; } static ListNode * find(string k, ListNode * l); static ListNode * remove(string k, ListNode * l); static void destruct(ListNode * l); };

Chained Hash Table Implementation


class ChainedHashTable { int hash(string value, int N); // as given above ListNode * * T; // T is an array of linked lists int capacity; public: ChainedHashTable(int numberOfChains); void insert(string key, Object info); Object lookup(string key); void remove(string key); ~ChainedHashTable(); };

Direct Address Tables


A hash table with separate chaining when set of keys is a reasonable set of integral values say m .. M e.g., 0..127 for ASCII characters we can use the key itself as the hash code table size is M - m + 1

Perfect Hashing
when set of keys is known beforehand EG a set of reserved words for a programming language we can construct a function that guarantees no collisions called `Perfect Hashing' but may require more than N slots in the table a `Minimal Perfect Hash Function` uses exactly N slots

Вам также может понравиться

  • Vtiger Test Case
    Vtiger Test Case
    Документ9 страниц
    Vtiger Test Case
    prasikumar
    0% (1)
  • Geh 6703 PDF
    Geh 6703 PDF
    Документ1 186 страниц
    Geh 6703 PDF
    Jair Trzynadlowski
    Оценок пока нет
  • Multimedia Making It Work Chapter2 - Text
    Multimedia Making It Work Chapter2 - Text
    Документ33 страницы
    Multimedia Making It Work Chapter2 - Text
    alitahirnsn
    Оценок пока нет
  • Hashing
    Hashing
    Документ42 страницы
    Hashing
    rootsha
    Оценок пока нет
  • 15 HashTables
    15 HashTables
    Документ27 страниц
    15 HashTables
    ayboaydo26
    Оценок пока нет
  • CH 4
    CH 4
    Документ58 страниц
    CH 4
    Maria Grizelda
    Оценок пока нет
  • 05 Hashing
    05 Hashing
    Документ47 страниц
    05 Hashing
    cloudcomputingitasec
    Оценок пока нет
  • Unit Vi (Graphs)
    Unit Vi (Graphs)
    Документ80 страниц
    Unit Vi (Graphs)
    Kumara Ragavendra
    Оценок пока нет
  • Chapter10 Part1
    Chapter10 Part1
    Документ12 страниц
    Chapter10 Part1
    Artemis Zeusborn
    Оценок пока нет
  • Chapter 5 Lec 3
    Chapter 5 Lec 3
    Документ37 страниц
    Chapter 5 Lec 3
    Nuhamin Birhanu
    Оценок пока нет
  • Hash Table PDF
    Hash Table PDF
    Документ25 страниц
    Hash Table PDF
    John Titor
    Оценок пока нет
  • Hash Tables - : Structure
    Hash Tables - : Structure
    Документ21 страница
    Hash Tables - : Structure
    Saaaaaaakkk
    Оценок пока нет
  • Hash Table
    Hash Table
    Документ31 страница
    Hash Table
    Shubhashree Seth
    Оценок пока нет
  • 210 Maps PDF
    210 Maps PDF
    Документ39 страниц
    210 Maps PDF
    HaroonRashid
    Оценок пока нет
  • Hashing PPT For Student
    Hashing PPT For Student
    Документ53 страницы
    Hashing PPT For Student
    shail
    Оценок пока нет
  • Ders7 - Data Structures and Search Algorithms
    Ders7 - Data Structures and Search Algorithms
    Документ41 страница
    Ders7 - Data Structures and Search Algorithms
    mcsurmeli39
    Оценок пока нет
  • Hash Tables
    Hash Tables
    Документ43 страницы
    Hash Tables
    reagan oloya
    Оценок пока нет
  • Analysis of Algorithms CS 477/677: Hashing Instructor: George Bebis
    Analysis of Algorithms CS 477/677: Hashing Instructor: George Bebis
    Документ53 страницы
    Analysis of Algorithms CS 477/677: Hashing Instructor: George Bebis
    பிரசன்னா அகுலா
    Оценок пока нет
  • Hashing RPK
    Hashing RPK
    Документ61 страница
    Hashing RPK
    Ankita
    Оценок пока нет
  • Dsa Merged
    Dsa Merged
    Документ339 страниц
    Dsa Merged
    Abhishek Mishra
    Оценок пока нет
  • Hash Tables: Map Dictionary Key "Address."
    Hash Tables: Map Dictionary Key "Address."
    Документ16 страниц
    Hash Tables: Map Dictionary Key "Address."
    Manstall
    Оценок пока нет
  • Hash Table
    Hash Table
    Документ9 страниц
    Hash Table
    Muzamil Yousaf
    Оценок пока нет
  • CS 561, Lecture 2: Randomization in Data Structures: Jared Saia University of New Mexico
    CS 561, Lecture 2: Randomization in Data Structures: Jared Saia University of New Mexico
    Документ46 страниц
    CS 561, Lecture 2: Randomization in Data Structures: Jared Saia University of New Mexico
    Αλέξανδρος Γεωργίου
    Оценок пока нет
  • Analysis of Algorithms CS 477/677: Hashing Instructor: George Bebis
    Analysis of Algorithms CS 477/677: Hashing Instructor: George Bebis
    Документ53 страницы
    Analysis of Algorithms CS 477/677: Hashing Instructor: George Bebis
    preethi
    Оценок пока нет
  • Hash Tables
    Hash Tables
    Документ30 страниц
    Hash Tables
    dastanktl26
    Оценок пока нет
  • Group 15 Hash Tables
    Group 15 Hash Tables
    Документ42 страницы
    Group 15 Hash Tables
    reagan oloya
    Оценок пока нет
  • Presentation 1 PPT Computer Club
    Presentation 1 PPT Computer Club
    Документ23 страницы
    Presentation 1 PPT Computer Club
    Sayan Chandra
    Оценок пока нет
  • 3 Hashing
    3 Hashing
    Документ20 страниц
    3 Hashing
    Shahnawaz Khan
    Оценок пока нет
  • Module5 Hashing
    Module5 Hashing
    Документ14 страниц
    Module5 Hashing
    Anver S R
    Оценок пока нет
  • Hashing and Hash Tables
    Hashing and Hash Tables
    Документ23 страницы
    Hashing and Hash Tables
    Andre Laurent
    Оценок пока нет
  • Hash Functions
    Hash Functions
    Документ26 страниц
    Hash Functions
    GANESH G 111905006
    Оценок пока нет
  • 8 Hashtables
    8 Hashtables
    Документ84 страницы
    8 Hashtables
    Taha Çakmak
    Оценок пока нет
  • Unit29 Hashing2
    Unit29 Hashing2
    Документ20 страниц
    Unit29 Hashing2
    Nikila Narayanan
    Оценок пока нет
  • L05 PDF
    L05 PDF
    Документ4 страницы
    L05 PDF
    Marcos Shepard
    Оценок пока нет
  • 9.map 1 HashTable
    9.map 1 HashTable
    Документ31 страница
    9.map 1 HashTable
    Gaith Rjoub
    Оценок пока нет
  • Hash Table
    Hash Table
    Документ24 страницы
    Hash Table
    Ram C. Gudavalli
    Оценок пока нет
  • Hashing 1
    Hashing 1
    Документ26 страниц
    Hashing 1
    f180183 Talha Shakil
    Оценок пока нет
  • Aps Collision Handling Schemes
    Aps Collision Handling Schemes
    Документ13 страниц
    Aps Collision Handling Schemes
    Pradeep Shockerzz
    Оценок пока нет
  • c11 Hashing
    c11 Hashing
    Документ9 страниц
    c11 Hashing
    gowtham sai
    Оценок пока нет
  • Lecture 5: Linear Sorting: Review
    Lecture 5: Linear Sorting: Review
    Документ5 страниц
    Lecture 5: Linear Sorting: Review
    Van Vy Hong
    Оценок пока нет
  • Lecture 14 Hashing
    Lecture 14 Hashing
    Документ44 страницы
    Lecture 14 Hashing
    Muhammad Abubakar
    Оценок пока нет
  • Unit IV Hashing and Set 9
    Unit IV Hashing and Set 9
    Документ8 страниц
    Unit IV Hashing and Set 9
    Jasmine Mary
    Оценок пока нет
  • Hashingclass 141012005012 Conversion Gate01
    Hashingclass 141012005012 Conversion Gate01
    Документ19 страниц
    Hashingclass 141012005012 Conversion Gate01
    jai Lakhinana
    Оценок пока нет
  • Topic 6 Hashing
    Topic 6 Hashing
    Документ31 страница
    Topic 6 Hashing
    Haire Kahfi Maa Takaful
    Оценок пока нет
  • Hashing
    Hashing
    Документ34 страницы
    Hashing
    Amisha Shetty
    Оценок пока нет
  • DSA MK Lect2 PDF
    DSA MK Lect2 PDF
    Документ92 страницы
    DSA MK Lect2 PDF
    Ankit Priyarup
    Оценок пока нет
  • 11 Hashtable-1
    11 Hashtable-1
    Документ48 страниц
    11 Hashtable-1
    farhan khan
    Оценок пока нет
  • Lec 13 - Hashing
    Lec 13 - Hashing
    Документ43 страницы
    Lec 13 - Hashing
    Muznah Adnan Sheikh
    Оценок пока нет
  • DS Lecture - 6 (Hashing)
    DS Lecture - 6 (Hashing)
    Документ32 страницы
    DS Lecture - 6 (Hashing)
    Lamia Alam
    Оценок пока нет
  • DS Lecture - 6 (Hashing)
    DS Lecture - 6 (Hashing)
    Документ27 страниц
    DS Lecture - 6 (Hashing)
    Noman Mirza
    Оценок пока нет
  • Practical # 2
    Practical # 2
    Документ69 страниц
    Practical # 2
    yumna
    Оценок пока нет
  • CC-Lec 4
    CC-Lec 4
    Документ40 страниц
    CC-Lec 4
    Ch Salman
    Оценок пока нет
  • 2 - Lists-Note Version
    2 - Lists-Note Version
    Документ135 страниц
    2 - Lists-Note Version
    Rakibul Islam
    Оценок пока нет
  • Chapter One - Hashing PDF
    Chapter One - Hashing PDF
    Документ30 страниц
    Chapter One - Hashing PDF
    Mebratu Asrat
    Оценок пока нет
  • CHAPTER 8 Hashing: Instructors: C. Y. Tang and J. S. Roger Jang
    CHAPTER 8 Hashing: Instructors: C. Y. Tang and J. S. Roger Jang
    Документ78 страниц
    CHAPTER 8 Hashing: Instructors: C. Y. Tang and J. S. Roger Jang
    gunjan1920
    Оценок пока нет
  • Hashing
    Hashing
    Документ35 страниц
    Hashing
    Adhara Mukherjee
    Оценок пока нет
  • Hashing
    Hashing
    Документ35 страниц
    Hashing
    Adhara Mukherjee
    Оценок пока нет
  • Hash Table
    Hash Table
    Документ29 страниц
    Hash Table
    Elamparithi Maniezhilan
    Оценок пока нет
  • 03 Hash Tables
    03 Hash Tables
    Документ39 страниц
    03 Hash Tables
    Hien Pham
    Оценок пока нет
  • Chapter 8 - Hashing
    Chapter 8 - Hashing
    Документ78 страниц
    Chapter 8 - Hashing
    sarvaang
    Оценок пока нет
  • Dsad L8
    Dsad L8
    Документ62 страницы
    Dsad L8
    Babuji Baskaran
    Оценок пока нет
  • IT Note Check
    IT Note Check
    Документ2 страницы
    IT Note Check
    aurolia lakey
    Оценок пока нет
  • Hashing
    Hashing
    От Everand
    Hashing
    Оценок пока нет
  • Device Lock Manual
    Device Lock Manual
    Документ346 страниц
    Device Lock Manual
    Steve Whistlecraft
    Оценок пока нет
  • Chapter 09 HP RUM 9 Engine Console Tools
    Chapter 09 HP RUM 9 Engine Console Tools
    Документ20 страниц
    Chapter 09 HP RUM 9 Engine Console Tools
    Masood Khan
    Оценок пока нет
  • Steps For Simulating VHDL Programs in OrCAD
    Steps For Simulating VHDL Programs in OrCAD
    Документ8 страниц
    Steps For Simulating VHDL Programs in OrCAD
    pcjoshi02
    Оценок пока нет
  • Ricoh 010 b230 - b237 - d042 Troubleshooting Guide
    Ricoh 010 b230 - b237 - d042 Troubleshooting Guide
    Документ19 страниц
    Ricoh 010 b230 - b237 - d042 Troubleshooting Guide
    dwina roche
    Оценок пока нет
  • Memory Management in LINUX
    Memory Management in LINUX
    Документ16 страниц
    Memory Management in LINUX
    Shifan Demahom
    Оценок пока нет
  • Partman Manual
    Partman Manual
    Документ15 страниц
    Partman Manual
    Val Pope
    Оценок пока нет
  • Authenticated Key Exchange Protocols For Parallel PDF
    Authenticated Key Exchange Protocols For Parallel PDF
    Документ14 страниц
    Authenticated Key Exchange Protocols For Parallel PDF
    Ninad Samel
    Оценок пока нет
  • Video Watermark Thesis
    Video Watermark Thesis
    Документ145 страниц
    Video Watermark Thesis
    Alina Codreanu
    Оценок пока нет
  • Proctorial Report
    Proctorial Report
    Документ57 страниц
    Proctorial Report
    gopalankush
    100% (3)
  • Commands For An RUS (RS232)
    Commands For An RUS (RS232)
    Документ3 страницы
    Commands For An RUS (RS232)
    elesey424
    Оценок пока нет
  • Penetration Testing Methodology - 0DAYsecurity
    Penetration Testing Methodology - 0DAYsecurity
    Документ2 страницы
    Penetration Testing Methodology - 0DAYsecurity
    KenMiller
    Оценок пока нет
  • Fluke View Forms Manual
    Fluke View Forms Manual
    Документ36 страниц
    Fluke View Forms Manual
    Samsudin Ahmad
    Оценок пока нет
  • E SAPERION ReleaseNotes75
    E SAPERION ReleaseNotes75
    Документ13 страниц
    E SAPERION ReleaseNotes75
    shark
    Оценок пока нет
  • Softlogix Simulateur 1789-SIM
    Softlogix Simulateur 1789-SIM
    Документ8 страниц
    Softlogix Simulateur 1789-SIM
    lellb7
    Оценок пока нет
  • Set5 DocTech V2D5 en
    Set5 DocTech V2D5 en
    Документ43 страницы
    Set5 DocTech V2D5 en
    mail87523
    Оценок пока нет
  • Intro SDL Suite
    Intro SDL Suite
    Документ32 страницы
    Intro SDL Suite
    gsmman2006
    Оценок пока нет
  • Perl Programming: David Schweikert
    Perl Programming: David Schweikert
    Документ14 страниц
    Perl Programming: David Schweikert
    kartheekb
    Оценок пока нет
  • Unix by Rahul Singh3
    Unix by Rahul Singh3
    Документ8 страниц
    Unix by Rahul Singh3
    Prakash Waghmode
    Оценок пока нет
  • Factorytalk Linx Getting Results Guide
    Factorytalk Linx Getting Results Guide
    Документ91 страница
    Factorytalk Linx Getting Results Guide
    Ingeniero aic
    Оценок пока нет
  • Profile 1
    Profile 1
    Документ12 страниц
    Profile 1
    nelsonlbtn
    Оценок пока нет
  • GPIO Code Examples
    GPIO Code Examples
    Документ7 страниц
    GPIO Code Examples
    crsarin
    Оценок пока нет
  • Red Rose Children Academy: ANNUAL EXAM-2018-19 Time:-2Hrs Class 3 MM. 50
    Red Rose Children Academy: ANNUAL EXAM-2018-19 Time:-2Hrs Class 3 MM. 50
    Документ6 страниц
    Red Rose Children Academy: ANNUAL EXAM-2018-19 Time:-2Hrs Class 3 MM. 50
    Ash Goshwami
    Оценок пока нет
  • Priya Khare:: Careeer Objective
    Priya Khare:: Careeer Objective
    Документ3 страницы
    Priya Khare:: Careeer Objective
    Alim Ullah Hashmi
    Оценок пока нет
  • HTML Report &amp Other in Jmeter
    HTML Report &amp Other in Jmeter
    Документ46 страниц
    HTML Report &amp Other in Jmeter
    nitintyagi1984
    Оценок пока нет
  • BIS Configuration Manual EnUS 66256288779
    BIS Configuration Manual EnUS 66256288779
    Документ210 страниц
    BIS Configuration Manual EnUS 66256288779
    maykellpineda1697
    Оценок пока нет
  • PassLeader 70-461 Exam Dumps (151-End)
    PassLeader 70-461 Exam Dumps (151-End)
    Документ20 страниц
    PassLeader 70-461 Exam Dumps (151-End)
    andrei
    Оценок пока нет
  • CPLEX Optimization Modelling Using Python
    CPLEX Optimization Modelling Using Python
    Документ36 страниц
    CPLEX Optimization Modelling Using Python
    mesfin Demise
    Оценок пока нет