0% found this document useful (0 votes)
17 views

3.Problem Solving Using Search Algorithms (3)

The document discusses various problem-solving techniques in artificial intelligence, including the Water Pouring problem, the Eight Puzzle, and the Eight Queens problem, detailing their states, actions, and goal tests. It also covers search algorithms, comparing uninformed methods like Breadth-First Search and Depth-First Search, along with their advantages and disadvantages. Additionally, it introduces concepts like completeness, optimality, and space complexity in search algorithms.

Uploaded by

Bablu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

3.Problem Solving Using Search Algorithms (3)

The document discusses various problem-solving techniques in artificial intelligence, including the Water Pouring problem, the Eight Puzzle, and the Eight Queens problem, detailing their states, actions, and goal tests. It also covers search algorithms, comparing uninformed methods like Breadth-First Search and Depth-First Search, along with their advantages and disadvantages. Additionally, it introduces concepts like completeness, optimality, and space complexity in search algorithms.

Uploaded by

Bablu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Example1: Water Pouring/jug problem

• Given a 4 gallon bucket and a 3 gallon bucket, how


can we measure exactly 2 gallons into one bucket?
• There are no markings on the bucket
• You must fill each bucket completely

• Initial state:
• The buckets are empty
• Represented by the tuple ( 0, 0 )
• Goal state:
• One of the buckets has two gallons of water in it
• Represented by either ( x, 2 ) or ( 2, y )
• Path cost:
• 1 per unit step

1
• Actions and Successor Function
• Fill a bucket
• (x, y) -> (4, y)
• (x, y) -> (x, 3)
• Empty a bucket
• (x, y) -> (0, y)
• (x, y) -> (x, 0)
• Pour contents of one bucket into
another
• (x, y) -> (0, x+y) or (x+y-4, 4)
• (x, y) -> (x+y, 0) or (3, x+y-3)

2
(0,0)

(4,0) (0,3)

(1,3) (4,3) (3,0)

(1,0) (0,1)
(3,3) (4,2)

(4,1)
(2,3)

(2,0) (0,2)

3
Example2: Eight Puzzle
• States:
• Description of the eight tiles
and location of the blank tile
7 2 4
• Successor Function: 5 6
• Generates the legal states
from trying the four actions 8 3 1
{Left, Right, Up, Down}
• Goal Test:
• Checks whether the state
matches the goal 1 2 3
configuration
• Path Cost: 4 5 6
• Each step costs 1 7 8

4
Example2: Eight Puzzle
• Eight puzzle is from a family of “sliding –block
puzzles”
• NP Complete
• 8 puzzle has 9!/2 = 181440 states
• 15 puzzle has approx. 1.3*1012 states
• 24 puzzle has approx. 1*1025 states

5
6
Example: Eight Queens
• Place eight queens on a Q
chess board such that no
queen can attack another Q
queen
Q
• No path cost because only Q
the final state counts! Q
Q
• Incremental formulations
• Complete state formulations Q
Q
7
Example: Eight Queens
• States: Q
• Any arrangement of 0 to 8
queens on the board Q
• Initial state:
• No queens on the board Q
• Successor function: Q
• Add a queen to an empty
square Q
• Goal Test:
• 8 queens on the board and Q
none are attacked
• 64*63*…*57 = 1.8*1014 Q
possible sequences Q
• Ouch!
8
Example: Eight Queens
• States: Q
• Arrangements of n queens,
one per column in the Q
leftmost n columns, with no
queen attacking another are Q
states
• Successor function: Q
• Add a queen to any square in Q
the leftmost empty column
such that it is not attacked by
any other queen.
Q
• 2057 sequences to Q
investigate
Q
9
Problem Solving Agents
• Problem solving agent
• A kind of “goal based” agent
• Finds sequences of actions that lead to desirable states.

• The algorithms are uninformed


• No extra information about the problem other than the
definition
• No extra information
• No heuristics (rules)

10
Goal Based Agent
Goal Based Agent

