You are on page 1of 94

CSEN2301 | ARTIFICIAL

INTELLIGENCE

Dr. MRUDULA OWK


ASSISTANT PROFESSOR
Dept.Of CSE, GIT,
GITAM UNIVERSITY
[Deemed to be University]
Topic ‘s Covered ?
Solving Problems by Searching:
Problem solving agents.
Example problems.
Searching for solutions.
Uninformed search strategies.
Avoiding repeated states.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


What is Search?:
 An agent with several immediate options of unknown value can decide
what to do by first examining different possible sequences of actions that
lead to states of known value, and then choosing the best sequence.
The process of looking for sequences actions from the current state to
reach the goal state is called search.
 Search is the systematic examination of states to find path from the
start/root state to the Goal State.
The set of possible states, together with operators defining their
connectivity constitute the search space.
 The output of a search algorithm is a solution, that is, a path from the
initial state to a state that satisfies the goal test.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


What is Searching for Solution?:
There are many ways to represent nodes, but we will assume that a node is a
data structure with five Components:
STATE: the state in the state space to which the node corresponds;
PARENT-NODE: the node in the search tree that generated this node;
ACTION: the action that was applied to the parent to generate the node;
PATH-COST: the cost, traditionally denoted by g ( n ) , of the path from the
initial state to the node, as indicated by the parent pointers; and
DEPTH: the number of steps along the path from the initial state.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


What is Search Space?:
 “Unlike state space, which is a physical configuration, the search space is
an abstract configuration represented by a search tree or graph of possible
solutions.”
A search tree is used to model the sequence of actions. It is constructed
with initial state as the root. The actions taken make the branches and the
nodes are results of those actions.
A node has depth, path cost and associated state in the state space.
The search space is divided into 3 regions, namely
 Explored
 Frontier
 Unexplored

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Types of Search?:
 Two Types of Search
 Uninformed Search
 Informed Search
Uninformed Search: This type of search does not use any domain
knowledge.(also called Blind)
 BFS (Breadth First Search): It expands the shallowest node (node
having lowest depth) first.
 DFS (Depth First Search): It expands deepest node first.
 DLS (Depth Limited Search): It is DFS with a limit on depth.
 IDS(Iterative Deepening Search): It is DFS with increasing limit
 UCS (Uniform Cost Search): It expands the node with least cost
(Cost for expanding the node).

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Types of Search?:
 Informed Search :This type of search uses domain knowledge.( also called
Heuristic)
 Greedy Search (best first search) : It expands the node that
appears to be closest to goal.
 A* Search: Minimize the total estimated solution cost that includes
cost of reaching a state and cost of reaching goal from that state.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Problem Solving Agent:
One kind of goal-based agent called a problem-solving agent.
Problem-solving agents decide what to do by finding sequences of actions
that lead to desirable states.
Goal : indicates solution of the problem
Goal Formation : based on the current situation and the agent's
performance measure, is the first step in problem solving. The agent’s task is
to find out which sequence of actions will get to a goal state.
Problem Formation : is the process of deciding what actions and states to
consider, given a goal.
Search: a general mechanism that can be used when no more direct
method is known.
Solution : a search algorithm takes a problem as input and returns a
solution in the form of an action sequence.
Execution phase : it carries out the actions recommended by a solution.
Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE
Problem Solving Agent:
function SIMPLE-PROBLEM-SOLVING-AGENT( percept) returns an action
static: seq, an action sequence, initially empty
state, some description of the current world state
goal, a goal, initially null
problem, a problem formulation
state UPDATE-STATE(state, percept)
if seq is empty then do
goal FORMULATE-GOAL(state)
problem FORMULATE-PROBLEM(state, goal)
seq SEARCH( problem)
if seq = failure then return a null action
action FIRST(seq);
seq REST(seq)
return action

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Problem Solving Agent:
Here the agent’s environment is:
 static : because formulating and solving the problem is done without
paying attention to changes occurring in the environment
 observable : agent’s design assumes that initial state is discrete – we can
enumerate “alternative courses of action”.
 deterministic : solutions to problems are single sequences of actions
(so, they can’t handle any unexpected events)
: also solutions are executed without paying attention to
the percepts.
(agent is open-loop system because ignoring the
percepts breaks the loop between agent and
environment)

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Well-defined Problems and Solutions :
A well-defined problem can be described by:
 A problem can be defined by five components:
