python – 在大图上运行DFS
作者:互联网
我试图在实现Kosaraju算法的大图中找到强连通组件.它需要在图形上反向运行DFS,然后转发.如果您感兴趣,此图表的边缘列表如下:https://dl.dropboxusercontent.com/u/28024296/SCC.txt.tar.gz
我不能在Python中递归地实现它,它超过了它的递归限制并且如果我增加它们会崩溃.我正在尝试通过迭代实现.
下面是我的代码1.将图形反向加载到字典中,以及2.对于每个节点从n – >运行迭代的DFS. 1.
此代码适用于小样本图,但不适用于此大图.我觉得它效率低但是有关如何使它工作的任何提示?
def reverseFileLoader():
graph = collections.defaultdict(lambda: {'g': [], 's': False, 't': None, 'u': None })
for line in open('/home/edd91/Documents/SCC.txt'):
k, v = map(int, line.split())
graph[v]['g'].append(k)
return graph
def DFS(graph, i):
global t
global source
stack = []
seen = []
seen.append(i)
stack.append(i)
while stack:
s = stack[-1]
j = len(graph[s]['g']) - 1
h = 0
while (j >= 0):
if graph[s]['g'][j] not in seen and graph[graph[s]['g'][j]]['t'] == None:
seen.append(graph[s]['g'][j])
stack.append(graph[s]['g'][j])
h += 1
j -= 1
if h == 0:
if graph[s]['t'] == None:
t += 1
graph[s]['u'] = source
graph[s]['t'] = t
stack.pop()
def DFSLoop(graph):
global t
t = 0
global source
source = None
i = len(graph)
while (i >= 1):
print "running for " + str(i)
source = i
DFS(graph, i)
i -= 1
解决方法:
Kosaraju的算法可能要求检查元素是否被看到是O(1)操作.但是您看到的数据结构有O(n)时间成员资格测试.从列表中看到转换为集合使得代码在我的系统上几秒钟内执行(在删除占用大部分剩余执行时间的打印之后).
为了完整性,您需要进行更改
>改变看见= []看到= set()
>将每个seen.append(…)更改为seen.add(…).
标签:python,graph,graph-algorithm,depth-first-search 来源: https://codeday.me/bug/20190702/1352960.html