What the world Sensors Percepts


State is like now

How the world evolves

Environment
What my actions do
What it will be like
if I do action A

What action I
Goals Actuators Actions
should do now

11
Goal Based Agents
• Assumes the problem environment is:
• Static
• The plan remains the same
• Observable
• Agent knows the initial state
• Discrete
• Agent can enumerate the choices
• Deterministic
• Agent can plan a sequence of actions such that each will lead to an
intermediate state

• The agent carries out its plans with its eyes closed
• Certain of what’s going on
• Open loop system

12
Well Defined Problems and
Solutions
• A problem
• Initial state
• Actions and Successor Function
• Goal test
• Path cost

13
Problem solving using search algorithms

 In artificial intelligence, problems can be solved by using


searching algorithms, evolutionary computations, knowledge
representations, etc.
 The process of problem-solving using searching consists of the
following steps.
1.Define the problem
2.Analyze the problem
3.Identification of possible solutions
4.Choosing the optimal solution
5.Implementation

14
Properties of search algorithms
 Completeness
 A search algorithm is said to be complete when it gives a solution
or returns any solution for a given random input.
 Optimality
 If a solution found is best (lowest path cost) among all the solutions
identified, then that solution is said to be an optimal one.
 Time complexity
 The time taken by an algorithm to complete its task is called time
complexity. If the algorithm completes a task in a lesser amount of
time, then it is an efficient one.
 Space complexity
 It is the maximum storage or memory taken by the algorithm at any
time while searching.
These properties are also used to compare the efficiency of the
different types of searching algorithms. 15
Types of search
 Based on algorithms
the search problems we can classify the search
algorithms into
1. Uninformed (Blind search) search and
2. Informed search (Heuristic search) algorithms.

16
1. Uninformed search algorithms
 The uninformed search algorithm does not have any domain
knowledge such
as closeness, location of the goal state, etc. it behaves in a brute-force
way.

 It only knows the information about how to traverse the given tree
and how to
find the goal state. This algorithm is also known as the Blind search
algorithm
or Brute -Force algorithm.

 The uninformed search strategies are of six types.


1. Breadth-first search (BFS)
2. Depth-first search (DFS)
17
3. Depth-limited search
1. Breadth-first search

 It is of the most common search strategies. It


generally starts from the root node and
examines the neighbor nodes and then moves
to the next level.
 It uses First-in First-out (FIFO) strategy as it
gives the shortest path to achieving the
solution.
 BFS is used where the given problem is very
small and space complexity is not considered. 18
Now, consider the following tree.

 Let’s take node A as the start


state and node F as the goal
state.
 The BFS algorithm starts with
the start state and then goes to
the next level and visits the
node until it reaches the goal
state.
 In this example, it starts from A
and then travel to the next level
and visits B and C and then
travel to the next level and The path of traversal is:
A —-> B —-> C —-> D —-> E —-> F
visits D, E, F and G. Here, the
19
goal state is defined as F. So,
Comparing Uninformed Search
Strategies
• Time and space complexity are measured in
• b – maximum branching factor of the search tree
• m – maximum depth of the state space
• d – depth of the least cost solution

20
Breadth-First Search
• Recall from Data Structures the basic algorithm for
a breadth-first search on a graph or tree

• Expand the shallowest unexpanded node

• Place all new successors at the end of a FIFO queue

21
Breadth-First Search

22
Breadth-First Search

23
Breadth-First Search

24
Breadth-First Search

25
Properties of Breadth-First
Search
• Complete
• Yes if b (max branching factor) is finite
• Time
• 1 + b + b2 + … + bd + b(bd-1) = O(bd+1)
• exponential in d
• Space
• O(bd+1)
• Keeps every node in memory
• This is the big problem; an agent that generates nodes at 10
MB/sec will produce 860 MB in 24 hours
• Optimal
• Yes (if cost is 1 per step); not optimal in general
26
Lessons From Breadth First
Search
• The memory requirements are a bigger problem for
breadth-first search than is execution time

