Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

Is there a way to run BFS in parallel in Python?

I'm just learning about parallel computing and I was wondering if there's a way to run breath first search in parallel, such that every node's neighbors will simultaneously be traversed.
I have the following BFS code:

# 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]

If it is possible, will I need a separate GPU for each neighbor set? I'm not entirely sure how this works. e.g. in the example above, 1 is connected to [2,4]. Then, 2 is connected to 3, and 4 is connected to [9,7]. Will I need a one GPU for [2,4] and one for [3] , [9,7]?

question from:https://stackoverflow.com/questions/65599102/is-there-a-way-to-run-bfs-in-parallel-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...