Topic 3A - Problem Solving Via Search v1
Topic 3A - Problem Solving Via Search v1
via Search
3
Problem-Solving
4
Optimization
5
Objective Function
6
State-Space Search
• Goal Formulation: Be in
Bucharest before the flight.
• Problem Formulation
States: Various cities in Romania.
Actions: Drive between cities.
Step Cost: Distance between cities.
• It can be observed that some paths are shorter while others are longer.
9
Search Algorithms
10
Search Algorithms
11
Heuristic Function Analogy
12
Search Algorithms Evaluation
17
Search Algorithms Evaluation
https://medium.com/@digitaldadababu/uninformed-search-algorithms-a-beginners-guide-2290e7079ebf 20
1. Breadth-first Search (BFS)
• All successors are searched across the level and expanded based on
the queue data structure on first in first out basis (FIFO).
Consider that A-node is the start state and G-node is the goal state. The
following is the step-wise working of BFS.
Queue List
A
Dequeue A, test is it goal? NO, enqueue B, C.
B, C
C Dequeue B, test is it goal? NO, enqueue D, E.
23
1. Breadth-first Search (BFS)
Queue List
C, D, E
D, E Dequeue C, test is it goal? NO, enqueue F, G.
D, E, F, G
E, F, G Dequeue D, test is it goal? NO, add successor.
Cannot be added, leaf node (no more successor).
F, G Dequeue E, test is it goal? NO, add successor.
Cannot be added, leaf node.
24
1. Breadth-first Search (BFS)
Queue List
G Dequeue F, test is it goal? NO, add successor.
Cannot be added, leaf node.
(empty) Dequeue G, is it goal? YES!
Return path A-C-G.
25
1. Breadth-first Search (BFS) \\ Properties
27
1. Breadth-first Search (BFS) \\ Properties
Note that BFS have exponential time and space complexities, implying it
impractical for large state-space search problem.
28
1. Breadth-first Search (BFS) \\ Properties
29
Big O Notation, O(⋅)
• i.e., when the input size increases, does the algorithm become
incredibly slow? Is it able to maintain its fast run time as the input size
grow? These questions or analyses enable us to determine the
efficiency of an algorithm as its input scales.
30
Big O Notation, O(⋅)
http://bigocheatsheet.com/ 31
2. Uniform-cost Search (UCS)
32
2. Uniform-cost Search (UCS)
• It expands the node with the lowest path cost rather than the
shallowest node, as in BFS. Additionally, UCS uses a priority queue
to remove the node with the minimum cost instead of queuing the
node at the back of the list like in a normal queue.
• Note: UCS will be stuck in infinite loop if expanding a node that has
step (or action) cost, leading to the same state again-and-again. Thus,
a minimal step cost of ϵ > 0 is required.
33
2. Uniform-cost Search (UCS)
Frontier List
A0
Calculate path cost (cost from root): g(A) = 0.
Dequeue A0, test is it goal? NO, enqueue successor B, C.
B1 , C2
C2 Calculate path cost: g(B) = 1, g(C) = 2.
Dequeue B1, test is it goal? NO, enqueue successor D, E.
frontier is a priority queue list ordered by path cost
34
2. Uniform-cost Search (UCS)
Frontier List
C2, D2, E3
D 2 , E3 Calculate path cost: g(D) = 2, g(E) = 3.
Dequeue C2, test is it goal? NO, enqueue successor F, G.
D2, E3, F3, G4
E3, F3, G4 Calculate path cost: g(F) = 3, g(G) = 4.
Dequeue D2, test is it goal? NO, add successor.
Cannot be added, leaf node.
35
2. Uniform-cost Search (UCS)
Frontier List
F3, G4 Dequeue E3, test is it goal? NO, add successor.
Cannot be added, leaf node.
G4 Dequeue F3, test is it goal? NO, add successor.
Cannot be added, leaf node.
(empty) Dequeue G4, test is it goal? YES!
Return path A-C-G.
36
2. Uniform-cost Search (UCS) \\ Properties
Complete and Yes. Since UCS have similar traversal like BFS, the
Optimal? state-space search of UCS is also similar to BFS. Thus,
also complete and optimal.
Time and Space Since BFS is a generalized UCS, the time and space
Complexities complexities of UCS are similar to BFS.
• All successors are searched along each branch first and expanded
based on the stack data structure on last in first out basis (LIFO).
• During exploration, successor nodes are added (push) into the top of
a Stack list. When checking whether it is a goal node, the top node
will be removed (pop) first.
38
3. Depth-first Search (DFS)
39
3. Depth-first Search (DFS)
Consider that A-node is the start state and M-node is the goal state. The
following is the step-wise working of DFS.
Stack List
A
Pop A, test is it goal? No, push successor B, C.
B, C
C Pop B, test is it goal? No, push successor D, E.
40
3. Depth-first Search (DFS)
Stack List
D, E, C
E, C Pop D, test is it goal? No, push successor H, I.
H, I, E, C
I, E, C Pop H, test is it goal? No, add successor.
Cannot be added, leaf node.
E, C Pop I, test is it goal? No, add successor.
Cannot be added, leaf node.
41
3. Depth-first Search (DFS)
Stack List
C Pop E, test is it goal? No, push successor J, K.
J, K, C
K, C Pop J, test is it goal? No, add successor.
Cannot be added, leaf node.
C Pop K, test is it goal? No, add successor.
Cannot be added, leaf node.
42
3. Depth-first Search (DFS)
Stack List
(empty) Pop C, test is it goal? No, push successor F, G.
F, G
G Pop F, test is it goal? No, push successor L, M.
L, M, G
M, G Pop L, test is it goal? No, add successor.
Cannot be added, leaf node.
(empty) Pop G, test is it goal? YES!
Return path A-C-F-M.
43
3. Depth-first Search (DFS) \\ Properties
44
Holiday in Romania \\ DFS Stuck in Branch with Loop
Problem: Currently in Arad but have a flight tomorrow at Bucharest.
Goal: Find the path from Arad to Bucharest.
3. Depth-first Search (DFS) \\ Properties
46
DFS vs BFS
DFS BFS
Pros 1. Simple to implement. 1. Guaranteed to find a solution (if one
exists)
2. Needs relatively small memory for
storing the state-space 2. Depending on the problem, can be
guaranteed to find an optimal solution
Cons 1. Cannot find solution in all cases, 1. Memory management along with
and hence, can sometimes fail to allocation is the key factor, and hence,
find a solution, especially when a more complex to implement.
branch has a loop.
2. Needs a lot of memory for storing the
2. Not guaranteed to find an optimal state space if the search space has a
solution. Can take a lot of time to high branching factor.
find a solution.
47
4. Depth-limited Search (DLS)
• After dealing with the DFS, we are aware that though it has desirable
properties (low memory usage), the probabilities of it getting
terminated with wrong expansions cannot be eliminated. Thus, the
notion of depth limit comes and DLS came about. The basic idea is
not allowing expansion after a certain depth of l.
• In some cases, depth limit restricts DLS from finding solution, since
solution may exist beyond prescribed depth limit (l < d). IDS helps in
addressing such problems. It further continues the DLS for the next
level by extending the depth limit iteratively until the goal node is
found.
Consider that A-node is the start state and M-node is the goal state. The
following shows the traversal of IDS, iterating from 0 to 3.
50
5. Iterative Deepening Search (IDS)
Since it combines the benefits of BFS (complete and optimal) and DFS
(consume lesser memory space), the time complexity is O(bd) and space
complexity is O(bd). 52
Summary of Uninformed Search Algorithm
53
Exercise 1
54
Exercise 2
55
Exercise 3
• Consider the
Holiday in Romania
example. Currently
in Arad but have a
flight tomorrow at
Bucharest.
• Which uninformed
search algorithm
would result a
solution path that is
optimal and why?
60
Informed/Heuristic Search Algorithm
61
Informed/Heuristic Search Algorithm
• h(n) provide the estimated cost of the cheapest path from node n to
goal node. If n is goal, then h(0) is 0. The further n is away from goal,
the larger the h(n).
63
Holiday in Romania \\ GBFS Not Optimal
Found: Araid ⟶ Sibiu ⟶ Fagaras ⟶ Bucharest (450km)
Holiday in Romania \\ GBFS Not Optimal
Shortest: Araid ⟶ Sibiu ⟶ Rimnicu Vilcea ⟶ Pitesti ⟶ Bucharest (418km)
Holiday in Romania \\ GBFS Incomplete
Supposed now start-node = Iasi and goal-node = Fagaras
Greedy Best First Search (GBFS)
• In terms of time and space complexities, GBFS worst case time and
space complexities can reach O(bm). However, a good heuristic can
give dramatic improvement to O(bd). Normally, its big O is O(bm).
67
A* Search
68
Holiday in Romania \\ A* Search
A* Search
70
Admissibility Property
71
Thank You