西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)03 Hashing

General Idea Purpose:Design a kind of tables that can perform insertions,deletions,and finds in constant average time. In this chapter,we discuss the Hash table ADT. The ideal hash table data structure is merely an array of some fixed size,containing the keys. Typically,a key is a string with an associated value (for instance,salary information)
General Idea ◼ Purpose: Design a kind of tables that can perform insertions, deletions, and finds in constant average time. ◼ In this chapter, we discuss the Hash table ADT. ◼ The ideal hash table data structure is merely an array of some fixed size, containing the keys. ◼ Typically, a key is a string with an associated value (for instance, salary information)

General Idea The implementation of hash tab/es is frequently called hashing. We will refer to the table size as Tab/eSize. The common convention is to have the table run from 0 to Tab/eSize-1
General Idea ◼ The implementation of hash tables is frequently called hashing. ◼ We will refer to the table size as TableSize. ◼ The common convention is to have the table run from 0 to TableSize-1

General Idea Each key is mapped into some number in the range 0 to Tab/eSize-1 and placed in the appropriate cell. ■ The mapping is called a hash function,which ideally should be simple to compute and should ensure that any two distinct keys get different cells. ■ Since there are a finite number of cells and a virtually inexhaustible supply of keys,this is clearly impossible, and thus we seek a hash function that distributes the keys evenly among the cells
General Idea ◼ Each key is mapped into some number in the range 0 to TableSize-1 and placed in the appropriate cell. ◼ The mapping is called a hash function, which ideally should be simple to compute and should ensure that any two distinct keys get different cells. ◼ Since there are a finite number of cells and a virtually inexhaustible supply of keys, this is clearly impossible, and thus we seek a hash function that distributes the keys evenly among the cells

General Idea 0 1 John 25000 2 3 Phil 45000 4 5 6 Dave 15000 7Mary35000 ■ The only remaining problems deal with choosing a function,deciding what to do when two keys hash to the same value (this is known as a collision),and deciding on the table size
General Idea ◼ The only remaining problems deal with choosing a function, deciding what to do when two keys hash to the same value (this is known as a collision), and deciding on the table size. 0 1 John 25000 2 3 Phil 45000 4 5 6 Dave 15000 7 Mary 35000

Hash Function If the input keys are integers,when simply returning (Key mod Tab/eSize)is generally a reasonable strategy,unless Key happens to have some undesirable properties. In this case,the choice of hash function needs to be carefully considered. For instance,if the table size is 10 and the keys all end in zero,then the standard hash function is a bad choice
Hash Function ◼ If the input keys are integers, when simply returning (Key mod TableSize) is generally a reasonable strategy, unless Key happens to have some undesirable properties. ◼ In this case, the choice of hash function needs to be carefully considered. ◼ For instance, if the table size is 10 and the keys all end in zero, then the standard hash function is a bad choice

Hash Function To avoid situations like the one above,it is usually a good idea to ensure that the table size is prime. When the input keys are random integers,then this function is not only very simple to compute but also distributes the keys evenly
Hash Function ◼ To avoid situations like the one above, it is usually a good idea to ensure that the table size is prime. ◼ When the input keys are random integers, then this function is not only very simple to compute but also distributes the keys evenly

