SlideShare una empresa de Scribd logo
1 de 37
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) 	      RDFS(1) 		RDFS(3)                                    RDFS(5)                                          RDFS(6)                                              visit 7 -> RDFS(7) Mark 6 as visited Mark Pred[6] Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) 	      RDFS(1) 		RDFS(3)                                    RDFS(5)                                          RDFS(6)                                              RDFS(7) -> Stop no more unvisited neighbors Mark 7 as visited Mark Pred[7] Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) 	      RDFS(1) 		RDFS(3)                                    RDFS(5)                                          RDFS(6) -> Stop Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) 	      RDFS(1) 		RDFS(3)                                    RDFS(5) -> Stop Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) 	      RDFS(1) 		RDFS(3) -> Stop Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) 	      RDFS(1) -> Stop Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) 	RDFS(9) -> Stop Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 )         RDFS(8) -> Stop Recursivecalls
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) -> Stop Recursivecalls
Example 0 8 2 9 1 7 3 6 4 5 Adjacency List Visited Table (T/F) source Pred Check our paths, does DFS find valid paths? Yes. Try some examples. Path(0) -> Path(6) -> Path(7) ->
Time Complexity of DFS(Using adjacency list) We never visited a vertex more than once We had to examine all edges of the vertices We know Σvertex v degree(v) = 2m  where m is the number of edges So, the running time of DFS is proportional to the number of edges and number of vertices (same as BFS) O(n + m) You will also see this written as: O(|v|+|e|)		|v| = number of vertices (n)			 		|e| = number of edges   (m)
DFS Tree Resulting DFS-tree. Notice it is much “deeper” than the BFS tree. Captures the structure of the recursive calls ,[object Object]
 whenever DFS returns from a vertex v, we climb up in the tree from v to its parent,[object Object]
Hash table ,[object Object]
Find
Insert
Delete. (deletions may be unnecessary in some applications)
Unlike binary search tree, AVL tree and B+-tree, the following functions cannot be done:
Minimum and maximum
Successor and predecessor
Report data within a given range
List out the data in order,[object Object]
Unrealistic solution insert, delete and find all take O(1) (worst-case) time Problem: The scheme wastes too much space if the universe is too large compared with the actual number of elements to be stored.  E.g. student IDs are 8-digit integers, so the universe size is 108, but we only have about 7000 students
Hashing Usually, m << N. h(Ki) = an integer in [0, …, m-1] called the hash value of Ki
Example applications Compilers use hash tables (symbol table) to keep track of declared variables. On-line spell checkers.  After prehashing the entire dictionary, one can check each word in constant time and print out the misspelled word in order of their appearance in the document. Useful in applications when the input keys come in sorted order.  This is a bad case for binary search tree.  AVL tree and B+-tree are harder to implement and they are not necessarily more efficient.
Hashing ,[object Object]
h: hash function
maps the universe U of keys into the slots of a hash table T[0,1,...,m-1]
an element of key k hashes to slot h(k)
h(k) is the hash value of key k,[object Object]
two keys may hash to the same slot
can we ensure that any two distinct keys get different cells?
No, if |U|>m, where m is the size of the hash table
Design a good hash function
that is fast to compute and
can minimize the number of collisions
Design a method to resolve the collisions when they occur,[object Object]

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Hash table
Hash tableHash table
Hash table
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Hashing data
Hashing dataHashing data
Hashing data
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Hashing
HashingHashing
Hashing
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Hashing gt1
Hashing gt1Hashing gt1
Hashing gt1
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Hashing 1
Hashing 1Hashing 1
Hashing 1
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Hash function
Hash functionHash function
Hash function
 
Hashing
HashingHashing
Hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 

Destacado

Trendi zold eletrevalo
Trendi zold eletrevaloTrendi zold eletrevalo
Trendi zold eletrevaloSimArp
 
Tekertanultulelvidam 4
Tekertanultulelvidam 4Tekertanultulelvidam 4
Tekertanultulelvidam 4SimArp
 
Megujulo energiavala szegenysegellen_11feb2
Megujulo energiavala szegenysegellen_11feb2Megujulo energiavala szegenysegellen_11feb2
Megujulo energiavala szegenysegellen_11feb2SimArp
 
Mersz ime 11maj07
Mersz ime 11maj07Mersz ime 11maj07
Mersz ime 11maj07SimArp
 
A kerekpar a_jovo_utja_alap
A kerekpar a_jovo_utja_alapA kerekpar a_jovo_utja_alap
A kerekpar a_jovo_utja_alapSimArp
 
Webdesign idepac
Webdesign idepacWebdesign idepac
Webdesign idepacJesJosSan
 

Destacado (7)

Trendi zold eletrevalo
Trendi zold eletrevaloTrendi zold eletrevalo
Trendi zold eletrevalo
 