• Exponential-complexity search problems cannot be


solved by uniformed methods for any but the
smallest instances

27
Advantages of BFS
 BFS will never be trapped in any unwanted nodes.
 If the graph has more than one solution, then BFS will return the
optimal solution which provides the shortest path.

Disadvantages of BFS
 BFS stores all the nodes in the current level and then go to the
next level. It requires a lot of memory to store the nodes.
 BFS takes more time to reach the goal state which is far away.

28
2. Depth-first search

 The depth-first search uses Last-in, First-out (LIFO) strategy and hence
it can be implemented by using stack.
 DFS uses backtracking. That is, it starts from the initial state and
explores each path to its greatest depth before it moves to the next
path.
 DFS will follow
Root node —-> Left node —-> Right node

 Here,
Now, consider
it starts the same
from the example tree mentioned above.
start state
A and then travels to B and then it
goes to D. After reaching D, it
backtracks to B.
 B is already visited, hence it goes
to the next depth E and then
backtracks to B. as it is already
visited, it goes back to A. A is
already visited. The path of traversal is:
 So, it goes to C and then to F. F is A —-> B —-> D —-> E —-> C —-
29
our goal state and it stops there. > F
Depth-First Search
• Recall from Data Structures the basic algorithm for
a depth-first search on a graph or tree

• Expand the deepest unexpanded node

• Unexplored successors are placed on a stack until


fully explored

30
Depth-First Search

31
Depth-First Search

32
Depth-First Search

33
Depth-First Search

34
Depth-First Search

35
Depth-First Search

36
Depth-First Search

37
Depth-First Search

38
Depth-First Search

39
Depth-First Search

40
Depth-First Search

41
Depth-First Search

42
Depth-First Search
• Complete
• No: fails in infinite-depth spaces, spaces with loops
• Modify to avoid repeated spaces along path
• Yes: in finite spaces
• Time
• O(bm)
• Not great if m is much larger than d
• But if the solutions are dense, this may be faster than breadth-first
search
• Space
• O(bm)…linear space
• Optimal
• No

43
Advantages of DFS
 It takes lesser memory as compared to BFS.
 The time complexity is lesser when compared to BFS.
 DFS does not require much more search.

Disadvantages of DFS
 DFS does not always guarantee to give a solution.
 As DFS goes deep down, it may get trapped in an infinite
loop.

44
3. Depth-limited search

 Depth-limited works similarly to depth-first search. The difference


here is that depth-limited search has a pre-defined limit up to
which it can traverse the nodes.
 Depth-limited search solves one of the drawbacks of DFS as it does
not go to an infinite path.
 DLS ends its traversal if any of the following conditions exits.

Standard Failure
 It denotes that the given problem does not have any solutions.
Cut off Failure Value
 It indicates that there is no solution for the problem within the
given limit.

45
 Now, consider the previous example.
 Let’s take A as the start node and C as the goal state and limit as 1.
 The traversal first starts with node A and then goes to the next
level 1 and the goal state C is there. It stops the traversal.

The path of traversal is: A —-> C


 If we give C as the goal node and the limit as 0, the algorithm will
not return any path as the goal node is not available within the given
limit.
46
 If we give the goal node as F and limit as 2, the path will be A, C, F.
Advantages of DLS

•It takes lesser memory when compared to other search techniques.

Disadvantages of DLS

•DLS may not offer an optimal solution if the problem has more than

one solution.

•DLS also encounters incompleteness.

47
Depth-Limited Search
• A variation of depth-first search that uses a depth
limit
• Alleviates the problem of unbounded trees
• Search to a predetermined depth l
• Nodes at depth l have no successors

• Same as depth-first search if l = ∞


• Can terminate for failure and cutoff

48
Depth-Limited Search
• Complete
• Yes if l < d
• Time
• O(bl)
• Space
• O(bl)
• Optimal
• No if l > d

