U 2
U 2
1
UNIT II
Topics to be Covered. State Space Search and
-Research. Heuristic Search
-state space search. Techniques:
-Defineing a Problem of Defining problems as State
state space search. Space search,Production
-Formalizing Problems as systems and characteristics,
State Space Search. Hill Climberg first and depth
-classification of search first search, Best first search.
algorithms.
-BFS,DFS Problems & solns
-BFS,DFS comarisions-BFS,DFS
algorithms
production system & chars.
Hill claimberg
First Best search
A* algorithm
2
What is Search?
-Search : it is the core component of many intelligent
processes.
-Search is the systematic examination of states to find
path from the start(root) state to the goal state.
-Many traditional search algorithms are used in Al
applications.
-For complex problems, the traditional algorithms are
unable to find the solution within some practical time &
space limits.
-Consequently, many special techniques are developed;
using heuristic functions.
Uninformed search (blind search) — Also called blind,
exhaustive or brute-force Search.
uses no information about the problem to guide the
search . therefore may not be very efficient. 3
Fig: tree search/
Blind search/
un informed search 4
State Space Search
5
case:1
h(x,y)=min(x,y) =>h(10,50)=min(10,50)=10;
now path decided by decision making for the agent to
travel. 6
case:2
consider new path 0-20-10-goal :30.
it may happan practically,may miss shotest path.
state space search may not gives optimizes solution. 7
Define problem as State Space Search
A State Space essentially consists
of a set of nodes representing each
state of the problem, arcs between
representing the legal moves from
one state to another,
Initial state and a Goal state:
Search is the process of
considering various possible
sequences of operators applied to
the Initial State, and finding
sequence which ends in a goal
state.
The search problem is to find a
sequence of actions which
transitions the agent from the
Initial state to a Goal state. 8
Components of State Space Search
10
A search problem,
represented by sequence of actions of
12
Formalizing Problems as State Space Search
13
Search Strategies
14
classification of search algorithms
15
Classification of search Algorithms
16
17
17
BFS:
S
A,B
C,D,G,H
E,F,I
K
=S,A,B,C,D,G,H,E,F,I,K
DFS:
S,A,B,D
E,C,G
=S,A,B,D,E,C,G
18
Bredth First Search(BFS):
1,2,5,4
2,7,6,9,3
=1,2,5,4,7,6,9,3
Explore vertex
19
Problem:
BFS,DFS Techniques.
BFS steps:
-to find the path betn unknown
initial to Goal state by BFS we need
the following..
i)data stucture to store
ii)Generate the sequence
iii)draw the BFS state diagram.
1 4 2 3 8 5 7 10 9 6
21
Assignment :generate BFS,DFS structure
22
BFS
8-pixel fuzzle
problem
LURB
23
DFS:
steps:
-to find the path betn unknown
initial to Goal state by DFS we
need the following..
i)stack to store
ii)Generate the sequence ,which
gives us start to goal states.
iii)draw the DFS state diagram.
sequence:1,4 etc..
1,4,2,3,8,7,5,9,10,6
24
DFS valid sequence:
1,4,3,10,9,2,8,7,5,6.
25
26
27
Water Jug Problem is a puzzle where two jugs with different
capacities are used to measure a specific amount of water, using
operations like filling, emptying, and pouring water between the
jugs.
Jug 1 Jug 2
analysis
(4 lt cacity) (3 lts capacity)
3 5
0 5
3 2
0 2
2 0
2 5
3 4
31
Jug 1 Jug 2
analysis
(3 lt cacity) (5 lts capacity)
1 4 2 3 8 5 7 10 9 6
33
Un-Informed/Blind Search :
• Breadth First Search(BFS):
Algorithm BFS:
Input: START and GOAL states
/* both OPEN and CLOSED are used as QUEUE data structure
Local variables: OPEN [], CLOSED[], STATE-X, SUCCs, FOUND, EMPTY
Output: YES, or NO
BFS(S):
append(OPEN,START) /* open stack start with first node */
CLOSED = [ ] /* closed nothing */
FOUND = NO /* goal found no */
FOUND = NO /* goal found no */
While( OPEN != EMPTY and FOUND = YES)
/*to open=nothing avilable & goal found */
STATE-X = remove(OPEN) /*remove open from current state */
IF STAE-X = GOAL THEN FOUND= YES
/* if current state-x is goal,then found yes is Goal/
append(OPEN,SUCCs)
//generate all neighbors of the STATE-X If and only if neighbors not in OPEN ,CLOSED
End While 34
return FOUND.
34
DFS valid sequence:
1,4,3,10,9,2,8,7,5,6.
35
State Space Representation for
water jug problem
37
37
Problem characteristics
Heuristic search is a very general method applicable to a large class
of problems.
v Decomposability of a problem
1. Role of Knowledge:
2. Consistency of a KB “Knowledge Base” used in solution:
3. Requirement of solution: “is Solution is absolute or
relative?
4. Can Solution steps be Ignored?
5. Is the problem universe predictable? 38
38
Water Jug problem DFS Prolog code:
% Define the capacity of each jug
(0,0)
capacity(5, 3).
{1}
% Define the goal state
goal(4, _). (5,0)
goal(_, 4). {3}
{2}
% Define the possible moves
move((X, Y), (5, Y)) :- capacity(5, _ ), X < 5. % Fill Jug X
(5,3)
x
move((X, Y), (X, 3)) :- capacity( _, 3), Y < 3. % Fill Jug Y
{2}
move((X, Y), (0, Y)) :- X > 0. % Empty Jug X (0,3)
move((X, Y), (X, 0)) :- Y > 0. % Empty Jug Y {1,4} {5}
move((X, Y), (X1, 3)) :- X + Y >= 3, X1 is X - (3 - Y). % Pour Jug X to Jug Y
move((X, Y), (5, Y1)) :- X + Y >= 5, Y1 is Y - (5 - X). % Pour Jug Y to Jug X x (3,0)
move((X, Y), (0, Y1)) :- X + Y < 3, Y1 is X + Y. % Pour Jug X to Jug Y {3}
move((X, Y), (X1, 0)) :- X + Y < 4, X1 is X + Y. % Pour Jug Y to Jug X
% Define the depth-first search algorithm
(3,3)
solve(X, Y, Visited) :- goal(X, Y), % Check if the goal state is reached {1,2,4} {7}
write('Solution: '), write((X, Y)), nl, write(Visited), nl. x (5,1)
solve(X, Y, Visited) :- move((X, Y), (X1, Y1), {2}
% Apply a move (0,1)
\+ member((X1, Y1), Visited), % Ensure the state has not been visited {5}
write(Visited), nl,
{1,2,4}
solve(X1, Y1, [(X1, Y1) | Visited]). % Recursively search for a solution x (1,0)
% Initiate the solution search from the initial state {1,2} {3}
solve :- x 39
(1,3) 39
capacity(XCap, YCap), {1,2,4}
solve(0, 0, [(0, 0)]. {5}
41
41
Water Jug problem using BFS
42
42