python - 有没有办法在 Python 中并行运行 BFS?
问题描述
我只是在学习并行计算,我想知道是否有一种方法可以并行运行呼吸优先搜索,这样每个节点的邻居都会同时被遍历。
我有以下 BFS 代码:
# BFS
import collections
def breadth_first_search(graph, root):
visited, queue = set(), collections.deque([root])
while queue:
vertex = queue.popleft()
yield vertex
visited.add(vertex)
queue.extend(n for n in graph[vertex] if n not in visited)
graph = {1: [2, 4], 2: [3], 3: [8], 4: [9, 7], 5: [9], 6: [5], 7: [9], 8: [5], 9: []}
list(breadth_first_search(graph, 1))
#[1, 2, 4, 3, 9, 7, 8, 5]
如果可能的话,我是否需要为每个邻居集使用单独的 GPU?我不完全确定这是如何工作的。例如,在上面的示例中,1
连接到[2,4]
. 然后,2
连接到3
,并且4
连接到[9,7]
。我需要一个 GPU[2,4]
和一个 GPU[3]
吗[9,7]
?
解决方案
推荐阅读
- sql - SQL Server 2014 nchar 转换为日期时间
- python - 烧瓶正则表达式搜索,AttributeError:'NoneType'对象没有属性'group',但仍然获取捕获的字符串
- javascript - Javascript & PouchDB:不能返回来自 pouchdb 的对象数组(因为它是异步的?)
- android - 似乎为该方案注册了不同包中的活动
- java - 使用其他版本的 JDBC 驱动程序
- gradle - 将 JaCoCo 与 Gradle 和 Kotlin 一起使用时如何忽略方法?
- kubernetes - Helm 图表为单个键传递多个环境值
- c++ - 我应该将什么传递给 glfwSetWindowSizeCallback?
- java - 使用 char 转大写后越界
- mongodb - 如何在 mongodb 中更新 json?