Chapter 3 - Searching-Part 2
Chapter 3 - Searching-Part 2
2
UCS example: traveling on a graph
C 2
B 9
2
3 F goal state
start state A D E
4
3 4
3
UCS example: traveling on a graph
state = A,
cost = 0
state = B, state = D,
cost = 3 cost = 3
state = A, state = F,
cost = 7 cost = 11
goal state!
state = B, state = D,
cost = 10 cost = 10
4
UCS example 2
5
UCS example 2
6
UCS example 2
7
UCS example 2
8
UCS example 2
9
UCS example 2
10
UCS example 2
11
Breadth-First vs. Uniform-Cost
• Breadth-first search (BFS) is a special case of uniform-cost search
when all edge costs are positive and identical.
12
Depth-First vs. Breadth-First
BFS DFS
13
Depth-First vs. Breadth-First
• Depth-first goes off into one branch until it reaches a leaf node
o Not good if the goal is on another branch
o Uses much less space than breadth-first
14
Depth-First vs. Breadth-First
15
Depth-limited search
• The depth-limited search (DLS) method is almost equal to depth-first search
(DFS), but DLS can work on the infinite state space problem because it bounds
the depth of the search tree with a predetermined limit L. Nodes at this depth limit
are treated as if they had no successors.
16
DLS: Example 1
• Now use the example in DFS to see what will happen if we use DLS with L=1.
• Below is the graph we will traverse. Same as DFS, we use the stack data structure
S1 to record the node we’ve explored. Suppose the source node is node a.
• S1 is empty
17
DLS: Example 1
• S1: a
18
DLS: Example 1
• After exploring a, now there are three nodes reachable: node b, c and
d. Suppose we pick node b to explore first. Push b into S1 and mark it
as visited. Current level is 1.
• S1: b, a
19
DLS: Example 1
• Since current level is already the max depth L. Node b will be treated
as having no successor. So there is nothing reachable. Pop b from S1.
Current level is 0.
• S1: a
20
DLS: Example 1
• Explore a again. There are two unvisited nodes c and d that are
reachable. Suppose we pick node c to explore first. Push c into S1 and
mark it as visited. Current level is 1.
• S1: c, a
21
DLS: Example 1
• Since current level is already the max depth L. Node c will be treated
as having no successor. So there is nothing reachable. Pop c from S1.
Current level is 0.
• S1: a
22
DLS: Example 1
• Explore a again. There is only one unvisited node d reachable. Push d
into S1 and mark it as visited. Current level is 1.
• S1: d, a.
23
DLS: Example 1
• Explore d and find no new node is reachable. Pop d from S1. Current
level is 0.
• S1: a.
24
DLS: Example 1
• Explore a again. No new reachable node. Pop a from S1
• S1 is empty
25
DLS: Example 2
• If L=2
26
Informed Search
27
Heuristic search
▪ A heuristic is:
▪ A function that estimates how close a state is to a goal
▪ Designed for a particular search problem
▪ Pathing?
▪ Examples: Manhattan distance, Euclidean distance for
pathing
10
5
11.2
28
Heuristic search
h(x)
29
Hill climbing strategy
• Simplest way of implementing heuristic search
• Expand the current state and evaluate the children and select the best
child for further expansion. Halt the search when it reaches a state that
is better than any of its children (i.e. there is not a child who is better
than the current state).
30
Hill climbing strategy
• Major Problem: tendency to become stuck at local maxima
• If the algorithm reach a local maximum, the algorithm fails to find a solution.
• An example of local maxima in 8-puzzle
• In order to move a particular tile to its destination, other tiles that are already in goal
position have to be moved. This move may temporarily worsen the board state.
31
Hill climbing strategy
• Procedural steps of HC
32
Hill climbing strategy
33
Blocks World Problem
• Global: For each block that has the correct support (i.e.
the complete structure underneath it is exactly as it should
be), add one point for every block in the support
structure.
• For each block that has an incorrect support structure,
subtract one point for every block in the existing support
structure.
• initial state
(-1) + 1 + 1 + 1 + 1 + 1 + 1 + (-1) = 4
• goal state
1+1+1+1+1+1+1+1=8
34
Blocks World Problem
• State 1
1+ -1 + 1 + 1 + 1 + 1 + 1 + 1 = 6
35
Blocks World Problem
• state 2-(a)
(-1) + 1 + 1 + 1 + 1 + 1 + 1 + (-1) = 4
• state 2-(b)
(-1) + 1 = 0
1 + 1 + 1 + 1 + 1 + (-1) = 4
• state 2-(c)
+1
-1
1 + 1 + 1 + 1 + 1 + (-1) = 4
36
Features of Hill-climbing algorithm
• It employs a greedy approach: This means that it moves in a direction in which the
cost function is optimized. The greedy approach enables the algorithm to establish
local maxima or minima.
37
Greedy Search
38
Greedy Search/Greedy best-first search
• A heuristic function h(n) = estimated cost of the cheapest path from
the state at node n to a goal state.
39
Greedy Search
o Expand the node that seems closest…
o Is it optimal?
o No. Resulting path to Bucharest is not the shortest!
40
Greedy Search
b
o Strategy: expand a node that you think is …
closest to a goal state
o Heuristic: estimate of distance to nearest goal
for each state
o A common case:
b
o Best-first takes you straight to the (wrong) …
goal
42
Greedy Search
43
Greedy Search
44
Greedy Search
45
Greedy Search
46
Greedy Search
47
Greedy Search
48
Greedy Search
49
A* search
50
A* search
UCS Greedy
A*
51
A* search
o Uniform-cost orders by path cost, or backward cost g(n)
o Greedy orders by goal proximity, or forward cost h(n) g=0
S h=6
8 g=1
h=5 a
e h=1
1 g=2 g=9
h=6 b d g=4 e h=1
1 3 2 h=2
S a d G
h=6 h=5 g=3 g=6 g = 10
1 h=2 h=0 h=7 c G d
h=0 h=2
1
c b
g = 12
h=7 h=6 G h=0
A* Search orders by the sum: f(n) = g(n) + h(n)
52
A* search
2 A 2
S h=3 h=0 G
2 B 3
h=1
54
A* search - Example
55
A* search - Example
56
A* search - Example
57
A* search - Example
58
A* search - Example
59
A* search - Example
60
A* search - Example
• We stop when the node with the lowest f-value is a goal state.
• Is this guaranteed to find the shortest path?
61
Properties of A*
Uniform-Cost A*
b b
… …
62
UCS vs A* Contours
63
Comparison
64
A* Applications
• Video games
• Pathing / routing problems
• Resource planning problems
• Robot motion planning
• Language analysis
• Machine translation
• Speech recognition
•…
65
A*: Summary
66
Creating Heuristics
• Most of the work in solving hard search problems optimally is in
coming up with heuristics
366
15
67
Example: 8 Puzzle
• The legal moves are: move the blank tile up, right, down, and left.
• make sure that it does not move the blank off of the board.
• All four moves are not applicable at all times.
70
Example: Tic-tac-toe
• The set of states are all different configurations of Xs and Os that the
game can have. [N]
• 39 ways to arrange {blank, X, O} in nine spaces.
• Arcs(steps) are generated by legal moves of the game, alternating
between placing an X and an O in an unused location [A]
• The start state is an empty board. [S]
• The goal state is a board state having three Xs in a row, column, or
diagonal. [GD]
71
Example: Tic-tac-toe
72
Example: Tic-tac-toe
• There are no cycles in the state space because the directed arcs of the
graph do not allow a move to be undone.
73
Example: Tic-tac-toe
• The total number of states needs to be considered is 9!
• Heuristic is …
• Move to the board in which x has the most winning lines.
x x
x
74
Example: Tic-tac-toe
• Most Winning Lines
x x
x
75
Example: Tic-tac-toe
• Most Winning Lines
x x
x
3 4 2
o o
x x
o
o
x
x
x x 76