I've been struggling with finding a solution to what seems to be a relatively simple problem.
Given a graph g:
g = {'A': ['B', 'C'],
'B': ['A', 'C'],
'C': ['D'],
'D': [],
}
One can find all paths using this solution (which I found here):
def paths(graph, v):
path = [v] # path traversed so far
seen = {v} # set of vertices in path
def search():
dead_end = True
for neighbour in graph[path[-1]]:
if neighbour not in seen:
dead_end = False
seen.add(neighbour)
path.append(neighbour)
yield from search()
path.pop()
seen.remove(neighbour)
if dead_end:
yield list(path)
yield from search()
paths(g,'A')
>> [['A', 'B', 'C', 'D'], ['A', 'C', 'D']]
To make matters more complex, I would like to find all paths in g but when the lists become nested. For example, take g to be
g2 = {'A': [['B', 'C'],['D']],
'B': [['A'], ['C']],
'C': [['D']],
'D': [[]]}
The solution I am looking for would be
[ [['A', 'B', 'C', 'D'], ['A', 'C', 'D']], ['A', 'D'] ]
where the first two paths are grouped together (i. e., in total I want to get two paths). However the above function is not sufficient. I have tried adapting this code to my problem but I have not been successful.
The actual graphs I am working with are quite a bit larger and many of the elements in the dictionary are these nested lists, so the number of paths may grow rather large rather quickly.
I hope this example makes sense. Any help would be greatly appreciated.