0% found this document useful (0 votes)
17 views

Topic 3A - Problem Solving Via Search v1

Uploaded by

Kent CT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Topic 3A - Problem Solving Via Search v1

Uploaded by

Kent CT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Problem-Solving

via Search

Dr. Hau Lee Cheun, DMBE


([email protected])

UEMH3073 UECS2153/2053 Artificial Intelligence


Learning Objectives

After completing this lecture, you should be able to :-

• Explain basic problem-solving terminology.

• Describe and solve the various search problems using uninformed


and informed search algorithms.

UEMH3073 UECS2153/2053 Artificial Intelligence 2


Q: Why do we build
AI in the first place?

3
Problem-Solving

• Problem-solving is fundamental to many AI-based


applications. It deals with the formulation of problem
in terms of states, actions/operations, and path cost
so that a set goal is achieved.

• For example, the 3x3 sliding-tile puzzle


States: Location of tiles
Actions: Blank tile movements (Up, Down, Left, Right)
Step Cost: 1 per move
Goal: Goal state (as given →)

4
Optimization

• Main objective of problem-solving is to


determine a solution with set of actions to
reach the goal optimally, without violating
the constraints defined in the problem.

• Optimization aims at making something


better from a set of ‘best’ input variables or
parameters as judged by the process’ output.

• The definition of ‘best’ depends on the


problem. Generally, a maximum/minimum of
some kind.

5
Objective Function

• So, any problem being optimized has some


objectives like minimal travel time, minimal
cost, maximum return, etc.

• The mathematical function describing the


objective is called the objective function (or
the cost function).

• The objective function depends on one or


more decision variable which are the inputs
to the process.

6
State-Space Search

• Solving a problem can be represented


as a search task to ‘Find’ the solution(s)
within a state-space.

• State-space consists of all the possible


states together with actions and their
path cost to solve a specific problem.

• In the graphical representation of state space, the states are represented


by nodes while the actions are represented by arcs. Any state space has
an initial ‘Start’ node and an ending ‘Goal’ node or multiple Goal States.
The path from the initial start node to the final goal node is known as
the ‘Solution’ for a given problem.
7
State-Space Search Example

• On holiday in Romania. Currently, in Arad but have a flight tomorrow at


Bucharest.

• Goal Formulation: Be in
Bucharest before the flight.

• Problem Formulation
States: Various cities in Romania.
Actions: Drive between cities.
Step Cost: Distance between cities.

• Search Solution: Sequence of cities from Arad to Bucharest.


If sequence is shortest path/least cost, then optimal solution.
8
State-Space Search Example

• It can be observed that some paths are shorter while others are longer.

• We can figure out the best/


shortest path where the state
space is small.

• What if the state-space has


hundreds of thousands of nodes?
How can we explore such a
state-space?

• Solution: Search algorithms.

9
Search Algorithms

• An algorithm is a step-by-step procedure or a set of defined


instructions, often wrapped around mathematical equations, for
performing specific tasks or solving a particular problem.

• A search algorithm defines how to find the path (solution) from an


initial state to the goal state. There are two types of search
algorithms: Uninformed and Informed.

• Uninformed search algorithms (also known as blind or non-heuristic


search) provide just a systematic way to explore the state space
without additional information/guideline to reach the goal. It simply
distinguishes between the goal and non-goal states only.

10
Search Algorithms

• Informed search algorithms (also known heuristic search) are based


on additional information/guideline (e.g., how far the present state is
away from the goal) to make the searching procedure both
systematic and effective.

• The additional information/guideline is given by a heuristic function


that tries to estimate/predict the remaining cost before reaching the
goal state from a given state.

• By estimating the remaining cost, the heuristic helps the algorithm


prioritize which paths to explore first. This focuses the search on
states that are likely to lead to the goal efficiently.

11
Heuristic Function Analogy

• Imagine you’re lost in a maze and want


to find the exit.

• A heuristic function, h(n) would be like


having a map that might not show the
exact route, but it gives you a good
idea of the general direction towards
the exit.

• This helps you focus your exploration


and avoid unnecessary detours.

12
Search Algorithms Evaluation

A search algorithm defines the order of node expansion in finding a


path (solution) from the initial state to a goal state. There are four
criteria to evaluate the performance of a search algorithm.

1. Completeness describes the algorithm’s ability to find a solution


when one exists. The algorithm should not miss any nodes before
reaching a goal node or the end of the state space.

2. Optimality describes the algorithm's ability to find the optimal,


