SlideShare ist ein Scribd-Unternehmen logo
Space and Time Tradeoffs (Hashing)




                                     1
Space and Time Tradeoffs
 Space and Time tradeoffs in algorithm design are a
 well-known issue .
    Example: computing values of a function at many points.


 One type of technique is to use extra space to
 facilitate faster and/or more flexible access to the
 data.
    This approach is called prestructuring.
    We illustrate this approach by Hashing.




                                                              2
Hashing
 A dictionary is a set that supports operations
 of searching, insertion, and deletion.
   Each element in the set contains a key and
   satellite data (the remainder of the record.)
   The keys are unique, but the satellite data are
   not.
 A hash table is an effective data structure for
 implementing dictionaries.
 Hashing is based on the idea of distributing
 keys among an one-dimensional array.

                                                     3
Direct-address Tables
 Suppose that an application needs a dynamic set in
 which each element has a key drawn from the
 Universe U = {0, 1, …, m-1}, where m is not too
 large. Denote direct-address table by T[0..m-1], in
 which each position, or slot, corresponds to a key in
 the universe U.
 Operations
    DIRECT-ADDRESS-SEARCH(T, k)          O(1)
     Return T[k]
    DIRECT-ADDRESS-INSERT(T, x)          O(1)
     T[key[x]]   x
    DIRECT-ADDRESS-DELETE(T, x)          O(1)
     T[key[x]]   NIL
                                                     4
Hash Tables
A hash table is used when the set K of keys stored in
dictionary is much smaller than the universe U = {0,
1, …, n-1}, of all possible Keys.
  An example, the key space of strings of characters.
  Requires much less storage while search cost is still O(1).
An example of hash table
Direct addressing vs. Hashing
  Direct addressing: an element with key k is stored in slot k;
  Hashing: an element with k is stored in slot h(k), where h(k)
  is the hash function.




                                                                5
Hash Tables
Hash function assigns an integer between 0 and m-1,
called hash address, to a key.
   An example hash function: h(K) = K mod m
     Integer keys (example)
     Character keys: ord(K), the position of the key in the alphabet.
     Character string keys:
            s −1
          (∑ ord (c j )) mod m
            i =0

         ( ord(c s-1) Cs-1   + ord(c
                                       s-2)   Cs-2   + … + ord(c
                                                                   0)   C0 ) mod m
  Let m = 13, calculate the hash address of the following
  strings
         A, FOOL, AND, HIS, MONEY, ARE, SOON, PARTED


                                                                                     6
Hash Function
 A hash function needs to satisfy two
 requirements:
   Needs to distribute keys among the cells of
   the hash table as evenly as possible. (m is
   usually chosen to be prime)
   Has to be easy to compute.




                                            7
Collision and Resolution
 Collision: two keys hash to the same
 slot.
 Collision resolution by open hashing
 (separate chaining)
 Collision resolution by closed hashing
 (open addressing)


                                          8
Open Hashing (Separate Chaining)
  Put all the elements that hash to the same
  slot in a linked list.
     Example
  Dictionary Operations
     CHAINED-HASH-SEARCH(T, k)
      search for an element with key k in list T[h(k)]
     CHAINED-HASH-INSERT(T, x)                   O(1)
      insert x at the head of list T[h(key[x])]
     CHAINED-HASH-DELETE(T, x)
      search and delete x from the list T[h(key[x])]
Exercise
                                                     9
Cost of Search
 Load factor of the hash table
    α = n/m, where n is the number of keys and m is
    the number of slots in the hash table.
    Too small: waste of space but fast in search
    Too large: save space but slow in search
 The worst case O(n): all keys hash to the same slot
 The average case
    Average cost of a successful search: O(1 + α / 2)
    Average cost of an unsuccessful search: O(α)
    If n is about equal to m, O(1)


                                                       10
Closed Hashing (Open Address Hashing)


 Open address hashing
    a strategy for storing all elements right in the array of the hash
    table, rather than using linked lists to accommodate collisions.
    Assumption: (m >=n)
    The idea is that if the hash slot for a certain key is occupied by a
    different element, then a sequence of alternative locations for the
    current element is defined.
     For every key k, a probe sequence <h(k, 0), h(k, 1), …, h(k, m-1)>
    is generated so that when a collision occurs, we successively
    examine, or probe the hash table until we find an empty slot in
    which to put the key..
 Probing policies
       Linear probing
       Quadratic probing
       Double hashing

                                                                    11
