用于BFS周期检测的伪代码,以检测源节点是否在周期中

时间:2018-11-06 07:24:51

标签: graph-theory cycle breadth-first-search

我在为BFS周期检测提出伪代码时遇到麻烦,该伪代码仅在源节点s在周期中时才返回true。现在我有这个:

Queue = {s} s is the source node
Visited is empty
while Queue is nonempty do
    set u to first node in queue, remove from queue
    if s is neighbor to u then return true
    for every neighbor v of u do
      if v is not in queue or visitedthen
        add v to end of queue
    add u to visited
return false

我知道我的算法是错误的,但是我无法解决它

0 个答案:

没有答案