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

Chapter 3 - Searching-Part 1

The search tree for this 4-state graph starting from state S would have 7 nodes - the root node S, plus 3 child nodes for the successors of S (a, b, G), plus another 3 child nodes for the successors of a and b. So the size of the search tree is 7 nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Chapter 3 - Searching-Part 1

The search tree for this 4-state graph starting from state S would have 7 nodes - the root node S, plus 3 child nodes for the successors of S (a, b, G), plus another 3 child nodes for the successors of a and b. So the size of the search tree is 7 nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

Artificial Intelligence

Dr. Basem H. Ahmed & Dr. Mohammed A. Awadallah

First semester 2022/2023


1
Chapter 3
Solving problems by searching

2
Solving Problems by Searching
• How an agent can look ahead to find a sequence of actions (a plan)
that will achieve its goal.

• Problem-solving agent is the agent needs to plan ahead: to consider a


sequence of actions that form a path to a goal state.
• The computational process agent undertakes is called search.

• Two types of algorithms:


1. Informed
2. Uninformed
3
4
5
Four-phase problem-solving process

• Goal Formulation: The agent adopts the goal of reaching Bucharest.

• Problem Formulation: The agent devises a description of the states


and actions necessary to reach the goal.
• For our agent, one good model is to consider the actions of traveling from one
city to an adjacent city, and therefore the only fact about the state of the world
that will change due to an action is the current city.

6
Four-phase problem-solving process

• Search: Before taking any action in the real world, the agent simulates
sequences of actions in its model, searching until it finds a sequence of
actions that reaches the goal.
• Such a sequence is called a solution.
• The agent might have to simulate multiple sequences that do not reach the
goal, but eventually it will find a solution, or it will find that no solution is
possible.

• Execution: The agent can now execute the actions in the solution, one
at a time.

7
Example: Traveling in Romania

o State space:
o Cities
o Successor function:
o Roads: Go to adjacent city with
cost = distance
o Start state:
o Arad
o Goal test:
o Is state == Bucharest?

o Solution?

8
Search Space Definitions
• State
• A description of a possible state of the world
• Includes all features of the world that are relevant to the problem
• Initial state
• Description of all relevant aspects of the state in which the agent starts the search
• Goal test
• Conditions the agent is trying to meet (e.g., have $1M)
• Goal state
• Any state which meets the goal condition
• Thursday, have $1M, live in NYC
• Friday, have $1M, live in Valparaiso
• Action
• Function that maps (transitions) from one state to another

9
Search Space Definitions
• Problem formulation
• Describe a general problem as a search problem
• Solution
• Sequence of actions that transitions the world from the initial state to a goal state
• Solution cost (additive)
• Sum of the cost of operators
• Alternative: sum of distances, number of steps, etc.
• Search
• Process of looking for a solution
• Search algorithm takes problem as input and returns solution
• We are searching through a space of possible states
• Execution
• Process of executing sequence of actions (solution)

10
Example – Eight Puzzle

States: tile (square) locations

Initial state: one specific tile configuration

Operators: move blank tile left, right, up, or down

Goal: tiles are numbered from one to eight around


the square

Path cost: cost of 1 per move (solution cost same


as number of most or path length)

Solver: https://deniz.co/8-puzzle-solver/

11
Example – Towers of Hanoi

States: combinations of poles and disks

Operators: move disk x from pole y to pole z


subject to constraints
• cannot move disk on top of a smaller disk
• cannot move disk if other disks on top

Goal test: disks from largest (at bottom) to smallest


on goal pole

Path cost: 1 per move

Solver:
https://www.mathsisfun.com/games/towerofhanoi.html

12
Example – Eight Queens

States: locations of 8 queens on chess board

Initial state: one specific queens configuration

Operators: move queen x to row y and column z

Goal: no queen can attack another (cannot be in


same row, column, or diagonal)

Path cost: 0 per move

Game: https://www.brainmetrix.com/8-queens/
Solutions:
https://users.encs.concordia.ca/~chvatal/8queens.html