Linear Probing
 Given an ordinary hash function: h’, an auxiliary hash function,
 the method of linear probing uses the hash function
 h(k, i) = (h’(k) + i) mod m, for i = 0, 1, …, m-1.
 Search
    Compare the given key with the key in the probed position until
    either the key is found or an empty slot is encountered.
 An example
 The problem with deletion and the solution
    Lazy deletion: mark the previously occupied locations as “obsolete”
    to distinguish them from locations that have not been occupied.
 Advantage & Disadvantage:
    Easy to implement
    but when the load factor approaches 1, it suffers from clustering:
    Long runs of occupied slots build up, increasing the average search
    time.
 Exercise
                                                                   12
Quadratic Probing
 Given an ordinary hash function: h’, an auxiliary hash
 function, the method of quadratic probing uses the
 hash function
 h(k, i) = (h’(k) + c1i + c2i2) mod m,
 where i = 0, 1, …, m-1, c1 and c2 ‡ 0.

 Advantage & Disadvantage:
    Easy to implement
    It suffers from a milder form clustering: If two keys have the
    same initial probe position, then their probe sequences are
    the same.


                                                             13
Double Hashing
 Given two auxiliary hash functions: h1 and h2,
 double hashing uses the hash function
 h(k, i) = (h1(k) + ih2(k)) mod m,
 where i = 0, 1, …, m-1.
 An example
 One of the best methods available for open
 addressing.




                                                  14

Weitere ähnliche Inhalte

Was ist angesagt?

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables Nifras Ismail
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2SHAKOOR AB
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingSangeethaSasi1
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingzukun
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 

Was ist angesagt? (20)

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Lec5
Lec5Lec5
Lec5
 
Hashing
HashingHashing
Hashing
 
Hash table
Hash tableHash table
Hash table
 
Hashing
HashingHashing
Hashing
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hashing
HashingHashing
Hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hashing
HashingHashing
Hashing
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashing
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Hashing
HashingHashing
Hashing
 
Lec8
Lec8Lec8
Lec8
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Hashing
HashingHashing
Hashing
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 

Ähnlich wie Algorithm chapter 7

Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of techniclokaprasaadvs
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Design data Analysis hashing.ppt by piyush
Design  data Analysis hashing.ppt by piyushDesign  data Analysis hashing.ppt by piyush
Design data Analysis hashing.ppt by piyush22001003058
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxjainaaru59
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and SetIntro C# Book
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingBenjamin Sach
 

Ähnlich wie Algorithm chapter 7 (20)

13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of technic
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Lec5
Lec5Lec5
Lec5
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
03.01 hash tables
03.01 hash tables03.01 hash tables
03.01 hash tables
 
Lec4
Lec4Lec4
Lec4
 
Design data Analysis hashing.ppt by piyush
Design  data Analysis hashing.ppt by piyushDesign  data Analysis hashing.ppt by piyush
Design data Analysis hashing.ppt by piyush
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
session 15 hashing.pptx
session 15   hashing.pptxsession 15   hashing.pptx
session 15 hashing.pptx
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect Hashing
 

Mehr von chidabdu

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffmanchidabdu
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphschidabdu
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsortschidabdu
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquerchidabdu
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysischidabdu
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitationschidabdu
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unitchidabdu
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organizationchidabdu
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8chidabdu
 

Mehr von chidabdu (20)

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphs
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8
 

Kürzlich hochgeladen

Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityScyllaDB
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FIDO Alliance
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessUXDXConf
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfFIDO Alliance
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfFIDO Alliance
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxEasyPrinterHelp
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101vincent683379
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeCzechDreamin
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaCzechDreamin
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024Stephanie Beckett
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2DianaGray10
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxJennifer Lim
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsUXDXConf
 

Kürzlich hochgeladen (20)

Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptx
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 

