3.Problem Solving Using Search Algorithms (3)
3.Problem Solving Using Search Algorithms (3)
• 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,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.
10
Goal Based Agent
Goal Based Agent
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
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.
20
Breadth-First Search
• Recall from Data Structures the basic algorithm for
a breadth-first search on a graph or tree
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
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
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
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.
Disadvantages of DLS
•DLS may not offer an optimal solution if the problem has more than
one solution.
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
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)
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
57
Advantages of IDDFS
Disadvantages of IDDFS
• It does all the works of the previous stage again and again.
58
5. Bidirectional search
59
Consider the graph.
Here, the start state is E
and the goal state is G.
60
Advantages of bidirectional search
61
6. Uniform cost search
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