shortest path, or least-cost solution. For instance, the algorithm
should find the goal nodes in shallower level first.

17
Search Algorithms Evaluation

3. Time Complexity describes how long the algorithm takes to process


the nodes, i.e., the execution time of the algorithm per CPU time. It is
generally estimated by the number of nodes generated to find a
solution, if it exists.

4. Space Complexity describes how much memory the algorithm uses to


store the nodes needed to perform the search. It is generally
estimated by the maximum number of nodes in memory during search.

Time and space complexity are measured in terms of:


• b: maximum branching factor of the search tree
• d: depth of the least-cost solution
• m: maximum depth of the state space (can be ∞)
18
Uninformed Search Algorithms

• It provides just a systematic way to explore the state space without


any guideline to reach the goal. It simply distinguishes between goal
or non-goal state. For example, you are to locate a book in a library,
where the books are not indexed and arranged properly.

• Then, there would be no option but to go


blindly and check every book rack. So,
systematically, you try to cover all book
racks to find the specific book. However,
this search can be long and tedious if the
search space is large. Thus, this type of
search algorithm is often only used when
the search space is small and systematic,
but blind. 19
5 Types of Uninformed Search Algorithms

1. Breadth-first Search (BFS)


2. Uniform-cost Search (UCS)

3. Depth-first Search (DFS)


4. Depth-limited Search (DLS)
5. Iterative Deepening Search (IDS)

https://medium.com/@digitaldadababu/uninformed-search-algorithms-a-beginners-guide-2290e7079ebf 20
1. Breadth-first Search (BFS)

• As the name implies, it explores the state space layer-by-layer.

• All successors are searched across the level and expanded based on
the queue data structure on first in first out basis (FIFO).

• During exploration (or traversal), successor (or children) nodes are


added (enqueued) to the end of a Queue list. When checking
whether it is a goal node, the front node will be removed (dequeued)
first.
21
1. Breadth-first Search (BFS)

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

Complete? Find all solutions in all cases?


Yes, provided b is finite (normally the case).

Optimal? Found the goal nodes in shallower level first?


Yes. Thus, optimal.

27
1. Breadth-first Search (BFS) \\ Properties

Time Number of nodes generated to find a solution:


Complexity b1 + b2 + … + bd = O(bd).
O(bd+1) if d =/= m.

Space Maximum number of nodes in memory during search:


Complexity Same as time complexity, O(bd).
BFS keeps every node in memory.

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

Space is often a bigger issue than time.

29
Big O Notation, O(⋅)

• Sometimes also called “asymptotic notation,” it analyze an algorithm’s


running time by identifying its behavior as its input size grows.

• 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.

• In short, Big O estimates the maximum time required by an algorithm


or the “worst-case” time/space complexity of an algorithm. Note that
this analysis is not about determining the exact time/space complexity
of an algorithm but their behavior as the input approaches ∞.

30
Big O Notation, O(⋅)

http://bigocheatsheet.com/ 31
2. Uniform-cost Search (UCS)

• Generally, every node the algorithm traverse in a search-space, there


is a different step cost associated with it. If step cost is constant, it is
a generalized UCS of BFS.

32
2. Uniform-cost Search (UCS)

• In UCS, the expansion of nodes take place in the order of costs of


the branches (or edges) from the root (start node) rather than the
order of the depth or the steps of the path.

• 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.

However, instead of O(bd) for BFS, the UCS have time


and space complexities of O(bC*/ɛ). C* is the optimal
solution cost and ɛ is the minimum step cost > 0.

The big O notation is estimated based on the worst-case


number of nodes with g(n) ≤ C*. Roughly around C*/ɛ.
37
3. Depth-first Search (DFS)

• As the name implies, it explores the state space along branches


depth wise.

• 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

Complete? Find all solutions in all cases?


DFS is normally not complete because m >> d and it can
get stuck in branches with loop, thus not reaching the goal
node.

Optimal? Found the goal nodes in shallower level first?


Since DFS identifies the deeper level goals before the
shallower level ones, it is not optimal.

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

Time Number of nodes generated to find a solution:


Complexity b1 + b2 + … + bm = O(bm). Worst than BFS if m >> d.

Space Maximum number of nodes in memory during search:


Complexity In contrast to BFS that keeps every node in memory,
DFS only stores the nodes along the branch. In the
worst-case scenario, the space complexity is estimated
to be O(bm). Linear space. Very low memory usage.

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.

• If I = d (by luck), then optimal. But If I < d, then incompleteness