49
4. Iterative deepening depth-first search (IDDFS)

 Iterative deepening depth-first search is a combination of depth-


first search and breadth-first search.
 IDDFS find the best depth limit by gradually adding the limit until
the defined goal state is reached.

 Consider the same


example tree.
 Consider, A as the start
node and E as the goal
node. Let the maximum
 The
depth be 2.
algorithm starts with
A and goes to the next The path of traversal is
level and searches for E. A —-> B —-> E
 If not found, it goes to the
next level and finds E. 50
Iterative Deepening Search
• Iterative deepening depth-first search
• Uses depth-first search
• Finds the best depth limit
• Gradually increases the depth limit; 0, 1, 2, … until a goal is
found

51
Iterative Deepening Search

52
Iterative Deepening Search

53
Iterative Deepening Search

54
Iterative Deepening Search

55
Iterative Deepening Search
• Complete
• Yes
• Time
• O(bd)
• Space
• O(bd)
• Optimal
• Yes if step cost = 1
• Can be modified to explore uniform cost tree

56
Lessons From Iterative
Deepening Search
• Faster than BFS even though IDS generates
repeated states
• BFS generates nodes up to level d+1
• IDS only generates nodes up to level d

• In general, iterative deepening search is the


preferred uninformed search method when there is
a large search space and the depth of the solution
is not known

57
Advantages of IDDFS

• IDDFS has the advantages of both BFS and DFS.

• It offers fast search and uses memory efficiently.

Disadvantages of IDDFS

• It does all the works of the previous stage again and again.

58
5. Bidirectional search

 The bidirectional search algorithm is completely different from


all other search strategies.
 It executes two simultaneous searches called forward-search
and backwards-search and reaches the goal state.
 Here, the graph is divided into two smaller sub-graphs. In one
graph, the search is started from the initial start state and in
the other graph, the search is started from the goal state.
 When these two nodes intersect each other, the search will be
terminated.
 Bidirectional search requires both start and goal start to be well
defined and the branching factor to be the same in the two
directions.

59
 Consider the graph.
 Here, the start state is E
and the goal state is G.

 In one sub-graph, the


search starts from E and in
the other, the search starts
from G.
 E will go to B and then A. G
will go to C and then A.
 Here, both the traversal
meets at A and hence the
traversal ends.
The path of traversal is
E —-> B —-> A —-> C —-> G

60
Advantages of bidirectional search

• This algorithm searches the graph fast.

• It requires less memory to complete its action.

Disadvantages of bidirectional search

• The goal state should be pre-defined.

• The graph is quite difficult to implement.

61
6. Uniform cost search

 Uniform cost search is considered the best search algorithm for a


weighted graph or graph with costs.
 It searches the graph by giving maximum priority to the lowest
cumulative cost. Uniform cost search can be implemented using a
priority queue.
 Consider the below graph where each node has a pre-defined cost.
 Here, S is the start node and G is the goal node.

From S, G can be reached in the following


ways.
S, A, E, F, G -> 19
S, B, E, F, G -> 18
S, B, D, F, G -> 19
S, C, D, F, G -> 23
Here, the path with the least cost is S, B, E,
62
The path with the least cost is S, B, E, F, G.

Advantages of UCS
•This algorithm is optimal as the selection of paths is based on the
lowest cost.
Disadvantages of UCS
•The algorithm does not consider how many steps it goes to reach the
lowest path. This may result in an infinite loop also. 63
2. Informed search algorithms

 The informed search algorithm is also called heuristic search or


directed search. In contrast to uninformed search algorithms,
informed search algorithms require details such as distance to
reach the goal, steps to reach the goal, cost of the paths which
makes this algorithm more efficient.
 Here, the goal state can be achieved by using the heuristic
function.
 The heuristic function is used to achieve the goal state with the
lowest cost possible. This function estimates how close a state is to
the goal.
 some of the informed search strategies.

1. Greedy best-first search algorithm


2. A* search algorithm 64
65

You might also like