2. Search >> 8-puzzle
Letâs consider 8-puzzle game.
2
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Initial State Goal State
(Game Over)
how?
3. Search >> 8-puzzle
Search >> The process of looking for a sequence
of actions that reaches the goal state is called
search.
Search algorithm >> It takes input a problem,
constructs the search tree and returns a solution
in the form of an action sequence.
State space graph >>
- nodes = different states
- edges = different actions
Search tree >>
- root = initial state
- nodes = states of state space graph
- branches = actions
3
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
4. Search >> 8-puzzle
Step cost >> The cost related to action a to go
from state s to state sâ
Path cost >> The sum of all step costs of the
individual actions along the path.
Solution >> It is an action sequence(path of
search tree) that leads from the initial state to a
goal state.
Optimal solution >> The solution that has the
lowest path cost among all the solutions.
4
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
5. Search >> 8-puzzle
5
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
How to build up this
search tree?
&
How to find the solution?
6. TreeSearch( ) =
Search Algorithm >> Tree Search
6
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S
B
D
G B
S
S
D
G S G
Problems
Loopy path: S -> B -> S âŚ
and S -> D -> S âŚ
Redundant path: S -> B -> G
and S -> D -> G
7. GraphSearch( ) =
Search Algorithm >> Graph Search
7
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S
B
D
G B
S
D
G
Here the search tree contains at
most 1 copy of each state by
tracking the already visited nodes
from explored set.
So minimizes redundant paths.
8. Performance Measure >> Search Algorithm
The performance of specific search algorithms is evaluated in the following 4 ways:
â¸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 complexities are expressed in terms of :
â¸b = branching factor, maximum no of successors of any node.
â¸d = the depth of the shallowest goal node.
â¸m = the maximum length of any path in the state space.
Time complexity >> It is often measured in terms of the number of nodes generated during the search.
Space complexity >> It is measured in terms of maximum number of nodes stored in memory.
8
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
9. Search Strategy
Search Strategy >> The process of expanding nodes until either a solution is found or there are no more states to expand.
Uninformed/Blind Search Strategy >>
- This strategy has no additional information about the states beyond in the problem definition.
- All they can do is generate successors and distinguish a goal state from non-goal state.
Informed/Heuristic Search Strategy >>
- This strategy uses problem-specific knowledge beyond the definition of the problem itself.
- It can find solutions more efficiently than can an uninformed strategy.
- This strategy knows whether one non-goal state is more promising than another.
9
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
10. Uninformed Search Strategy
Different types of uninformed search strategies are:
â¸Breadth-first search (BFS)
â¸Uniform-cost search (UCS)
â¸Depth-first search (DFS)
â¸Depth-limited search (DLS)
â¸Iterative deepening depth-first search (ID DFS)
â¸Bidirectional search
10
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
11. 1. Breadth-first search
â¸BFS is a simple strategy in which the root node is expanded first,
then all the successors of the root node are expanded next,
then their successors,
and so on.
â¸Here the shallowest unexpanded node is chosen for expansion.
â¸Frontier >> The set of all nodes(leaf nodes) available for expansion at any given point is called the frontier.
â¸BFS uses a FIFO queue for the frontier, thus new nodes go to the back of the queue and old nodes which are shallower
than the new nodes get expanded first.
11
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
12. 1. Breadth-first search >> Algorithm [Graph Search]
12
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
13. 1. Breadth-first search >> How it searches!
13
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
14. 1. Breadth-first search >> Example
14
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
State Space : S = initial state, G =goal state, other nodes = intermediate states and links = legal transitions.
S G
A B
D E
C
F
15. 1. Breadth-first search >> Example
15
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
S
Before :
Frontier = { }
Explored = { }
Process:
Node = initial state = S
Final:
Goal(Node) = False
Frontier = { S }
Explored = { }
16. 1. Breadth-first search >> Example
16
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { S }
Explored = { }
Process:
Node = Pop(Frontier) = S
Child = Expand S = { A, D }
Final:
Goal(Child) = False
Frontier = { A, D }
Explored = { S }
S
A D
17. 1. Breadth-first search >> Example
17
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { A, D }
Explored = { S }
Process:
Node = Pop(Frontier) = A
Child = Expand A = { B, S, D }
Final:
Goal(Child) = False
Frontier = { D, B }
Explored = { S, A }
S
A D
B
18. 1. Breadth-first search >> Example
18
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { D, B }
Explored = { S, A }
Process:
Node = Pop(Frontier) = D
Child = Expand D = { E, S, A }
Final:
Goal(Child) = False
Frontier = { B, E }
Explored = { S, A, D }
S
A D
B E
19. 1. Breadth-first search >> Example
19
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { B, E }
Explored = { S, A, D }
Process:
Node = Pop(Frontier) = B
Child = Expand B = { C, A, E }
Final:
Goal(Child) = False
Frontier = { E, C }
Explored = { S, A, D, B }
S
A D
B E
C
20. 1. Breadth-first search >> Example
20
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { E, C }
Explored = { S, A, D, B }
Process:
Node = Pop(Frontier) = E
Child = Expand E = { F, D, B }
Final:
Goal(Child) = False
Frontier = { C, F }
Explored = { S, A, D, B, E }
S
A D
B E
C
F
21. 1. Breadth-first search >> Example
21
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { C, F }
Explored = { S, A, D, B, E }
Process:
Node = Pop(Frontier) = C
Child = Expand C = { B }
Final:
Goal(Child) = False
Frontier = { F }
Explored = { S, A, D, B, E, C }
S
A D
B E
C
F
22. 1. Breadth-first search >> Example
22
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { F }
Explored = { S, A, D, B, E, C }
Process:
Node = Pop(Frontier) = F
Child = Expand F = { G, E }
Final:
Goal(Child) = True
return
S
A D
B E
C
F
G
23. 1. Breadth-first search >> Performance Measure
23
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Complete >> Yes if the branching factor b is finite.
Optimal >> Yes if step costs are all identical.
Time Complexity >>
- depth 1: nodes generated = b
- depth 2: nodes generated= b * b = b2
- depth 3: nodes generated = b2 * b = b3
⌠⌠âŚ
- depth d: nodes generated = bd
So, Total number of nodes generated = b + b2 + b3 + ⌠⌠⌠+ bd-1 + bd = O(bd)
Space Complexity >> Total = O(bd)
b + b2 + b3 + ⌠⌠⌠+ bd-1 + bd
Explored set = O(bd-1)
Frontier set = O(bd)
What is time and space complexity
when the goal test is checked during
popping states from the frontier?
24. 2. Uniform-cost search
24
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
â¸Similar to BFS but gives optimal solution with any step-cost function.
â¸It expands the node with the lowest path cost g(n)
â¸It uses priority queue that sorts based on g(n) value
â¸Here, goal test is applied to a node when it is selected for expansion rather than when it is first generated.
â¸Also an extra checkpoint is added in case a better path is found to a node currently on the frontier.
25. 2. Uniform-cost search >> Algorithm [Graph Search]
25
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
26. 2. Uniform-cost search >> How it searches!
26
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
27. 2. Uniform-cost search >> Example
27
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
State Space : S = initial state, G =goal state, other nodes = intermediate states, links = legal transitions, labels = step cost.
S
C
B
A
G
1 10
5 5
15
5
28. 2. Uniform-cost search >> Example
28
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Before :
Frontier = { }
Explored = { }
Process:
Node = Initial State = S
Final:
Frontier = { (S,0,Nil) }
Explored = { }
S
29. 2. Uniform-cost search >> Example
29
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Before :
Frontier = { (S,0, Nil) }
Explored = { }
Process:
Node = Pop(Frontier) = (S,0, Nil)
Goal(Node) = False
Child={ A, B, C }
Final:
Frontier = { (A,1,S), (B,5,S), (C,15,S) }
Explored = { (S,0,Nil) }
S
A
1
B
5
C
15
30. 2. Uniform-cost search >> Example
30
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Before :
Frontier = { (A,1,S), (B,5,S), (C,15,S) }
Explored = { (S,0,Nil) }
Process:
Node = Pop(Frontier) = (A,1,S)
Goal(Node) = False
Child={ G, S }
Final:
Frontier = { (B,5,S), (G,11,A),(C,15,S) }
Explored = { (S,0,Nil), (A,1,S) }
S
A 1B
5
C
15
11
G
31. 2. Uniform-cost search >> Example
31
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Before :
Frontier = { (B,5,S), (G,11,A), (C,15,S) }
Explored = { (S,0,Nil), (A,1,S) }
Process:
Node = Pop(Frontier) = (B,5,S)
Goal(Node) = False
Child={ G, S }
Final:
Frontier = { (G,10,B), (C,15,S) }
Explored = { (S,0,Nil), (A,1,S), (B,5,S) }
S
A 1B
5
C
15
10
G
32. 2. Uniform-cost search >> Example
32
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Before :
Frontier = { (G,10,B), (C,15,S) }
Explored = { (S,0,Nil), (A,1,S), (B,5,S) }
Process:
Node = Pop(Frontier) = (G,10,B)
Goal(Node) = True
return
S
A 1B
5
C
15
10
G
33. 2. Uniform-cost search >> Performance Measure
33
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Complete >> Yes if branching factor b is finite and step costs is >= some positive constant Ďľ
Optimal >> Yes
Time Complexity >>
-- Here Final depth = 1 + floor(C*/Ďľ) where C* = cost of the optimal solution
-- So Time Complexity = O(bdepth) = O(b1+floor(C*/Ďľ))
Space Complexity >> As we need to save all the nodes either in Explored/Frontier Set, So required space= O(b1+floor(C*/Ďľ))
34. 3. Depth-first Search
34
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
â¸Expand deepest unexpanded node.
â¸DFS uses LIFO queue as Frontier.
â¸LIFO queue means that the most recently generated node is chosen for expansion as this node must be the deepest
unexpanded node cause it is one deeper than its parent.
35. 3. Depth-first Search >> Algorithm [Tree Search]
35
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
function DFS(problem) returns a solution, or failure
node ď problem.INITIAL-STATE, PATH-COST = 0
frontier ď a LIFO queue with node as the only element
loop do
if Empty?(frontier) then return failure
node ď POP(frontier)
if problem.GOAL-STATE(node.STATE) then return SOLUTION(node)
for each action in problem.ACTIONS(node.STATE) do
child ď CHILD-NODE(problem, node, action)
if child not in current-path do
frontier ď INSERT(child, frontier)
[ Here, frontier works like a LIFO queue ]
36. 3. Depth-first Search >> How it works!
36
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
37. 3. Depth-first Search >> Example
37
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { S }
Process:
Node = Pop(Frontier) = S
Goal(Node) = False
Child = Expand S = { A, D }
Final:
Frontier = { A, D }
S
A D
38. 3. Depth-first Search >> Example
38
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { A, D }
Process:
Node = Pop(Frontier) = A
Goal(Node) = False
Child = Expand A = { B, D, S }
Final:
Frontier = { B, D, D }
S
A D
B D
39. 3. Depth-first Search >> Example
39
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { B, D, D }
Process:
Node = Pop(Frontier) = B
Goal(Node) = False
Child = Expand B = { C, E, A }
Final:
Frontier = { C, E, D, D }
S
A D
B D
EC
40. 3. Depth-first Search >> Example
40
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { C, E, D, D }
Process:
Node = Pop(Frontier) = C
Goal(Node) = False
Child = Expand C = { B }
Final:
Frontier = { E, D, D }
S
A D
B D
EC
41. 3. Depth-first Search >> Example
41
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { E, D, D }
Process:
Node = Pop(Frontier) = E
Goal(Node) = False
Child = Expand E = { D, F, B }
Final:
Frontier = { D, F, D, D }
S
A D
B D
EC
D F
42. 3. Depth-first Search >> Example
42
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { D, F, D, D }
Process:
Node = Pop(Frontier) = D
Goal(Node) = False
Child = Expand D = { S, A, E }
Final:
Frontier = { F, D, D }
S
A D
B D
EC
D F
43. 3. Depth-first Search >> Example
43
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { F, D, D }
Process:
Node = Pop(Frontier) = F
Goal(Node) = False
Child = Expand S = { G }
Final:
Frontier = { G, D, D }
S
A D
B D
EC
D F
G
44. 3. Depth-first Search >> Example
44
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S G
A B
D E
C
F
Before :
Frontier = { G, D, D }
Process:
Node = Pop(Frontier) = G
Goal(Node) = True
S
A D
B D
EC
D F
G
45. 3. Depth-first search >> Performance Measure
45
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Complete >> No
Optimal >> No
Time Complexity >>
- If maximum tree depth = m ; m>=d
- Then DFS will generate maximum O(bm) nodes
assuming 1 goal leaf at the RHS at depth d
Space Complexity >>
- At depth m we have b nodes
- and (b-1) nodes at earlier depths from depth 0 to m-1
- Total = b+(m-1)*(b-1) = O(bm) nodes
46. 4. Depth-limited Search
46
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
â¸It solves the infinite-path problem by providing a depth limit = l
â¸If l < d then this problem will be incomplete
â¸If l >d then this problem solution will be non-optimal
â¸Depth-limit can be selected based on problem knowledge, but typically not known ahead of time in practice
â¸This search algorithm returns two kinds of failures:
- standard failure indicates no solution
- cutoff indicates no solution within that depth limit
47. 4. Depth-limited Search >> Algorithm [Tree Search]
47
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
function DLS(problem) returns a solution, or failure
node ď problem.Initial-state, Path-cost=0
frontier ď a LIFO queue with node as the only element
limit= l
loop do
if Empty?(frontier) then return failure
node ď Pop(frontier)
if problem.Goal-state(node.State) then return Solution(node)
if depth == l then return cutoff
else expand the chosen node, adding the resulting nodes to the frontier if not in the path.
end loop
[ Here, frontier works like a LIFO queue ]
48. 4. Depth-limited Search >> How it works!
48
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
49. 4. Depth-limited search >> Performance Measure
49
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Complete >> No when depth limit < d
Optimal >> No when depth limit >= d
Time Complexity >>
- If maximum tree depth = l
- Then DLS will generate maximum O(bl) nodes
assuming 1 goal leaf at the RHS at depth d
Space Complexity >>
- At depth l we have b nodes
- and (b-1) nodes at earlier depths
- Total = b+(l-1)*(b-1) = O(bl) nodes
50. 5. Iterative Deepening DFS Search
50
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
â¸Run multiple DFS searches with increasing depth-limits
â¸It combines the benefits of DFS and BFS
-- Like DFS itâs memory requirements are modest: O(bd)
-- Like BFS, it is complete when the branching factor is finite and optimal when the path cost is a non-decreasing
function of the depth of the node.
51. 5. Iterative Deepening DFS Search >> How it works!
51
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
52. 5. Iterative Deepening DFS Search >> Example
52
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
53. 5. Iterative Deepening DFS Search >> Example
53
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
54. 5. Iterative Deepening DFS Search >> Performance Measure
54
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Complete >> Yes if branching factor b is finite.
Optimal >> Yes if step costs are all identical
No if step costs are different.
Time Complexity >>
- Total no. of nodes=
b + (b+b2) + ⌠⌠⌠+ (b+b2+ ⌠⌠+ bd) = O(bd)
Space Complexity >>
- O(bd)
55. 5. Iterative Deepening DFS Search >> When to use?
55
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
â¸Isnât IDS wasteful?
- Repeated searches on different iterations
- Compare IDS and BFS:
E.g., b = 10 and d = 5
N(IDS) ~ db + (d-1)b2 +⌠⌠+ bd = 50+400+3000+20000+100000 = 123,450
N(BFS) = b + b2 +⌠⌠+ bd = 10+100+1000+10000+100000 =111,110
- Difference is only about 10%
- Most of the time is spent at depth d, which is the same amount of time in both algorithms
â¸In practice, IDS is the preferred uninformed search method with a large search space and unknown solution depth
56. Practice 1
56
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Apply the following search strategies in the state space graph as shown and show the corresponding search trees, solution
paths and path costs.
- BFS
- DFS
- IDS
- UCS
57. Previous Question
57
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Apply the following search strategies in the state space graph as shown and show the corresponding search trees, solution
paths and path costs.
- BFS
- DFS
- IDS
- UCS
58. Previous Question
58
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Apply the following search strategies in the state space graph as shown and show the corresponding search trees, solution
paths and path costs.
- BFS
- DFS
- IDS
- UCS
59. Previous Question
59
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Apply the following search strategies in the state space graph as shown and show the corresponding search trees, solution
paths and path costs.
- BFS
- DFS
- IDS
- UCS
60. Previous Question
60
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Apply the following search strategies in the state space graph as shown and show the corresponding search trees, solution
paths and path costs.
- BFS
- DFS
- IDS
- UCS
61. Previous Question
61
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Apply the following search strategies in the state space graph as shown and show the corresponding search trees, solution
paths and path costs.
- BFS
- DFS
- IDS
- UCS