Search Algorithms 4
Search Algorithms 4
Search Algorithms
B A G F E C D H D
B E
C F K G
Depth First Search Order ABDHIEJLMCFKG Visit a nodes children before its siblings
Breadth First Search Order ABCDEFGHIJKLM Visit a nodes siblings before its children
Trees v. Graphs
Previous slide showed search order in a tree - only one route to a node In a graph we must take care not to revisit anywhere already visited
H D A B E C F L M K
3 4
Depth-First Search
Organise the toDo list as a stack (last-in first-out - LIFO queue) New nodes are always added at the front of the list Since nodes are always taken off from the front this makes toDo operate as a stack.
Depth-First Example
A B D E C F L M K G
toDo [A] alreadySeen [] 1) Visit A toDo [BC] alreadySeen[A] 2) Visit B toDo [HDEC] alreadySeen[BA] 3) Visit H toDo [DEC] alreadySeen[HBA] 4) Visit D toDo[IEC] alreadySeen[DHBA] 5) Visit I toDo[EC] alreadySeen[IDHBA] etc
7
Breadth-First Search
Organise the toDo list as a FIFO (first-in first-out) queue New nodes are always added at the back of the list Since nodes are always taken off from the front this makes toDo operate as a FIFO queue.
Breadth-First Example
A B D E C F L M K G
toDo [A] alreadySeen [] 1) Visit A toDo [BC] alreadySeen[A] 2) Visit B toDo [CHDE] alreadySeen[BA] 3) Visit C toDo [HDEFG] alreadySeen[CBA] 4) Visit H toDo[DEFG] alreadySeen[HCBA] 5) Visit D toDo[EFGI] alreadySeen[DHCBA] etc
9
Best-First Search
Organise the toDo list as an ordered list i.e. make sure it is kept sorted, with the best node at the front New nodes are always added at the appropriate place in the list i.e. go down the list until one reaches the first node that is worse than the one to be inserted, and insert the new node in front of it. Insert new node at end if there is no node worse than it. Since nodes are always taken off from the front this makes toDo operate as a best-first ordered queue. NOTE: This should really be called something like apparently-best-first search. Usually there is no guarantee the chosen node is really the best - just the one that seems the best at the time.
11
Best-First Example
A 0 B 3 D 5 E 5 C 4 F 7 L 10 M 8 K 9 In this example numbers are costs - Smaller is better G 6
H 8
I 9
J 7
toDo [A] alreadySeen [] 1) Visit A toDo [BC] alreadySeen[A] 2) Visit B toDo [CDEH] alreadySeen[BA] 3) Visit C toDo [DEGFH] alreadySeen[CBA] 4) Visit D toDo[EGFHI] alreadySeen[DCBA] 5) Visit E toDo[GFJHI] alreadySeen[EDCBA] etc
12
Evaluation Functions
The mapping from a node to its goodness value (usually thought of as a measure of distance to the goal, or as a cost of getting to the goal) define a function f f is known as the evaluation function. It assigns a value f(n) for each node n in the search graph. Usually smaller values of f are considered better Therefore nodes with the lowest values of the evaluation function are at the front of the toDo list, and will be selected next
13
Reminder
Nodes in the search graph have two parts:
A state A history of the previous nodes searched to get to this one
Therefore nodes in the search graph can have values associated with them which reflect something about the search history e.g. depth, total cost etc.
14
Heuristic Functions
Evaluation functions f often use some combination of:
A cost function g, which measures the cost g(n) to get from the start node to a node n A heuristic function h which computes an estimate h(n) of the cost of the shortest path from n to the goal. If a state n satisfies the goal condition then h(n) must equal zero
Examples
Consider the route finding problem i.e. we are given a set of nodes (cities) and distances (roads) between them. We wish to find a route between two cities Evaluation function 1: g(n) is just the depth of n in the search graph, and f(n) = g(n) i.e. no heuristic function This is equivalent to breadth first search (nodes searched in order of increasing depth) This will find a route if it exists, but not necessarily the optimum (shortest) one first.
g(n) usually depends on the history part of the search node n h(n) usually only depends on the state part of the search node n h is what one uses to embed problem specific knowledge, and hence intelligence into the search
15
16
Examples continued
Evaluation Function 2: g(n) is the total distance travelled on the path from the root to node n. Again no heuristic function, so f(n) = g(n) in this case. This is equivalent to what is called uniform cost search. It will find the shortest route first.
Examples continued
Evaluation Function 3: g(n) is N - depth(n), where N is the total number of cities. Again no heuristic function, so f(n) = g(n) in this case. This is equivalent to depth-first search NOTE: the uninformed search strategies (depth-first, best-first, uniform cost) are equivalent to best-first search with an evaluation function which does not depend on the state information in any way
18
17
Examples continued
Evaluation Function 4: Suppose we are given additional information about the map e.g. a table of the straight-line (as the crow flies) distances between each of the cities
City A B C D E A 0 10 5 27 3 B 10 0 18 20 15 C 5 18 0 7 12 D 27 20 7 0 8 E 3 15 12 8 0
Examples continued
Let g(n) be the total distance travelled on the path from the root to node n. Let h(n) be the straight-line distance from n to the goal city Let the evaluation function be f(n) = g(n)+h(n) f can be interpreted as the estimated cost of the cheapest solution passing through n This is guaranteed to find the optimal solution
20