Python BFS 与集合
我遇到了 BFS 代码,其中涉及集合和双端队列,但我不太理解。我希望这里的一些 Python 爱好者可以帮助新手。
from collections import deque
def bfs(g, start):
queue, enqueued = deque([(None, start)]), set([start])
while queue:
parent, n = queue.popleft()
yield parent, n
new = set(g[n]) - enqueued
enqueued |= new
queue.extend([(n, child) for child in new])
问题:
1) |= 运算符似乎与按位运算有关 - 我不知道它与 BFS 有何关系,有什么提示吗?
2) 据我了解,popleft()应该只返回一个值,那么它是如何返回parent和n的呢?
3) 访问的一系列节点是新的吗?如果我想要这些节点,我是否只需将它们附加到列表中?
提前致谢。
克雷格
I came across a BFS code which involves collections and deques but I could not understand it much. I hope some of the pythonistas here can help a n00b out.
from collections import deque
def bfs(g, start):
queue, enqueued = deque([(None, start)]), set([start])
while queue:
parent, n = queue.popleft()
yield parent, n
new = set(g[n]) - enqueued
enqueued |= new
queue.extend([(n, child) for child in new])
Questions:
1) The |= operator seems to be related to bitwise operations - I have no idea how it relates to a BFS, any hints?
2) popleft() should return only one value from what I understand, so how is it returning parent and n here?
3) Is new the series of nodes visited? If I want the nodes, do I just keep appending them to a list?
Thanks in advance.
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
a |= b
对于集合来说是相同的对于集合来说
a = a.union(b).
popleft()
确实返回只有一个元素,恰好
是一个 2 元组,因此可以是
解压缩为两个值。
new
是尚未的集合访问过的节点。
a |= b
for sets is the sameas
a = a.union(b)
.popleft()
does indeed returnonly one element, which happens to
be a 2-tuple, and therefore can be
unpacked into two values.
new
is the set of not yetvisited nodes.
只是回答你的最后一个问题:你的代码片段有一个生成器,这意味着它在广度优先遍历图时会产生节点。它不执行任何实际搜索,只是为您遍历节点。使用这段代码的方式是迭代结果,这将依次为您提供所有节点(按广度优先顺序):
或者,例如,如果您想要匹配特定条件的所有节点的列表:
Just to answer your last question: the piece of code you have there is a generator, which means it yields nodes as it traverses the graph breadth-first. It doesn't do any actual searching, it just traverses the node for you. The way you use this piece of code is by iterating over the result, which will give you all nodes in turn (in breadth-first order):
Or if you want, for example, a list of all nodes that match a particular condition:
1)
x |= y
将 x 设置为 x 和 y 的布尔 OR。set
覆盖运算符以表示集合并集。基本上,这是一种编写enqueued.update(new)
的奇特方式。2)
queue
的第一个元素始终是 2 元组。是一种奇特的书写方式
3)
new
只是新的(未访问的)节点的临时变量。enqueued
包含结果。1)
x |= y
sets x to the boolean OR of x and y.set
overrides the operator to mean the set union. Basically, it's a fancy way of writingenqueued.update(new)
.2)
The first element of
queue
is always a 2-tuple.is a fancy way of writing
3)
new
is just a temporary variable for - well - the new(unvisited) nodes.enqueued
contains the result.