I’ve a directed cyclic graph, want to find all possible paths starting from given (or by default root) node without repeating the same path again.
Here, node “B”, “D”, and “C” is cyclic.
My code :
path = path + [start]
paths = [path]
if len(graph[start]) == 0:
return [path]
for node in graph[start]:
if node not in path:
newpaths = find_all_possible_paths(graph, node, path)
for newpath in newpaths:
paths.append(newpath)
else:
return paths
return paths```
print(find_all_paths_wo_end_node(direct_cyclic_graph , 'A'))
**Output**
[
['A'],
['A', 'B'],
['A', 'B', 'D'],
['A', 'B', 'D', 'C'],
['A', 'B', 'D', 'F'],
['A', 'C'],
['A', 'C', 'B'],
['A', 'C', 'B', 'D'],
['A', 'E']
]
**Expected **
I'm just missing one path i.e. ['A', 'C', 'B', 'D', 'F']
Not sure what am I missing in my code. Pls help.