You are on page 1of 115

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.

How the problems are solved ?


• Defining the problem.
• Generating alternatives.
• Evaluating and selecting alternatives.
• Implementing solutions.
2
What are the approaches to deal with a
problem
• Diagram
• Drill Down
• Plan do check act
• Etc..

• Out of many, the best way is to divide the


problem into smaller parts and find the link
between them. May be a diagrammatic
representation may help on this..
3
problem-solving
• According to psychology, “a problem-solving refers
to a state where we wish to reach to a definite goal
from a present state or condition.”
• According to computer science, a problem-solving is
a part of artificial intelligence which encompasses a
number of techniques such as algorithms, heuristics
to solve a problem.

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

• In Artificial Intelligence, Search techniques are


universal problem-solving methods.

• Rational agents or Problem-solving agents in AI


mostly used these search strategies or algorithms to
solve a specific problem and provide the best result.

• Problem-solving agents are the goal-based agents


and use atomic representation and focuses on
satisfying the goal.

6
Steps to problem-solving in AI:

These are the following steps which require to solve a


problem :

Goal Formulation: This one is the first and simple step in


problem-solving. It organizes finite steps to formulate a
target/goals which require some action to achieve the
goal. Today the formulation of the goal is based on AI
agents.

Problem formulation: It is one of the core steps of


problem-solving which decides what actions and states
should be consider to achieve the formulated goal. 7
8
9
Well-defined problems and solutions
A problem can be defined formally by five components:

1. The initial state that the agent starts in.

For example, the initial state for our agent in Romania might be described as
In(Arad).

2. A description of the possible actions available to the agent. Given a


particular state s, ACTIONS(s) returns the set of actions that can be executed in
s. We say that each of these actions is applicable in s.
For example, from the state In(Arad), the applicable actions are
{Go(Sibiu), Go(Timisoara), Go(Zerind)}.

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.

For example, we have


RESULT(In(Arad),Go(Zerind)) = In(Zerind) .

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

Search: Searching is a step by step procedure to solve a search-problem in a

given search space.

A search problem can have three main factors:

1. Search Space: Search space represents a set of possible solutions, which

a system may have.

2. Start State: It is a state from where agent begins the search.

3. Goal test: It is a function which observe the current state and returns

whether the goal state is achieved or not.

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: A description of what each action do, can be represented as a

transition model.

Path Cost: It is a function which assigns a numeric cost to each path.

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

The problem-solving approach has been applied to a vast array of task


environments. We list some of the best known here, distinguishing between
toy and real-world problems.

A toy problem is intended to illustrate or exercise various problem-


solving methods. It can be given a concise, exact description and hence is
usable by different researchers to compare the performance of algorithms.

A real-world problem is one whose solutions people actually care


about. Such problems tend not to have a single agreed-upon description, but
we can give the general flavor of their formulations.

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:

This checks whether all the squares are clean.

• 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.

Figure 3.4 A typical instance of the 8-puzzle.


22
Cont…
• The problem formulation is as follows:
• States:
It describes the location of each numbered tiles and the blank
tile.
• Initial State:
We can start from any state as the initial state.
• Actions:
Here, actions of the blank space is defined, i.e., either left,
right, up or down(Different subsets of these are possible
depending on where the blank is.)
• Transition Model:
It returns the resulting state as per the given state and actions.
for example, if we apply Left to the start state in Figure
3.4, the resulting state has the 5 and the blank switched 23
Cont…

• 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.

 A solution is an action sequence, so


search algorithms work by considering
various possible action sequences.

 The possible action sequences starting


at the initial state form a search tree
with the initial state at the root; the
branches are actions and the nodes
correspond to states in the state space
of the problem.

 Figure 3.6 shows the first few steps in


growing the search tree for finding a
route from Arad to Bucharest.

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.

 There is no “official exchange rate” between the two, but it might be


reasonable in this case to convert kilometers into milliseconds by
using an estimate of the car’s average speed (because time is what
the agent cares about). This enables the agent to find an optimal
tradeoff point at which further computation to find a shorter path
becomes counterproductive.

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

• Expand shallowest unexpanded node


• Implementation:
– fringe is a FIFO queue, i.e., new successors go at
end

63
Breadth-first search

• Expand shallowest unexpanded node


• Implementation:
– fringe is a FIFO queue, i.e., new successors go at
end

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.

• DFS uses a stack data structure to keep track of vertices.


• depth-first search uses a LIFO queue.
• A LIFO queue means that the most recently generated node is
chosen for expansion

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).

Optimal: DFS search algorithm is non-optimal, as it may generate


a large number of steps or high cost to reach to the goal node.

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

Minimum cost from S to G is =3

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

• Bidirectional search algorithm runs two simultaneous


searches, one from initial state called as forward-search
and other from goal node called as backward-search, to
find the goal node.
• Bidirectional search replaces one single search graph
with two small subgraphs in which one starts the search
from an initial vertex and other starts from goal vertex.
• The search stops when these two graphs intersect each
other.
• Bidirectional search can use search techniques such as
BFS, DFS, DLS, etc.
100
101
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

You might also like