Professional Documents
Culture Documents
Lecture 5 - Searching Algorithms
Lecture 5 - Searching Algorithms
Lecture 5 - Searching Algorithms
Searching Algorithms
For some of the lectures in this course, we will use “Word” style rather than “PowerPoint” style notes to facilitate explanation of ideas and review.
Searching 搜索是一种系统地寻找或构造问题的解决方案的技术。
Tree Graph
Tree 树有许多节点(一个根节点、叶节点和其他节点)
⚫ A tree has a number of nodes (A root node, leaf nodes, and other
nodes)
每个节点都可能有通向其他节点的链接。
⚫ Each node may have links leading to other nodes.
每个链路将一个父节点与一个子节点连接起来
⚫ Each link connect a parent node with a child node
从根节点到某个节点的路由称为其路径
⚫ The route from the root node to a certain node is called its path
树不能包含循环(到任何节点的根路径)
⚫ A tree cannot contain cycles (only one path form root to any node)
Graph
一个图也由节点和链接组成
然而,在图中允许循环(循环)(这对于基
⚫ A graph also consist of nodes and links 于树的搜索算法来说可能是个问题)
⚫ Examples
Directed graph
Graphs and Trees
许多搜索算法都是基于树形的(不允许进行循环)。
⚫ Many searching algorithms are tree-based (no loops allowed).
然而,许多问题实际上都是图。
⚫ However, many problems are actually graphs.
解决方案:使用树表示一个图,在必要时重复一些节点
⚫ Solution: represent a graph using a tree, repeating some nodes if
necessary
右边的树是左边图的树表示(注意,一些节点是重复的)。
Representing Problems using Trees (or Graphs)
the left) to a goal state (the example on the right) by applying a series of
Try it out:
https://murhafsousli.github.io/8puzzle/#/
我们希望找到一种方法,通过应用一系列操作符(例如,将相邻的数字移动到空
白方块中),将初始状态(例如,左边的例子)转换到目标状态(右边的例子)
在这种情况下,我们感兴趣的是从根状态到目标状态(隐藏在树的某个地方)的路径
⚫ In this case, we are interested in the path that leads from the root to
Tree:
Input:
piece (0 – 8, blank))
• Operators
square (UP/DOWN/LEFT/RIGHT)
Output:
Graph:
Route planning problem
Input:
traveler)
Output:
本示例中的DFS将按顺序检查节点
For example: DFS in this example would inspect the nodes in the order
[A, B, D, E, C, F, G]
DFS:
[A, B, E, I, J, C, F, G, K, L, H, D]
A B E I J C F G K L H D
or, from right to left:
A D C H G L K F B E J I
[A, D, C, H, G, L, K, F, B, E, J, I]
BFS
[A, B, C, D, E, F, G, H, I, J, K, L]
Report FAILURE
Tree Searching Algorithm (Basic version)
Start
Goal
Tree Searching algorithm: Basic version
⚫ next_node = A Tree:
⚫ visited = [ ]
⚫ to_visit = [ ]
Tree Searching algorithm: Basic version
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
⚫ next_node = A Tree:
⚫ visited = [A]
⚫ to_visit = [ ]
Tree Searching Algorithm: Basic Idea
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
Tree:
⚫ next_node = A
⚫ visited = [A]
⚫ to_visit = [B,C]
Tree Searching Algorithm: Basic Idea
4. Repeat until SUCCESS or FAILURE:
⚫ next_node = ?
⚫ visited = [A]
⚫ to_visit = [ ? ] #(was: [B, C])
A. If there are more nodes in the list
Remove one of the nodes from the list and select it as the next node
从to_visisList中删除哪个节点?
Question: which node to remove from the to_visit List?
The first one? The last one? Random?
Answer: It depends:
For DFS: remove the last one (i.e., from the END of the LIST)
For BFS: remove the first one (i.e., from the FRONT of the LIST)
Depth First Search Algorithm
1. Start at the root node (called the next node).
Report FAILURE
Breadth First Search Algorithm
1. Start at the root node (called the next node).
Report FAILURE
我们将继续使用深度优先搜索继续前面的示例
We will now continue the previous example using Depth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the LAST nodes from the list and select it as the next node
⚫ next_node = C
⚫ visited = [A]
⚫ to_visit = [B C] #(was: [B, C])
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
Tree:
⚫ next_node = C
⚫ visited = [A, C]
⚫ to_visit = [B C]
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
⚫ next_node = C
⚫ visited = [A, C]
⚫ to_visit = [B, F, G]
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the LAST nodes from the list and select it as the next node
C. If it is not the goal, add its children to the end of the to_visit list
⚫ next_node = G
⚫ visited = [A, C, G]
⚫ to_visit = [B, F]
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
⚫ next_node = G
⚫ visited = [A, C, G]
⚫ to_visit = [B, F] (No child to add)
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the LAST nodes from the list and select it as the next node
⚫ next_node = F
⚫ visited = [A, C, G]
⚫ to_visit = [B] (was: B, F)
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
⚫ next_node = F
⚫ visited = [A, C, G, F]
⚫ to_visit = [B] (F has no child to add)
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the LAST nodes from the list and select it as the next node
⚫ next_node = B
⚫ visited = [A, C, G, F]
⚫ to_visit = [ ] (was: B)
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
C. If it is not the goal, add its children to the end of the to_visit list
⚫ next_node = B
⚫ visited = [A, C, G, F, B]
⚫ to_visit = [D, E]
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the LAST nodes from the list and select it as the next node
next_node = E
visited = [A, C, G, F, B]
to_visit = [D ]
Tree Searching Algorithm: Depth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
next_node = E (Goal!)
visited = [A, C, G, F, B, E]
to_visit = [D ]
Note: The path from root to goal is [A, B, E] Goal
Next, we repeat the search using Breadth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the FIRST nodes from the list and select it as the next node
⚫ next_node = ?
⚫ visited = [A]
⚫ to_visit = [ ? ] #(was: [B, C])
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
next_node = B
visited = [A, B]
to_visit = [ C ]
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
next_node = B
visited = [A, B]
to_visit = [ C, D, E ]
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the FIRST nodes from the list and select it as the next node
⚫ next_node = C
⚫ visited = [A, B ]
⚫ to_visit = [ D, E] (was: C, D, E)
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
next_node = C
visited = [A, B, C]
to_visit = [ D, E] (was: C, D, E)
Tree Searching Algorithm: Breadth First Search
5. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
next_node = C
visited = [A,B,C]
to_visit = [D,E,F,G]
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the FIRST nodes from the list and select it as the next node
⚫ next_node = D
⚫ visited = [A, B, C]
⚫ to_visit = [E,F,G] (was:[D,E,F,G])
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
next_node = D
visited = [A, B, C, D]
to_visit = [E, F, G]
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
Remove the FIRST nodes from the list and select it as the next node
⚫ next_node = E
⚫ visited = [A, B, C, D]
⚫ to_visit = [F,G] (was: E, F, G)
Tree Searching Algorithm: Breadth First Search
4. Repeat until SUCCESS or FAILURE:
C. If it is not the goal, add its children to the end of the to_visit list
Goal
Depth First Search
Implementation (Version 1)
In this example, we represent a tree in a Python
2: [4, 5],
3: [6, 7]}
goal = 5
Goal
return x==goal
Depth First Search
Implementation (Version 1)
And the search function closely follows the
graph data:
然而,在一些问题中,预先预先定义整个graph是不现实的。
⚫ However, in some problems, it is not practical
problems, as follows:
为了解决这种可能性,我们可以用更通用的find_chiilds()函数替换红
色标记的行,以生成子代,可以针对不同的问题进行更改,如下所示:
Depth First Search
Implementation (Version 2)
上述版本在查找目标状态时工作得很好。但是,它不记得从根目录到
目标的路径。
Depth First Search Implementation
(Version 3): Remember the path
⚫ The above versions work fine in finding a goal
state. However, it does not remember the path
The path is
[1, 2, 5]
Goal
为此,我们可以记录从根到每个节点的路径,通过将其存储在to_
visit List中的每个状态旁边。
⚫ To do so, we can record the path from the root
to each node, by storing it next to each state
in the to_visit List.
因此,to_visit列表中的每个元素现在是一个两部分的元组(state,
path),
⚫ Thus, each element of the to_visit list is
其中
now a 2-part tuple (state, path), where
state是问题中的一种状态(与以前相同)
◼ state is a state in the problem (same as
before)
path是从根节点到当前节点的状态列表
◼ path is a list of the states from the root to
of DFS version 2.
visited.
⚫
Exercise 3: Maze
Write a program in Python to find a path through
Hints:
Python?
[A, B, D, I, K, M, N]
Comparison of DFS and BFS
Depth First Search Breadth First Search
Problems Solutions occur deep in the tree Solutions occur at a reasonable level
requirement Need to store to_visit nodes Need to store all nodes of the current
any time
Complete? Cannot find solution if a branch Can always find solution (if it exists)