(i) Initial State (ii) Actions (iii) Transition Model
(iv) Goal test (v) Path cost
Initial state
Operator or successor function - for any state x returns s(x), the set of
states reachable from x with one action.
State space - all states reachable from initial by any sequence of actions.
Path - sequence through state space.
Path cost - function that assigns a cost to a path. Cost of a path is the sum
of costs of individual actions along the path.
Goal test - test to determine if at goal state.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Route Finding Problem:

A simplified Road Map of part of Romania

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Route Finding Problem:

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Goal Formation for Route Finding Problem:
Problem Formation of Simplified road map of part of Romania
Initial State: the agent starts in here
from the diagram, it is described as In(A).
Actions: a description of possible actions available to the agent
Ex: SUCCESSOR-FN (x) (x is a given state)
It returns a set of <action, successor> ordered pairs where:
 each action is one of the legal actions in state x and
 each successor is a state that can be reached from x by applying the
action
Ex: from the state In(A), the successor function would return
{ <Go(D), In(D)>, <Go(E), In(E)>, <Go(B), In(B)> }
State space -- the set of all states reachable from the initial state
Path in a state space -- a sequence of states connected by a sequence of actions

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Goal Formation for Route Finding Problem:
Problem Formation of Simplified road map of part of Romania
Transition model:
 A description of what each action does.
 It is specified by a function RESULT(s, a) that returns the state that results
from doing action a in state s.
 We also use the term successor to refer to any state reachable from a
given state by a single action.
Ex: We have
RESULT(In(Arad),Go(Zerind)) = In(Zerind)
Goal test: It determines whether a given state is a goal state.
-- Here, the goal is the singleton set { In(M) }.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Goal Formation for Route Finding Problem:
Problem Formation of Simplified road map of part of Romania
Path cost: This function assigns a numeric cost to each path.

 Here, the cost of a path is the sum of the costs of individual actions
along the path.
 Cost of a path is its length in kilometers.

Step cost -- it is the cost of taking action a to go from state x to state y denoted by
c(x, a, y).
Solution -- a path from the initial state to a goal state
Optimal solution – it has the lowest path cost among all solutions

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Goal Formation for Route Finding Problem:

Goal Formation of Route Finding Problem