Tekertanultulelvidam 4
Tekertanultulelvidam 4Tekertanultulelvidam 4
Tekertanultulelvidam 4
 
Ode to nature
Ode to natureOde to nature
Ode to nature
 
Megujulo energiavala szegenysegellen_11feb2
Megujulo energiavala szegenysegellen_11feb2Megujulo energiavala szegenysegellen_11feb2
Megujulo energiavala szegenysegellen_11feb2
 
Mersz ime 11maj07
Mersz ime 11maj07Mersz ime 11maj07
Mersz ime 11maj07
 
A kerekpar a_jovo_utja_alap
A kerekpar a_jovo_utja_alapA kerekpar a_jovo_utja_alap
A kerekpar a_jovo_utja_alap
 
Webdesign idepac
Webdesign idepacWebdesign idepac
Webdesign idepac
 

Similar a Presentation1

Similar a Presentation1 (20)

Hashing
HashingHashing
Hashing
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
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
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
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
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 
LISP: Introduction To Lisp
LISP: Introduction To LispLISP: Introduction To Lisp
LISP: Introduction To Lisp
 
Lec5
Lec5Lec5
Lec5
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
03.01 hash tables
03.01 hash tables03.01 hash tables
03.01 hash tables
 
Lec4
Lec4Lec4
Lec4
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 

Más de Saurabh Mishra (8)

Sorting2
Sorting2Sorting2
Sorting2
 
Sorting
SortingSorting
Sorting
 
Searching
SearchingSearching
Searching
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graphs
GraphsGraphs
Graphs
 
Data structures
Data structuresData structures
Data structures
 
Trees
TreesTrees
Trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 

