[* Go to the same page *] Basic Search Algorithm tutorial

Source: Internet
Author: User

Search AlgorithmIt is a method that uses the high performance of computers to find a solution to a problem by providing a purposeful reference to the part of a problem or all possible situations.Search ProcessIn fact, it is the process of constructing an answer tree based on the initial conditions and expansion rules and finding nodes that meet the target State.
From the perspective of its final algorithm implementation, all search algorithms can be divided into two parts: control structure and generation system, all algorithms are optimized and improved by modifying their control structure. Now we mainly discuss its control structure, so we will make the following Conventions on its production system:
Function expendnode (situation: tsituation; expendwayno: integer): tsituation;
Indicates that the given node status sitution adopts the expendwayno Extension Rule and returns the expanded status.
(The algorithm description language used in this article is Pascal-like .)
I. Backtracking Algorithm
The backtracking algorithm is the most basic algorithm in all search algorithms. It uses the idea of "turning around without passing through" as its control structure, it uses the root Traversal method to construct the answer tree, which can be used to find the solution or all solutions and the optimal solution. The specific algorithm is described as follows:
[Non-recursive algorithms]

CODE:  
<Type>
Node (Node type) = Record
Situtation: TSituation (current node status );
Way-NO: Integer (number of extended rules used );
End
<Var>
List (backtracking table): Array [1 .. Max (maximum depth)] of Node;
Pos (number of the current extended node): Integer;
<Init>
List <-0;
Pos <-1;
List [1]. Situation <-Initial State;
<Main Program>
While (Pos> 0 (there is a way to go) and ([not reaching the target]) Do
Begin
If POS> = max then (Data overflow, jump out of the main program );
List [POS]. Way-No: = list [POS]. Way-No + 1;
If (list [POS]. Way-No <= totalexpendmethod) Then (if there are still unused extension Rules)
Begin
If (the current extension rule can be used) then
Begin
(Use the way rule to expand the current node)
List [POS + 1]. Situation: = expendnode (list [POS]. situation,
List [POS]. Way-No );
List [POS + 1]. Way-No: = 0;
POs: = POS + 1;
End-if;
End-if
Else Begin
Pos: = pos-1;
End-Else
End-While;

[Recursive algorithm]

CODE:  
Procedure BackTrack (Situation: TSituation; deepth: Integer );
Var I: Integer;
Begin
If deepth> Max then (the space reaches the limit and jumps out of this process );
If Situation = Target then (find the Target );
For I: = 1 to TotalExpendMethod do
Begin
BackTrack (ExpendNode (Situation, I), deepth + 1 );
End-;
End;

Example: there is a horse on a certain point on an M * m board, and you need to find a route to jump all the points on the board from this point without repeating.
Evaluation: The backtracking algorithm consumes less space. When used together with the branch and boundary method, it can effectively solve problems with deep layers in the solution tree. However, it is necessary to avoid the use of the successor node in the same way as the successor node, so as to avoid loops.

2. Deep Search and wide search
The control structure and Generation System of Deep Search and wide search are similar. The only difference lies in the selection of extended nodes. Because it retains all the forward nodes, some duplicate nodes can be removed when a successor node is generated, thus improving the search efficiency. Each time the two algorithms expand all the subnodes of a node, the difference is that the next extension of the deep search is one of the Child Nodes extended this time, extended search is the brother node of the extended node. To improve efficiency, different data structures are adopted.
[Wide search]

CODE:  
<Type>
Node (Node type) = Record
Situtation: TSituation (current node status );
Level: Integer (current node depth );
Last: Integer (parent node );
End
<Var>
List (Node table): Array [1 .. Max (maximum number of nodes)] of Node (Node type );
Open (total number of nodes): Integer;
Close (node number to be extended): Integer;
New-S: TSituation; (New node)
<Init>
List <-0;
Open <-1;
Close <-0;
List [1]. Situation <-Initial State;
List [1]. Level: = 1;
List [1]. Last: = 0;
<Main Program>
While (close <open (and unexpanded nodes) and
(Open <Max (space not used up) and
(Target node not found) do
Begin
Close: = close + 1;
For I: = 1 to TotalExpendMethod do (extends a layer of subnodes)
Begin
New-S: = ExpendNode (List [close]. Situation, I );
If Not (New-S in List) then
(Extended nodes have never appeared)
Begin
Open: = open + 1;
List [open]. Situation: = New-S;
List [open]. Level: = List [close]. Level + 1;
List [open]. Last: = close;
End-If
End-;
End-While;

[Deep Search]

CODE:  
<Var>
Open: array [1 .. Max] of node; (node table to be extended)
Close: array [1 .. Max] of node; (expanded node table)
Openl, closel: integer; (table length)
New-S: tsituation; (new state)
<Init>
Open <-0; close <-0;
Openl <-1; closel <-0;
Open [1]. Situation <-Initial State;
Open [1]. level <-1;
Open [1]. Last <-0;
<Main program>
While (openl> 0) and (closel <max) and (openl <max) Do
Begin
Closel: = closel + 1;
Close [closel]: = open [openl];
Openl: = openL-1;
For I: = 1 to totalexpendmethod do (extends a layer of subnodes)
Begin
New-S: = expendnode (close [closel]. Situation, I );
If not (New-s in list) then
(Extended nodes have never appeared)
Begin
Openl: = openl + 1;
Open [openl]. Situation: = new-S;
Open [openl]. Level: = close [closel]. Level + 1;
Open [openl]. Last: = closel;
End-if
End-;
End;

Example: The Labyrinth problem, solving the shortest path and accessible path.
Evaluation: breadth search is a better method for solving the optimal solution. It will be further optimized later. Deep Search is often used only when there are many duplicate nodes in the answer tree that are difficult to judge. However, it can often be replaced by branch demarcation or backtracking algorithms.

Related Article

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.