简体   繁体   中英

Large graph as input to find all paths

I'm using the following python code in order to find all paths between each two nodes. It's not any problem for small graphs.

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")

But, I want to find all paths between each two nodes, for a large graph with about 4K nodes and 20K edges. The program intrupts and doesn't return any output.

May you please help me how to set the input graph and also, how to set the output to add in a separated file?

Your answer is it may couldn't be done , except the case that your graph is a special graph, number of paths between two nodes in such graph may be enormous.consider following case: For a complete graph of 200 vertices and 20K edges there is 198!/2 different paths between any two vertices. If your graph contains a cycle then there is infinite paths in it.
Your graph could be have such number of paths between two vertices that even a super computer couldn't compute the number in a decade.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM