0% found this document useful (0 votes)
277 views19 pages

Search Algorithms in AI

The document discusses various search algorithms used in artificial intelligence, including uninformed search algorithms like depth-first search, breadth-first search, and uniform cost search. It also covers informed search algorithms like greedy search, A* tree search, and A* graph search. Key aspects like heuristics, strategies, time and space complexity are explained for each algorithm through examples.

Uploaded by

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

Search Algorithms in AI

The document discusses various search algorithms used in artificial intelligence, including uninformed search algorithms like depth-first search, breadth-first search, and uniform cost search. It also covers informed search algorithms like greedy search, A* tree search, and A* graph search. Key aspects like heuristics, strategies, time and space complexity are explained for each algorithm through examples.

Uploaded by

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

GEEKSFORGEEKS

Search Algorithms in AI

Arti cial Intelligence is the study of building agents that act rationally. Most of the time,
these agents perform some kind of search algorithm in the background in order to
achieve their tasks.

A search problem consists of:


A State Space. Set of all possible states where you can be.
A Start State. The state from where the search begins.
A Goal Test. A function that looks at the current state returns whether or not it is the
goal state.

The Solution to a search problem is a sequence of actions, called the plan that
transforms the start state to the goal state.
This plan is achieved through search algorithms.

Types of search algorithms

There are far too many powerful search algorithms out there to t in a single article.
Instead, this article will discuss six of the fundamental search algorithms, divided into
two categories, as shown below.

Apps & videos for workouts


Gym load and mental relax with best apps and
videos for sport and healthy lifestyle

Fitness Club Open


Note that there is much more to search algorithms that the chart I have provided above.
However, this article will mostly stick to the above chart, exploring the algorithms given
there.

Uninformed Search Algorithms

The search algorithms in this section have no additional information on the goal node
other than the one provided in the problem de nition. The plans to reach the goal state
from the start state di er only by the order and/or length of actions. Uninformed search
is also called Blind search.

The following uninformed search algorithms are discussed in this section.

1. Depth First Search


2. Breath First Search
3. Uniform Cost Search

Each of these algorithms will have:

A problem graph, containing the start node S and the goal node G.
A strategy, describing the manner in which the graph will be traversed to get to G .
A fringe, which is a data structure used to store all the possible states (nodes) that you
can go from the current states.
A tree, that results while traversing to the goal node.
A solution plan, which the sequence of nodes from S to G.

Depth First Search

Depth- rst search (DFS) is an algorithm for traversing or searching tree or graph data
structures. The algorithm starts at the root node (selecting some arbitrary node as the
root node in the case of a graph) and explores as far as possible along each branch before
backtracking.

Example:

Question. Which solution would DFS nd to move from node S to node G if run on the
graph below?
Solution. The equivalent search tree for the above graph is as follows. As DFS traverses
the tree “deepest node rst”, it would always pick the deeper branch until it reaches the
solution (or it runs out of nodes, and goes to the next branch). The traversal is shown in
blue arrows.

Path: S -> A -> B -> C -> G

Let = the depth of the search tree = number of levels of the search tree.

= number of nodes in level .

Time complexity: Equivalent to the number of nodes traversed in DFS.

Space complexity: Equivalent to how large can the fringe get.


Completeness: DFS is complete if the search tree is nite, meaning for a given nite
search tree, DFS will come up with a solution if it exists.
Optimality: DFS is not optimal, meaning the number of steps in reaching the solution, or
the cost spent in reaching it is high.
Breadth First Search

Breadth- rst search (BFS) is an algorithm for traversing or searching tree or graph data
structures. It starts at the tree root (or some arbitrary node of a graph, sometimes
referred to as a ‘search key’), and explores all of the neighbor nodes at the present depth
prior to moving on to the nodes at the next depth level.

Example:

Question. Which solution would BFS nd to move from node S to node G if run on the
graph below?

Solution. The equivalent search tree for the above graph is as follows. As BFS traverses
the tree “shallowest node rst”, it would always pick the shallower branch until it
reaches the solution (or it runs out of nodes, and goes to the next branch). The traversal is
shown in blue arrows.
Path: S -> D -> G

Let = the depth of the shallowest solution.

= number of nodes in level .

Time complexity: Equivalent to the number of nodes traversed in BFS until the

shallowest solution.

Space complexity: Equivalent to how large can the fringe get.


Completeness: BFS is complete, meaning for a given search tree, BFS will come up with a
solution if it exists.
Optimality: BFS is optimal as long as the costs of all edges are equal.
Uniform Cost Search

UCS is di erent from BFS and DFS because here the costs come into play. In other words,
traversing via di erent edges might not have the same cost. The goal is to nd a path
where the cumulative sum of costs is least.

Cost of a node is de ned as:

cost(node) = cumulative cost of all nodes from root

cost(root) = 0

Example:

Question. Which solution would UCS nd to move from node S to node G if run on the
graph below?
Solution. The equivalent search tree for the above graph is as follows. Cost of each node is
the cumulative cost of reaching that node from the root. Based on UCS strategy, the path
with least cumulative cost is chosen. Note that due to the many options in the fringe, the
algorithm explores most of them so long as their cost is low, and discards them when a
lower cost path is found; these discarded traversals are not shown below. The actual
traversal is shown in blue.
Path: S -> A -> B -> G

Cost: 5

Let = cost of solution.


