幅優先探索プログラムのアルゴリズム
The breadth-first search algorithm searches a state space by constructing a hierarchical tree structure consisting of a set of nodes and links. The algorithm defines a way to move through the tree structure, examining the value at nodes. From the start, it looks at each node one edge away. Then it moves out from those nodes to all two edges away from the start. This continues until either the goal node is found or the entire tree is searched.
The algorithm follows:
1.Create a queue and add the first node to it.
2.Loop:
If the queue is empty, quit.
Remove the first node from the queue.
If the node contains the goal state, then exit with the node as the solution.
For each child of the current node: add the new state to the back of the queue.
深さ優先探索プログラム
The depth-first algorithm follows a single branch of the tree down as many levels as possible until we either reach a solution or a dead end. It searches from the start or root node all the way down to a leaf node. If it does not find the goal node, it backtracks up the tree and searches down the next untested path until it reaches the next leaf.
The algorithm follows:
1.Create a queue and add the first node to it.
2.Loop:
If the queue is empty, quit.
Remove the first node from the queue.
If the node contains the goal state, then exit with the node as the solution.
For each child of the current node: add the new state to the front of the queue.