0% found this document useful (0 votes)
386 views37 pages

Breadth First Search

Breadth First Search (BFS) searches the problem space in layers, starting with the root node and exploring all nodes at the current level before moving to the next level. It uses a queue to store nodes to be explored, adding child nodes of the dequeued node to the back of the queue. BFS is guaranteed to find the shortest path if one exists and runs in O(bd) time where b is the branching factor and d is the depth of the solution. However, it uses significant memory to store all nodes at each level.

Uploaded by

Angelina Joy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
386 views37 pages

Breadth First Search

Breadth First Search (BFS) searches the problem space in layers, starting with the root node and exploring all nodes at the current level before moving to the next level. It uses a queue to store nodes to be explored, adding child nodes of the dequeued node to the back of the queue. BFS is guaranteed to find the shortest path if one exists and runs in O(bd) time where b is the branching factor and d is the depth of the solution. However, it uses significant memory to store all nodes at each level.

Uploaded by

Angelina Joy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Breadth First Search

Breadth First Search (BFS) searches breadth-wise in the problem space. Breadth-First search is
like traversing a tree where each node is a state which may a be a potential candidate for
solution. It expands nodes from the root of the tree and then generates one level of the tree at a
time until a solution is found. It is very easily implemented by maintaining a queue of nodes.
Initially the queue contains just the root. In each iteration, node at the head of the queue is
removed and then expanded. The generated child nodes are then added to the tail of the queue.

Algorithm: Breadth-First Search

1. Create a variable called NODE-LIST and set it to the initial state.


2. Loop until the goal state is found or NODE-LIST is empty.
a. Remove the first element, say E, from the NODE-LIST. If NODE-LIST was empty
then quit.
b. For each way that each rule can match the state described in E do:

i) Apply the rule to generate a new state.


ii) If the new state is the goal state, quit and return this state.
iii) Otherwise add this state to the end of NODE-LIST

Since it never generates a node in the tree until all the nodes at shallower levels have been
generated, breadth-first search always finds a shortest path to a goal. Since each node can be
generated in constant time, the amount of time used by Breadth first search is proportional to the
number of nodes generated, which is a function of the branching factor b and the solution d.
Since the number of nodes at level d is bd, the total number of nodes generated in the worst case
is b + b2 + b3 +… + bd i.e. O(bd) , the asymptotic time complexity of breadth first search.

S= R G=E Search= RABCDE

Time Complexity = O(bd) = 23 = 8

B = 2 d =3

S= R G=B b=2 d=2 TC=4

Page 1
Look at the above tree with nodes starting from root node, R at the first level, A and B at the
second level and C, D, E and F at the third level. If we want to search for node E then BFS will
search level by level. First it will check if E exists at the root. Then it will check nodes at the
second level. Finally it will find E a the third level.

Advantages of Breadth-First Search

1. Breadth first search will never get trapped exploring the useless path forever.
2. If there is a solution, BFS will definitely find it out.
3. If there is more than one solution then BFS can find the minimal one that requires less number
of steps.

Disadvantages of Breadth-First Search 

1. The main drawback of Breadth first search is its memory requirement. Since each level of the
tree must be saved in order to generate the next level, and the amount of memory is
proportional to the number of nodes stored, the space complexity of BFS is O(b d). As a result,
BFS is severely space-bound in practice so will exhaust the memory available on typical
computers in a matter of minutes.
2. If the solution is farther away from the root, breath first search will consume lot of time.

Page 2
Depth First Search

Depth First Search (DFS) searches deeper into the problem space. Breadth-first search always
generates successor of the deepest unexpanded node. It uses last-in first-out stack for keeping the
unexpanded nodes. More commonly, depth-first search is implemented recursively, with the
recursion stack taking the place of an explicit node stack.

Algorithm: Depth First Search

1.If the initial state is a goal state, quit and return success.
2.Otherwise, loop until success or failure is signaled.
a) Generate a state, say E, and let it be the successor of the initial state. If there is no successor,
signal failure.
b) Call Depth-First Search with E as the initial state.
c) If success is returned, signal success. Otherwise continue in this loop.

Advantages of Depth-First Search 

 The advantage of depth-first Search is that memory requirement is only linear with respect to
the search graph. This is in contrast with breadth-first search which requires more space. The
reason is that the algorithm only needs to store a stack of nodes on the path from the root to
the current node.  

 The time complexity of a depth-first Search to depth d is O(b^d) since it generates the same set
of nodes as breadth-first search, but simply in a different order. Thus practically depth-first
search is time-limited rather than space-limited.  

 If depth-first search finds solution without exploring much in a path then the time and space it