Hash Function Usually,the keys are strings;in this case,the hash function needs to be chosen carefully. One option is to add up the ASCII values of the characters in the string. int Hash(char *Key,int TableSize) unsigned int HashVal=0; while(*Key!=O') HashVal+=*Key++; return HashVal%TableSize;
Hash Function ◼ Usually, the keys are strings; in this case, the hash function needs to be chosen carefully. ◼ One option is to add up the ASCII values of the characters in the string. int Hash(char *Key, int TableSize) { unsigned int HashVal=0; while (*Key!=‘\0’) HashVal+=*Key++; return HashVal%TableSize; }

Hash Function However,if the table size is large,the function does not distribute the keys well. For instance,suppose that 7ab/eSize=10007 (10007 is a prime number),and all keys are eight or fewer characters long. ■ Since a charhas an integer value that is always at most 127,the hash function can only assume values between 0 and 1016,which is 127*8. This is clearly not an equitable distribution!
Hash Function ◼ However, if the table size is large, the function does not distribute the keys well. ◼ For instance, suppose that TableSize=10007 (10007 is a prime number), and all keys are eight or fewer characters long. ◼ Since a char has an integer value that is always at most 127, the hash function can only assume values between 0 and 1016, which is 127*8. ◼ This is clearly not an equitable distribution!

Hash Function int Hash(char *Key,int TableSize) return (Key[0]+27*Key[1]+729*Key[2])TableSize; This hash function assumes that Key has at least two characters plus the NULL terminator. The value 27 represents the number of letters in the English alphabet,plus the blank,and 729 is 272. This function examines only the first three characters, but if these are random and the table size is 10,007, as before,then we would expect a reasonably equitable distribution
Hash Function ◼ This hash function assumes that Key has at least two characters plus the NULL terminator. ◼ The value 27 represents the number of letters in the English alphabet, plus the blank, and 729 is 272 . ◼ This function examines only the first three characters, but if these are random and the table size is 10,007, as before, then we would expect a reasonably equitable distribution. int Hash(char *Key, int TableSize) { return (Key[0]+27*Key[1]+729*Key[2]) % TableSize; }

Hash Function Unfortunately,English is not random. Although there are 263=17,576 possible combinations of three characters (ignoring blanks),a check of a reasonably large on-line dictionary reveals that the number of different combinations is actually only 2,851. Even if non of these combinations collide,only 28 percent of the table can actually be hashed to
Hash Function ◼ Unfortunately, English is not random. ◼ Although there are 263=17,576 possible combinations of three characters (ignoring blanks), a check of a reasonably large on-line dictionary reveals that the number of different combinations is actually only 2,851. ◼ Even if non of these combinations collide, only 28 percent of the table can actually be hashed to
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)08 Algorithm Design Techniques.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)07 Graph Algorithms.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)06 Algorithm Analysis and Sorting.ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Iterative Improvement for Domain-Specific Problems(Chapter 16 Network Flow Chapter 17 Matching).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Coping with Hardness(Chapter 13 Backtracking Chapter 14 Randomized Algorithms Chapter 15 Approximation Algorithms).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)First-Cut Techniques(Chapter 8 The Greedy Approach Chapter 9 Graph Traversal).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Techniques Based on Recursion(Chapter 5 Induction Chapter 6 Divide and Conquer Chapter 7 Dynamic Programming).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Chapter 01 Basic Concepts in Algorithmic Analysis(主讲:刘静).ppt
- 《计算机基础》课程教学资源(参考论文)Motif Difficulty(MD):A Predictive Measure of Problem Difficulty for Evolutionary Algorithms using Network Motifs.pdf
- 《计算机基础》课程教学资源(参考论文)An Organizational Evolutionary Algorithm for Numerical Optimization.pdf
- 《计算机基础》课程教学资源(参考论文)A Multiagent Evolutionary Algorithm for Constraint Satisfaction Problems.pdf
- 《计算机基础》课程教学资源(参考论文)Comments on “the 1993 DIMACS Graph Coloring Challenge” and “Energy Function-Based Approaches to Graph Coloring”.pdf
- 《计算机基础》课程教学资源(参考论文)Moving Block Sequence and Organizational Evolutionary Algorithm for General Floorplanning with Arbitrarily Shaped Rectilinear Blocks.pdf
- 《计算机基础》课程教学资源(参考论文)A Multi-Agent Genetic Algorithm for Global Numerical Optimization.pdf
- 《计算机基础》课程教学资源(参考论文)An Organizational Coevolutionary Algorithm for Classification.pdf
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 12,14,15 Lecture_Computer Software(2/2).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 10-11 Lecture_Computer Software(1/2).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 1-5 Lecture_Computer Hardware(主讲:刘静).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 6-8 Lecture_Computer Codes.ppt
- 中国科学技术大学:《计算机网络 Computer Networks(计算机通信网)》课程教学资源(PPT课件讲稿,2022)Chapter 04 局域网与介质访问控制(卢汉成).pptx
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)04 Priority Queues(Heaps).ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)05 Disjoint Set ADT.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)02 Trees(主讲:刘静).ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)01 Lists, Stacks, and Queues.ppt
- 中国科学技术大学:Robust Speech Recognition with Speech Enhanced Deep Neural Networks.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第九章 机器无关的优化.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第八章 代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第七章 运行时刻环境.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第六章 中间代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第五章 语法制导的翻译.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第四章 语法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第三章 词法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第一章 引论(许畅).pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)编译课程复习(许畅).pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验一 词法分析与语法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验二 语义分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验三 中间代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验四 目标代码生成.pdf
- 《编译原理 Principles and Techniques of Compilers》课程教学资源(学习资料)Assemblers,Linkers,and the SPIM Simulator(MIPS32 and SPIM).pdf
- 南京大学:《软件工程导论 Introduction to Software Engineering Research》课程教学电子教案(课件讲义)04 Conduct Rigorous and Scientific Research(Experiment Design in Software Engineering Research).pdf