"Algorithm" Hash list

Source: Internet
Author: User

Hash list hash function definition

The hash function maps the input to a number. That is, whatever data you give it, it gives you a number.

Hash functions must meet some requirements

L It must be consistent . For example, suppose you get 4 when you enter Apple, and you have to get 4 each time you enter Apple.

L It should map different inputs to different numbers . For example, if a hash function returns 1 regardless of what the input is, it is not a good hash function. Ideally, you would map different inputs to different numbers.

The hash function accurately points to the storage location for the following reasons:

The L hash function always maps the same input to the same index.

The L hash function maps different inputs to different indexes.

The hash function knows how large the array is and returns only valid indexes.

Hash table

L use hash functions and arrays to create a data structure called a hash Table (hashtable) .

An L hash table is a data structure that contains additional logic. Arrays and linked lists are mapped directly to memory, but the hash table is more complex, and it uses a hash function to determine where the element is stored .

The hash list also uses arrays to store data, so it gets the same speed as the array

Python uses a dictionary for the hash table function, which can be used to create a hash table using the function dict

The app uses a hash table to find

Example:

L Create mappings.

L Find.

>>> phonebook={}>>> phonebook[' li ']=123456>>> phonebook[' P ']=987654>>> print ( phonebook[' P ']) 987654

Prevent duplication

Example: Check if an element exists

Voted={}def Check_voter (name):    if Voted.get (name):              #使用函数get来返回是否存在. Does not exist return none        print (' Kick them out! ')    else:        voted[name]=true        print (' Let them vote ') Check_voter (' Tom ') Check_voter (' Jerry ') Check_voter (' Jerry ')

Use a hash table as a cache

How caching Works : The Web site remembers the data and no longer recalculates it.

Caching benefits

Users can see the Web page more quickly

L need to do less work.

Caching is a common way of accelerating, and all large sites use caching, while cached data is stored in a hash table!

Access process

Cache={}def get_page (URL):    if Cache.get (URL):                  #检查缓存中是否存储了该页面 return        Cache[url]                  #存储了, that is, returning it    Els E:        data=get_data_from_server (URL)            #没存储, call Cache[url]=data from        server                           #将其存储到缓存中        return Data                               #返回该页面

  

Summary

The hash table is suitable for:

L Simulation mapping relationship;

l prevent duplication;

L Cache/Memorize data so that servers are not processed to generate them

Conflict (collision) definition

Assign the same location to the two keys.

How conflicts are handled

If two keys are mapped to the same location, a linked list is stored in this location.

Experience

L hash function is important . Avoid hashing functions that map all keys to a single location, and ideally, the hash function maps the keys evenly to different locations of the hash table.

If the list is stored in a long chain, the hash table will drop sharply . However, if the hash function used is good, these lists will not be very long!

Hash functions are important, and good hash functions rarely lead to collisions.

Performance

On average, the hash table performs various operations at an O (1)time. O (1) is called constant time .

Performance of the hash table

Operation

Average situation

Worst case scenario

Find

O (1)

O (N)

Insert

O (1)

O (N)

Delete

O (1)

O (N)

Hash table comparison with arrays and linked lists

O (n)

O (n)

valign= "Top" width= "_" >

action

Average

Worst case

Array

List

find

O (1)

O (n)

O (1)

O (n)

Insert

O (1)

O (n)

O (1)

Delete

O (1)

O (n)

O (1)

On average, the lookup of a hash table (getting the value at a given index) is as fast as an array, and the insertion and deletion speed is as fast as the list, so it combines the advantages of both!

But in the worst case, the various operations of the hash table are slow.

When using a hash table, it is important to avoid the worst situations. Need to have:

L Low filling factor

L Good hash function.

Calculation formula of filling factor

Fill factor for hash table = number of elements/positions in the hash table

Adjustment length (resizing)

A fill factor greater than 1 means that the number of elements exceeds the number of positions in the array.

Once the fill factor starts to increase, you need to add a location to the hash table, which is called the adjustment length (resizing)

Experience

The lower the filling factor, the less likely the conflict is, and the higher the performance of the hash table.

A good rule of thumb is that once the fill factor is greater than 0.7, the length of the hash table is adjusted .

Good hash function

A good hash function makes the values in the array evenly distributed .

Bad hash functions let the values get together, causing a lot of collisions.

Summary

L can combine hash functions and arrays to create a hash table.

L conflicts are bad, you should use hash functions that minimize conflicts .

The hash list is very fast to find, insert, and delete.

The L hash table is suitable for simulating mapping relationships .

• Once the fill factor exceeds 0.7, the length of the hash table should be adjusted .

The hash list can be used to cache data (for example, on a Web server).

The L hash table is ideal for preventing duplication .

"Algorithm" Hash list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.