13
Example - Route Finding Problem
• States: locations

• Initial state: starting point

• Successor function (operators): move from one Car Navigation


location to another Airline travel planning

• Goal test: arrive at a certain location

• Path cost: may be quite complex such as money,


time, travel comfort, scenery,
Routing in computer Networks

14
Example - Automatic Assembly Sequencing

• States: location of components

• Initial state: no components assembled

• Successor function (operators): place


component

• Goal test: system fully assembled

• Path cost: number of moves

15
Searching for Solutions

• Traversal of the search space


– From the initial state to a goal state.
– Legal sequence of actions as defined by successor function.

• A search tree is generated


– Nodes are added as more states are visited

16
State Space Graphs and Search Trees

17
State Space Graphs
o State space graph: A mathematical
representation of a search problem
a G
o Nodes are (abstracted) world configurations
o Arcs represent successors (action results) b c
o The goal test is a set of goal nodes (maybe only
e
one) d f
S h
o In a search graph, each state occurs only
p r
once! q

Tiny search graph for a tiny search


o We can rarely build this full graph in problem
memory (it’s too big), but it’s a useful idea

18
State Space Graphs vs. Search Trees

Each NODE in the


State Space Graph search tree is an Search Tree
entire PATH in the
state space graph. S

a G d e p
b c
b c e h r q
e
d f h r p q f
a a
S h We construct both
on demand – and p q f q c G
p q r
we construct as q c G a
little as possible.
a

19
State Space Graphs vs. Search Trees

Consider this 4-state graph: How big is its search tree (from S)?

s
a
a b

S G b G a G
a G b G
b

… …

Important: Lots of repeated structure in the search tree!


20
Finding a path from Arad to Bucharest? 21
Searching with a Search Tree

o Search:
o Expand out potential plans (tree nodes)
o Maintain a fringe of partial plans under consideration
o Try to expand as few tree nodes as possible

22
General Tree Search

o Important ideas:
o Fringe
o Expansion
o Exploration strategy

o Main question: which fringe nodes to explore?


23
Example: Tree Search

a G
b c

e
d f
S h

p r
q

24
Example: Tree Search
a G
b c
e
d f
S h
p q r

S s
s→d
d e p s→e
s→p
b c e h r q s→d→b
s→d→c
a a h r p q f s→d→e
s→d→e→h
p q f q c G s→d→e→r
a s→d→e→r→f
q c G
s→d→e→r→f→c
a s→d→e→r→f→G 25
Search Algorithms

• A search algorithm takes a search problem as input and returns a


solution

• Each node in the search tree corresponds to a state in the state space
and the edges in the search tree correspond to actions.

• The root of the tree corresponds to the initial state of the problem.

26
Search Algorithms

• The state space describes the set of states in the world, and the actions
that allow transitions from one state to another.

• The search tree describes paths between these states, reaching towards
the goal.

• The search tree may have multiple paths to any given state, but each
node in the tree has a unique path back to the root (as in all trees).

27
Searching Strategies

Uninformed Search Informed Search


– breadth-first search – best-first search
– uniform-cost search – search with heuristics
– depth-first search – memory-bounded search
– depth-limited search – iterative improvement search
– iterative deepening search
– bi-directional search

Uninformed Search (blind search) Informed Search (heuristic search)


• Number of steps, path cost unknown • Agent has background information about the
• Agent knows when it reaches a goal problem

28
Depth-First Search (DFS)

29
Depth-First Search (DFS)
a G
b c
Strategy: expand a
e
deepest node first d f
S h
p q r
Implementation:
Fringe is a LIFO stack S

d e p

b c e h r q

a a h r p q f

p q f q c G

q c G a
30
a
Depth-First search

• DFS begins at some arbitrary vertex, exploring as far as


possible down a branch before backtracking.

• For example, in the figure shown: DFS traverses S, A, D,


G, E, B before backtracking to E to G and then visiting
F then C.

• Backtracking is implemented using a stack to return to


the previous vertex to start a search, when a dead end
is reached.

31
Example - DFS

32
Example - DFS