On holiday in Romania: currently in Arad.
Flight leaves tomorrow from Bucharest
Formulate goal: be in Bucharest
Formulate problem:
states: various cities
actions: drive between cities
Find solution: sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Problem Formation for Route Finding Problem:
Problem formulation of Route Finding Problem
A problem is defined by four items:
The initial state that the agent starts in. For example, the initial state for our agent
in Romania might be described as In(Arad) e.g., “at Arad"
successor function S(x) = set of action-state pairs ; e.g., S (Arad) = {[Arad -> Zerind;
Zerind]…}
{ (G o(Sibzu),I n(Sibiu)), (Go(T imisoara), In( Tzmisoara)), (Go(Zerznd),I n(Zerind))
goal test, can be explicit, e.g., x = at Bucharest" ; implicit, e.g., NoDirt(x)
path cost (additive)
e.g., sum of distances, number of actions executed, etc. c(x; a; y) is the step cost,
assumed to be >= 0
A solution is a sequence of actions leading from the initial state to a goal state.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Problem Formation for Route Finding Problem:
Problem formulation of Route Finding Problem
The above formulation of the problem omits many aspects of the real world.
The initial state In(A) can include:
traveling companions,
condition of the road,
weather and so on.
Since these are irrelevant, these details are removed from the state description.
This is called abstraction.
We must also use abstract actions.
We consider only the change in location (other actions like taking up
time, consuming fuel, generating pollution etc. are omitted)

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Problem Formation for Route Finding Problem:
Problem formulation of Route Finding Problem
The above formulation of the problem omits many aspects of the real world.
The initial state In(A) can include:
traveling companions,
condition of the road,
weather and so on.
Since these are irrelevant, these details are removed from the state description.
This is called abstraction.
We must also use abstract actions.
We consider only the change in location (other actions like taking up
time, consuming fuel, generating pollution etc. are omitted)

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:

The problem-solving approach has been applied to a variety of task environments.

Two of them are:

 Toy problems -- intended to illustrate various problem-solving methods

 Real-world problems -- people are interested in the solutions of these problems

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Toy Problems: Vacuum world
Problem Formulation:
States: Agent is in one of two locations, each of which might or might not contain
dirt.
Thus, there are 2x22 = 8 possible world states.

Initial State: Any state can be designated as the initial state.


Actions: Left, Right and Suck(see the above figure)
Transition Model: The actions have their expected effects, except that moving Left in
the leftmost square, moving Right in the rightmost square, and Sucking in a clean
square have no effect.
Goal test:It checks whether all the squares are clean.
Path cost: Each step cost is 1, so the path cost is the number of steps in the path.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Toy Problems: 8 - Puzzle
Problem Formulation:
States: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.

Initial State: Any state can be designated as the initial state.


Actions: Left, Right, Up or Down (movement of the blank space)
Transition model: Given a state and action, this returns the resulting state;
-- here, if we apply Left to the start state in Figure 3.4,
the resulting state has the 5 and the blank switched.
Goal test: This checks whether the state matches the goal configuration.
Path cost: Each step costs 1, so the path cost is the number of steps in the path.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Toy Problems: 8 – Queen Problem
To place 8 queens on a chess board such that no queen attacks any other (in the
same row, column or diagonal).

Problem Formulation:
States: Any arrangement of 0 to 8 queens on the board is a state.
Initial State: No queens on the board.
Actions: Add a queen to any empty square.
Transition model: Returns the board with a queen added to the specified square.
Goal test: 8 queens are on the board, none attacked.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Real World Problems : Touring Problems

These are closely related to route-finding problems but with an important difference.

For example, from the road map figure,


“visit every city at least once, starting and ending in city M”.

The state space is also different.


Each state must include not just the current location but also the set of cities the
agent has visited.
Ex: The initial state would be: “In M; visited { M }”
An intermediate state: “ In R; visited { M, O, R }”
Goal test could check whether the agent is in M and all 20 cities have been visited.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Real World Problems : VLSI Layout Problem

It requires positioning of millions of components and connections on a chip


-- to minimize area, minimize circuit delays and maximize manufacturing
yield.

There are two parts:

 Cell layout -- primitive components of the circuit are grouped into cells, each
of which performs some recognized function.
 Channel routing -- finds a specific route for each wire through the gaps
between the cells (search problem).

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Real World Problems : Robot Navigation

It is a generalization of the route-finding problem.

Rather than a discrete set of routes, a robot can move in a continuous space with
an infinite set of possible actions and states.

 2-D space : for a robot moving on a flat surface.


 Multidimensional space: when the robot has arms and legs or wheels that must
also be controlled.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Example Problems:
Real World Problems : Automatic Assembly Sequencing

The aim is to find an order in which to assemble the parts of some object.

If the wrong order is chosen, there will be no way to add some part later in the
sequence without undoing some of the work already done.

The Traveling Salesperson Problem:

It is a touring problem in which each city must be visited exactly once. The aim is
to find the shortest tour.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Search For Solutions:
The fringe of each tree consists of those nodes with bold outlines.
 We continue choosing, testing and expanding until either a solution is
found or there are no more states to be expanded.
The choice of which state to expand is determined by the Search Strategy.
 A partial search tree for finding the route from Arad to Bucharest is shown
in the next diagram (next slide).
There are three types of nodes:
Nodes that have been expanded
Nodes that have been generated but not yet expanded
Nodes that have not yet been generated

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Searching for Solution:

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Search For Solutions:
 We also need to represent the collection of nodes that have been
generated but not yet expanded—this collection is called the frontier (fringe
as per 2nd edition book) (sometimes called an open list).
Each element of the frontier is a leaf node, that is a node with no
successors in the tree.
In earlier slide figure, the frontier of each tree consists of those nodes with
bold outlines.
The search tree then would be a function that selects the next node to be
expanded from this set.
Therefore, we will assume that the collection of nodes is implemented as a
queue.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Implementation As A Queue:
The fringe of each tree consists of those nodes with bold outlines.
The simplest representation of the fringe would be a set of nodes.
The search strategy then would be a function that selects the next node to
be expanded from this set. Although this is conceptually straightforward, it
could be computationally expensive, because the strategy function might
have to look at every element of the set to choose the best one.
 Therefore, we will assume that the collection of nodes is implemented as
a queue. The operations on a queue are as follows:

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Implementation As A Queue:
MAKE-QUEUE (element…) creates a queue with the given element(s).
EMPTY? (queue) returns true only if there are no more elements in the
queue.
FIRST (queue) returns FIRST (queue) and removes it from the queue.
INSERT (element, queue) inserts an element into the queue and returns
the resulting queue.
INSERT-ALL(elements, queue) inserts a set of elements into the queue
and returns the resulting queue.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


An informal Description of Tree Search Algorithm:
function T R E E - S E A R C H (problem) returns a solution, or failure
initialize the frontier using the initial state of problem
loop do
if the frontier is empty then return failure
choose a leaf node for expansion according to frontier
if the node contains a goal state then return the corresponding solution
expand the chosen node , adding the resulting nodes to the frontier

An informal description of the general tree-search algorithm.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


An informal Description of Graph Search Algorithm:
function GRAPH - S E A R C H (problem) returns a solution, or failure
initialize the frontier using the initial state of problem
initialize the explored set to be empty
loop do
if the frontier is empty then return failure
choose a leaf node for expansion according to frontier
if the node contains a goal state then return the corresponding solution
add the node to the explored set
expand the chosen node , adding the resulting nodes to the frontier
only if not in the frontier or explored set

An informal description of the general Graph-search algorithm.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


 In some cases, redundant paths are unavoidable.
Ex: All problems where the actions are reversible, such as route-finding problems
and sliding-block puzzles (8-puzzle).
 Route-finding on a rectangular grid (like the one used later for Figure 3.9) is a
particularly important example in computer games. It has many redundant
paths.
 Thus, following redundant paths can cause a tractable problem to become
intractable. The way to avoid exploring redundant paths is to remember where
one has been.
 To do this, we augment the TREE-SEARCH algorithm with a data structure
called the explored set(also known as the closed list), which remembers every
expanded node.
 Newly generated nodes that match previously generated nodes—ones in the
explored set or the frontier—can be discarded instead of being added to the
frontier.
Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE
Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE
 Clearly, the search tree constructed by the GRAPH-SEARCH algorithm contains
at most one copy of each state.
The algorithm has another nice property: the frontier separates the state-
space graph into the explored region and the unexplored region, so that every
path from the initial state to an unexplored state has to pass through a state in
the frontier.
 This property is illustrated in Figure 3.9.
 As every step moves a state from the frontier into the explored region while
moving some states from the unexplored region into the frontier, we see that
the algorithm is systematically examining the states in the state space, one by
one, until it finds a solution.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Infrastructure for a search algorithm:
Search algorithms require a data structure to keep track of the search tree
that is being constructed.
For each node n of the tree, we have a structure that contains four
components:
 n.STATE: the state in the state space to which the node corresponds
 n.PARENT: the node in the search tree that generated this node
 n.ACTION: the action that was applied to the parent to generate the node
 n.PATH-COST: the cost, traditionally denoted by g(n), of the path from the
initial state to the node, as indicated by the parent pointers.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE
 The function CHILD-NODE takes a parent node and an action and returns
the resulting child node.
 The frontier needs to be stored in such a way that -- the search algorithm
can easily choose the next node to expand according to its preferred
strategy.
 The appropriate data structure for this is a queue. The operations on a
queue are as follows:
EMPTY?(queue) --returns true only if there are no more elements in
the queue.
POP(queue) --removes the first element of the queue and returns it.
INSERT(element, queue) -- inserts an element and returns the
resulting queue

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


 Three common variants of a Queue:
FIFO queue which pops the oldest element of the queue;
LIFO queue (also known as a stack), which pops the newest element of
the queue; and
Priority queue which pops the element of the queue with the highest
priority according to some ordering function.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Simple Tree Search Algorithm:
function T R E E - S E A R C H (problem, fringe) returns a solution, or failure
fringe  INSERT(MAKE-NODE(INITIAL- STATE[problem]), fringe)
loop do
if EMPTY?( fringe) then return failure
node REMOVE-FIRST (fringe)
if GOAL-TEST [problem] applied to STATE[node] succeeds
then return SOLUTION (node )
fringe INSERT-ALL ( EXPAND(problem,) , fringe)

The general tree-search algorithm. (Note that the fringe argument must be an
empty queue, and the type of the queue will affect the order of the search.)
The SOLUTION function returns the sequence of actions obtained by following
parent pointers back to the root.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Simple Tree Search Algorithm:
function EXPAND ( problem, fringe ) returns a set of nodes
successors t the empty set
for each (action, result) in SUCCESSOR-FN [ problem ] ( STATE [ node ] ) do
s a new NODE
STATE[s]result
PARENT-NODE[s] node
ACTION[s]action
PATH-COST[s] PATH-COST [node]+ STEP-COST(STATE[node], action, result)
DEPTH[s] DEPTH[node]+ 1
add s to successors
return successors

The SOLUTION function returns the sequence of actions obtained by following parent
pointers back to the root.
Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE
Measuring Problem – Solving Performance:
The output of a problem-solving algorithm is either failure or a solution. (Some
algorithms might get stuck in an infinite loop and never return an output.) We
will evaluate an algorithm's performance in four ways:
 Completeness: Is the algorithm guaranteed to find a solution when there is
one?
 Optimality: Does the strategy find the optimal solution, as defined?
 Time complexity: How long does it take to find a solution?
 Space complexity: How much memory is needed to perform the search?

complexity is expressed in terms of three quantities: b, the branching factor or


maximum number of successors of any node; d, the depth of the shallowest goal
node; and m, the maximum length of any path in the state space.

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Uninformed Search Strategies:
Uninformed Search is also called ‘Blind Search’.
 BFS (Breadth First Search): It expands the shallowest node (node having
lowest depth) first.
 UCS (Uniform Cost Search): It expands the node with least cost (Cost for
expanding the node).
 DFS (Depth First Search): It expands deepest node first.
 DLS (Depth Limited Search): It is DFS with a limit on depth.
 IDS(Iterative Deepening Search): It is DFS with increasing limit
 Bidirectional Search

Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE


Breadth First Search [BFS]:
 In this, the root node is expanded first, then all the successors of the root
node are expanded next, then their successors and so on.
 All the nodes are expanded at a given depth in the search tree before any
nodes at the next level are expanded.
 Pseudocode is given in Figure 3.11.
 Breadth-first search is an instance of the general graph-search algorithm
(Figure 3.7) in which the shallowest unexpanded node is chosen for
expansion.
 This is achieved very simply by using a FIFO queue for the frontier.
 Thus, new nodes (which are always deeper than their parents) go to the back
of the queue, and old nodes, which are shallower than the new nodes, get
expanded first.
 Figure 3.12 shows the progress of the search on a simple binary tree.
Friday, November 17, 2023 CSEN2031|ARTIFICIAL INTELLIGENCE
Breadth First Search [BFS]:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Breadth First Search [BFS]:

BFS is:

 Complete -- if the shallowest goal node is at some finite depth d, BFS will eventually
find it after expanding all shallower nodes (provided the branching factor b is finite)
 Optimal -- for unit step-costs (if the path cost is a non-decreasing function of the
depth of the node)(same cost)

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Breadth First Search [BFS]:
Branching factor: (Courtesy: Wikipedia)
 The branching factor is the number of children at each node,
the ’outdegree’. If this value is not uniform, an average branching factor can
be calculated.
 Ex: If the branching factor is 10, then there will be 10 nodes one level down
from the current position, 102 (or 100) nodes two levels down, 103 (or 1,000)
nodes three levels down, and so on. The higher the branching factor, the
faster this "explosion" occurs. The branching factor can be cut down by
a pruning algorithm.
 The average branching factor can be quickly calculated as the number of
non-root nodes (the size of the tree, minus one; or the number of edges)
divided by the number of non-leaf nodes.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Breadth First Search [BFS]:
 Time and space complexity are always considered with respect to some
measure of the problem difficulty.
 In theoretical computer science, the typical measure is the size of the state
space graph, |V | + |E|, where V is the set of vertices (nodes) of the graph
and E is the set of edges (links).
 Complexity is expressed in terms of three quantities:
 b, the branching factor or maximum number of successors of any node;
 d, the depth of the shallowest goal node (i.e., the number of steps along
the path from the root); and
 m, the maximum length of any path in the state space.
 Time is often measured in terms of the number of nodes generated during
the search, and
 space in terms of the maximum number of nodes stored in memory.
Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Breadth First Search [BFS]:
 Time and Space Complexity – Consider a hypothetical state space where
every state has b successors.
 Root of the tree generates b nodes at the first level (level 1),
 b2 nodes at level 2 (each of level 1 nodes generates b nodes),
 b3 nodes at level 3 and so on. Suppose b=2. Then the tree is:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Breadth First Search [BFS]:
 Suppose the goal is at level 3 (last node in the worst case).
 Since the goal is not expanded, there will be
bd+1 – b nodes at level d+1 (goal is at level d (=3))
i.e., 23+1 -2 = 16 – 2 = 14 nodes at level 4.
Then the total number of nodes generated is:
b + b2 + b3 + ……… + bd + (bd+1 – b) = O(bd+1)
 i.e., 2 + 22 + 23 + (23+1 – 2) = 2 + 4 + 8 + 14 = 28 (total number of nodes for
the above example)
 Every node that is generated must remain in memory.
(because it is either part of the frontier or is an ancestor of a frontier node)
 Therefore, the space complexity is the same as the time complexity (plus one
node for the root).

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Breadth First Search [BFS]:
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.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Uniform Cost Search:
BFS is optimal when all step costs are equal, because it always expands the
shallowest unexpanded node.
By a simple extension, we can find an algorithm that is optimal with any step
cost function.
Instead of expanding the shallowest node, uniform-cost search expands the
node n with the lowest path cost g(n).
(if all step costs are equal, this is identical to BFS)
 Uniform-cost search stores the frontier as a priority queue ordered by g.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Uniform Cost Search:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


SADG

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Uniform Cost Search:
 Uniform-cost search is optimal in general.
Whenever it selects a node n for expansion, the optimal path to that node has
been found.
Because step costs are nonnegative, paths never get shorter as nodes are added.
 These two facts together imply that uniform-cost search expands nodes in order of
their optimal path cost.
 Hence, the first goal node selected for expansion must be the optimal solution.
 Uniform-cost search does not care about the number of steps a path has, but only
about their total cost.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Uniform Cost Search:
 Time and Space Complexity -- Uniform-cost search is guided by path costs rather
than depths, so its complexity cannot easily be characterized in terms of b and d.
 Completeness -- is guaranteed provided the cost of every step is greater than or
equal to some small positive constant ε.
 Let C* be the cost of the optimal solution and assume that every action costs at
least ε.
 Then the algorithm's worst-case time and space complexity is
which can be much greater than bd.
 When all step costs are equal, is just bd+1 .
 When all step costs are the same, uniform-cost search is similar to breadth-first
search.
 (BFS stops as soon as it generates a goal, whereas uniform-cost search examines
all the nodes at the goal’s depth to see if one has a lower cost)
Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Uniform Cost Search:
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.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Difference between BFS and Uniform Cost Search:
1. Ordering of the queue: by path cost (in Uniform-cost search)
2. Goal test: applied to a node when it is selected for expansion (in Uniform-cost
search) rather than when it is first generated (in BFS) .
The reason is that the first goal node that is generated may be on a suboptimal path.
3. A test is added in case a better path is found to a node currently on the frontier (in
Uniform-cost search)

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Difference between BFS and Uniform Cost Search:
See Figure 3.15 for (2) and (3).
1. The successors of Sibiu are Rimnicu Vilcea and Fagaras, with costs 80 and 99,
respectively.
2. The least-cost node, Rimnicu Vilcea, is expanded next, adding Pitesti with cost 80
+ 97=177.
3. The least-cost node is now Fagaras, so it is expanded, adding Bucharest with cost
99+211=310.
4. Now a goal node has been generated, but uniform-cost search keeps going,
choosing Pitesti for expansion and adding a second path to Bucharest with cost
80+97+101= 278.
5. Now the algorithm checks to see if this new path is better than the old one; it is,
so the old one is discarded. Bucharest, now with g-cost 278, is selected for

Friday, expansion and


November 17, 2023 the solution is returned. ECS 302|ARTIFICIAL INTELLIGENCE
Depth First Search [DFS]:
 It always expands the deepest node in the current frontier of the search tree.
 The progress of the search is illustrated in Figure 3.16.
 The search proceeds immediately to the deepest level of the search tree,
where the nodes have no successors.
 As those nodes are expanded, they are dropped from the frontier, so then
the search “backs up” to the next deepest node that still has unexplored
successors

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Depth First Search [DFS]:
 The DFS algorithm is an instance of the graph-search algorithm (in Fig.3.7).
 BFS uses a FIFO queue. DFS uses a LIFO queue.
 A LIFO queue means that the most recently generated node is chosen for
expansion.
 This must be the deepest unexpanded node because it is one deeper than its
parent—which, in turn, was the deepest unexpanded node when it was
selected.
 It is common to implement DFS with a recursive function that calls itself on
each of its children in turn.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Depth First Search [DFS]:
 The properties of DFS depend strongly on whether the graph-search or tree-
search version is used.
 Graph-search version : avoids repeated states and redundant paths; complete
in finite state spaces (because it will eventually expand every node)
 Tree-search version : not complete.
Ex: in Figure 3.6, the algorithm will follow the Arad–Sibiu–Arad–Sibiu loop forever.
 For similar reasons, both versions are non-optimal.
Ex: In Figure 3.16, DFS will explore the entire left sub-tree even if node C is a goal
node.
 If node J were also a goal node, then DFS would return it as a solution instead
of C, which would be a better solution; hence, DFS is not optimal.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Depth First Search [DFS]:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Depth First Search [DFS]:
 The time complexity of:
depth-first graph search -- bounded by the size of the state space : O(V+E)
(V—No. of Vertices, E—No. of Edges)
depth-first tree search -- may generate all of the O(bm) nodes in the search tree
(m is the maximum depth of any node);
 Why to consider DFS? - The reason is the space complexity.
 For a graph search, there is no advantage, but a depth-first tree search needs to
store only a single path from the root to a leaf node, along with the remaining
unexpanded sibling nodes for each node on the path.
 Once a node has been expanded, it can be removed from memory as soon as all its
descendants have been fully explored. (See Figure 3.16.)
 For a state space with branching factor b and maximum depth m, DFS requires
storage of only O(bm) nodes.
Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Depth First Search [DFS]:
 For a state space with branching factor b and maximum depth m, DFS requires
storage of only (bm)+1 nodes.
depth = 2 ; m = 2; bm + 1 = 5 nodes.
 Backtracking search:
A variant of depth-first search uses still less memory.
Only one successor is generated at a time rather than all successors; each
partially expanded node remembers which successor to generate next.
 Only O(m) memory is needed rather than O(bm).
 Drawback: Even if the goal is nearer to the root node, sometimes DFS may go
down in an unfruitful path.
 Movement: A  B  C  D  H  …..

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Depth First Search [DFS]:
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:
 There is the possibility that many states keep re-occurring, and there is no
guarantee of finding the solution.
 DFS algorithm goes for deep down searching and sometime it may go to the infinite
loop.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Depth Limited Search [DLS]:
 DFS can be supplied with a predetermined depth limit l, to avoid the problem of
unbounded trees.
 That is, nodes at depth ‘l’ are treated as if they have no successors. (called depth-
limited search)
 It solves the infinite-path problem.
Drawbacks:
It introduces an additional source of incompleteness if we choose l < d, i.e., the
shallowest goal is beyond the depth limit.(goal is at depth d)
It will be non-optimal if we choose l > d.
Time complexity is O(bl).
Space complexity is O(bl).
 DFS can be viewed as a special case of depth-limited search with l = ∞.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Depth Limited Search [DLS]:
 Sometimes, depth limits can be based on knowledge of the problem.
Ex: On the map of Romania there are 20 cities.
 Therefore, we know that if there is a solution, it must be of length 19 at the
longest, so = 19 is a possible choice. But in fact if we studied the map carefully, we
would discover that any city can be reached from any other city in at most 9 steps.
 This number, known as the diameter of the state space, gives us a better depth
limit, which leads to a more efficient depth-limited search.
 For most problems, however, we will not know a good depth limit until we have
solved the problem.
 Depth-limited Search can be implemented as a simple recursive algorithm (Figure
3.17.).
 It can terminate with two kinds of failure:
the standard failure value indicates no solution
the17,cutoff
Friday, November 2023 value indicates no solution within the depth
ECS limit.
302|ARTIFICIAL INTELLIGENCE
Depth Limited Search [DLS]:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Depth Limited Search [DLS]:
Advantage:
 Depth-limited search is Memory efficient
Disadvantage:
 Depth-limited search also has a disadvantage of incompleteness.
 It may not be optimal if the problem has more than one solution.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:
 It is a general strategy used in combination with DFS, that finds the best depth
limit.
 It does this by gradually increasing the limit—first 0, then 1, then 2 and so on—
until a goal is found.
 This will occur when the depth limit reaches d, the depth of the shallowest goal
node.
 Iterative deepening combines the benefits of DFS and BFS.
 Like DFS, its memory requirements are O(bd).
 Like BFS, it is complete when the branching factor is finite and optimal when the
path cost is a non-decreasing function of the depth of the node.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:
 IDS may seem wasteful because states are generated multiple times. It turns out
this is not too costly.
 In IDS, the nodes on the bottom level (depth d) are generated once,
those on the next-to-bottom level are generated twice, and so on,
up to the children of the root, which are generated d times.
 Reason: In a search tree with the same (or nearly the same) branching factor at
each level, most of the nodes are in the bottom level, so it does not matter much
that the upper levels are generated multiple times.
 In general, iterative deepening is the preferred uninformed search method when
the search space is large and the depth of the solution is not known.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:
 So the total number of nodes generated in the worst case is
N(IDS) = (d)b + (d-1)b2+……+(1)bd
which gives a time complexity of O(bd) (the same as BFS)
 There is some extra cost for generating the upper levels multiple times, but it is
not large.
For example, if b = 10 and d = 5, the numbers are
N(IDS) = 50 + 400 + 3, 000 + 20, 000 + 100, 000 = 1,23, 450
N(BFS) = 10 + 100 + 1, 000 + 10, 000 + 100, 000 = 1,11, 110

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Iterative Deepening Search [IDS]:
Advantage:
 It combines the benefits of BFS and DFS search algorithm in terms of fast search
and memory efficiency.
Disadvantage:
 The main drawback of IDDFS is that it repeats all the work of the previous phase.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Bidirectional Search:
 The idea is to run two simultaneous searches— one forward from the initial state
and the other backward from the goal, stopping when the two searches meet in
the middle.
 The motivation is that
bd/2 + bd/2 is much less than bd, or
in the figure, the area of the two small circles is less than the area of one
big circle centered on the start and reaching to the goal.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Bidirectional Search:
 It is implemented by replacing the goal test with a check to see
whether the frontiers of the two searches intersect;
if they do, a solution has been found.
 The first such solution found may not be optimal, even if the two searches are both
breadth-first; some additional search is required to make sure there isn’t another
short-cut across the gap.
 Ex: If a problem has solution depth d=6, and each direction runs breadth-first
search one node at a time, then in the worst case the two searches meet when
they have generated all of the nodes at depth 3.
 For b=10, this means a total of 2,220 node generations, compared with 1,111,110
for a standard BFS.
 Thus, the time complexity of bidirectional search using breadth-first searches in
both directions is O(bd/2).
 The space
Friday, November complexity
17, 2023 is also O(bd/2). ECS 302|ARTIFICIAL INTELLIGENCE
Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE
Bidirectional Search:
Advantage:
 Bidirectional search is fast.
 Bidirectional search requires less memory
Disadvantage:
 Implementation of the bidirectional search tree is difficult.
 In bidirectional search, one should know the goal state in advance.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Comparing Uninformed Search Strategies :

Criterion\
Search BFS Uniform Cost DFS DLS IDS Bidirectional
Algorithm

Complete YESa YESa,b NO NO YESa YESa,d

Time
O (bd+1) O (b1+[C*/€]) O (bm) O (bl) O (bd) O (bd/2)
complexity

Space
O (bd+1) O (b1+[C*/€]) O (bm) O (bl) O (bd) O (bd/2)
complexity

Optimal YESc YES NO NO YESc YESc,d

Evaluation of search strategies. b is the branching factor; d is the depth of the shallowest solution; m is the maximum
depth of the search tree; 1 is the depth limit. Superscript caveats are as follows: a complete if b is finite; b complete if
step costs ≥ € for positive €; c optimal if step costs are all identical; d if both directions use breadth-first search.

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Q&A

Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE


Friday, November 17, 2023 ECS 302|ARTIFICIAL INTELLIGENCE

You might also like