Eight digital algorithm Summary

Eight digital

Eight digital JiuGongTu also called, is a classic problem in artificial intelligence search state in which the problem is described as: in a 3 × 3 chessboard, put eight pieces, each piece is marked with a 1-8 in a digital, different pieces on the subject of numbers are not the same. There is a space on the board, the pieces can be moved to a space adjacent the space provided. It is required to solve the problem: given an initial state and a target state, to find a transition from an initial state to the target minimum number of steps to move a pawn moving step.

This is a typical graph search problem, but the problem is not the need to establish a data structure to be solved, but the search method abstract figure, applied to the problem. The following is the analysis:

First, consider the problem of abstraction and coding algorithms. If space record to 0, all states on board a total of 9! =
362,880 kinds. We first have to find a way to encode the state of the board.

Readily occur visual representation of the state, using the encoded string, i.e., the flattened 3x3 squares is a length of the string 9, so that a string represents a state of the current to the entire digital disc, for example: status 123456780 expressed as follows:

1 2 3
4 5 6
7 8 0

Further, consider the transfer (changing) state, given only by a space and a number on the exchange, so the digital position from 0 to start considering a transition state, for example: The above state transition can be performed in both orientations, 8 and 0 is the swap, swap 0 and 6, illustrated as follows:

1 2 3
4 5 0
7 8 6

Transfer up mode

1 2 3
4 5 6
7 0 8

Shift to the left direction

This can be expanded from an initial state to a target state for the status of the search tree is as follows:

Picture 1 status of the search tree

Not difficult to see the depth of a search tree, is the current number of spaces are swapped.

Analysis of Algorithms

Enumeration state violence

If the space as 0, then the total number of states should be arranged in a number 0-9, namely 9! = 362,880 states, enumeration algorithm used is a binary, i.e. enumerates all meet the current state of arrangement in line with that part of the transfer request, whether to enumerate every possible next state, such algorithms are inefficient and or even can not be solved within a limited time.

  1. Depth-first search

Further reflection, considering the start transfer state conditions, and other digital mobile Alternatively 0 is equivalent to a position around its moved position 0 to be replaced, therefore, to each state transition from the position of 0 considerations. This defines the idea of ​​searching, further divided, the search can be divided into excellent search depth and breadth-first search.

For depth-first search, that is, the "current path of a mind to go in the end," until the current state matching your search criteria, or has been unable to continue to expand as new state suspended. Such a search is likely to stay for a long time on a deep branch, away from the initial distribution target for the search direction of the search tree, the efficiency is very low. Extended search state of the search tree shown below:

Picture 2 breadth-first search path

For this Example is: Path BFS words can be described as:

Root state (initial state 1)
  1. Access to the current node (the node 2) have not visited most of the left, and the node is not the target state.

  2. Access to the current node (the node 4) have not visited most of the left, and the node is not the target state.

  3. Access to the current node (the node sub-tree section 4) the most left unvisited, and the node is not the target state.

  4. No extension of the current node node, backtracking (back to Node 4)

  5. No extension of the current node node, backtracking (back to Node 2)

  6. Access to the current node (the node 5) have not visited most of the left, and the node is not the target state.

  7. ... (and so on)

  8. The current node is the target node, the search ends, the output current and the depth of the search tree.

  9. Otherwise, go back to the root node has been expanded and the search is less than the target state, out of the search.

Thus, it is clear to see, assuming that the search is the first search the left sub-tree, when the target state (solution) at the rightmost child node of the rightmost sub-tree, is very inefficient search efficiency, the number of searches is approximately equal to the number of permutations 9! = 362,880. And if the target state is located in the most left-most node of the left subtree, then the search will be a constant level, that come fall leaf nodes of the tree can output the optimal solution.

Pseudo code may be described as follows:

First, the root node into the stack.
  1. Take the first node from the stack, and verify that it is the goal.

If the target is found, the end of the search and return the results.

Otherwise it one of the direct child had not been checked in to join in the stack.

  1. Repeat Step 2.

  2. If undetected through the direct child node does not exist.

The upper-level node is added in the stack.

Repeat Step 2.

  1. Repeat step 4.

  2. If the stack is empty, it represents the entire map have checked - that is the figure did not want the search target. The end of the search and return "not find the target."

So, sum up, the depth-first search algorithm to improve the state of violence enumeration that time obtained within an acceptable period of time, but still have some questions, such as:

  1. Search time instability. Time search depends on the distribution of the target state, if luck is better, similar to the target state and the initial search direction and distribution of shallow in the trees, so you can quickly get the solution, if luck is poor, needs the way back, until it reaches the target state sub-tree is located, such efficiency is approximately equal to the number of permutations hard to enumerate the search space is enormous.

  2. More brute force algorithm. Careful analysis for each branch of the tree, the search to the target state is a certain probability that different, and these probabilities can be certain as to optimize information search and depth-first search is all for each branch equivalent believes its search to the target state probabilities are the same, this is obviously unreasonable, the lack of optimization.

Picture 3 insufficient depth-first search

