ARTIFICIAL INTELLIGENCE
LECTURE-4:
SOLVING PROBLEMS BY SEARCHING
By MUHAMMAD AFZAL
Solving Problems by
2
Searching
Sometimes, it's not easy for an agent to know
what to do next.
When this happens, a agent might need to
think ahead.
It can plan a series of steps to reach a goal. We
call this program a problem-solving agent, and
the process of planning is called searching.
Search algorithms are distinguished as:
informed algorithms: agent can estimate how far it
is from the goal
uninformed algorithms: where no such estimate is
available.
Problem-Solving Agents
3
Problem-solving agent
A kind of goal-based agent
It solves problem by
finding sequences of actions that lead to target
If the environment is unknown and the agent has no
additional information, then it can execute one of
the action randomly
Problem-Solving Agents
4
To solve a problem, an agent can follow four-
stages process:
1. GOAL FORMULATION
2. PROBLEM FORMULATION
3. SEARCH
4. EXECUTION
GOAL FORMULATION
This is first stage of problem solving agent based on
the current situation.
Goals help us focus on what we want to achieve.
They limit what we try to do, so we can focus on
important things.
Problem-Solving Agents
5
PROBLEM FORMULATION: the agent plans a
description of the states and actions necessary to
reach the goal
SEARCH: Before taking any action in the real
world, the agent simulates sequences of actions in
its model, searching until it finds a sequence of
actions that reaches the goal. Such a sequence is
called a solution
EXECUTION: The agent can now execute the
actions in the solution, one at a time.
Problem-Solving Agents
6
A simplified road map of part of Romania, with road distances in
Problem-Solving Agents
7
In a fully observable and known environment,
the solution to any problem is a fixed
sequence of actions: start from Arad drive to
Sibiu, then Fagaras, then Bucharest
If the model is correct, and the agent has
found a solution, it can ignore its percepts
while it is executing the actions, This is called
an open-loop system.
If the model is incorrect, or the environment
is unobservable, then the agent would be
safer using a closed-loop approach that
Search Algorithms
8
A search algorithm takes a search problem as
input and returns a solution, or an indication of
failure
All problems are transformed as a search
tree.
Search Tree: A tree representation of search
problem is called Search tree
Search Space: Search space represents a set
of possible solutions, which a system may
have.
Initial state
Three partial search
trees for finding a
Search Algorithms
route from Arad to
Bucharest
9
10
Search Algorithms
11
Now we must choose which of these three
child nodes to consider next.
If we select Sibiu first, we call this the
frontier of the search tree
Any state for which a node has been
generated is called as reached
While a search tree may have cyclic paths:
A-B-A-B-A-B
A good search strategy should avoid cyclic
paths
Search Algorithms
12
A node is having five components:
STATE: which state it is in the state space
PARENT-NODE: from which node it is generated
ACTION: which action applied to its parent-node
to generate it
PATH-COST: the cost from initial state to the node
n itself
DEPTH: number of steps along the path from the
initial state
Measuring problem-solving
performance
13
The evaluation of a search strategy
Completeness:
is
the strategy guaranteed to find a solution when
there is one?
Optimality:
does
the strategy find the highest-quality solution
when there are several different solutions?
Time complexity:
how long does it take to find a solution?
Space complexity:
howmuch memory is needed to perform the
search?
Types of search algorithms
14
Based on the search problems we can
classify the search algorithms into:
Uninformed (Blind
search/Brute-force/exhaustive search)
algorithm
An uninformed search algorithm is given no
clue about how close a state is to the goal(s).
Informed search (Heuristic) search
algorithm
An informed search knows which state is close
to the goal(s)
Types of search algorithms
15
Un-informed Search Strategies
16
Breadth-first search (BFS):
BFS algorithm starts searching from
the root node of the tree and expands
all successors node at the current level
before moving to nodes of the next
level then their successors, and so on.
The breadth-first search algorithm is an
example of a general-graph search
algorithm
Uninformed Search Strategies
17
BFS uses an early goal test, checking
whether a node is a solution as soon as it is
generated
Breadth-first search on a simple
binary tree
Un-informed Search Strategies
18
Advantages:
BFS will provide a solution if any solution exists
If there are more than one solutions for a given
problem, then BFS will provide the minimal
solution which requires the least number of
steps
Disadvantages:
It requires lots of memory since each level of
the tree must be saved into memory to expand
the next level
BFS needs lots of time if the solution is far away
from the root node
Un-informed Search Strategies
19
Example:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I----
>K
Un-informed Search Strategies
20
Uniform Cost Search: This search
algorithm works when a different cost is
available for each edge
It is a searching algorithm used for
traversing a weighted tree or graph
It finds a path to the goal node which
has the lowest cumulative cost
It can be used to solve any graph/tree
where the optimal cost is in demand
Un-informed Search Strategies
21
The shortest path
here is
A−>B−>C−>E
forming the cost
17
Un-informed Search Strategies
22
Advantages:
Uniform cost search is optimal because at every
state the path with the least cost is chosen
Disadvantages:
It does not care about the number of steps involve
in searching and only concerned about path cost
Due to which this algorithm may be stuck in an
infinite loop
Un-informed Search Strategies
23
Depth-first search: Depth-first search always
expands the deepest node in the frontier first
It starts from the root node and follows each path to
its greatest depth node before moving to the next
path
Only when the search hits a dead end
goes back and expands nodes at shallower levels
Dead end leaf nodes but not the goal
DFS uses a stack data structure for its
implementation
Depth-first search
Depth-first search
Depth-first search
26
Advantage:
DFS requires very less memory as it only needs
to store a stack of the nodes on the path from
root node to the current node
It takes less time to reach to the goal node than
BFS algorithm (if it traverses in the right path)
Disadvantage:
DFS algorithm goes for deep down searching
and sometime it may go to the infinite loop
Informed Search Strategies
27
Informed Search also called as Heuristic
Search
Informed search algorithms use domain
knowledge
In an informed search, problem information is
available which can guide the search
Informed search strategies can find a solution
more efficiently than an uninformed search
strategy
Informed search can solve much complex
problem which could not be solved in another
Informed Search Strategies
28
Best-first Search Algorithm (Greedy
Search):
A greedy algorithm is an approach for
solving a problem by selecting the
best option available at the
moment
Greedy best-first search algorithm
always selects the path which appears
best at that moment
Best-first Search Algorithm (Greedy
Search):
29
Best-first Search Algorithm (Greedy
Search):
30
Best-first Search Algorithm (Greedy
Search):
31
Advantages:
This algorithm is more efficient than BFS and
DFS algorithms.
Disadvantages:
It can get stuck in a loop as DFS.
This algorithm is not optimal.
Informed Search Strategies
32
A* search:
A* search algorithm is a simple and efficient
that can be used to find the optimal path
between two nodes in a graph
It searches for the shortest path between
the initial and the final state
Informed Search Strategies
33
Informed Search Strategies
34
Advantages:
A* search algorithm is the best algorithm than
other search algorithms.
A* search algorithm is optimal and complete.
This algorithm can solve very complex problems.
Disadvantages:
It does not always produce the shortest path as it
mostly based on heuristics and approximation.
The main drawback of A* is memory requirement
as it keeps all generated nodes in the memory, so
it is not practical for various large-scale problems.