# [* 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.
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:
 Node (Node type) = RecordSitutation: TSituation (current node status );Way-NO: Integer (number of extended rules used );EndList (backtracking table): Array [1 .. Max (maximum depth)] of Node;Pos (number of the current extended node): Integer;List <-0;Pos <-1;List . Situation <-Initial State;
While (Pos> 0 (there is a way to go) and ([not reaching the target]) DoBeginIf 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)BeginIf (the current extension rule can be used) thenBegin(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-ifElse BeginPos: = pos-1;End-ElseEnd-While;

[Recursive algorithm]

CODE:
 Procedure BackTrack (Situation: TSituation; deepth: Integer );Var I: Integer;BeginIf 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 doBeginBackTrack (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:
 Node (Node type) = RecordSitutation: TSituation (current node status );Level: Integer (current node depth );Last: Integer (parent node );EndList (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)List <-0;Open <-1;Close <-0;List . Situation <-Initial State;List . Level: = 1;List . Last: = 0;
While (close

[Deep Search]

CODE:
 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)Open <-0; close <-0;Openl <-1; closel <-0;Open . Situation <-Initial State;Open . level <-1;Open . Last <-0;
While (openl> 0) and (closel

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 Keywords:
Related Article

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.