Search Algorithms
Search Algorithms
node←NODE(STATE=problem.INITIAL)
reached←a lookup table, with one entry with key problem. INITIAL and value node
node←POP(frontier)
s←child.STATE
reached[s]←child
return failure
s←node.STATE
s’←problem.RESULT(s, action)
Figure The best-first search algorithm, and the function for expanding a node.
A very general approach is called best-first search, in which we choose a node, n, with minimum
value of some evaluation function, f(n). On each iteration we choose Evaluation function a node on
the frontier with minimum f(n) value, return it if its state is a goal state, and otherwise apply
EXPAND to generate child nodes. Each child node is added to the frontier if it has not been reached
before, or is re-added if it is now being reached with a path that has a lower path cost than any
previous path. The algorithm returns either an indication of failure, or a node that represents a path
to a goal. By employing different f(n) functions, we get different specific algorithms,
Search data structures
Search algorithms require a data structure to keep track of the search tree. A node in the tree
• node.ACTION: the action that was applied to the parent’s state to generate this node;
• node.PATH-COST: the total cost of the path from the initial state to this node. In mathematical
formulas, we use g(node) as a synonym for PATH-COST.
We need a data structure to store the frontier. The appropriate choice is a queue of some
• POP(frontier) removes the top node from the frontier and returns it.
• TOP(frontier) returns (but does not remove) the top node of the frontier.
• ADD(node, frontier) inserts node into its proper place in the queue.
Priority queue • A priority queue first pops the node with the minimum cost according to some
evaluation function, f . It is used in best-first search.
FIFO queue • A FIFO queue or first-in-first-out queue first pops the node that was added to the
queue first; we shall see it is used in breadth-first search.
LIFO queue • A LIFO queue or last-in-first-out queue (also known as a stack) pops first the most
node←NODE(problem.INITIAL)
reached← {problem.INITIAL}
s←child.STATE
add s to reached
return failure