takes will be very less. 

Disadvantages of Depth-First Search 

 The disadvantage of Depth-First Search is that there is a possibility that it may go down the left-
most path forever. Even a finite graph can generate an infinite tree. One solution to this
problem is to impose a cutoff depth on the search. Although the ideal cutoff is the solution
depth d and this value is rarely known in advance of actually solving the problem. If the chosen
cutoff depth is less than d, the algorithm will fail to find a solution, whereas if the cutoff depth is
greater than d, a large price is paid in execution time, and the first solution found may not be an
optimal one.

Page 3
 Depth-First Search is not guaranteed to find the solution. 

 And there is no guarantee to find a minimal solution, if more than one solution exists.

Bidirectional Search

Searching a graph is quite famous problem and have a lot of practical use. We have already
discussed here how to search for a goal vertex starting from a source vertex using BFS. In
normal graph search using BFS/DFS we begin our search in one direction usually from source
vertex toward the goal vertex, but what if we start search form both direction
simultaneously.

Bidirectional search is a graph search algorithm which find smallest path form source to goal
vertex. It runs two simultaneous search –

1. Forward search form source/initial vertex toward goal vertex


2. Backward search form goal/target vertex toward source vertex

Bidirectional search replaces single search graph(which is likely to grow exponentially) with two
smaller sub graphs – one starting from initial vertex and other starting from goal vertex. The
search terminates when two graphs intersect.

Just like A* algorithm, bidirectional search can be guided by a heuristic estimate of remaining
distance from source to goal and vice versa for finding shortest path possible.

Consider following simple example-

Page 4
Suppose we want to find if there exists a path from vertex 0 to vertex 14. Here we can execute
two searches, one from vertex 0 and other from vertex 14. When both forward and backward
search meet at vertex 7, we know that we have found a path from node 0 to 14 and search can be
terminated now. We can clearly see that we have successfully avoided unnecessary exploration.

When to use bidirectional approach?

We can consider bidirectional approach when-

1. Both initial and goal states are unique and completely defined.
2. The branching factor is exactly the same in both directions.

Performance measures

 Completeness : Bidirectional search is complete if BFS is used in both searches.


 Optimality : It is optimal if BFS is used for search and paths have uniform cost.

Page 5
Informed Search Strategies

This section shows how an informed search strategy - one that uses problem-specific
knowledge beyond the definition of the problem itself - can find solutions more efficiently than
can an uninformed search strategy.

The general approach we consider is called best first search. Best-first search is an instance of
the general Tree-Search or Graph-Search algorithm in which a node is selected for expansion
based on an evaluation function, f(n). The evaluation function is construed as a cost-estimate, so
the node with the lowest evaluation is expanded first. The implementation of best first graph
search is identical to that for Uniform-Cost Search, except for the use of f instead of g to order
the priority queue.

The choice of f determines the search strategy. Most best-fit algorithms include as a component
of f a heuristic function denoted h(n):

h(n) = estimated cost of the cheapest path from the state at node n to a goal state.

Notice that h(n) takes a node as an input, but, unlike g(n) it depends only on the state at that
node. For example, one might estimate the cost of the cheapest path from Arad to Bucharest via
the straight-line distance from Arad to Bucharest.

Heuristic functions are the most common form in which additional knowledge of the problem is
imparted to the search algorithm. We also consider them to be arbitrary, nonnegative, problem
specific functions, with one constraint: if n is a goal node, then h(n)=0. Following are two ways
to use heuristic information to guide search.

Greedy Best-First Search


Greedy best-first search tries to expand the node that is closest to the goal, on the grounds that
it is likely to lead to a solution quickly. Thus, it evaluates nodes by using just the heuristic
function; that is, f(n)=h(n).

Let us see how this works for the route-finding problems in Romania; we use the straight-line
distance heuristic, which we will call hSLD. If the goal is Bucharest, we need to know the
straight-line distances to Bucharest, which are shown in Figure ISS-1:

City Distance City Distance City Distance City Distance

Page 6
Arad 366 Bucharest 0 Craiova 160 Drobeta 242

Eforie 161 Fagaras 176 Giurgiu 77 Hirsova 151

Iasi 226 Lugoj 244 Mehadia 241 Neamt 234

Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253

Timisoara 329 Urziceni 80 Valusi 199 Zerind 374

Figure ISS-1: Values of hSLD - straight line distances to Bucharest

Notice that the values of hSLD cannot be computed from the problem description itself. Moreover,
it takes a certain amount of experience to know that hSLD is correlated with actual road distances
and is, therefore, a useful heuristic.

