我在为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
我知道我的算法是错误的,但是我无法解决它