3. Searching Algorithms (1)
3. Searching Algorithms (1)
Artificial
Intelligence
Mohiuddin Ahmed
Assistant Professor
Department of CSE
RUET
Problem and Goal Formulation
Problem formulation is the process of deciding what actions and states
to consider, given a goal.
Example: Visiting Dhaka from Rajshahi
Goal formulation is the first step in problem solving that is based on the
current situation and the agent’s performance measure.
2
Well Defined Problems and Solution
3
Well Defined Problems and Solution
• Transition Model: A description of what each action
does specified by a function RESULT(s, a) that returns
the state that results from doing action a in state s. For
example, we have
RESULT(In(Rajshahi), Go(Food-village)) = In(Food-village)
5
Building Goal-Based Agents
6
Search Example: Route Finding
4 3
Search Example: 8-Queens
Search Example: Remove 5 Sticks Problem
Remove exactly 5 of
the 17 sticks so the
resulting figure
forms exactly 3
squares
Search Terminology
• State/Problem Space: The initial state, actions, and transition model
implicitly define the state space of the problem—the set of all states reachable
from the initial state by any sequence of actions.
• Problem Space Graph: Represents problem space. States are shown by nodes
and operators i.e. actions are shown by edges.
• Path: A path in the state space is a sequence of states
connected by a sequence of actions.
• Step Cost: Cost of taking action a in state s to reach state s’ is denoted by c(s, a,
s’).
• Depth of Problem: Shortest sequence of operators from initial state to goal state
i.e. length of a shortest path.
• Admissibility: A property of an algorithm to always find an optimal solution.
Searching Algorithms
S
start The size of a problem
is usually described
5 2 4
in terms of the number
A B C of possible states:
9 4 6 2
6 G 1 Tic-Tac-Toe: 39 states
D E goal F
Checkers: 1040 states
7 Rubik's Cube: 1019 states
Chess: 10120 states
H
15
Evaluating Search Strategies
• Completeness
If a solution exists, will it be found?
• a complete algorithm will find a solution (not all)
• Optimality / Admissibility
If a solution is found, is it guaranteed to be optimal?
• an admissible algorithm will find a solution with minimum cost
• Time Complexity
How long does it take to find a solution?
• usually measured for worst case
• measured by counting number of nodes expanded
• Space Complexity
How much space is used by the algorithm?
• measured in terms of the maximum size
of the Frontier during the search
16
8-Puzzle State-Space Search
Tree
(Not all nodes shown;
e.g., no “backwards”
moves)
Search Strategies
18
Uninformed Search Strategies
19
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier list
{S} 5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
H
21
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier list
{S} 5 2 4
S not goal {A,B,C}
A B C
9 4 6 2
6 G 1
D E goal F
H
22
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 2, expanded: 2 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A not goal {B,C,D,E}
9 4 6 2
6 G 1
D E goal F
H
23
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B not goal {C,D,E,G}
9 4 6 2
6 G 1
D E goal F
H
24
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C not goal {D,E,G,F}
6 G 1
D E goal F
H
25
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D not goal {E,G,F,H} D E goal F
H
26
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 6, expanded: 6 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D {E,G,F,H} D E goal F
E not goal {G,F,H,G}
7
H
27
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 7, expanded: 6 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D {E,G,F,H} D E goal F
E {G,F,H,G}
G goal {F,H,G} no expand 7
H
28
Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 7, expanded: 6 start
expnd. node Frontier list
{S} 5 2 4
S {A,B,C}
A B C
A {B,C,D,E}
B {C,D,E,G}
9 4 6 2
C {D,E,G,F}
6 G 1
D {E,G,F,H} D E goal F
E {G,F,H,G}
G {F,H,G} 7
path: S,B,G
H cost: 8
29
Performance of BFS
2023-09-24 20
Performance Breadth-First Search (BFS)
33
Depth-First Search
Expand the deepest node first
1. Select a direction, go deep to the end
2. Slightly change the end
3. Slightly change the end some more…
Use a Stack to order nodes on the Frontier
Goal
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier
{S} 5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
H
35
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier
{S} 5 2 4
S not goal {A,B,C}
A B C
9 4 6 2
6 G 1
D E goal F
H
36
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 2, expanded: 2 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A B C
A not goal {D,E,B,C}
9 4 6 2
6 G 1
D E goal F
H
37
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D not goal {H,E,B,C}
9 4 6 2
6 G 1
D E goal F
H
38
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H not goal {E,B,C}
6 G 1
D E goal F
H
39
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H {E,B,C}
6 G 1
E not goal {G,B,C} D E goal F
H
40
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H {E,B,C}
6 G 1
E {G,B,C} D E goal F
G goal {B,C} no expand
7
H
41
Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A B C
A {D,E,B,C}
D {H,E,B,C}
9 4 6 2
H {E,B,C}
6 G 1
E {G,B,C} D E goal F
G {B,C}
7
path: S,A,E,G
H cost: 15
42
Depth-First Search (DFS)
• Not complete
43
Depth-First Search (DFS)
• Space Complexity: A depth-first tree search needs to store only a
single path from the root to a leaf node, along with the remaining
unexpanded sibling nodes for each node on the path. Once a node
has been expanded, it can be removed from memory as soon as all
its descendants have been fully explored. For a state space with
branching factor b and depth d, depth-first search requires storage of
only O(bd) nodes.
• Time complexity: O(bd) exponential
• Performs “chronological backtracking”
• i.e., when search hits a dead end, backs up one level at a time
• problematic if the mistake occurs because of a bad action choice near the top
of search tree
44
Depth-Limited Search (DLS)
• We perform DFS to a limited depth which is called Depth
Limited Search.
• Depth is limited to a certain value; E.g. l = 3.
• Depth limit solves the infinite-path problem.
• Depth limits can be based on knowledge of the problem.
• Depth-first search can be viewed as a special case of depth- limited search with
l=∞
45
Performance of Depth-Limited Search (DLS)
• Optimality: Not optimal, though l > d, because DFS is not
optimal itself.
• Completeness: If l < d, DLS may not find the goal. Hence,
incomplete.
• Time Complexity: O(bl)
• Space Complexity: O(bl)
46
Uniform-Cost Search (UCS)
47
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier list
{S} 5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
H
48
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier list
{S:0} 5 2 4
S not goal {B:2,C:4,A:5}
A B C
9 4 6 2
6 G 1
D E goal F
H
49
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 2, expanded: 2 start
expnd. node Frontier list
{S} 5 2 4
S {B:2,C:4,A:5}
A B C
B not goal {C:4,A:5,G:2+6}
9 4 6 2
6 G 1
D E goal F
H
50
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier list
{S} 5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C not goal {A:5,F:4+2,G:8}
9 4 6 2
6 G 1
D E goal F
H
51
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier list
{S} 5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A not goal {F:6,G:8,E:5+4,
D:5+9} 6 G 1
D E goal F
H
52
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier list
{S} 5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A {F:6,G:8,E:9,D:14}
6 G 1
F not goal {G:4+2+1,G:8,E:9, D E goal F
D:14}
7
H
53
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list
{S} 5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A {F:6,G:8,E:9,D:14}
6 G 1
F {G:7,G:8,E:9,D:14} D E goal F
G goal {G:8,E:9,D:14}
no expand 7
H
54
Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list
{S} 5 2 4
S {B:2,C:4,A:5}
A B C
B {C:4,A:5,G:8}
C {A:5,F:6,G:8}
9 4 6 2
A {F:6,G:8,E:9,D:14}
6 G 1
F {G:7,G:8,E:9,D:14} D E goal F
G {G:8,E:9,D:14}
7
path: S,C,F,G
H cost: 7
55
Performance of UCS
56
Uniform-Cost Search (UCS)
• Complexity: UCS is guided by path costs rather than depths, so its
complexity is not easily characterized in terms of b and d. Instead, let
C* be the cost of the optimal solution, and assume that every action costs at
least ϵ. Then the algorithm’s worst-case time and space complexity is
O(
57
Iterative-Deepening Search (IDS)
58
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes expanded: 0, tested: 0 start
expnd. node Frontier
{S} 5 2 4
A B C
9 4 6 2
6 G 1
D E goal F
H
59
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 1, expanded: 1 start
expnd. node Frontier
{S} 5 2 4
S not goal {A,B,C}
A B C
9 4 6 2
6 G 1
D E goal F
H
60
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 2, expanded: 1 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A not goal {B,C} no expand A B C
9 4 6 2
6 G 1
D E goal F
H
61
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 3, expanded: 1 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B not goal {C} no expand
9 4 6 2
6 G 1
D E goal F
H
62
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 4, expanded: 1 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C not goal { } no expand-FAIL 9 4 6 2
6 G 1
D E goal F
H
63
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 4(1), expanded: 2 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S no test {A,B,C} 6 G 1
D E goal F
H
64
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 4(2), expanded: 3 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A no test {D,E,B,C}
7
H
65
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 5(2), expanded: 3 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A {D,E,B,C}
D not goal {E,B,C} no expand 7
H
66
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 6(2), expanded: 3 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A {D,E,B,C}
D {E,B,C} 7
E not goal {B,C} no expand
H
67
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 6(3), expanded: 4 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A {D,E,B,C}
D {E,B,C} 7
E {B,C}
B no test {G,C} H
68
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 7(3), expanded: 4 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A {D,E,B,C}
D {E,B,C} 7
E {B,C}
B {G,C} H
69
G goal {C} no expand
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 7(3), expanded: 4 start
expnd. node Frontier
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A {D,E,B,C}
D {E,B,C} 7
E {B,C} path: S,B,G
B {G,C} H cost: 8
70
G {C}
Iterative-Deepening Search (IDS)
71
Iterative-Deepening Search (IDS)
72
Example
S
1 5 8
A B C
3 7 9 4 5 How are nodes expanded by
• Breadth-First Search: S A B C D E G
Solution found: S A G
• Uniform-Cost Search: S A D B C E G
Solution found: S B G
• Iterative-Deepening Search: S A B C S A D E G
Solution found: S A G
Informed/Heuristic Search
• Manhattan distance
• Euclidean distance
Heuristic Function
f(n) = h(n)
S
# of nodes tested: 0, expanded: 0 h=8
expnd. node Frontier
{S:8} 1 5 8
A B C
h=8 h=4 h=3
3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search
f(n) = h(n)
S
# of nodes tested: 1, expanded: 1 h=8
expnd. node Frontier
{S:8} 1 5 8
S not goal {C:3,B:4,A:8} A B C
h=8 h=4 h=3
3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search
f(n) = h(n)
S
# of nodes tested: 2, expanded: 2 h=8
expnd. node Frontier
{S:8} 1 5 8
S {C:3,B:4,A:8} A B C
C not goal {G:0,B:4,A:8} h=8 h=4 h=3
3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search
f(n) = h(n)
S
# of nodes tested: 3, expanded: 2 h=8
expnd. node Frontier
{S:8} 1 5 8
S {C:3,B:4,A:8} A B C
C {G:0,B:4, A:8} h=8 h=4 h=3
G goal {B:4, A:8} not expanded
3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search
f(n) = h(n)
S
# of nodes tested: 3, expanded: 2 h=8
expnd. node Frontier
{S:8} 1 5 8
S {C:3,B:4,A:8} A B C
C {G:0,B:4, A:8} h=8 h=4 h=3
G {B:4, A:8}
3 7 9 4 5
D E G
h=∞ h=∞ h=0
• Fast but not optimal
path: S,C,G
cost: 13
Greedy Best-First Search
S
h=5
• Not complete 2 2
A B
• Not optimal/admissible h=3 h=4
Greedy search finds the left goal (solution 1 2
cost of 7) C D
Optimal solution is the path to the right goal h=3 h=1
(solution cost of 5) 1 1
E G
h=2 goal
3
G
goal
A* Search
▪ A* minimizes the total path cost. Under the right condition, A*
provides the cheapest cost solution in the optimal path.
Here, g(n) → The cost to get from the start state to state n.
h(n) → The estimated cost to get from state n to the goal
state.
This is often referred to as the heuristic, which is
nothing but a kind of smart guess (the heuristic).
▪ Loops are avoided. The same state is not expanded twice.
A* Search
3 7 9 4 5
D E G
h=∞ h=∞ h=0
A* Search
3 7 9 4 5
D E G
h=∞ h=∞ h=0
A* Search
Limitations:
C 6x 8 y
subject to the system of inequalities
40x 10 y 2400
10x 15y 2100
5x 15y 1500
x0
y0
A Nutrition Problem
y
40x 10y 2400
A(0,
240)
20
10x 15y 2100 0 S
B(30,
120)
5x 15y 1500 10
0 C(120, 60)
D(300, 0)
x
100 200 300
Advantages of Hill Climbing
▪ Hill climbing technique is useful in job shop scheduling,
automatic programming, circuit designing, and vehicle routing
and portfolio management.
• It is widely used in game theory and artificial intelligence to model and solve
competitive scenarios, such as chess, checkers, and other two-player games.
Games Vs Search
Adversary Search
• Problem formulation
– Initial state: initial board position + whose move it is
– Operators: legal moves a player can make
– Goal (terminal test): game over?
– Utility (payoff) function: measures the outcome of the game and its
desirability
• Search objective:
– Find the sequence of player’s decisions (moves) maximizing its utility
(payoff)
– Consider the opponent’s moves and their utility
• Example:
Tic-Tac-Toe, Chess, Checkers etc.
Tic-Tac-Toe
Objectives:
• The main drawback of the minimax algorithm is that it gets really slow for
complex games such as Chess, go, etc. This type of games has a huge
branching factor, and the player has lots of choices to decide.