4You Dialy

Tech news for you daily

Artificial Intelligence: Search algorithms

8 min read

Not all who wander, lost

                                                                                                 J. R. R. Tolkin

Search in the state space (English State Space Search) is a group of mathematical methods designed to solve problems of artificial intelligence.


⇡#Search in the space of states: from the wording of the problem to solve

And let’s start with the practice and try to solve the classic II-task. So, three missionaries and three cannibal are on one side of the river, where the boat is also located, which can withstand no more than two people. Find a way to transport everyone to the other side of the river, while it is impossible to leave missionaries less than cannibals. This task was once widely discussed and was analyzed in detail in 1968 by Amarela. In the 60s, the search algorithms aroused a surge of interest, and their decisions were made in the classical problem of AI. At the end of the article, we will show you a tree of solving this task, and now we will analyze some important terms.

Any search algorithm takes some task as input and returns a solution in the form of a sequence of actions. To begin with, we will understand which items are the concept of “goal”, “task” and its “solution”. So, imagine that we rest at the resort, where we are faced with a complex and multi-component task – the best way to spend your long-awaited vacation. To fulfill it, you need to take into account many factors: if you like nightlife, you need to choose a lively turbine; If you want to just wander around the city, you need to visit the most picturesque parks, streets, etc. Excellent, you relax, the list of cases is still far from completion, but … you have a ticket to Moscow for tomorrow, not refundable. You fly, even if you have not yet been everywhere and did not try all local delicacies?

So, in this case, you are a decision agent. In your case, you need to strive to achieve the goal – to get on your plane. All other actions will only interfere with you. The first step is to formulate a goal, taking into account the current situation and performance of your performance as a decision agent. The purpose of the goal allows to organize the behavior, limiting the agent in choosing intermediate stages. I believe that everyone has an intuitive understanding of the goal, and we are familiar with the need to sacrifice something for its achievement. Thus, the task of the decision-making agent is to find such a sequence of actions that ultimately lead it to the target state. This sequence is called searching.

Fine, the goal is, the task is to come to her, but what’s next? Next – the wording of the task is a very important stage for successfully achieving your goal. That is, you will probably try to find out how to get to the airport, how much time it takes to it, – what actions and conditions you need to take into account to achieve your goal. Speaking formally, the task consists of four parts: the initial state, a variety of actions, the function check function and the cost function of the path. The last two definitions need probably explain.

Target check function: Did I get to the destination? Way cost function: What should I stand every stage? In the very trivial case, the cost of a sequence of actions is equal to the number of action themselves. In fact, of course, each step is unequivocal and has its price. Now, why do we speak exactly about the search in the space of states: the environment of the problem is represented by the space of states, and the path through it from the initial to the target state is a solution. Algorithmically let’s say this: To solve the problem, we will apply the search based on trees, and its various variations embody the various possible strategies.

⇡#Uninformed search

Typically study five strategies of non-informed search – this in which no other information on states are used, except that there is in the condition of the task. These strategies can only produce successors and distinguish the target state from the misuse. Nevertheless, they underlie many relevant methods. All search strategies differ in what order the nodes are deployed. We will list these strategies below for your information and tell you more about two main:

  • Width search
  • Search by cost criterion
  • Depth search
  • Search with a restriction of depth,
  • Search in depth with iterative deepening,
  • Bidirectional search.

The task with which we started today’s story is solved by searching in width (Breadth First Search, BFS). This is one of the classical methods for bypassing the graph, in which we visit at once the closest “neighbors” of our original point, then their “neighbors”, calculating the distance (minimum number of edges) from our original point to each top of the vertex. So we are moving wish, until we find our desired point or until the neighbors turn will not end. For some reason, the image of the prisoner of the criminal comes to me when the search comes simultaneously in several sectors of the city around the crime scene, providing the most complete coverage of the territory. Let’s write this algorithm on Python:

S[begin] = 0
Q = [begin]
Qbegin = 0
While Qbegin u = Q[Qbegin]
QBBIRE + = 1
For v in V[u]:
If S[v] is None:

Now consider the search in the depth. When searching in a depth in memory, only those nodes that lie on the root to the current node are stored. Simply put, from the original point we make one step, one more and so we move from it until we reach the target state or to a state, from which it is impossible to go further. If this happened, the algorithm is recursively returns and from the return point goes back until the solution is detected. If you happened to get lost, then you imagine the principle of work of this algorithm: the phone was discharged, you understand that you are not going there, return to a familiar monument and are looking for a way from it in another direction. So it may look like a Python:

Path = [None] * (n + 1)
Def dfs(begin, visited, path, g):
    Visited[begin] = True
    For u in g [Begin]: Variable U visits all the neighbors of the Begin peak
        If not visited[u]:
            Path[u] = begin

Take a graph again with vertices from 1 to n. Let’s start the DFS algorithm (acronym Depth First Search): We call Begin, the nodes in which you can get from it will fall into the list of visited (formal language, vertices lying in one connected component with Begin). Let’s look at all neighboring nodes and visit everyone. When visiting the neighbors, let’s see on their neighbors. We need a PATH list to build paths from the Begin to all available vertices. As you can see, this algorithm is based on recursion. So that it does not bike, we need to be noted, in what vertices we visited before (in the list of visited). And the predecessors of each vertex are stored to then build a search tree.

Above talked about two main strategies of non-informed search. If you want to learn more, we recommend reading the book “Artificial Intelligence: Modern Approach” (Stewart Russell, Peter Norvig).

Informed search

It is believed that the strategy of informed search as a whole provide a more effective solution to the solution. The general principle is here – search on the first best coincidence (BEST-FIRST-SEARCH). There is a whole family of algorithms for this kind with various evaluation functions. A distinctive feature of these algorithms is a heuristic function, denoted as h (n): H (n) = assessment of the cost of the least expensive path from the node n to the target node. As an example, we briefly consider the so-called greedy search and in more detail – algorithm A *.

Search A *: Minimization of the total valuation of the value of the solution. Search a * – the most famous search algorithm for the best coincidence. The reason for its success lies in the evaluation function – the algorithm takes the most rational decision at every step. It is more convenient to formally describe it:

F (n) = assessment of the cost of the least expensive solution path passing through node n.

F (n) = g (n) + h (n), where G (n) is the cost of achieving this node, and H (n) is the cost of passing from this node to the target. To find the least expensive solution, it is best to choose a node with the smallest value of this function. As it turned out, search A *, subject to some requirements for his heuristics (and what – will reply in the next article), it can be complete, and optimal, so that this strategy fully justifies itself. I wonder how to write a program for a * or code already too much? While let’s decide with its application a small task:

Consider this graph. How will it cost him *?

  • Start from the node A.
  • From the node and you can get into the node B and node F.

The algorithm A * calculates F (B) and F (F).

  • F(B) = 6 + 8 = 14
  • F(F) = 3 + 6 = 9

Path: A → F

From the node f, you can get into the G node and node H.

Algorithm A * calculates F (G) and F (H).

  • F(G) = (3+1) + 5 = 9
  • F(H) = (3+7) + 3 = 13

Path: A → F → G

Step 3:

From the node g, you can only get into the node I.

Algorithm A *, however, calculates F (I).

  • F(I) = (3+1+3) + 1 = 8

It was decided to go to Knot I.

Path: A → F → G → I

From the node I can be reached into the node E, H and J.

The algorithm A * calculates F (E), F (H) and F (J).

  • F(E) = (3+1+3+5) + 3 = 15
  • F(H) = (3+1+3+2) + 3 = 12
  • F(J) = (3+1+3+3) + 0 = 10

Since the value of F (j) is less, it is decided to go to the J. node

Path: A → F → G → I → J

The story about the search algorithms cannot be complete without a genetic algorithm, but he will need to devote a separate article. Within the framework of the University Course, the AI ​​also studies the search in the face of countering on the example of a minimax search and an alpha-beta clip – about this one another. And now the promised tree to the challenge from the beginning of the article.

Please note, we excluded the situation when only one person is in the boat, because the second must drive it back. Then, only two cannibal or one missionary with one cannibal can swim together. This is shown on the two solely possible nodes that are outgoing from the top. Another option is possible when the boat goes and returns with the same characters, it is not shown in the diagram. However, if you constitute an algorithm, this situation will have to take into account. Next, the task is solved by no problem, since the state has been achieved, of which the crossing can be carried out in compliance with the conditions of the problem.

We hope that our story about search algorithms in AI caused your desire more deeply in them to figure out and, perhaps, try your strength in writing your own programs.


  • Stewart Russell, Peter Norvig. “Artificial Intellect: Modern Approach.”
  • Materials of the course of the course “Artificial Intelligence and its industrial applications” of the National Institute of Science and Technology Taiwan.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © All rights reserved. | Newsphere by AF themes.