BFS

Breadth-first search is a search strategy in other big branch, the idea is no longer "fall away in the end", but "Pan expansion." Thus, at the same depth profile in the target node search tree and the right to the left of the search tree nodes are distributed in the number of searches is substantially the same.

The same will be described using the legend:

It can be seen from the root node node begins to expand, the first visit is the root of the child nodes are then extended from a child node to the next level of their child nodes are that there is a hierarchy of expansion. Thus, for a set of levels is located near the leaf nodes, we can have a fair probability search. Node access order is to first figure above the level of access for all of the nodes 1, followed by all the nodes in level 2, until the search to the target solution, or can not continue to expand ended. Thus, for breadth-first search, the search is extended every time the current state of a state can expand that search is a tree in each layer.

Pseudo code may be described as follows:

Queue Q; visited [n] = 0;
  1. Visits vertices v; visited [v] = 1; Q vertex v queues;

  2. while (non-empty queue Q)   
                  v = Q queue dequeue the head element;
                  first neighbor vertex v w =;
                 while (w present) 
                         if w is not accessed, the access vertex w;
                         visited [w] =. 1;
                         queues the vertex w Q;
                         next adjacent point w = vertex v.

Similarly, the depth-first search algorithm is also a brute force search and disadvantages performance is as follows:

  1. Undifferentiated search, and the same depth-first search, breadth-first search is also a non-discriminatory equal probability of selection branch search, and breadth-first search of the tree in the left and right extreme imbalance can lead to low efficiency of the search. However, in the present problem, due to the expansion of only about four cases the vertical direction, therefore, the extended branches are more balanced. Tree does not tilt with a side. So, breadth-first search is on this issue is better than depth-first search.

  2. Huge space consumption, while maintaining the breadth-first search requires two tables, table space overhead close and open the table are enormous.

Breadth-first search comes hash table

Algorithm on a good solution to go in the end will fall into the first search of the depth of the problem, and analyzed the difference between the two on eight digital. Although, breadth-first search has been able to solve in a limited time and can be accepted, but must be very rough solving methods, there are many places to be optimized.

First, for each time extension, should find the current node is not visited, which would involve re-check operation, and breadth-first method is to use a map before rechecking the data structure, the efficiency of such a data structure query is the number of levels, but the space for each query expansion needs, the cost in time and space of the accumulation will be high.

To solve this problem, we consider a query extremely efficient data structure, such as a hash table is a data structure. It can be such that the time constant falls query complexity level, and the space for storage, if the hash function is designed, may be regarded as approximately linear rate loss.

Hash algorithm section can be described as:

Insert elements:

Calculates a hash value is inserted into elements
  1. To see if there is a hash conflict

There is a conflict, jump, until you can insert.

Conflict-free, insert elements.

Removing elements:

Compute the hash value.
  1. View the current location is to be deleted elements.

Yes, remove elements

No, the calculated hash jump, know where to find the location delete elements.

Bidirectional BFS

For a description of front breadth-first search algorithm is very detailed, where its further expansion. Discuss a more efficient algorithm.

For breadth-first search, the search can only be in one direction, that is from the initial search state in the direction to the target state, i.e., the entire search path can be regarded as a triangular extension process.

Figure:

Picture 4 bfs search directions

The above mentioned, if the target node is located close to the leaf node position, starting position from the start of breadth-first search efficiency will be very low.

So, can we consider, from the direction of the target began to search, when the state began to expand in the target tree with the initial state of the expansion node of the tree met, it indicates that the search has been the goal. This is a two-way depth-first search algorithm, which is very close to dissociate if the leaf node, the bottom-up branch can quickly search and top-down branches converge, can effectively solve the solution is close to the root and cause inefficiencies.

Picture 5 extending from both sides of the two-way BFS

Extended from both sides, was added to the open table search.

Heuristic Algorithm A *

In previously discussed, the use of indiscriminate search is not reasonable, because the chance to find the solution not the same on each search route. Therefore, the need to find a way to find algorithms that prioritize search on the route to the greatest possible search solution. A * search algorithm is the addition of such a mechanism, called the valuation function of the depth-first search, making it possible to have a high probability preference to search for possible branch.

In which the evaluation function is likely to search for the current branch to the target probability estimates.

In each iteration of the main loop, A
* is determined to expand the path. It is based on the cost of the path and the path has been extended to the cost estimates needed to operate a target. Specifically, A
* path selection minimized

{\ displaystyle f(n)= g(n)+ h(n)}

Photo 6 valued function

Wherein n is a node on the path, G (n) is the cost from the start node to the path n, H (n) is a heuristic function, the cost from cheapest to the target n for estimating path .

Thus, under the effect of the valuation function, search the route would be a reasonable choice determined by the valuation function of the current situation is more reasonable route.

After reviewing some data and found that in the design of the valuation function, should make dynamic changes,
the general criteria primer Baidu Encyclopedia :

Picture 7 Valuation design criterion function

Guess you like

Origin www.cnblogs.com/adameta/p/11788841.html