Search Algorithms in AI
Search Algorithms in AI
Courses @90% Refund Data Science Data Science Projects Data Analysis Data Visualization Machine Learni
Search Algorithms in AI
Last Updated : 22 Mar, 2023
Artificial 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.
There are far too many powerful search algorithms out there to fit in a single
article. Instead, this article will discuss six of the fundamental search
algorithms, divided into two categories, as shown below.
Note that there is much more to search algorithms than the chart I have
provided above. However, this article will mostly stick to the above chart,
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 1/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
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.
Example:
Question. Which solution would DFS find 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 first”, 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.
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 3/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
= the depth of the search tree = the number of levels of the search tree.
= number of nodes in level .
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 4/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
Example:
Question. Which solution would BFS find 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 first”, 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.
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 5/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
Optimality: BFS is optimal as long as the costs of all edges are equal.
UCS is different from BFS and DFS because here the costs come into play. In
other words, traversing via different edges might not have the same cost. The
goal is to find a path where the cumulative sum of costs is the least.
Example:
Question. Which solution would UCS find 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. The cost
of each node is the cumulative cost of reaching that node from the root. Based
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 6/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
on the UCS strategy, the path with the 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.
Advantages:
UCS is complete only if states are finite and there should be no loop with
zero weight.
UCS is optimal only if there is no negative cost.
Disadvantages:
Here, the algorithms have information on the goal state, which helps in more
efficient 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
Greedy Search:
In greedy search, we expand the node closest to the goal node. The
“closeness” is estimated by a heuristic h(x).
Strategy: Expand the node closest to the goal state, i.e. expand the node with
a 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.
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 8/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
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:
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 9/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
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:
Example:
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
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 10/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
Note that in the fourth set of iterations, we get two paths with equal summed
cost f(x), so we expand them both in the next set. The path with a lower cost
on further expansion is the chosen path.
S 7 0 7
S -> A 9 3 12
S -> D 5 2 7
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 11/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
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 different 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 the
graph search heuristic is called consistency.
Consistency:
Example:
Question. Use graph searches to find paths from S to G in the following graph.
the 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.
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 12/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
Get IBM Certification and a 90% fee refund on completing 90% course in
90 days! Take the Three 90 Challenge today.
Master Machine Learning, Data Science & AI with this complete program and
also get a 90% refund. What more motivation do you need? Start the
challenge right away!
Similar Reads
Artificial Intelligence Tutorial | AI Tutorial
Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to
think and act like humans. It involves the development of algorithms and computer programs that can perfor…
7 min read
History of AI
The term Artificial Intelligence (AI) is already widely used in everything from smartphones to self-driving cars.
AI has come a long way from science fiction stories to practical uses. Yet What is artificial intelligence and…
7 min read
Types of AI
11 min read
Problem Solving in AI
Search Algorithms in AI
Artificial 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…
10 min read
8 min read
10 min read
4 min read
14 min read
Learning in AI
Generative AI
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Advertise with us
Company Languages
About Us Python
Legal Java
In Media C++
Contact Us PHP
Advertise with us GoLang
GFG Corporate Solution SQL
Placement Training Program R Language
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 15/17
13/01/2025, 21:31 Search Algorithms in AI - GeeksforGeeks
GeeksforGeeks Community Android Tutorial
Tutorials Archive
https://www.geeksforgeeks.org/search-algorithms-in-ai/ 17/17