Figure ISS-2 shows the progress of a greedy best-first search using hSLD to find a path from Arad
to Bucharest:

Page 7
Page 8
The first node to be expanded from Arad will be Sibiu because it is closer to Bucharest then
either Zerind or Timisoara. The next node to be expanded will be Fagaras because it is closest.
Fagaras, in turn, generates Bucharest, which is the goal. For this particular problem, greedy best-
first search using hSLD finds a solution without ever expanding a node that is not on the solution
path; hence, its search cost is minimal. It is not optimal, however: the path via Sibiu and Fagaras
to Bucharest is 32 kilometers longer than the path through Rimnicu Vilcea and Pitesti. This
shows why the algorithm is called "Greedy" - at each step it tries to get as close to the goal as it
can.

Greedy best-first tree search is also incomplete even in a finite state space, much like depth-first
search. Consider the problem of getting from Iasi to Fagaras. The heuristic suggests that Neamt
will be expanded first because it is closest to Fagaras, but it is a dead end street. The solution is
to go first to Vaslui - a step that is actually farther from the goal according to the heuristic - and
then continue to Urziceni, Bucharest, and Fagaras. The algorithm will never find this solution,
however, because expanding neamt puts Iasi back into the frontier, Iasi is closer to Fagaras than
Valusi is, and so Iasi will be expanded again, leading to an infinite loop. (The graph search
version is complete in finite spaces, but not in infinite ones.) The worst case time and space
complexity for the tree version is O(bm), where m is the maximum depth fo the search space.
With a good heuristic function, however, the complexity can be reduced substantially. The
amount of reduction depends on the particular problem and on the quality of the heuristic.