Último

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Último (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Presentation1

  • 1. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) visit 7 -> RDFS(7) Mark 6 as visited Mark Pred[6] Recursivecalls
  • 2. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) RDFS(7) -> Stop no more unvisited neighbors Mark 7 as visited Mark Pred[7] Recursivecalls
  • 3. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) -> Stop Recursivecalls
  • 4. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) -> Stop Recursivecalls
  • 5. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) -> Stop Recursivecalls
  • 6. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) -> Stop Recursivecalls
  • 7. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) RDFS(9) -> Stop Recursivecalls
  • 8. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) RDFS(8) -> Stop Recursivecalls
  • 9. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred RDFS( 2 ) -> Stop Recursivecalls
  • 10. Example 0 8 2 9 1 7 3 6 4 5 Adjacency List Visited Table (T/F) source Pred Check our paths, does DFS find valid paths? Yes. Try some examples. Path(0) -> Path(6) -> Path(7) ->
  • 11. Time Complexity of DFS(Using adjacency list) We never visited a vertex more than once We had to examine all edges of the vertices We know Σvertex v degree(v) = 2m where m is the number of edges So, the running time of DFS is proportional to the number of edges and number of vertices (same as BFS) O(n + m) You will also see this written as: O(|v|+|e|) |v| = number of vertices (n) |e| = number of edges (m)
  • 12.
  • 13.
  • 14.
  • 15. Find
  • 17. Delete. (deletions may be unnecessary in some applications)
  • 18. Unlike binary search tree, AVL tree and B+-tree, the following functions cannot be done:
  • 21. Report data within a given range
  • 22.
  • 23. Unrealistic solution insert, delete and find all take O(1) (worst-case) time Problem: The scheme wastes too much space if the universe is too large compared with the actual number of elements to be stored. E.g. student IDs are 8-digit integers, so the universe size is 108, but we only have about 7000 students
  • 24. Hashing Usually, m << N. h(Ki) = an integer in [0, …, m-1] called the hash value of Ki
  • 25. Example applications Compilers use hash tables (symbol table) to keep track of declared variables. On-line spell checkers. After prehashing the entire dictionary, one can check each word in constant time and print out the misspelled word in order of their appearance in the document. Useful in applications when the input keys come in sorted order. This is a bad case for binary search tree. AVL tree and B+-tree are harder to implement and they are not necessarily more efficient.
  • 26.
  • 28. maps the universe U of keys into the slots of a hash table T[0,1,...,m-1]
  • 29. an element of key k hashes to slot h(k)
  • 30.
  • 31. two keys may hash to the same slot
  • 32. can we ensure that any two distinct keys get different cells?
  • 33. No, if |U|>m, where m is the size of the hash table
  • 34. Design a good hash function
  • 35. that is fast to compute and
  • 36. can minimize the number of collisions
  • 37.
  • 38. h(k) = k mod m
  • 39.
  • 40. e.g. if m=2p, then h(k) is just the p lowest-order bits of k; the hash function does not depend on all the bits
  • 41. Similarly, if the keys are decimal numbers, should not set m to be a power of 10
  • 42. It’s a good practice to set the table size m to be a prime number
  • 43. Good values for m: primes not too close to exact powers of 2
  • 44. e.g. the hash table is to hold 2000 numbers, and we don’t mind an average of 3 numbers being hashed to the same entry
  • 45.
  • 46. Most hash functions assume that the keys are natural numbers
  • 47. if keys are not natural numbers, a way must be found to interpret them as natural numbers
  • 49. Add up the ASCII values of the characters in the string
  • 51. Different permutations of the same set of characters would have the same hash value
  • 52.
  • 53. If the first 3 characters are random and the table size is 10,0007 => a reasonably equitable distribution
  • 55. English is not random
  • 56. Only 28 percent of the table can actually be hashed to (assuming a table size of 10,007)
  • 59. involves all characters in the key and be expected to distribute wella,…,z and space 272
  • 60. Collision Handling: (1) Separate Chaining Instead of a hash table, we use a table of linked list keep a linked list of keys that hash to the same value h(K) = K mod 10
  • 61.
  • 62. Compute h(K) to determine which list to traverse
  • 63. If T[h(K)] contains a null pointer, initiatize this entry to point to a linked list that contains K alone.
  • 64. If T[h(K)] is a non-empty list, we add K at the beginning of this list.
  • 65. To delete a key K
  • 66.
  • 67. Therefore, we expect that each search, insertion, and deletion can be done in constant time.
  • 68. Disadvantage: Memory allocation in linked list manipulation will slow down the program.
  • 69.
  • 70. relocate the key K to be inserted if it collides with an existing key. That is, we store K at an entry different from T[h(K)].
  • 72. what is the relocation scheme?
  • 73. how to search for K later?
  • 74. Three common methods for resolving a collision in open addressing
  • 77.
  • 78. Linear Probing f(i) =i cells are probed sequentially (with wraparound) hi(K) = (hash(K) + i) mod m Insertion: Let K be the new key to be inserted. We compute hash(K) For i = 0 to m-1 compute L = ( hash(K) + I ) mod m T[L] is empty, then we put K there and stop. If we cannot find an empty entry to put K, it means that the table is full and we should report an error.
  • 79. Linear Probing hi(K) = (hash(K) + i) mod m E.g, inserting keys 89, 18, 49, 58, 69 with hash(K)=K mod 10 To insert 58, probe T[8], T[9], T[0], T[1] To insert 69, probe T[9], T[0], T[1], T[2]
  • 80. Primary Clustering We call a block of contiguously occupied table entries a cluster On the average, when we insert a new key K, we may hit the middle of a cluster. Therefore, the time to insert K would be proportional to half the size of a cluster. That is, the larger the cluster, the slower the performance. Linear probing has the following disadvantages: Once h(K) falls into a cluster, this cluster will definitely grow in size by one. Thus, this may worsen the performance of insertion in the future. If two cluster are only separated by one entry, then inserting one key into a cluster can merge the two clusters together. Thus, the cluster size can increase drastically by a single insertion. This means that the performance of insertion can deteriorate drastically after a single insertion. Large clusters are easy targets for collisions.
  • 81. Quadratic Probing f(i) = i2 hi(K) = ( hash(K) + i2 ) mod m E.g., inserting keys 89, 18, 49, 58, 69 withhash(K) = K mod 10 To insert 58, probe T[8], T[9], T[(8+4) mod 10] To insert 69, probe T[9], T[(9+1) mod 10], T[(9+4) mod 10]
  • 82.
  • 84. probe sequence for k1: 30,30+1, 30+4, 30+9
  • 85. probe sequence for k2: 29, 29+1, 29+4, 29+9
  • 86. If the table size is prime, then a new key can always be inserted if the table is at least half empty (see proof in text book)
  • 88. Keys that hash to the same home position will probe the same alternative cells
  • 89. Simulation results suggest that it generally causes less than an extra half probe per search
  • 90.
  • 91. Double Hashing hi(K) = ( hash(K) + f(i) ) mod m; hash(K) = K mod m f(i) = i * hash2(K); hash2(K) = R - (K mod R), Example: m=10, R = 7 and insert keys 89, 18, 49, 58, 69 To insert 49, hash2(49)=7, 2nd probe is T[(9+7) mod 10] To insert 58, hash2(58)=5, 2nd probe is T[(8+5) mod 10] To insert 69, hash2(69)=1, 2nd probe is T[(9+1) mod 10]
  • 92.
  • 93. For any key K, hash2(K) must be relatively prime to the table size m. Otherwise, we will only be able to examine a fraction of the table entries.
  • 94. E.g.,if hash(K) = 0 and hash2(K) = m/2, then we can only examine the entries T[0], T[m/2], and nothing else!
  • 95.
  • 96.