Uninformed Search Algorithms in AI
Uninformed Search Algorithms in AI
SEARCHING IN AI
SEARCH ALGORITHMS
2 3 2 3 2 8 3
B 1 8 4
C 1 8 4
D 1 4
7 6 5 7 6 5 7 6 5
1 2 3
E
8 4
7 6 5
1 2 3 1 2 3
8 4 7 8 4
7 6 5 6 5
F G
SOLUTION- PROBLEM I CONTD……
DFS : Initial State – A
2 8 3
2 3
2 3
1 4
B 1 8 4
C 1 8 4 D 7 6 5
7 6 5
7 6 5
1 2 3 2 8
E F 2 3 4 G 2 8 H3 I3 2 8 3
8 4 1 4 1 4 1 6 4
1 8
7 6 5 7 6 5 7 5
7 6 5 7 6 5
1 2 3 1 2 3 2 3 4 2 3 4 8 3 2 8 3 2 8 2 8 3 2 8 3 2 8 3
8 4 7 8 4 1 8 1 8 5 2 1 4 7 1 4 1 4 3 1 4 5 1 6 4 1 6 4
7 6 5 6 5 7 6 5 7 6 7 6 5 6 5 7 6 5 7 6 7 5 7 5
J K L M N O P Q R S
SOLUTION- PROBLEM I CONTD……
BFS : Initial State – A
Step OPEN CLOSED
1 {(A,NIL)} {}
2 ((B,A)(C,A)(D,A)} {(A,NIL)}
3 {(C,A)(D,A)(E,B)} {(A,NIL),(B,A)}
4 {(D,A)(E,B)(F,C)} {(A,NIL),(B,A)(C,A)}
5 {(E,B)(F,C)(G,D)(H,D)(I,D)} {(A,NIL),(B,A)(C,A)(D,A)}
6 {(F,C)(G,D)(H,D)(I,D)(J,E)(K,E)} {(A,NIL),(B,A)(C,A)(D,A)(E,B)}
7 {(G,D)(H,D)(I,D)(J,E)(K,E)(L,F)(M,F)} {(A,NIL),(B,A)(C,A)(D,A)(E,B)(F,C)}
8 {(H,D)(I,D)(J,E)(K,E)(L,F)(M,F)(N,G)( {(A,NIL),(B,A)(C,A)(D,A)(E,B)(F,C)(G,D)}
O,G)}
9 {(I,D)(J,E)(K,E)(L,F)(M,F)(N,G)(O,G)( {(A,NIL),(B,A)(C,A)(D,A)(E,B)(F,C)(G,D)(H,
P,H)(Q,H)} D)}
10 {(J,E)(K,E)(L,F)(M,F)(N,G)(O,G)(P,H) {(A,NIL),(B,A)(C,A)(D,A)(E,B)(F,C)(G,D)(H,
(Q,H)(R,I)(S,I)} D)(I,D)}
11 {(K,E)(L,F)(M,F)(N,G)(O,G)(P,H)(Q,H {(A,NIL),(B,A)(C,A)(D,A)(E,B)(F,C)(G,D)(H,
)(R,I)(S,I)} D)(I,D)(J,E)}
SOLUTION- PROBLEM I
Therefore BFS traversal is A→B →C →D →E →F →G
→H →I →J
Path A → B → E → J
PERFORMANCE EVALUATION OF BFS AND DFS
Completeness:
In case of finite search tree with fixed branching factor
(number of children a node have), both DFS and BFS find the
solution and are hence complete.
In case of infinite search trees, a DFS algorithm is caught in
blind alley i.e. searching at a greater depth in the left path of
the search tree while the solution may lie on the right part.
BFS can find a solution even in infinite graph but take lot of
time and memory.
PERFORMANCE EVALUATION OF BFS AND DFS CONTD…
Optimality:
Since BFS algorithm traverse level by level and finds the goal node which is
nearest to the node. Hence the solution of BFS is always optimal (provided
the path cost is constant).
DFS, on the other hand, may not give the optimal solution (with least path
cost).
For instance, in the state space search shown below DFS would return path
from start state S to goal state G of 4 unit cost (S → C → D → F → G)
where as BFS would return the optimal path of 2 units (S→B →G).
S
C A
B
H
D E
F G
PERFORMANCE EVALUATION OF BFS AND DFS CONTD…
Time Complexity: Consider a search space which grows
exponentially with fixed branching factor b (worse case
scenario).
If the solution lies at depth d at the right most node , then the
number of nodes generated would be same for both DFS and
BFS and are given by:
1+b+b2+b3+………….(bd+1-b) = O(bd+1)
(1 node at level 0, b nodes at level 1, b2 nodes at level 2,….bd nodes at
level d, and (bd+1-b) nodes at level d+1 (as the goal node at depth d
would not be expanded)
PERFORMANCE EVALUATION OF BFS AND DFS CONTD…
In case, the time complexity is measured as the number of
nodes expanded, then time complexity is given by:
1+b+b2+b3+………….+bd=O(bd)
In average case, DFS would expand less number of nodes. The
time taken by BFS to DFS is (b+1) to b. Thus BFS takes
slightly more time than DFS.
PERFORMANCE EVALUATION OF BFS AND DFS
CONTD…
Space Complexity: In case of BFS, the OPEN grows by a
factor of b at each level.
Therefore, if the solution lies at depth d, then space complexity
is O(bd+1) as all the nodes of the current level needs to be stored.
In case of DFS, the OPEN grows linearly. At each level the
DFS algorithm adds b nodes. It only stores current nodes of the
current path. Therefore space complexity is O(bd).
Depth-first search would require 12 kilobytes instead of 111
terabytes memory in case of BFS at depth d=12, b=10,a
factor of 10 billion times less space.
DIFFERENCE BETWEEN DFS AND BFS
Depth First Search Breadth First Search
1. Explores all the nodes in the depth 1. Explores all the nodes in the
first fashion. breadth first fashion
2. May caught in blind alley i.e. 2. Never caught at blind alley because
searching at a greater depth in the left it explores all the nodes at the lower
path of the search tree while the depth before moving to the next node.
solution may lie on the right part.
3. It uses less space because only 3. It uses more space as all the nodes
current nodes at the current path need of the current level needs to be stored.
to be stored. The space increases with increase in
branching factor.
4. It may find the first solution which 4. It always finds the optimal solution
may not be optimal. (if the path cost is uniform).
DEPTH-LIMITED SEARCH
Depth Limited Search is a variant of DFS.
It solves the problem of blind alley of depth-first search by
imposing a cut-off on the maximum depth of a path i.e. it
does not apply DFS algorithm beyond a limit (cut-off).
Depth-limited search is not complete because solution
may lie beyond the cut-off depth.
It does not guarantee to find the shortest solution first:
depth-limited search is also not optimal.
The time and space complexity of depth-limited search is
similar to depth-first search. It takes O(bl) time and O(bl)
space, where l is the depth limit.
DEPTH- FIRST SEARCH WITH ITERATIVE
DEEPENING (DFS-ID)
The major advantage of BFS algorithm is its optimality and that
of DFS is the space complexity.
DFS-ID combines depth-first search’s space-efficiency and
breadth-first search’s optimality.
DFS-ID calls DFS for different depths starting from an initial
value. In every call, DFS is restricted from going beyond given
depth. So basically it performs DFS in a BFS fashion.
The search is started with depth one. If the solution is not
found, then the level of depth is increased by one and then the
search is performed again in depth first fashion.
Thus, deepening of depth continues till a solution is found.
DFS-ID ALGORITHM
DFS-ID (problem) returns success or failure
for depth= 0 to ∞
result = DEPTH-LIMITED-SEARCH(problem,depth) return result
end for
EXAMPLE- DFS-ID
Consider the following search tree with initial state A and goal
node G.
B C
D E F
G H
EXAMPLE- DFS-ID CONTD…..
At Depth =1
The starting node is A. The initial OPEN is {(A,NIL)}. Since it
is not a goal node, the queue will be empty and the DFS-ID will
stop.
At Depth=2
Step OPEN CLOSED
1 {(A,NIL)) {}
2 {(B,A),(C,A)} {(A,NIL))
3 {(C,A)} {(A,NIL),B,A)}
4 {} {(A,NIL),B,A), (C,A)}
EXAMPLE- DFS-ID CONTD…..
At Depth =3
Ste OPEN CLOSED
p
1 {(A,NIL)} {}
2 {(B,A),(C,A)} {(A,NIL)}
3 {(D,B),(E,B),(C,A)} {(A,NIL),(B,A)}
4 {(E,B),(C,A)} {(A,NIL),(B,A)(D,B)}
5 {(C,A)} {(A,NIL),(B,A)(D,B),(E,B)}
6 {(F,C) } {(A,NIL),(B,A)(D,B),(E,B),(C,A)}
7 {} {(A,NIL),(B,A)(D,B),(E,B),(C,A),(F,C)}
EXAMPLE- DFS-ID CONTD…..
At Depth =4
Step OPEN CLOSED
1 {(A,NIL)} {}
2 {(B,A),(C,A)} {(A,NIL)}
3 {(D,B),(E,,B),(C,A)} {(A,NIL),(B,A)}
4 {(G,D),(E,B)(C,A)} {(A,NIL),(B,A)(D,B)}
5 {(E,B)(C,A)} {(A,NIL),(B,A)(D,B)(G,D)}
2 8 3
2 3
2 3
1 4
B 1 8 4
C 1 8 4 D 7 6 5
7 6 5
7 6 5
1 2 3 2 8
E F 2 3 4 G 2 8 H3 I3 2 8 3
8 4 1 4 1 4 1 6 4
1 8
7 6 5 7 6 5 7 5
7 6 5 7 6 5
1 2 3 1 2 3
8 4 7 8 4
7 6 5 6 5
J K
SOLUTION- PROBLEM I CONTD……
DFS-ID Initial State- A,
At Depth=1
The starting node is A. The initial OPEN is {(A,NIL)}. Since it is not a goal node,
the queue will be empty and the DFS-ID will stop.
At Depth=2
Step OPEN CLOSED
1 {(A,NIL)} {}
2 {(B,A)(C,A)(D,A)} {(A,NIL)}
3 {(C,A)(D,A)} {(A,NIL)(B,A)}
4 {(D,A)} {(A,NIL)(B,A)(C,A)}
5 {} {(A,NIL)(B,A)(C,A)(D,A)}
SOLUTION- PROBLEM I CONTD……
At Depth =3
Step OPEN CLOSED
1 {(A,NIL)} {}
2 {(B,A),(C,A),(D,A)} {(A,NIL)}
3 {(E,B),(C,A),(D,A)} {(A,NIL)(B,A)}
4 {(C,A)(D,A)} {(A,NIL)(B,A)(E,B)}
5 {(F,C)(D,A)} {(A,NIL)(B,A)(E,B)(C,A)}
6 {(D,A)} {(A,NIL)(B,A)(E,B)(C,A)(F,C)}
7 {(G,D)(H,D)(I,D)} {(A,NIL)(B,A)(E,B)(C,A)(F,C)(D,A)}
8 {(H,D)(I,D)} {(A,NIL)(B,A)(E,B)(C,A)(F,C)(D,A)(G,D)}
9 {(I,D)} {(A,NIL)(B,A)(E,B)(C,A)(F,C)(D,A)(G,D)(H,D)}
10 {} {(A,NIL)(B,A)(E,B)(C,A)(F,C)(D,A)(G,D)(H,D)(I,D)
}
SOLUTION- PROBLEM I CONTD……
At Depth =4
Step OPEN CLOSED
1 {(A,NIL)} {}
2 {(B,A),(C,A),(D,A)} {(A,NIL)}
3 {(E,B),(C,A),(D,A)} {(A,NIL)(B,A)}
4 {(J,E),(K,E),(C,A),(D,A)} {(A,NIL)(B,A)(E,B)}
5 {(K,E),(C,A),(D,A)} {(A,NIL)(B,A)(E,B)(J,E)}
For the search space shown below, find the optimal path from
S to D using UCS algorithm
1
4
A 2 B
5 2
1
2
D C
3
UCS EXAMPLE
g(S)=0
Add (S,φ,0) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,φ,0)} {}
Iteration I
Remove head node (S,φ,0) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S i.e. A
and B are produced (case 1: both are new not in OPEN and
CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+1 =1
B : g(B)=g(S)+cost(S,B)
Iteration OPEN = 0+4 =4 CLOSED
0 {(S,φ,0)} {}
1 {(A,S,1) (B,S,4) {(S,φ,0)}
UCS EXAMPLE
Iteration II
Remove head node (A,S,1) from OPEN and add to CLOSED.
Since A is not goal node, therefore successors of A i.e. S, B , C, and D are produced (for
S it is case 3 as it is already in CLOSED, for B it is case 2 as it is already in OPEN and
for C and D it is case 1 which is not in OPEN and CLOSED)
Successors of A:
S :newg(S)=g(A)+cost(A,S) = 1+1=2
since newg(S) is not less than g(S), so this successor is ignored
B : newg(B)=g(A)+cost(A,B) = 1+2=3
since newg(B) is less than g(B), so update (B,A,3) in OPEN
C: g(C)=g(A) +cost(A,C) = 1 + 5=6,
Add (C,A,6) to OPEN
D: g(D)= g(A) +cost(A,D)=1+12=13
Add (D,A,13) to OPEN
Iteration OPEN CLOSED
0 {(S,φ,0)} {}
1 { (A,S,1) (B,S,4)} {(S,φ,0)}
2 {(B,A,3) (C,A,6) (D,A,13)} {(S,φ,0) (A,S,1)}
UCS EXAMPLE
Iteration III
Remove head node (B,A,3) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S, A , C are produced (for S and
A it is case 3 as it is already in CLOSED, for C it is case 2 as it is already in OPEN)
Successors of B:
S :newg(S)=g(B)+cost(B,S) =3+4=7
since newg(S) is not less than g(S), so this successor is ignored
A : newg(A)=g(B)+cost(B,A) = 3+2=5
since newg(A) is not less than g(A) , so this successor is ignored.
C: newg(B)=g(B) +cost(B,C) = 3+ 2=5
since newg(C) is less than g(C) , so update (C,B,5) in OPEN