Page 9
A* Search: Minimizing the total estimated solution cost
The most widely known form of best-first search is called A* search (pronounced "A-star
search"). It evaluates nodes by combining g(n), the cost to reach the node, and h(n), the cost to
get from the node to the goal:

f(n)=g(n)+h(n).

Since g(n) gives the path cost from the start node to node n, the goal node, and h(n) is the
estimated cost of the cheapest path from n to the goal, we have:

f(n) = estimated cost of the cheapest solution through n.

Thus, if we are trying to find the cheapest solution, a reasonable thing to try first is the node with
the lowest value of g(n)+h(n). As it turns out, this strategy is more than just reasonable: provide
that the heuristic function, h(n), satisfies certain conditions, A* search is both complete and
optimal. The algorithm is identical to Uniform-Cost Search except that A* uses g+h instead of g.

Conditions for optimality: Admissability and Consistency

The first condition that we require for optimality is that h(n) be an admissable heuristic. An
admissable heuristic is one that never overestimates the cost to reach the goal. Because g(n) is
the actual cost to reach n along the current path, and f(n)=g(n)+h(n), we have as an immediate
consequence that f(n) never overestimates the true cost of a solution along the current path
through n.

Admissible heuristics are by nature optimistic because they think the cost of solving the problem
is less than it actuall is. An obvious example of an admissable heuristic is the straight line
distance, hSLD that we used in getting to Bucharest. Straight-line distance is admissable because
the shortest path between any two points is a straight line, so the straight line cannot be an
overestimate. Figure ISS-4 shows the progress of an A* tree search for Bucharest:

Page
10
The cost values for each node are given in parentheses below each node. The cost is determined
by following the path from the start as it progresses through each expansion. Notice in particular,
that in level 4 Bucharest, by way of Fagaras, has a lowest possible of cost of 450, but, at the
same time, the node at Pitesti is only 417. A* is not ready to settle just yet, so 1 more expansion
takes place uncovering the best possible cost with the Bucharest goal only costing 417. Thus the
path from A* is Arad-Sibiu-Rimnicu Vilcea-Pitesti-Bucharest.

A second, slightly stronger condition called consistency, or sometimes monotonicity, is required


only for applications of A* to graph search. A heuristic h(n) is consistent if, for every node n and
every successor n' of n generated by any action a, the estimated cost of reaching the goal from n
is no greater than the step cost of getting to n':

h(n) ≤ c(n,a,n') +h (n).

Page
11
This is a form of the general triangle inequality, which stipulates that each side of a triangle
cannot be longer than the sum of the other two sides. Here, the triangle is formed by n,n', and the
goal Gn closest to n. For an admissable heuristic, the inequality makes perfect sense: if there were
a route form n to Gn via n' that was cheaper than h(n), that would violate the property that h(n) is
a lower bound on the cost to reach Gn.

It is fairly easy to show that every consistent heuristic is also admissable. Consistency is
therefore a stricter requirement than admissability, but one has to work quite hard to concoct
heuristics that are admissable but not consistent. All the admissable heuristics we discuss in this
chapter are also consistent. Consider, for example, hSLD. We know that the general triangle
inequality is satisified when each side is measured by the straight-line distance and that the
straight-line distance between n and n' is no greater than c(n,a,n'). Hence, hSLD is a consistent
heuristic.

Optimality of A*
As we mentioned eariler, A* has the following properties: the tree-search version of A* is
optimal if h(n) is admissable, while the graph-search version is optimal if h(n) is consistent.

The first step is to establish the following: if h(n) is consistent, then the values of f(n) along any
path are nondecreasing. The proof follows directly from the definition of consistency:

Suppose n' is a successor of n; then g(n') = g(n) + c(n,a,n') for some action a and we have:

f(n) = g(n') + h(n') = g(n) + c(n,a,n') + h(n') ≥ g(n) + h(n) = f(n).

The next step is to prove that whenever A* selects a node n for expansion, the optimal path to
that node has been found. Were this not the case, there would have to be another frontier node n',
and by the graph separation property as illustrated in the following diagram:

Page
12
Figure ISS-5: Expanded Node Graph Separation Property

where the frontier (white nodes) always separate the explored region (black nodes) from the
unexplored region (gray nodes). Because f is nondecreasing along any path, n' would have lower
f-cost than n and would have been selected first.

From the two preceding observations, it follows that the sequence of nodes expanded by A*
using Graph-Search is in nondecreasing order of f(n). Hence, the first goal node selected for
expansion must be an optimal solution because f is the true cost for goal nodes (which have h =
0) and all later goals will be at least as expensive.

The fact that f-costs are nondecreasing along any path also means that we can draw contours in
the state space, just like the contours in a topographic map:

Figure ISS-6: Map of Romania showing contours at f = 380, f=400, f=420, with Arad as the start state.
Nodes inside a given contour have f-costs less than or equal to the contour value.

Page
13
Inside the contour labeled 400, all nodes have f(n) less than or equal to 400, and so on. Then
because A* expands the frontier of lowest f-cost, we can see than an A* search fans out from the
start node, adding nodes in concentric bands of increasing f-cost.

With uniform-cost search (A* search using h(n) = 0), the bands will be circular around the start
state. With more accurate heuristics, teh bands will stretch toward the goal state and become
more narrowly focused around the optimal path. If C* is the cost of the optimal solution path,
then we can say the following:

 A* expands all nodes with f(n) < C*.


 A* might then expand some of the nodes right on the "goal contour" (where f(n) = C*) before
selecting the goal node.

Completeness requires that there be only finitely many nodes with cost less than or equal to C*, a
condition that is true if all step costs exceed some finite ε and if b is finite.

Notice that A* expands no nodes with f(n) > C* - for example, Timisoara is not expanded in
Figure ISS-4, even though it is a child of the root. We say that the subtree below Timisoara is
pruned; because hSLD is admissable, the algorithm can safely ignore this subtree while still
guaranteeing optimality. The concept of pruning - eliminating possibilities from consideration
without having to examine them - is important for many areas of AI.

One final observation is that among optimal algorithms of this type - algorithms that extend
search paths from the root and use the same heuristic information - A* is optimally efficient for
any given consistent heuristic. That is, no other optimal algorithm is guaranteed to expand fewer
nodes than A* (except possibly through tie-breaking among nodes with f(n) = C*). This is
because any algorithm that does not expand all nodes with f(n) < C* runs the risk of missing the
optimal solution.

That A* search is complete, optimal, and optimally efficient among all such algorithms is rather
satisfying. Unfortunately, it does not mean that A* is the answer to all our searching needs. The
catch is that, for most problems, the number of states within the goal contour search space is still
exponential in the length of the solution. The basic results of the analysis are as follows: For
problems with constant step costs, the growth in run time as a function of the optimal solution
depth d is analyzed in terms of absolute error or the relative error of the heuristic. The
absolute error is defined as Δ ≡ h* - h, where h* is the actual cost of getting from the root to the
goal, and the relative error is defined as ε ≡ (h* - h) / h*.

The complexity results depend very strongly on the assumptions made about the state space. The
simplest model studied is a state space that has a single goal and is essentially a tree with
reversible actions. In this case, the time complexity of A* is exponential in the maximum
absolute error, that is, O(bΔ). For constant step costs, we can write this as O(bεd), where d is the
solution depth. For almost all heuristics in practical use, the absolute error is at least proportional
to the path cost h*, so ε is constant or growing and the time complexity is exponential in d. We
can also see the effect of a more accurate heuristic: O(bεd) = O((bε)d), so the effective branching
factor is bε.

Page
14
When the state space has many goal states - particularly near-optimal goal states - the search
process can be led astray from the optimal path and there is an extra cost proportional to the
number of goals whose cost is within a factor ε of the optimal cost. Finally, in the general case of
a graph, the situation is even worse. There can be exponentially many states with f(n) < C* even
if the absolute error is bounded by a constant. For example, consider a version of the vacuum
world where the agent can clean up any square for unit cost without even having to visit it: in
that case, squares can be cleaned in any order. With N initially dirty squares, there are 2 N states
where some subset has been cleaned and all of them are on an optimal solution path - and hence
satisfy f(n) < C* - even if the heuristic has an error of 1.

The complexity of A* often makes it impractical to insist on finding an optimal solution. One
can use variants of A* that find suboptimal solutions quickly, or once can sometimes design
heuristics that are more accurate but not strictly admissable. In any case, the use of a good
heuristic still provides enormous savings compared to the use of an uninformed search.

Computation time is not, however, A*'s main drawback. Because it keeps all generated nodes in
memory (as do all Graph-Search algorithms), A* usually runs out of space long before it runs out
of time. For this reason, A* is not practicla for many large scale-problems. There are, however,
algorithms that overcome the space problem withou sacrifiing optimality or completeness, but
with a small cost in execution time.

Page
15
Heuristic Search
All of the search methods in the preceding section are uninformed in that they did not take into
account the goal. They do not use any information about where they are trying to get to unless
they happen to stumble on a goal. One form of heuristic information about which nodes seem the
most promising is a heuristic function h(n), which takes a node n and returns a non-negative real
number that is an estimate of the path cost from node n to a goal node. The function h(n) is an
underestimate if h(n) is less than or equal to the actual cost of a lowest-cost path from node n to a
goal.

The heuristic function is a way to inform the search about the direction to a goal. It provides an
informed way to guess which neighbor of a node will lead to a goal.

There is nothing magical about a heuristic function. It must use only information that can be
readily obtained about a node. Typically a trade-off exists between the amount of work it takes to
derive a heuristic value for a node and how accurately the heuristic value of a node measures the
actual path cost from the node to a goal.

A standard way to derive a heuristic function is to solve a simpler problem and to use the actual
cost in the simplified problem as the heuristic function of the original problem.

Example 3.12: For the graph of Figure 3.2, the straight-line distance in the world between the node and
the goal position can be used as the heuristic function. The examples that follow assume the following
heuristic function:

h(mail) = 26 h(ts) = 23 h(o103) = 21

h(o109) = 24 h(o111) = 27 h(o119) = 11

h(o123) = 4 h(o125) = 6 h(r123) = 0

h(b1) = 13 h(b2) = 15 h(b3) = 17

h(b4) = 18 h(c1) = 6 h(c2) = 10

h(c3) = 12 h(storage) = 12

This h function is an underestimate because the h value is less than or equal to the exact cost of a
lowest-cost path from the node to a goal. It is the exact cost for node o123. It is very much an
underestimate for node b1, which seems to be close, but there is only a long route to the goal. It
is very misleading for c1, which also seems close to the goal, but no path exists from that node to
the goal.

Example 3.13: Consider the delivery robot of Example 3.2, where the state space includes the parcels to
be delivered. Suppose the cost function is the total distance traveled by the robot to deliver all of the
parcels. One possible heuristic function is the largest distance of a parcel from its destination. If the

Page
16
robot could only carry one parcel, a possible heuristic function is the sum of the distances that the
parcels must be carried. If the robot could carry multiple parcels at once, this may not be an
underestimate of the actual cost.

The h function can be extended to be applicable to (non-empty) paths. The heuristic value of a
path is the heuristic value of the node at the end of the path. That is:

h(⟨no,...,nk⟩)=h(nk)

A simple use of a heuristic function is to order the neighbors that are added to the stack
representing the frontier in depth-first search. The neighbors can be added to the frontier so that
the best neighbor is selected first. This is known as heuristic depth-first search. This search
chooses the locally best path, but it explores all paths from the selected path before it selects
another path. Although it is often used, it suffers from the problems of depth-fist search.

Another way to use a heuristic function is to always select a path on the frontier with the lowest
heuristic value. This is called best-first search. It usually does not work very well; it can follow
paths that look promising because they are close to the goal, but the costs of the paths may keep
increasing.

Figure 3.8: A graph that is bad for best-first search

Page
17
Example 3.14: Consider the graph shown in Figure 3.8, where the cost of an arc is its length. The aim is
to find the shortest path from s to g. Suppose the Euclidean distance to the goal g is used as the
heuristic function. A heuristic depth-first search will select the node below s and will never terminate.
Similarly, because all of the nodes below s look good, a best-first search will cycle between them, never
trying an alternate route from s.

Page
18
Page
19
AI - Popular Search Algorithms

Searching is the universal technique of problem solving in AI. There are some single-player
games such as tile games, Sudoku, crossword, etc. The search algorithms help you to search for a
particular position in such games.

Single Agent Pathfinding Problems


The games such as 3X3 eight-tile, 4X4 fifteen-tile, and 5X5 twenty four tile puzzles are single-
agent-path-finding challenges. They consist of a matrix of tiles with a blank tile. The player is
required to arrange the tiles by sliding a tile either vertically or horizontally into a blank space
with the aim of accomplishing some objective.

The other examples of single agent pathfinding problems are Travelling Salesman Problem,
Rubik’s Cube, and Theorem Proving.

Search Terminology
 Problem Space − It is the environment in which the search takes place. (A set of states
and set of operators to change those states)

 Problem Instance − It is Initial state + Goal state.

 Problem Space Graph − It represents problem state. States are shown by nodes and
operators are shown by edges.

 Depth of a problem − Length of a shortest path or shortest sequence of operators from


Initial State to goal state.

 Space Complexity − The maximum number of nodes that are stored in memory.

 Time Complexity − The maximum number of nodes that are created.

 Admissibility − A property of an algorithm to always find an optimal solution.

 Branching Factor − The average number of child nodes in the problem space graph.

 Depth − Length of the shortest path from initial state to goal state.

Brute-Force Search Strategies


They are most simple, as they do not need any domain-specific knowledge. They work fine with
small number of possible states.

Page
20
Requirements −

 State description
 A set of valid operators
 Initial state
 Goal state description

Breadth-First Search

It starts from the root node, explores the neighboring nodes first and moves towards the next
level neighbors. It generates one tree at a time until the solution is found. It can be implemented
using FIFO queue data structure. This method provides shortest path to the solution.

If branching factor (average number of child nodes for a given node) = b and depth = d, then
number of nodes at level d = bd.

The total no of nodes created in worst case is b + b2 + b3 + … + bd.

Disadvantage − Since each level of nodes is saved for creating next one, it consumes a lot of
memory space. Space requirement to store nodes is exponential.

Its complexity depends on the number of nodes. It can check duplicate nodes.

Depth-First Search

It is implemented in recursion with LIFO stack data structure. It creates the same set of nodes as
Breadth-First method, only in the different order.

Page
21
As the nodes on the single path are stored in each iteration from root to leaf node, the space
requirement to store nodes is linear. With branching factor b and depth as m, the storage space is
bm.

Disadvantage − This algorithm may not terminate and go on infinitely on one path. The solution
to this issue is to choose a cut-off depth. If the ideal cut-off is d, and if chosen cut-off is lesser
than d, then this algorithm may fail. If chosen cut-off is more than d, then execution time
increases.

Its complexity depends on the number of paths. It cannot check duplicate nodes.

Bidirectional Search

It searches forward from initial state and backward from goal state till both meet to identify a
common state.

The path from initial state is concatenated with the inverse path from the goal state. Each search
is done only up to half of the total path.

Uniform Cost Search

Sorting is done in increasing cost of the path to a node. It always expands the least cost node. It
is identical to Breadth First search if each transition has the same cost.

It explores paths in the increasing order of cost.

Disadvantage − There can be multiple long paths with the cost ≤ C*. Uniform Cost search must
explore them all.

Iterative Deepening Depth-First Search

It performs depth-first search to level 1, starts over, executes a complete depth-first search to
level 2, and continues in such way till the solution is found.

Page
22
It never creates a node until all lower nodes are generated. It only saves a stack of nodes. The
algorithm ends when it finds a solution at depth d. The number of nodes created at depth d is bd
and at depth d-1 is bd-1.

Comparison of Various Algorithms Complexities

Let us see the performance of algorithms based on various criteria −

Interactive
Criterion Breadth First Depth First Bidirectional Uniform Cost
Deepening

Time bd bm bd/2 bd bd

Space bd bm bd/2 bd bd

Optimality Yes No Yes Yes Yes

Completeness Yes No Yes Yes Yes

Page
23
Informed (Heuristic) Search Strategies
To solve large problems with large number of possible states, problem-specific knowledge needs
to be added to increase the efficiency of search algorithms.

Heuristic Evaluation Functions

They calculate the cost of optimal path between two states. A heuristic function for sliding-tiles
games is computed by counting number of moves that each tile makes from its goal state and
adding these number of moves for all tiles.

Pure Heuristic Search

It expands nodes in the order of their heuristic values. It creates two lists, a closed list for the
already expanded nodes and an open list for the created but unexpanded nodes.

In each iteration, a node with a minimum heuristic value is expanded, all its child nodes are
created and placed in the closed list. Then, the heuristic function is applied to the child nodes and
they are placed in the open list according to their heuristic value. The shorter paths are saved and
the longer ones are disposed.

A * Search

It is best-known form of Best First search. It avoids expanding paths that are already expensive,
but expands most promising paths first.

f(n) = g(n) + h(n), where

 g(n) the cost (so far) to reach the node


 h(n) estimated cost to get from the node to the goal
 f(n) estimated total cost of path through n to goal. It is implemented using priority queue by
increasing f(n).

Greedy Best First Search

It expands the node that is estimated to be closest to goal. It expands nodes based on f(n) = h(n).
It is implemented using priority queue.

Disadvantage − It can get stuck in loops. It is not optimal.

Page
24
Local Search Algorithms
They start from a prospective solution and then move to a neighboring solution. They can return
a valid solution even if it is interrupted at any time before they end.

Hill-Climbing Search

It is an iterative algorithm that starts with an arbitrary solution to a problem and attempts to find
a better solution by changing a single element of the solution incrementally. If the change
produces a better solution, an incremental change is taken as a new solution. This process is
repeated until there are no further improvements.

function Hill-Climbing (problem), returns a state that is a local maximum.

inputs: problem, a problem


local variables: current, a node
neighbor, a node
current <-Make_Node(Initial-State[problem])
loop
do neighbor <- a highest_valued successor of current
if Value[neighbor] ≤ Value[current] then
return State[current]
current <- neighbor

end

Disadvantage − This algorithm is neither complete, nor optimal.

Local Beam Search

In this algorithm, it holds k number of states at any given time. At the start, these states are
generated randomly. The successors of these k states are computed with the help of objective
function. If any of these successors is the maximum value of the objective function, then the
algorithm stops.

Otherwise the (initial k states and k number of successors of the states = 2k) states are placed in a
pool. The pool is then sorted numerically. The highest k states are selected as new initial states.
This process continues until a maximum value is reached.

function BeamSearch( problem, k), returns a solution state.

start with k randomly generated states


loop
generate all successors of all k states
if any of the states = solution, then return the state
else select the k best successors

Page
25
end

Simulated Annealing

Annealing is the process of heating and cooling a metal to change its internal structure for
modifying its physical properties. When the metal cools, its new structure is seized, and the
metal retains its newly obtained properties. In simulated annealing process, the temperature is
kept variable.

We initially set the temperature high and then allow it to ‘cool' slowly as the algorithm proceeds.
When the temperature is high, the algorithm is allowed to accept worse solutions with high
frequency.

Start

 Initialize k = 0; L = integer number of variables;


 From i → j, search the performance difference Δ.
 If Δ <= 0 then accept else if exp(-Δ/T(k)) > random(0,1) then accept;
 Repeat steps 1 and 2 for L(k) steps.
 k = k + 1;

Repeat steps 1 through 4 till the criteria is met.

End

Travelling Salesman Problem

In this algorithm, the objective is to find a low-cost tour that starts from a city, visits all cities en-
route exactly once and ends at the same starting city.

Start
Find out all (n -1)! Possible solutions, where n is the total number of
cities.
Determine the minimum cost by finding out the cost of each of these (n -1)!
solutions.
Finally, keep the one with the minimum cost.
end

Page
26
Page
27
Introduction to Genetic Algorithms — Including Example Code

A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural
evolution. This algorithm reflects the process of natural selection where the fittest individuals are
selected for reproduction in order to

produce offspring of the next generation.

Notion of Natural Selection

The process of natural selection starts with the selection of fittest individuals from a population.
They produce offspring which inherit the characteristics of the parents and will be added to the
next generation. If parents have better fitness, their offspring will be better than parents and have
a better chance at surviving. This process keeps on iterating and at the end, a generation with the
fittest individuals will be found.

This notion can be applied for a search problem. We consider a set of solutions for a problem
and select the set of best ones out of them.

Page
28
Five phases are considered in a genetic algorithm.

1. Initial population
2. Fitness function
3. Selection
4. Crossover
5. Mutation

Initial Population

The process begins with a set of individuals which is called a Population. Each individual is a
solution to the problem you want to solve.

An individual is characterized by a set of parameters (variables) known as Genes. Genes are


joined into a string to form a Chromosome (solution).

In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of
an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the
genes in a chromosome.

Fitness Function

The fitness function determines how fit an individual is (the ability of an individual to compete
with other individuals). It gives a fitness score to each individual. The probability that an
individual will be selected for reproduction is based on its fitness score.

Page
29
Selection

The idea of selection phase is to select the fittest individuals and let them pass their genes to the
next generation.

Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with
high fitness have more chance to be selected for reproduction.

Crossover

Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be
mated, a crossover point is chosen at random from within the genes.

For example, consider the crossover point to be 3 as shown below.

A1 = 000111= 0*25 + 0*24 +0*23 +1*22 +1*21 +1*20 =0+0+0+4+2+1=7

A2 = 111000=1*25 + 1*24 +1*23 +0*22 +0*21 +0*20 =32+16+8=56

Offspring are created by exchanging the genes of parents among themselves until the crossover
point is reached.

Page
30
The new offspring are added to the population.

Mutation

In certain new offspring formed, some of their genes can be subjected to a mutation with a low
random probability. This implies that some of the bits in the bit string can be flipped.

Mutation occurs to maintain diversity within the population and prevent premature convergence.

Page
31
Termination

The algorithm terminates if the population has converged (does not produce offspring which are
significantly different from the previous generation). Then it is said that the genetic algorithm
has provided a set of solutions to our problem.

Comments

The population has a fixed size. As new generations are formed, individuals with least fitness
die, providing space for new offspring.

The sequence of phases is repeated to produce individuals in each new generation which are
better than the previous generation.

Mini-Max Algorithm in Artificial Intelligence


o Mini-max algorithm is a recursive or backtracking algorithm which is used in decision-
making and game theory. It provides an optimal move for the player assuming that
opponent is also playing optimally.
o Mini-Max algorithm uses recursion to search through the game-tree.
o Min-Max algorithm is mostly used for game playing in AI. Such as Chess, Checkers, tic-
tac-toe, go, and various tow-players game. This Algorithm computes the minimax
decision for the current state.
o In this algorithm two players play the game, one is called MAX and other is called MIN.
o Both the players fight it as the opponent player gets the minimum benefit while they get
the maximum benefit.
o Both Players of the game are opponent of each other, where MAX will select the
maximized value and MIN will select the minimized value.
o The minimax algorithm performs a depth-first search algorithm for the exploration of the
complete game tree.
o The minimax algorithm proceeds all the way down to the terminal node of the tree, then
backtrack the tree as the recursion.

Working of Min-Max Algorithm:

Page
32
o The working of the minimax algorithm can be easily described using an example.
Below we have taken an example of game-tree which is representing the two-
player game.
o In this example, there are two players one is called Maximizer and other is called
Minimizer.
o Maximizer will try to get the Maximum possible score, and Minimizer will try to
get the minimum possible score.
o This algorithm applies DFS, so in this game-tree, we have to go all the way
through the leaves to reach the terminal nodes.
o At the terminal node, the terminal values are given so we will compare those
value and backtrack the tree until the initial state occurs. Following are the main
steps involved in solving the two-player game tree:

Step-1: In the first step, the algorithm generates the entire game-tree and apply the
utility function to get the utility values for the terminal states. In the below tree diagram,
let's take A is the initial state of the tree. Suppose maximizer takes first turn which has
worst-case initial value =- infinity, and minimizer will take next turn which has worst-
case initial value = +infinity.

Page
33
Step 2: Now, first we find the utilities value for the Maximizer, its initial value is -∞, so
we will compare each value in terminal state with initial value of Maximizer and
determines the higher nodes values. It will find the maximum among the all.

5.3M
141
C++ vs Java

o For node D         max(-1,- -∞) => max(-1,4)= 4


o For Node E         max(2, -∞) => max(2, 6)= 6
o For Node F         max(-3, -∞) => max(-3,-5) = -3
o For node G         max(0, -∞) = max(0, 7) = 7

Page
34
Step 3: In the next step, it's a turn for minimizer, so it will compare all nodes value with
+∞, and will find the 3rd layer node values.

o For node B= min(4,6) = 4


o For node C= min (-3, 7) = -3

Page
35
Step 4: Now it's a turn for Maximizer, and it will again choose the maximum of all nodes
value and find the maximum value for the root node. In this game tree, there are only 4
layers, hence we reach immediately to the root node, but in real games, there will be
more than 4 layers.

o For node A max(4, -3)= 4

Page
36
That was the complete workflow of the minimax two player game.

Page
37

You might also like