Algorithm chapter 7

  • 1. Space and Time Tradeoffs (Hashing) 1
  • 2. Space and Time Tradeoffs Space and Time tradeoffs in algorithm design are a well-known issue . Example: computing values of a function at many points. One type of technique is to use extra space to facilitate faster and/or more flexible access to the data. This approach is called prestructuring. We illustrate this approach by Hashing. 2
  • 3. Hashing A dictionary is a set that supports operations of searching, insertion, and deletion. Each element in the set contains a key and satellite data (the remainder of the record.) The keys are unique, but the satellite data are not. A hash table is an effective data structure for implementing dictionaries. Hashing is based on the idea of distributing keys among an one-dimensional array. 3
  • 4. Direct-address Tables Suppose that an application needs a dynamic set in which each element has a key drawn from the Universe U = {0, 1, …, m-1}, where m is not too large. Denote direct-address table by T[0..m-1], in which each position, or slot, corresponds to a key in the universe U. Operations DIRECT-ADDRESS-SEARCH(T, k) O(1) Return T[k] DIRECT-ADDRESS-INSERT(T, x) O(1) T[key[x]] x DIRECT-ADDRESS-DELETE(T, x) O(1) T[key[x]] NIL 4
  • 5. Hash Tables A hash table is used when the set K of keys stored in dictionary is much smaller than the universe U = {0, 1, …, n-1}, of all possible Keys. An example, the key space of strings of characters. Requires much less storage while search cost is still O(1). An example of hash table Direct addressing vs. Hashing Direct addressing: an element with key k is stored in slot k; Hashing: an element with k is stored in slot h(k), where h(k) is the hash function. 5
  • 6. Hash Tables Hash function assigns an integer between 0 and m-1, called hash address, to a key. An example hash function: h(K) = K mod m Integer keys (example) Character keys: ord(K), the position of the key in the alphabet. Character string keys: s −1 (∑ ord (c j )) mod m i =0 ( ord(c s-1) Cs-1 + ord(c s-2) Cs-2 + … + ord(c 0) C0 ) mod m Let m = 13, calculate the hash address of the following strings A, FOOL, AND, HIS, MONEY, ARE, SOON, PARTED 6
  • 7. Hash Function A hash function needs to satisfy two requirements: Needs to distribute keys among the cells of the hash table as evenly as possible. (m is usually chosen to be prime) Has to be easy to compute. 7
  • 8. Collision and Resolution Collision: two keys hash to the same slot. Collision resolution by open hashing (separate chaining) Collision resolution by closed hashing (open addressing) 8
  • 9. Open Hashing (Separate Chaining) Put all the elements that hash to the same slot in a linked list. Example Dictionary Operations CHAINED-HASH-SEARCH(T, k) search for an element with key k in list T[h(k)] CHAINED-HASH-INSERT(T, x) O(1) insert x at the head of list T[h(key[x])] CHAINED-HASH-DELETE(T, x) search and delete x from the list T[h(key[x])] Exercise 9
  • 10. Cost of Search Load factor of the hash table α = n/m, where n is the number of keys and m is the number of slots in the hash table. Too small: waste of space but fast in search Too large: save space but slow in search The worst case O(n): all keys hash to the same slot The average case Average cost of a successful search: O(1 + α / 2) Average cost of an unsuccessful search: O(α) If n is about equal to m, O(1) 10
  • 11. Closed Hashing (Open Address Hashing) Open address hashing a strategy for storing all elements right in the array of the hash table, rather than using linked lists to accommodate collisions. Assumption: (m >=n) The idea is that if the hash slot for a certain key is occupied by a different element, then a sequence of alternative locations for the current element is defined. For every key k, a probe sequence <h(k, 0), h(k, 1), …, h(k, m-1)> is generated so that when a collision occurs, we successively examine, or probe the hash table until we find an empty slot in which to put the key.. Probing policies Linear probing Quadratic probing Double hashing 11
  • 12. Linear Probing Given an ordinary hash function: h’, an auxiliary hash function, the method of linear probing uses the hash function h(k, i) = (h’(k) + i) mod m, for i = 0, 1, …, m-1. Search Compare the given key with the key in the probed position until either the key is found or an empty slot is encountered. An example The problem with deletion and the solution Lazy deletion: mark the previously occupied locations as “obsolete” to distinguish them from locations that have not been occupied. Advantage & Disadvantage: Easy to implement but when the load factor approaches 1, it suffers from clustering: Long runs of occupied slots build up, increasing the average search time. Exercise 12
  • 13. Quadratic Probing Given an ordinary hash function: h’, an auxiliary hash function, the method of quadratic probing uses the hash function h(k, i) = (h’(k) + c1i + c2i2) mod m, where i = 0, 1, …, m-1, c1 and c2 ‡ 0. Advantage & Disadvantage: Easy to implement It suffers from a milder form clustering: If two keys have the same initial probe position, then their probe sequences are the same. 13
  • 14. Double Hashing Given two auxiliary hash functions: h1 and h2, double hashing uses the hash function h(k, i) = (h1(k) + ih2(k)) mod m, where i = 0, 1, …, m-1. An example One of the best methods available for open addressing. 14