Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Storage by HashingTravis RoeTopics of Computer ScienceChapter 432-5-2006OutlineA ProblemA Solution: HashingQuestionsQ & AA ProblemCompany organizing data using social security numbers, or similar.Need to add and search through collections of identifiers to find objects.Potential Solution: 1-1One index per potential locationAdding: O(1)Searching: O(1)Pros: Very fast, very easy to implementCons: Far too much memory, much of it unusedPotential Solution: Unsorted ArrayAdding. O(1)Searching. O(n)Pros: Easy to implement, fast adding.Cons: Everything else. O(n) ridiculously slow.Potential Solution: Sorted ArrayAdding. O(n)Searching. O(lg n)Pros: Fast searching.Cons: Slow adding.Potential Solution: Balanced BSTAdding. O(lg n)Searching. O(lg n)Pros: Fast speed for adding, searching.Cons: Hard to program. Not O(1).A New Solution: HashingAdding.Use the keys to choose an index.Place the object at the index.O(1)Searching.Use the keys to find the index.Get the object from that index.O(1)Hashing: An Example154-38-1287987-65-4321123-45-6789192-83-7465Social Security Numbers are the keyThe hash-key is based off the last 4 digits of the number.........1287432167897465CollisionsExpected problems:Two objects with the same keyTwo keys, after hashing, with same value.Ways to solve the problems:ChainingProbingCollision Handling: ChainingEvery node is a list of some sort.Whenever there is a collision, put the new item into the list.Collision Handling: ProbingWhenever there is a collision, go to another location some distance away and attempt to fill that location.Can cause grouping.h(k) + a * x; a = 2123-45-6789543-21-6789Reducing CollisionsUse prime numbers for array sizesTake more space than you'll needChoose a better hash functionReferencesDewdney, A.K. “Storage By Hashing”. The New Turing Omnibus. 1993. Computer Science Press.“Hash Tables”. Recording My Programming Path. http://qiang-ma.blogspot.com/2007/10/hash-tables.html <Accessed last 2-5-2008>Standish, Thomas. Data Structures, Algorithms & Software Principles in C. 1995. Addison-Wesley Publishing Company, Inc. pp450-475(ish)Questions1.What are the two methods for handling collisions that were discussed in this lecture?2.What is one situation where hash-tables are not useful
View Full Document