results and If I > d, then not optimal. DLS, hence, neither complete
nor optimal because not all nodes are explored, and goal nodes
cannot be found effectively

• Time complexity: O(bl) similar idea as DFS


Space complexity: O(bl)
48
5. Iterative Deepening Search (IDS)

• 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.

• It is an enhanced version of the DLS that marry the pros of BFS


(complete and optimal) and DFS (consume lesser memory space). But
the main drawback of IDS is that it repeats all the work of the previous
phase.

• In general, IDS is the preferred uninformed search method when there


is a large search space, and the depth of the solution is not known.
49
5. Iterative Deepening Search (IDS)

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 the state-space is explored iteratively (layer-by-layer) if the goal node


is in a shallower level, it is found first before going deeper (optimal), and all
the nodes in a layer are checked completely (completeness) 51
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

BFS UCS DFS DLS IDS


Method Explore along Traversal along Traversal along depth
breadth/level depth/branch and breadth iteratively
Fixed ɛ Vary ɛ m can be ∞ Limit m
Complete? √ √ X X √
Optimal? √ √ X X √
Time O(bd) O(bC*/ɛ) O(bm) O(bl) O(bd)
Space O(bd) O(bC*/ɛ) O(bm) O(bl) O(bd)

53
Exercise 1

• Consider the state space graph beside. The


goal states have bold borders, and the nodes
are expanded left to right when there are
ties.

• What solution path is returned by BFS, UCS,


DFS, and IDS? Which solution is preferred
and why?

54
Exercise 2

• Consider the state space graph


beside. If G is the goal state, then S
what solution path would be 3 1 8
returned by BFS, DFS, UCS, and
IDS? A B C
3 15
• List the expanded node, node list, 7 20 5
and determine the number of
nodes expanded (including goal D E G
node). Which solution is preferred
and why?

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

• Uses problem-specific knowledge/guideline in searching. The


knowledge is given by a heuristic function, h(n) that tries to estimate
the remaining cost before reaching the goal state from a given state,
i.e., the straight-line distances to Bucharest as follows:

61
Informed/Heuristic Search Algorithm

• By estimating the remaining cost, the heuristic helps the algorithm


prioritize which paths to explore first. This focuses the search on
states that are likely to lead to the goal efficiently.

• Manhattan distance, Euclidean distance, etc. are some examples of


heuristic function where the lesser the distance, the closer the
particular node of interest is to the goal node; more likely a path that
leads to a solution quickly.

• There are two common types of informed search algorithms:


(1) Greedy Best First Search, and
(2) A* Search
62
Greedy Best First Search (GBFS)

• Unlike the uninformed search algorithm such as UCS, which expands


the node with the lowest path cost of g(n), GBFS ignores g(n) and
expands the node wih the lowest h(n).

• 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).

• GBFS, however, is not always optimal. In the Holiday in Romania


example, the path found is not the shortest. It is also incomplete
because it doesn’t find all solutions in all cases and can get stuck in
loops if start-node is at Lasi and goal-node is Fagaras.

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)

• GBFS is not always optimal because it doesn’t always find the


shortest or least cost path. Thus, the algorithm is called ‘Greedy’
because on each iteration, it tries to get as close to a goal as it can,
but greediness can lead to worse results.

• It is also incomplete because it doesn’t find all solutions in all cases


and can get stuck in loops.

• 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

• It consider both path cost g(n) and heuristics h(n) in evaluating a


particular node to avoid expanding paths that are already expensive.

• Evaluation function f(n) = g(n) + h(n)


g(n) = cost so far to reach n
h(n) = estimated cost from n to goal
f(n) = estimated total cost of path through n to goal

68
Holiday in Romania \\ A* Search
A* Search

• A* algorithm is complete since it checks all the nodes before


reaching to goal node/end of the state space.

• It is also optimal if heuristic is admissible (never overestimates the


cost to reach the goal). This is because A* consider both path cost
and heuristic values. The lowest path with the lowest heuristics is
guaranteed.

• A drawback of A* is it stores all the nodes it processes in the


memory. Although lesser than uninformed search, its space and time
complexities are O(bd) and is not practical for many large-scale
problems.

70
Admissibility Property

71
Thank You

Prepared by Dr. Hau Lee Cheun

Consultation Hours @ KB 8th Floor FE31(2)


Friday, 10 am – 12 pm & 2 pm – 4 pm
REMINDER
1. WK1 Monday Replacement #2 on 4 July (Thursday)
6 to 7 PM at KB209

2. Mid Term Test on WK6 Monday,


22 July 8 AM at KB208

You might also like