33
Example - DFS

34
Properties of DFS
o Complete: Guaranteed to find a solution if one exists?
o Optimal: Guaranteed to find the least cost path?
o Time complexity?
1 node
o Space complexity? b
… b nodes
b2 nodes
o Cartoon of search tree:
m tiers
o b is the branching factor
o m is the maximum depth
o solutions at various depths
bm nodes
o Number of nodes in entire tree?
o 1 + b + b2 + …. bm = O(bm)

35
Properties of DFS
o What nodes DFS expand?
o Some left prefix of the tree. 1 node
b
o Could process the whole tree! … b nodes
o If m is finite, takes time O(bm) b2 nodes
m tiers
o How much space does the fringe take?
o Only has siblings on path to root, so O(bm)

bm nodes
o Is it complete?
o m could be infinite, so only if we prevent
cycles (more later)

o Is it optimal?
o No, it finds the “leftmost” solution,
regardless of depth or cost 36
Example
• Apply DFS algorithm to the following graph starting from node S.
Show the contents of the stack.

A B C

Stack is EMPTY: End of


B C
D D D D D
Algorithm
stack A A A A A A A
S S S S S S S S S Output: {S, A, D, B, C}.
37
Example

38
Example

39
Example

40
Example

41
Example

42
Example

43
Example

44
Example

45
Example

46
Example

47
Example

48
Example

49
Example

50
Example

51
Example

52
53
Breadth-first search (BFS)

54
Breadth-first search
Strategy: expand a a G
shallowest node first b c
Implementation: e
d f
Fringe is a FIFO S h
queue p q r

d e p
Search
b c e h r q
Tiers
a a h r p q f

p q f q c G

q c G a
55
a
Breadth-first search

• Which node from the frontier to expand next.

• Choose a node with minimum value of some evaluation function f(n).

• The root node is expanded first, then all the successors of the root
node are expanded next, then their successors, and so on.

56
Prosperities of BFS
o What nodes does BFS expand?
o Processes all nodes above shallowest solution 1 node
b
o Let depth of shallowest solution be s … b nodes
s tiers
o Search takes time O(bs) b2 nodes

o How much space does the fringe take? bs nodes


o Has roughly the last tier, so O(bs)

bm nodes
o Is it complete?
o s must be finite if a solution exists

o Is it optimal?
o Only if costs are all 1 (more on costs later)

57
Breadth-first search

58
FIFO queue
• Enqueue the starting node S and mark it as visited. S

A B C
Output front QUEUE
S
D
S
Nodes adjacent to S: A,B,C
A B C (alphabetical order)
S, A
B C D D is adjacent to A
S, A, B
C D No adjacent to B
S, A, B, C
D No adjacent to C
S, A, B, C, D
No adjacent to D

59
Breadth-first search

60
Breadth-first search

61
Breadth-first search

62
Breadth-first search

63
Breadth-first search

64
Breadth-first search

65
Breadth-first search - Code

graph = { def bfs(visited, graph, node): #function for BFS


'5' : ['3','7'], visited.append(node)
'3' : ['2', '4'], queue.append(node)
'7' : ['8'], while queue: # Creating loop to visit each node
'2' : [], m = queue.pop(0)
'4' : ['8'], print (m, end = " ")
'8' : [] for neighbour in graph[m]:
} if neighbour not in visited:
visited = [] # List for visited nodes. visited.append(neighbour)
queue = [] #Initialize a queue queue.append(neighbour)
# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling

66
67
BFS vs DFS

o When will BFS outperform DFS?

o When will DFS outperform BFS?


68
Video of Demo Maze Water DFS/BFS (part 1)

69
Video of Demo Maze Water DFS/BFS (part 2)

70
Question?
• Consider the following graph. If there is ever a decision between multiple
neighbor nodes in the BFS or DFS algorithms, assume we always choose the letter
closest to the beginning of the alphabet first.

