Undirected Graph的特殊广度优先搜索(bfs)

时间:2015-11-07 22:06:27

标签: recursion breadth-first-search

我的问题主要与算法有关,而不是特定于特定编程语言

假设我们有一个由列表列表表示的图形,其中每个内部列表代表两个节点和一个编号的边缘,Is可以实现一个递归BFS(广度优先搜索)函数以下12项功能?我们的bfs递归函数应该有5个参数:

  • 要搜索的图表
  • 要查找的元素
  • 搜索队列
  • 访问过的节点列表
  • 当前元素我们正在关注

图表示例:

    e1
   / \
  e2  e3
  | \  |
  e5  e4

((e1 1 e2) (e1 2 e3) (e2 3 e4) (e3 4 e4) (e2 5 e5))

这是功能:

create-gr         ; creates an empty list
is-graph-el:      ; check if a list is of the format above (for graph elements)
el-contains-n     ; check if a graph element contains an atom representing a node (e.g., a)
is-mem            ; check if a list contains an atom
push-unq          ; gets a list and an atom and inserts it at the end of the list if it's not already there
remove-vis        ; gets a graph (list of list), removing all the graph elements containing the specified atom
remove-all-vis    ; same as above, but we can pass a list of atoms to be removed
del-from-list     ; remove all occurrences of an atom from a list
del-all-from-list ; same as above, but we can pass a list of atoms to be removed
first-el          ; return the first element of a node (e.g., for [a, 1, b], return a
third-el          ; return third element (similar to above)
convert-to-els    ; receives a graph and returns a flat list of all first and third elements listed in order

这就是我实现这个功能的方式:

  • 我的递归调用实际上有两个基本情况:当队列为空时(这意味着我们无法到达我们正在搜索的元素),并且当要从队列中弹出的元素是我们正在寻找的元素
  • 在每次调用中,我定义了一个函数来查找当前节点的路径(找到我们可以去的节点),然后我将这些节点推送到队列
  • 然后我将当前节点推送到访问列表并重复

我的问题是我已经定义了两个函数(一个用于查找路径,另一个用于将这些路径的目标节点推送到搜索队列),这些函数不在上面的函数列表中。

我想知道是否可以使用 ONLY 这些功能来进行BFS?

真心感谢任何形式的帮助......

0 个答案:

没有答案
相关问题