= arcs cost.

Then e ective depth

Time complexity:

Space complexity:

Advantages:

UCS is complete.
UCS is optimal.

Disadvantages:

Explores options in every “direction”.


No information on goal location.

Informed Search Algorithms

Here, the algorithms have information on the goal state, which helps in more e cient
searching. This information is obtained by something called a heuristic.
In this section, we will discuss the following search algorithms.

1. Greedy Search
2. A* Tree Search
3. A* Graph Search

Search Heuristics: In an informed search, a heuristic is a function that estimates how


close a state is to the goal state. For examples – Manhattan distance, Euclidean distance,
etc. (Lesser the distance, closer the goal.)
Di erent heuristics are used in di erent informed algorithms discussed below.

Greedy Search

In greedy search, we expand the node closest to the goal node. The “closeness” is
estimated by a heuristic h(x) .

Heuristic: A heuristic h is de ned as-


h(x) = Estimate of distance of node x from the goal node.
Lower the value of h(x), closer is the node from the goal.

Strategy: Expand the node closest to the goal state, i.e. expand the node with lower h
value.

Example:

Question. Find the path from S to G using greedy search. The heuristic values h of each
node below the name of the node.
Solution. Starting from S, we can traverse to A(h=9) or D(h=5). We choose D, as it has the
lower heuristic cost. Now from D, we can move to B(h=4) or E(h=3). We choose E with lower
heuristic cost. Finally, from E, we go to G(h=0). This entire traversal is shown in the search
tree below, in blue.
Path: S -> D -> E -> G

Advantage: Works well with informed search problems, with fewer steps to reach a goal.
Disadvantage: Can turn into unguided DFS in the worst case.

A* Tree Search

A* Tree Search, or simply known as A* Search, combines the strengths of uniform-cost


search and greedy search. In this search, the heuristic is the summation of the cost in
UCS, denoted by g(x), and the cost in greedy search, denoted by h(x). The summed cost is
denoted by f(x).

Heuristic: The following points should be noted wrt heuristics in A* search.

Here, h(x) is called the forward cost, and is an estimate of the distance of the current
node from the goal node.
And, g(x) is called the backward cost, and is the cumulative cost of a node from the
root node.
A* search is optimal only when for all nodes, the forward cost for a node h(x)
underestimates the actual cost h*(x) to reach the goal. This property of A* heuristic is
called admissibility.

Admissibility:
Strategy: Choose the node with lowest f(x) value.

Example:

Question. Find the path to reach from S to G using A* search.

Solution. Starting from S, the algorithm computes g(x) + h(x) for all nodes in the fringe at
each step, choosing the node with the lowest sum. The entire working is shown in the
table below.

Note that in the fourth set of iteration, we get two paths with equal summed cost f(x), so
we expand them both in the next set. The path with lower cost on further expansion is the
chosen path.
Path h(x) g(x) f(x)

S 7 0 7

       
S -> A 9 3 12

S -> D ✔ 5 2 7

       

S -> D -> B ✔ 4 2+1=3 7

S -> D -> E 3 2+4=69

       

S -> D -> B -> C ✔ 2 3+2=5 7

S -> D -> B -> E ✔ 3 3+1=4 7

       
S -> D -> B -> C -> G 0 5+4=99

S -> D -> B -> E -> G ✔0 4+3=7 7


Path: S -> D -> B -> E -> G

Cost: 7

A* Graph Search
A* tree search works well, except that it takes time re-exploring the branches it has
already explored. In other words, if the same node has expanded twice in di erent
branches of the search tree, A* search might explore both of those branches, thus
wasting time
A* Graph Search, or simply Graph Search, removes this limitation by adding this rule:
do not expand the same node more than once.
Heuristic. Graph search is optimal only when the forward cost between two successive
nodes A and B, given by h(A) - h (B) , is less than or equal to the backward cost between
those two nodes g(A -> B). This property of graph search heuristic is called
consistency.

Consistency:

Example

Question. Use graph search to nd path from S to G in the following graph.

Solution. We solve this question pretty much the same way we solved last question, but in
this case, we keep a track of nodes explored so that we don’t re-explore them.
Path: S -> D -> B -> C -> E -> G

Cost: 7

References

CS 188 Lecture Slides


AI: A Modern Approach, 3e
Recommended Posts:

Meta Binary Search | One-Sided Binary Search


Binary Search in PHP
Interpolation Search
Binary Search In JavaScript
How Google Search Works!!
Indexed Sequential Search
2-3 Trees | (Search and Insert)
How to detect search engine bots with PHP ?
8085 program for Binary search
Voice search Wikipedia using Python
How ranking in Google Search Works !
Print all even nodes of Binary Search Tree
Print all odd nodes of Binary Search Tree
What is Search Engine and Google Page Ranking?
ML | Monte Carlo Tree Search (MCTS)

MdRa Akhtar
Check out this Author's contributed articles.
If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to [email protected]. See
your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you nd anything incorrect by clicking on the "Improve
Article" button below.

Article Tags : Machine Learning Technical Scripter Artificial Intelligence Technical Scripter 2018

Practice Tags : Machine Learning

Read Full Article


710-B, Advant Navis Business Park,
Sector-142, Noida, Uttar Pradesh - 201305
[email protected]

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise GBlog
Contests Videos
Subjective Questions

@geeksforgeeks, Some rights reserved

You might also like