• In what order will the nodes be visited using a Breadth First Search?
The answer is: ABDCEGHF
• In what order will the nodes be visited using a Depth First Search?
The answer is: ABCEHFGD 71
Iterative Deepening
o Idea: get DFS’s space advantage with
BFS’s time / shallow-solution b
advantages …

o Run a DFS with depth limit 1. If no


solution…
o Run a DFS with depth limit 2. If no
solution…
o Run a DFS with depth limit 3. …..

o Isn’t that wastefully redundant?


o Generally most work happens in the lowest
level searched, so not so bad!
72
Iterative Deepening (search l =1)

73
Iterative Deepening (search l =2)

74
Iterative Deepening (search l =3)

75
Cost-Sensitive Search
a GOAL
2 2
b c
3
2
1 8
2 e
3 d f
9 8 2
START h
1 4 2
p 4 r
1 q
5

BFS finds the shortest path in terms of the number of actions.

It does not find the least-cost path. We will now cover a similar algorithm that How?
does find the least-cost path.
76
Uniform Cost Search (UCS)

77
Uniform Cost Search
2 a G
Strategy: expand a b c
1 8 2
cheapest node first: 2 e
3 d f
9 2
S h 8 1
Fringe is a priority queue 1 p r
q
(priority: cumulative cost) 15

S 0

d 3 e 9 p 1

b 4 c e 5 h 17 r 11 q 16
11
Cost a 6 a h 13 r 7 p q f
contours
p q f 8 q c G

q 11 c G 10 a

a 78
Properties of UCS
o What nodes does UCS expand?
o Processes all nodes with cost less than cheapest solution!
b c1
o If that solution costs C* and arcs cost at least  , then the …
“effective depth” is roughly C*/ c2
C*/ “tiers”
o Takes time O(b ) (exponential in effective depth)
C*/
c3

o How much space does the fringe take?


o Has roughly the last tier, so O(bC*/)

o Is it complete?
o Assuming best solution has a finite cost and minimum
arc cost is positive, yes!

o Is it optimal?
o Yes! (Proof next lecture via A*) 79
Uniform Cost Search
o Remember: UCS explores increasing cost … c1
contours c2
c3

o The good: UCS is complete and optimal!

o The bad:
o Explores options in every “direction”
o No information about goal location
Start Goal

o We’ll fix that soon!

80
Uniform-Cost-First

• Visits the next node which has the least total cost from the root, until a
goal state is reached.

• Similar to BREADTH-FIRST, but with an evaluation of the cost for


each reachable node.

• g(n) = path cost(n) = sum of individual edge costs to reach the current
node.

81
UCS Example

Open list: C

82
UCS Example

Open list: B(2) T(1) O(3) E(2) P(5)

83
UCS Example

Open list: T(1) B(2) E(2) O(3) P(5)

84
UCS Example

Open list: B(2) E(2) O(3) P(5)

85
UCS Example

Open list: E(2) O(3) P(5)

86
UCS Example

Open list: E(2) O(3) A(3) S(5) P(5) R(6)

87
UCS Example

Open list: O(3) A(3) S(5) P(5) R(6)

88
UCS Example

Open list: O(3) A(3) S(5) P(5) R(6) G(7)

89
UCS Example

Open list: A(3) S(5) P(5) R(6) G(7)

90
UCS Example

Open list: A(3) I(4) S(5) N(5) P(5) R(6) G(7)

91
UCS Example

Open list: I(4) P(5) S(5) N(5) R(6) G(7)

92
UCS Example

Open list: P(5) S(5) N(5) R(6) Z(6) G(7)

93
UCS Example

Open list: S(5) N(5) R(6) Z(6) F(6) G(7) D(8) L(10)

94
UCS Example

Open list: N(5) R(6) Z(6) F(6) G(7) D(8) L(10)

95
UCS Example

Open list: Z(6) F(6) G(7) D(8) L(10)

96
UCS Example

Open list: F(6) G(7) D(8) L(10)

97
UCS Example

98
UCS Example

99
Uniform-Cost-First

100
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or
UCS? (part 1)

101
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or
UCS? (part 2)

102
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or
UCS? (part 3)

103

You might also like