Broad search optimization techniques Summary

A basic realization

a. a first selected as the starting vertex node, and its gray dyed, remaining as white node.

b. Place the starting node into the queue. And find out all adjoining nodes.

c. selecting a vertex from the head of the queue, will find the adjacent node in the queue tail,

The nodes have been visited painted black. If the vertex color is gray, indicating that the queue has been placed,

If the vertex color is white, it has not yet discovered d. Follow the same method as the next node processing queue.

Basic is the team's apex turned black and gray in the queue, and the team is not white.

For example, from the vertex 1 start breadth-first search:

An initial state, starts from the vertex 1, queue 1} = {
access adjacent vertices 1, a black team, 2,3 enqueued, the queue = {2,3}
to access the adjacent node 2, two teams, 4 is enqueued, the queue = {3,4}
adjacent node visit 3, 3 dequeued queue = 4} {
access adjacent node 4, 4 dequeued queue empty = {1} to node 5 is unachievable.

II. Deque

The right side of the normal queue for searching the shortest given value.

And each is to reach optimal decisions (do not take min).

Because all states have a monotonic levels in accordance with the order into the team, every expansion, are out of step,

From the beginning to meet the optimality of the state (do not take the min / size ratio would not, if so lost its meaning).

Deque right side can search for the shortest 1/0.

For an edge from u to v, if the right side of this value is 0, it will be push_front (v), or push_back (k),

Each taking the first team, so we have to ensure that the monotonic (that is, each preference optimal).

Attention to detail: the corners of a grid connection points on the edge as if the right side and match the original status of 0, 1 otherwise.

We also put in the queue of u, so as to achieve will update every time out,

That is placed in tuple (u, v) in the queue. Complexity: O (r * c).

Three. Hash heavy sentence

1, wherein the hash table 

Deep in the search] [path finding problem can often be achieved by construction Hash table.

Hash code value is a key (Key value) to directly access a data structure.

To access the records by key values ​​are mapped to table a position to speed up the search.

This mapping function called a hash function, recording storage array is called a hash table.

Where f is called the correspondence between the hash function, also called a hash (Hash function),

The records are stored in a hash contiguous storage space, this storage space is called continuous hash or a hash table.

Key to the hash table is fixed by means of a function algorithm, i.e. a so-called hash function conversion into an integer number,

The numbers then modulo the length of the array, as a result take over the array index,

The value to be stored in the digital space for the next target array.

(Or: the input of any length (also known as pre-mapping), through a hash algorithm, converted into a fixed-length output,

The output is the hash value. This conversion is a compression map, i.e., the space hash value is typically much smaller than the input space,

Different inputs may hash to the same output, and is impossible to uniquely determine the value of the input from the hash value.

Simply means that the message of any length A to the compression function of the message digest of a fixed length. )

And when queried using a hash table, the hash function is to use again the key into the corresponding array index,

And the target value acquisition space, this way, can take full advantage of the capability array to locate the positioning data.

 

2, implementation of the hash table: the fastener Method

Features of the array is: Addressing easy, difficult insertions and deletions.

The list of features are: addressing difficult, insertions and deletions easy.

An addressing easy, and easy insertion and deletion of data structures: a hash table.

The most commonly used hash table implementation - the zipper method, we can be understood as "an array of linked list" as shown:

 

Obviously the left is an array, each member of the array includes a pointer to a head of the list,

According to some of the characteristic elements assigned to different elements in the list, and again based on these features, elements and find the correct list.

 

3, Hash applications

1, Hash mainly used in the field of information security encryption algorithms, some of the information which the different lengths of coding into 128,

These coded values ​​called the Hash value. Hash ie, find the mapping between a data content and data storage address.

2. Find: hash tables, also known as hash, look for a more efficient technology.

Before we find the idea: to come up with a set of elements, comparing, ranging from narrow to continue the search.

And the hash table is completely the other way around: the key value can be calculated directly know the position of the element in the collection.

3, Hash table is widely used in mass data processing.

Hash Table query speed is very fast, almost O (1) time complexity.

Q: If two strings corresponding position in the hash table how to do the same?

It can be converted to address list, each entry in the hash table as long as a linked list, all of the corresponding string stored on OK.


 

4, hashing

Wherein the subject element into the hashing method is an array. Here are three of the more common:

1, the division hashing 

Most intuitive one, the figure uses this hashing equation of the form: index = value% 16

2, square distributed column method

Seeking index is very frequent operation, division and multiplication is more than save time,

Therefore, we consider the division into a multiplication and shift operation. Formula: index = (value * value) >> 28   

(Right, divided by 2 ^ 28 notation: left larger, it is to take the right smaller, is in addition.)

3, Fibonacci (Fibonacci) hashing

Find an ideal multiplier, multiplier and not take it as a value in itself.

1) For 16-bit integer, this multiplier is 40503 
2) for 32-bit integers, the multiplier is 2654435769 
3) for the 64-bit integers, the multiplier is 11400714819323198485

These "ideal multiplier" is how to get out of it? This is followed by a relevant law, called the golden rule,

The golden rule describes the most classic expression is undoubtedly the famous Fibonacci columns,

I.e., the form of such a sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377, 610 ....

For our common 32-bit integers, the formula: index = (value * 2654435769) >> 28

If this Fibonacci hashing, then this becomes the top of the FIG:

Note: much better than the original modulo hashing after adjustment with the Fibonacci hashing.

 

[Common construction method Hash Table]

State compression method - binary recording state.
Direct modulo Method - select a prime number as a divisor M.
Middle-square method - calculates the square value of the key, taking the middle of r bits.
Folding Method - all the characters of the ASCII code together.
 

Guess you like

Origin www.cnblogs.com/xcg123/p/10992781.html