简体   繁体   中英

find a path using list of tuple of tuples in python

I have a list of tuple of tuples like that. I want to get all paths from source to destination. for example my source is 1 and destination is 5. Then ((1, 3), [5, 45, 86, 3]), ((3, 7), [16, 187, 48, 4]), ((7, 9), [31, 111, 63, 59]), ((9, 5), [78, 14, 53, 6]) is one path.

[((1, 3), [5, 45, 86, 3]), ((7, 2), [62, 122, 23, 9]), ((5, 8), [98, 137, 52, 31]), ((8, 7), [82, 194, 66, 23]), ((4, 0), [93, 161, 49, 29]), ((6, 9), [12, 40, 51, 23]), ((6, 5), [87, 22, 89, 10]), ((0, 9), [24, 190, 57, 28]), ((1, 0), [32, 45, 100, 29]), ((3, 7), [16, 187, 48, 4]), ((10, 4), [30, 157, 85, 49]), ((9, 10), [46, 58, 100, 48]), ((9, 7), [22, 66, 61, 24]), ((6, 4), [81, 16, 78, 3]), ((2, 0), [73, 124, 35, 6]), ((6, 10), [71, 2, 61, 24]), ((9, 1), [88, 177, 30, 19]), ((9, 7), [20, 2, 44, 27]), ((10, 7), [54, 177, 92, 30]), ((4, 7), [57, 143, 83, 68]), ((1, 8), [90, 190, 96, 8]), ((3, 8), [77, 56, 84, 62]), ((3, 6), [55, 135, 75, 64]), ((4, 3), [76, 4, 69, 64]), ((0, 6), [16, 39, 82, 27]), ((8, 9), [38, 1, 17, 4]), ((2, 0), [97, 108, 84, 2]), ((8, 3), [37, 200, 31, 13]), ((1, 9), [42, 5, 65, 35]), ((5, 0), [52, 65, 60, 31]), ((1, 0), [40, 18, 38, 2]), ((3, 9), [70, 8, 99, 52]), ((0, 8), [79, 184, 98, 68]), ((7, 9), [31, 111, 63, 59]), ((9, 2), [99, 187, 50, 17]), ((4, 9), [4, 25, 67, 52]), ((9, 5), [78, 14, 53, 6]), ((8, 1), [13, 66, 60, 35]), ((3, 10), [78, 200, 38, 9]), ((6, 9), [46, 7, 95, 54]), ((1, 8), [86, 84, 42, 2]), ((10, 5), [76, 88, 27, 22]), ((2, 3), [3, 143, 90, 70]), ((10, 0), [39, 160, 48, 45]), ((10, 1), [99, 8, 73, 14])]

I have tried recursion for this problem. I get first element of edge and check if it is source. If it is then I check it second element is destination. If it is I return path. If it is not I recursivly check all options.

def findpath(s,d):
    path = []
    for n in graph:
        e = n[0]
        w_list = n[1]
        if e[0] == s:
            if e[1] == d:
                path.append(e)
                #print(e)
            else:
                pah = findpath2(e[1],d)
                path.append(pah)
    return path
def findpath2(f,d):
    path2 = []
    for n in graph:
        e = n[0]
        w_list = n[1]
        if e[0] == f:
            if e[1] == d:
                path2.append(e)
                break
                #print(e)
            else:
               findpath2(e[1],d)

s= 1
d = 5
path = findpath(s,d)
print(path)

[(1, 3),(3, 7),(7, 9),(9, 5)] something like that. But I am getting this error. maximum recursion depth exceeded in comparison

If you don't need weight lists, just use networkx :

import networkx as nx

# You didn't write your graph object, so it is the dummy random graph
graph = [
    [(0,1),[1,2,3,4,5]],
    [(2,3),[1,2,3,4,5]],
    [(1,3),[1,2,3,4,5]],
    [(1,2),[1,2,3,4,5]],
    [(3,4),[1,2,3,4,5]],
    [(0,5),[1,2,3,4,5]],
    [(5,6),[1,2,3,4,5]],
    [(4,6),[1,2,3,4,5]],
    [(2,6),[1,2,3,4,5]],
]

G = nx.DiGraph()
G.add_edges_from(e[0] for e in graph)
list(nx.all_simple_paths(G, 1, 6))

returns all simple paths:

[[1, 2, 3, 4, 6], [1, 2, 6], [1, 3, 4, 6]]

Recursion is VERY bad for problems like this because you will stuck in the maximum call stack value (~50-100). Every non-small graph will force your script to crash.

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