Search Algorithms in AI: Last Updated: 22 Mar, 2023
Search Algorithms in AI: Last Updated: 22 Mar, 2023
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.
Open In App
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, exploring the algorithms given there.
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 canOpen In App
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:
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.
Open In App
Path: S -> A -> B -> C -> G
= the depth of the search tree = the number of levels of the search
tree.
= number of nodes in level .
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.
Open In App
= 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.
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?
Open In App
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 on the UCS strategy, the path with the least
cumulative cost is chosen. Note that due to the many options in the
fringe,
Python the algorithm
for Machine Learning explores most of
Machine Learning them
with so longLearning
R Machine as their cost is
Algorithms low,
EDA Ma
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:
Open In App
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.
Open In App
Path: S -> D -> E -> G
A* Tree Search:
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 work is shown in the table below.
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
Open In App
S -> D -> B 4 2+1= 7
3
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:
Open In App
Example:
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.
Open In App
Summer-time is here and so is the time to skill-up! More than 5,000
learners have now completed their journey from basics of DSA to
advanced level development programs such as Full-Stack, Backend
Development, Data Science.
82 Suggest improvement
Next
Similar Reads
Open In App
Implement Phonetic Search in Similarity Search for Time-Series
Python with Soundex Algorithm Data
M MdRafiAk…
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
Open In App
R Language Competitive Programming
Android Tutorial
Open In App