4 Search - V3
4 Search - V3
CHAPTER 4
SEARCH
4:0
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
3 SEARCH
Graph Theory
▪ Graph consists of a set of:
o Nodes – a state.
o Arcs – connect pairs of nodes – a transitions between states.
o Ex. Chess – node represent a state of chessboard. The arc represents legal
moves from one state to another.
▪ If a directed arc connects Nj and Nk, the Nj is called the parent of Nk and Nk is called the
child of Nj.
▪ If (Nj,Ni) then Nk and Ni are called siblings.
▪ A rooted graph has a unique node Ns from which all paths originate.
▪ A tip or leaf is a node that has no children.
▪ On a path in a rooted graph, a state is said to be an ancestor of all states positioned
after it (to its right) and a descendant of all states before it (to its left).
▪ A tree is a graph in which there is a unique path between every pair of nodes. Each
node in a rooted tree has u unique parent.
4:1
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
A 100
B
75
125 12
5
50
100
125 75
E
50 C
D 100
Starting at A, find the shortest path through all the cities, visiting each city exactly once and
returning to A. The figure below shows the search tree for the given TSP.
A
B C D E
C D E
D E C E
E D E C
A A A
4:2
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Question
Draw a complete search tree showing the state space of the above map from
the city A to city Z.
Solution A
X M Y
A A
G M
A A
M G Y Z S
A X G M M
M A A A
A
4:3
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
if CS has no children (excluding nodes in DE, SL, NSL) NSL: contain child of
while SL is not empty and CS = the first element
of SL previous CS with
add CS to DE previous NSL.
remove first element from SL
remove first element from NSL
CS first element in NSL
end while
DE: node will not dead
add CS to SL until node has no child
end
else
or node’s child has
place children of CS into NSL (except nodes dead.
already in DE, SL, NSL)
CS first element of NSL
add CS to SL
end
end while
CS SL NSL DE
A
A [A] [A] []
B [B,A] [B,C,A] []
D [D,B,A] [D,E,B,C,A] [] B C
E [E,B,A] [E,B,C,A] [D]
C [C,A] [C,A] [B,E,D]
F [F,C,A] [F,C,A] [B,E,D]
D E F
[] [] [] [A,C,F,B,E,D]
4:4
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
C F B
Q W E R T Y S D G
Solution CS SL NSL DE
4:5
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
BLIND SEARCH
According to Callan, Blind Search algorithms perform a methodical exhaustive search. There
is no information used in guiding the search but it uses a fixed strategy for generating states
and testing for the goal. There are 2 simple algorithms that falls in Blind Search category:
i. Breadth-First Search
ii. Depth-First Search
B C
D E F G H
I J K L M N
O P Q
The algorithm for implementing BFS makes use of two structures, which are: -
4:6
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Node current
Queue opened
Based on the previous algorithm, the queue in BFS is based on First-In-First-Out (FIFO)
data structure, where the next element to be removed will be the one that was placed
earliest onto the queue.
4:7
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
B C
D E F G H
I J K L M N
Goal Found
O P Q
Same as BFS, DFS also use two structures in implementing the algorithm, which are:
Node current
Queue opened
4:8
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Compared to BFS, the queue for DFS is based on Last-In-First-Out (LIFO) data structure,
where the next element to be removed will be the one that was placed earliest onto the
queue. Below is the list on how the queue of states opened and the closed list develop
during execution of DFS algorithm on the given tree.
4:9
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
HEURISTIC SEARCH
Heuristic is a method that guaranteed to find a good solution in reasonable time, but
sometimes it might not find the best solution. According to Callan, a heuristic function is a
rule that will return a value for a node that indicates how promising the search will be if it
explores paths that emerge from that node.
A heuristic function can be called to evaluate how promising the search will be if the paths
leading from that node are explored. The nodes in opened can be ordered according to the
values that the function returns for each node and this will depend on the way the problem is
expressed.
Here, the value is treated by a function as a cost and look to minimize the cost. Therefore,
problems are usually expressed so that nodes with lower values take precedence over those
with high value.
There are 3 types of heuristic search that will be covered in this chapter, which are:-
• Hill Climbing
• Best First Search
• A* Algorithm
HILL CLIMBING
Hill climbing looks like to maximize cost as opposed to minimizing it. Hill climbing search is a
heuristic search that always seeks to improve on the existing state with the next move,
where it never makes a move to a less desirable state. If the search reaches the local ridge,
it will get stuck because all successive moves take the search down before being able to
move back up (improving).
The idea is do not keep the big list of state around, but just keep track of the one state you
are considering and the path that got you there from the initial state. This strategy expands
the current state of the search and evaluates its children. Here, the best child is selected for
further expansion. This will lead to the goal and continue from there. Hill climbing comes
from the idea that you are trying to find the top and you got direction that is up from wherever
you are. This strategy often works but since it only uses information, it can be fooled. Here,
only the best child is maintained. If the node has no children, the searching process is
terminated, and no turning back (no backtracking). The goal is detected only if the minimum
cost function is found, where the cost function for child is more than the parent’s cost
function.
i. Evaluate the initial state if it is goal state quit otherwise current state is initial
state.
ii. Select a new operator for this state and generate a new state.
iii. Evaluate the new state
a. if it is closer to goal state than current state makes it current state
b. if it is no better ignore
iv. If the current state is goal state or no new operators available, quit. Otherwise
repeat from 2.
4:10
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
For example:
Based on the search tree below, perform hill climbing. Given H0 is goal.
A∞
B10 C11
D8 E13 G∞ H0
K14 L15
[A∞] []
A∞ [B10, C11] [A∞]
B10 [D8, E13] [B10, A∞]
D8 [K14, L15] [D8,B10, A∞]
Search terminated and failed.
***Here, since in the question H0 is given as a goal, the searching process is terminated and
failed.
1. Local Maxima
The major problem of hill climbing is local maxima. If it reaches to a state that has better
solution than its children, the algorithm stops. If the state is not the goal, but just a local
maximum, the algorithm may fail to find the best solution.
3 4 2
4 2 3 3
3 5 4 3 2 3
4:11
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
2. Plateau
Another problem with hill climbing is plateau. This occurs when it gets to a “flat” part of a
search space. It usually happens when the heuristics are all very close to each other. This
kind of flatness can cause the algorithm to cease progress and wander aimlessly.
Question Perform hill climbing onto the following tree to the goal K.
1 3 7
S D F
9 7 5 4 1 2
G H J K L Q
5
4
W E
8
1
2
R T Y
Solution Evaluated
Node Opened Closed
4:12
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Best-First Search is a search that orders the search on the basis of node preference (Callan,
2003). It is a combination of Depth-First Search and Breadth-First Search. The search can
be implemented based on the following algorithm:
PROCEDURE BestFirstSearch(Tree tree, Node root, Goal goal, List closed)
node current
<DS> opened
end for
add current to closed
end while
Given is a tree that has been populated with costs to illustrated heuristic search
8 A
6 B 7 C
5 D 2 E 3 F 8 G 6 H
11 I 9 J 7 K 3 L 2 M 8 N
10 O 2 P 0 Q
4:13
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
A priority queue will order its elements according to some precedence. Best First Search can
be implemented so that every time a child is placed on opened all of the nodes are ordered
so that the lowest cost node is always the next node to be removed. The heuristic function
determines the cost and therefore the ordering of nodes. Below is the order of node visits for
the previous tree as determined by a Best First Search.
Question The following state-space search is a hypothetical solution for a game tree,
where A is the start state and L is the goal state.
B:5 C:6
Show all the steps involved for each open and closed list while performing
hill climbing search.
Evaluated
Solution Node Opened Closed
4:14
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
A* ALGORITHM
where g(n) measures the actual length of the path from any state n to the start state, and
h(n) is a heuristic estimate of the distance from state n to goal. In general, h(n)describes
how close node n to the goal. h(n) always relies upon some heuristic information available
from the problem domain.
The function f(n) provides an estimated cost of the cheapest solution through node n. It
sums the cost of the path from the start node to node n, with cheapest path estimate from n
to the goal. If the function h(n) never overestimates the cost of reaching the goal, then h(n)
is said to be admissible. If best-first search uses f(n) as the heuristic function and h(n) is
admissible, the search is known as A* algorithm.
Consider a family vacation where the Dad, Mom and the two kids are all crammed into a car.
After about 5 minutes, the kids start asking “How much father ‘til we get there?” To sate the
children’s curiosity for a few minutes, the dad will make a rough guess. “Another 300 miles. It
will be a while, so why don’t you take a nap?”. If the family had already driven 100 miles at
that point, that would represent g(n), the total distance traveled so far. The estimate of 300
miles would be h(n) – the guess on how much farther it would be. So, for f(n) would be 100
+ 300 = 400 miles.
Terms.
Admissibility
When a search algorithm is guaranteed to find the minimal path to the goal, whenever the
path exists, then the search algorithm is called admissible.
Informedness
For two heuristics h1 and h2, if h1(n) ≤ h2(n) for all states n in the search space, heuristic h1
is said to be more informed than h2.
Monotonicity
A heuristic function h is monotone if:
1. For all states ni and nj, where nj is a descendent of ni, h(ni) - h(nj) ≤ cost(ni,nj), where
cost(ni,nj) is the actual cost (in number of moves) of going from state ni to nj.
2. The heuristic evaluation of the goal state is zero, or h(goal) = 0.
4:15
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Ex1 A pipeline is to be built that will link six cities. The cost (in millions) of
constructing each potential link depends on distance and terrain and is
shown in the weighted graph below. Use A* algorithm to find the
minimum path to build the pipeline from A to D.
A 1.5 B
h(n)
1.6
0.8
A 3.3
2.5
C 1.8 B 1.7
A
1.9 C 1.7
1.7 A
D 0
2.9 2.2
E 1.1
1.1
F 2.3
E D
1.2
C D
4:16
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Ex2 An ant has to navigate its way from square A3 to square E3. It cannot enter the
dark squares as they represent walls.
A B C D E
1
2
3 Goal
4
5
Given that, the cost to move is the number of boxes it goes through. And the
h(n) are given in the following table.
A B C D E
1 6 5 4 3 2
2 6 4 1
3 5 3 0
4 5 4 3 2 1
5 5 4 3 2
Solution
4:17
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Two search techniques have been developed to cater games problem, minimax and alpha
beta pruning. Both techniques are suitable for two player games.
MINIMAX
In Minimax search algorithm, the opponents in the game are referred as MIN and MAX. The
significance of referring with these names are as follows:
MAX: represents the player that trying to maximize his advantage in order to win the
game.
MIN: represents the player who attempts to minimize MAX’s score. Always move to a
state that is worst for MAX.
The minimax search propagates the values up the graph, through successive parents,
according to these rules:
If the parent state is MAX, give it the maximum value of its children.
If the parent state is MIN, give it the minimum values of its children.
Exercise Consider the following game tree. Find the best move for the MAX player using
the minimax procedure.
A
2 3 5 9 0 7 4 2 1 5 6
4:18
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
/*
alpha is the best score for max along the path to state
beta is the best score for min along the path to state
Exercise Consider the following game tree. Find the best move for the MAX player using
the alpha-beta search.
2 3 5 9 0 7 4 2 1 5 6
4:19
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
1. Draw a complete state space search for the following map, to move from A to F.
F E A C
4:20
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
T:8 U:6
V:5 X:1
a) Perform Best First Search onto the given graph to move from A to S.
4:21
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
b) Perform Depth First Search onto the given graph to move from A to S.
d) Perform Breadth First Search onto the given graph to move from A to S.
4:22
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
B:5 C:6
a) Perform Breadth First Search onto the given graph to move from A to M.
b) Perform Depth First Search onto the given graph to move from A to M.
4:23
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
d) Perform Best First Search onto the given graph to move from A to M.
4:24
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
4.
A
36
B
61
31
32 80 L
D C
52
31
E F 102
43 112
G K
20 122
32
H M
40 36
I
45
J
Using the A* algorithm work out a route from town A to town M. Use the following cost
functions.
➢ G(n) = The cost of each move as the distance between each town (shown on map).
➢ H(n) = The straight line distance between any town and town M. These distances are
given in the table below.
Provide the search tree for your solution and indicate the order in which you expanded the
nodes. Finally, state the route you would take and the cost of that route.
Straight Line Distance to M
A 223 E 165 I 100 M 0
B 222 F 136 J 60
C 166 G 122 K 32
D 192 H 111 L 102
A
42
48
B
20
23
D C 40 L
42
29
E F 20
10 42
G K
20 40
41
H M
10
32
I
23 J
4:25
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Using the A* algorithm work out a route from town A to town M. Use the following cost
functions.
➢ G(n) = The cost of each move as the distance between each town (shown on map).
➢ H(n) = The straight line distance between any town and town M. These distances are
given in the table below.
Provide the search tree for your solution, showing the order in which the nodes were
expanded and cost at each node. You should not re-visit states previously visited. Finally,
state the route you would take and the cost of that route.
Straight Line Distance to M
A 51 E 42 I 50 M 0
B 50 F 14 J 32
C 32 G 33 K 41
D 28 H 43 L 56
73
64
104
C
B 89
D E
83 64
F 40 G
31 35
89
H 84
I J
36 20
35 28
53
K L M 113
63 50 80
O N
41
72
P Q
65
65
R
4:26
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Using the A* algorithm work out a route from A to R, using the following cost functions
g(n) = the distance between each town (shown on map)
h(n) = the straight line distance between any town and town R. These distances are given in
the table below
Straight Line Distance to R
A 240
B 186
C 182
D 163
E 170
F 150
G 165
H 139
I 120
J 130
K 122
L 104
M 100
N 77
O 72
P 65
Q 65
R 0
i) The search tree that is produced, showing the cost function at each node
(10)
ii) State the order in which the nodes were expanded
(2)
iii) State the route that is taken, and give the total cost
(1)
K 20 20 M
L
Using the A* algorithm work out a route from town A to town M. Use the following cost
functions.
➢ G(n) = The cost of each move as the distance between each town (shown on map).
➢ H(n) = The straight line distance between any town and town M. These distances are
given in the table below.
4:27
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
(14 marks)
b) Assume the straight line distance table was replaced by the following table.
What route would now be returned by the A* algorithm and what would the cost of that route
be?
(6 marks)
4:28
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
Start
g(n) = 0 2 8 3 State A
h(n) = 4
1 6 4 f(n) = 4
7 5
g(n) = 1
4:29
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
9. Apply left-to-right / right-to-left α-β pruning on the tree and show the path which would
lead to a win for minimum / maximum (refer to respective questions).
i) left-to-right - Minimizing
2 7 1 8
2 7 1 8
4:30
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
2 7 1 8
2 7 1 8
4:31
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
10. Apply left-to-right / right-to-left α-β pruning on the tree and show the path which would
lead to a win for minimum / maximum (refer to respective questions).
i) left-to-right - Minimizing
3 12 8 2 4 6 14 5 2
3 12 8 2 4 6 14 5 2
4:32
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
3 12 8 2 4 6 14 5 2
3 12 8 2 4 6 14 5 2
4:33
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
11. Apply a left-to-right α-β pruning on the tree which would lead to a win for maximum. If
there is any cutoff, indicate where it occurs and state the intermediate value for each
path.
2 5 5 7 8
7 6 0 7 7 8 5 4
12. Apply a left-to-right α-β pruning on the tree which would lead to a win for maximum. If
there is any cutoff, indicate where it occurs and state the intermediate value for each
path
1 3 -2 2 4 -1 1 2 -1 4 7 3 6 -1 -1 1 2 4 5 -2 7
4:34
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
i) Consider the game tree given below and answer the questions below. Apply a right-to-
left α-β pruning on the tree which would lead to a win for minimum. If there is any
cutoff, indicate where it occurs and state the intermediate value for each path
1 3 -2 2 4 -1 1 2 -1 4 7 3 6 -1 -1 1 2 4 5 -2 7
B C D
E F G Goal
H I J
4:35
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
B C
D E F G
B C
D E F G
I J
4:36
MODULE – LECTURE NOTES ISP542/ITS462 SEARCH
16. Based on the following search tree, perform the following search algorithm.
A:10
a. Hill climbing
b. Best first search
4:37