Module 2
Module 2
Problem-solving by Searching
1
What is a problem??
• A matter or situation regarded as unwelcome
or harmful and needing to be dealt with and
overcome.
4
Searching Algorithm in AI
• Searching in AI is the process of navigating from
a starting state to a goal
state by transitioning through intermediate states.
• The process of looking for a sequence of actions that
reaches the goal is called search.
• A search algorithm takes a problem as input and
returns a solution in the form of an action sequence.
• Once a solution is found, the actions it recommends
can be carried out. This is called the execution phase.
5
Problem-solving agents
6
Steps to problem-solving in AI:
For example, the initial state for our agent in Romania might be described as
In(Arad).
10
3. A description of what each action does; the formal name for this is the
transition model, 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.
Together, the initial state, actions, and transition model implicitly define the
state space of the problem—the set of all states reachable from the initial
state by any sequence of actions. The state space forms a directed network
or graph in which the nodes are states and the links between nodes are
actions. (The map of Romania shown in Figure can be interpreted as a
state-space graph if we view each road as standing for two driving actions,
one in each direction.) A path in the state space is a sequence of states
connected by a sequence of actions.
11
4. The goal test, which determines whether a given state is a goal state. Sometimes
there is an explicit set of possible goal states, and the test simply checks whether the
given state is one of them.
Example:
• The agent’s goal in Romania is the singleton set {In(Bucharest )}.
5. path cost function that assigns a numeric cost to each path. The problem-
solving agent chooses a cost function that reflects its own performance measure.
Example:
For the agent trying to get to Bucharest, time is of the essence, so the cost of a path
might be its length in kilometers.
we assume that the cost of a path can be described as the sum of the costs of the
individual actions along the path. The step cost of taking action a in state s to reach
state s is denoted by c(s, a, s ). The step costs for Romania are shown in Figure, as
route distances. We assume that step costs are nonnegative.
12
Problem-solving agents
Formulation
Search
Execute
13
Search Algorithm Terminologies
3. Goal test: It is a function which observe the current state and returns
14
Search Algorithm Terminologies
Search tree: A tree representation of search problem is called Search tree. The root of
the
search tree is the root node which is corresponding to the initial state.
Actions: It gives the description of all the available actions to the agent.
transition model.
Solution: It is an action sequence which leads from the start node to the goal node.
Optimal Solution: If a solution has the lowest cost among all solutions.
15
State Space
• Initial state, actions, and transition model together define
the state-space of the problem implicitly.
• State-space of a problem is a set of all states which can be
reached from the initial state followed by any sequence of
actions.
• The state-space forms a directed map or graph where nodes
are the states, links between the nodes are actions, and the path
is a sequence of states connected by the sequence of actions.
16
Properties of Search Algorithms
• Completeness: A search algorithm is said to be complete
if it guarantees to return a solution if at least any solution
exists for any random input.
• Optimality: If a solution found for an algorithm is
guaranteed to be the best solution (lowest path cost)
among all other solutions, then such a solution for it is
said to be an optimal solution.
• Time Complexity: Time complexity is a measure of time
for an algorithm to complete its task.
• Space Complexity: It is the maximum storage space
required at any point during the search, as the
complexity of the problem.
17
Types of problems
18
Examples : Toy Problem
Example 1.
vacuum world.
This can be formulated as a problem as follows:
• States:
The state is determined by both the agent location and the dirt locations. The agent
is in one of two locations, each of which might or might not contain dirt. Thus,
there are 2 × 22 = 8 possible world states. A larger environment with n locations has
n ・ 2 n states.
• Initial state:
Any state can be designated as the initial state.
• Actions:
In this simple environment, each state has just three actions: Left, Right, and Suck.
Larger environments might also include Up and Down.
• 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. The complete state space is shown in Figure 3.3.
•
19
vacuum world example cntd…
Goal test:
• Path cost:
Each step costs 1, so the path cost is the number of steps in the path.
20
Figure 3.3 The state space for the vacuum world. Links denote
actions: L = Left, R = Right, S = Suck.
21
Example 2 –
8 Puzzle Problem: Here, we have a 3×3 matrix with
movable tiles numbered from 1 to 8 with a blank space.
The tile adjacent to the blank space can slide into that
space.
• Goal test:
This checks whether the state matches the goal
configuration
• Path cost:
The path cost is the number of steps in the path
where the cost of each step is 1.
24
Example 3.
8 Queens Problem
• The aim of this problem is to place eight queens on a
chessboard in an order where no queen may attack
another..
25
26
Example 4 :
Robotic Assembly
27
Real world Problems
• Water Jug
• Travelling sales man
• Towers of Hanoi
• 8 Queen
• 8 Puzzle
• Route-finding problem
• Touring problems
• VLSI layout design: positioning millions of components and connections on a chip to
minimize area, minimize circuit delays, minimize stray capacitances, and maximize
manufacturing yield
• Robot navigation
• Internet searching
• Automatic assembly sequencing
• Protein design
28
Water Jug Problem
29
30
31
32
33
34
Travelling Salesman Problem
35
Travelling Salesman Problem
36
37
SEARCHING FOR SOLUTIONS
Having formulated some problems,
we now need to solve them.
38
Tree terminology
39
1. Root
40
2. Edge
41
3. Parent
42
4. Child
43
5. Siblings
44
6. Leaf
45
7. Internal Nodes
46
8. Degree
47
9. Level
48
10. Height
49
11. Depth
50
12. Path
51
13. Sub Tree
52
Search strategies
• A search strategy is defined by picking the order of node
expansion
Strategies are evaluated along the following dimensions:
Completeness: Is the algorithm guaranteed to find a solution when
there is one?
Optimality : Does the strategy find the optimal solution?
Time complexity: How long does it take to find a solution?
Space complexity: How much memory is needed to perform the
search?
• Time and space complexity are measured in terms of
– b: the branching factor or maximum number of successors of any node
– d: depth of the least-cost solution (i.e., the number of steps along the path
from the root);
– 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.
53
To assess the effectiveness of a search algorithm, we can consider
just the search cost— which typically depends on the time
complexity but can also include a term for memory usage—or we
can use the total cost, which combines the search cost and the path
cost of the solution found.
For the problem of finding a route from Arad to Bucharest, the search
cost is the amount of time taken by the search and the solution cost
is the total length of the path in kilometers. Thus, to compute the total
cost, we have to add milliseconds and kilometers.
54
Search Algorithm Types
• Uninformed Search
• Informed Search
55
Uninformed Search
• As the name ‘Uninformed Search’ means the machine blindly
follows the algorithm regardless of whether right or wrong,
efficient or in-efficient.
• These algorithms are brute force operations, and they don’t
have extra information about the search space; the only
information they have is on how to traverse or visit the nodes
in the tree.
• Thus uninformed search algorithms are also called blind
search algorithms.
• The search algorithm produces the search tree without using
any domain knowledge, which is a brute force in nature. They
don’t have any background information on how to approach
the goal or whatsoever.
• But these are the basics of search algorithms in AI.
56
Informed search
• Use domain knowledge
• Problem definition guides to solve the problem
• More efficient since they are guided
• Heuristic search
• Heuristic search guarantees good solution with
reasonable time. (May not be best solution)
• Used to solve complex problem by various
alternative ways
57
Difference
58
Un-informed search
Following are the various types of uninformed search
algorithms:
• Breadth-first Search
• Depth-first Search
• Depth-limited Search
• Iterative deepening depth-first search
• Uniform cost search
• Bidirectional Search
59
Breadth – first search
Breadth-first Search:
• Breadth-first search is the most common search strategy for
traversing a tree or graph. This algorithm searches
breadthwise in a tree or a graph, so it is called breadth-first
search.
• BFS algorithm starts searching from the root node of the tree
and expands all successor node at the current level before
moving to nodes of the next level.
• The breadth-first search algorithm is an example of a general-
graph search algorithm.
• Breadth-first search is implemented using FIFO queue data
structure.
60
Breadth-first search
• Expand shallowest unexpanded node
• Implementation:
– fringe is a FIFO queue, i.e., new successors go at
end
61
Breadth-first search
• Expand shallowest unexpanded node
• Implementation:
– fringe is a FIFO queue, i.e., new successors go at
end
62
Breadth-first search
63
Breadth-first search
64
Example
65
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.
66
• Time Complexity: Time Complexity of BFS algorithm can
be obtained by the number of nodes traversed in BFS
until the shallowest Node.
Where d = depth of shallowest solution and
b is the number of nodes in every level.
T (b) = 1+b2+b3+.......+ bd= O (bd)
• Space Complexity: Space complexity of BFS algorithm is
given by the Memory size of frontier which is O(bd).
• Completeness: BFS is complete, which means if the
shallowest goal node is at some finite depth, then BFS
will find a solution.
• Optimality: BFS is optimal if path cost is a non-decreasing
function of the depth of the node.
67
An exponential complexity bound such as O(bd) is scary. Table 1, shows why. It lists, for
various values of the solution depth d, the time and memory required for a breadth-first search
with branching factor b = 10. The table assumes that 1 million nodes can be generated per
second and that a node requires 1000 bytes of storage. Many search problems fit roughly
within these assumptions (give or take a factor of 100) when run on a modern personal
computer.
Table 1 Time and memory requirements for breadth-first search. The numbers shown
assume branching factor b = 10; 1 million nodes/second; 1000 bytes/node
68
Two lessons can be learned from Table 1.
First, the memory requirements are a bigger problem for breadth-first search than
is the execution time. One might wait 13 days for the solution to an important
problem with search depth 12, but no personal computer has the petabyte of
memory it would take. Fortunately, other strategies require less memory.
The second lesson is that time is still a major factor. If your problem has a solution
at depth 16, then (given our assumptions) it will take about 350 years for breadth-
first search (or indeed any uninformed search) to find it. In general, exponential-
complexity search problems cannot be solved by uninformed methods for any but
the smallest instances.
69
Depth-first Search
• Depth-first search is a recursive algorithm for
traversing a tree or graph data structure.
• It is called the depth-first search because it starts
from the root node and follows each path to its
greatest depth node before moving to the next
path.
• DFS uses a stack data structure for its
implementation.
• The process of the DFS algorithm is similar to the
BFS algorithm.
70
Depth First Search(DFS)
• The algorithm starts at the root (top) node of a tree and goes as
far as it can down a given branch (path), then backtracks until
it finds an unexplored path, and then explores it.
• The algorithm does this until the entire graph has been
explored.
71
Depth-first search
• Example:
It will start searching from root
node S, and traverse A, then B,
then D and E, after traversing E,
it will backtrack the tree as E
has no other successor and still
goal node is not found. After
backtracking it will traverse
node C and then G, and here it
will terminate as it found goal
node.
72
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.
73
Completeness: DFS search algorithm is complete within finite
state space as it will expand every node within a limited
search tree.
Time Complexity: Time complexity of DFS will be equivalent to
the node traversed by the algorithm. It is given by:
T(n)= 1+ n2+ n3 +.........+ nm=O(nm)
Where, m= maximum depth of any node and this can be
much larger than d (Shallowest solution depth)
Space Complexity: DFS algorithm needs to store only single path
from the root node, hence space complexity of DFS is
equivalent to the size of the fringe set, which is O(nm).
74
Depth-Limited Search Algorithm:
• A depth-limited search algorithm is similar to depth-first
search with a predetermined limit.
• Depth-limited search can solve the drawback of the
infinite path in the Depth-first search. In this algorithm,
the node at the depth limit will be treated as it has no
successor nodes further.
75
Step by Step Example
Consider the given graph with Depth Limit(l)=2, Target Node=H and the given
source node=A
76
Now, the first element of the source node is
pushed onto the stack.
77
A being the top element is now popped from the stack and the neighbouring
nodes B and C at depth=1(<l) of A are pushed onto the stack.
Traversal: A
78
C being the topmost element is popped from the stack and the neighbouring
node F at depth=2(=l) is pushed onto the stack.
Traversal: AC
79
F being the topmost element is popped from the stack and the
neighbouring nodes I and J at depth=3(>l) will not be pushed onto the
stack.
Traversal: ACF 80
B being the topmost element is popped from the stack and the
neighbouring nodes D and E at depth=2(=l) are pushed onto the stack.
Traversal: ACFB
81
E being the topmost element is popped from the stack and since E has no
neighbouring nodes, no nodes are pushed onto the stack.
Traversal: ACFBE
82
D being the topmost element is popped from the stack and the
neighbouring nodes G and H at depth=3(>l) will not be pushed onto the
stack.
Traversal: ACFBED
83
• Since the stack is now empty, all nodes
within the depth limit have been visited, but
the target node H has not been reached.
84
Process: If depth is fixed to 2, DLS carries out depth
first search till second level in the search tree.
85
Advantages:
• Depth-limited search is Memory efficient.
Disadvantages:
• Depth-limited search also has a disadvantage
of incompleteness.
• It may not be optimal if the problem has more
than one solution.
86
• Completeness: DLS search algorithm is complete
if the solution is above the depth-limit.
• Time Complexity: Time complexity of DLS
algorithm is O(bℓ).
• Space Complexity: Space complexity of DLS
algorithm is O(b×ℓ).
• Optimal: Depth-limited search can be viewed as
a special case of DFS, and it is also not optimal
even if ℓ>d.
87
Uniform-cost Search Algorithm
• Uniform-cost search is a searching algorithm used for traversing a
weighted tree or graph.
• This algorithm comes into play when a different cost is available for
each edge.
• The primary goal of the uniform-cost search is to find a path to the
goal node which has the lowest cumulative cost.
• Uniform-cost search expands nodes according to their path costs
from the root node.
• It can be used to solve any graph/tree where the optimal cost is in
demand.
• A uniform-cost search algorithm is implemented by the priority
queue.
• It gives maximum priority to the lowest cumulative cost.
• Uniform cost search is equivalent to BFS algorithm if the path cost
of all edges is the same. 88
Uniform-cost Search Algorithm
Step by Step procedure
89
Uniform-cost Search
90
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.
91
Completeness:
Uniform-cost search is complete, such as if there is a solution, UCS will
find it.
Time Complexity:
Let C* is Cost of the optimal solution, and ε is each step to get closer to
the goal node. Then the number of steps is = C*/ε+1. Here we have
taken +1, as we start from state 0 and end to C*/ε.
Hence, the worst-case time complexity of Uniform-cost search is O(b1 +
[C*/ε]
).
Space Complexity:
The same logic is for space complexity so, the worst-case space
complexity of Uniform-cost search is O(b1 + [C*/ε]).
Optimal:
Uniform-cost search is always optimal as it only selects a path with the
lowest path cost. 92
Iterative deepening depth-first Search
93
94
Iterative deepening search
Iterative deepening search l =0
Iterative deepening search
Iterative deepening search l =1
Iterative deepening search
Iterative deepening search l =2
Iterative deepening search
Iterative deepening search l =3
Bidirectional Search Algorithm
102
103
104
105
106
107
108
109
110
Why bidirectional approach?
• Because in many cases it is faster, it dramatically
reduce the amount of required exploration.
• Suppose if branching factor of tree is b and distance
of goal vertex from source is d, then the normal
BFS/DFS searching complexity would be O(bd).
• On the other hand, if we execute two search
operation then the complexity would be O(bd/2) for
each search and total complexity would
be O(bd/2 +bd/2) which is far less than O(bd).
111
When to use bidirectional approach?
• We can consider bidirectional approach when-
1. Both initial and goal states are unique and completely
defined.
2. The branching factor is exactly the same in both
directions.
Performance measures
• Completeness : Bidirectional search is complete if BFS
is used in both searches.
• Optimality : It is optimal if BFS is used for search and
paths have uniform cost.
• Time and Space Complexity : Time and space
complexity is O(bd/2). 112
Advantages:
• Bidirectional search is fast.
• Bidirectional search requires less memory
Disadvantages:
• Implementation of the bidirectional search
tree is difficult.
• In bidirectional search, one should know the
goal state in advance.
113
Comparing search strategies
115