【问题标题】:Large graph as input to find all paths大图作为输入以查找所有路径
【发布时间】:2016-07-18 08:24:11
【问题描述】:

我正在使用以下 python 代码来查找每两个节点之间的所有路径。小图没问题。

def bfs(graph, start, end):
    # maintain a queue of paths
    queue = []
    # push the first path into the queue
    queue.append([start])
    while queue:
        # get the first path from the queue
        path = queue.pop(0)
        # get the last node from the path
        node = path[-1]
        # path found
        if node == end:
            return path
        # enumerate all adjacent nodes, construct a new path and push it into the queue
        for adjacent in graph.get(node, []):
            new_path = list(path)
            new_path.append(adjacent)
            queue.append(new_path)

for node3 in graph:
        for node4 in graph:
            few = bfs(graph, node3, node4)
            if not few == None:
                print ("{0} -> {1}".format(node3,node4))
                print (few)
                print ("\n")

但是,对于具有大约 4K 节点和 20K 边的大图,我想找到每两个节点之间的所有路径。程序中断,不返回任何输出。

能否请您帮助我如何设置输入图以及如何设置输出以添加到单独的文件中?

【问题讨论】:

    标签: python graph shortest-path bigdata


    【解决方案1】:

    您的回答是可能无法完成除了您的图是特殊图的情况下,该图中两个节点之间的路径数可能是巨大的。考虑以下情况: 对于 200 个顶点和 20K 边的完整图,任意两个顶点之间有 198!/2 个不同的路径。 如果您的图表包含一个循环,则其中有 无限 路径。
    您的图可能在两个顶点之间有如此多的路径,即使是超级计算机也无法在十年内计算出这个数字。

    【讨论】:

    • 它只是社交网络的一部分,我认为它不是一个包含数百万个节点和边的巨大图。它有大约 20K 边。
    • 我认为我们可以避免循环作为修改后的 DFS。
    • 另外,我认为这是 BFS,BFS 避免了循环
    • @SiavashR 这个数字可能非常大。您可以测试它在数字超过固定值时退出,看看数字是否合适。
    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多