I am trying to find all paths in DAG from selected node.
So I am randomly generating list of tuples that looks like:
glavnaLista = [(6, 7), (6, 15), (15, 16), (16, 21), (15, 9), (9, 13), (13, 4), (4, 1), (1, 5)]
From this list we can see that node "6" is starting point of graph
Now I am creating graph:
G = nx.DiGraph()
G.add_edges_from(glavnaLista)
Now I am trying to find all paths (completed) from starting node with code:
def find_all_paths(graph, start, path=[]):
path = path + [start]
if start not in graph:
return [path]
paths = [path]
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, path)
for newpath in newpaths:
print (newpath)
paths.append(newpath)
return paths
Result is list of all paths:
[[6], [6, 7], [6, 15], [6, 15, 16], [6, 15, 16, 21], [6, 15, 9], [6, 15, 9, 13], [6, 15, 9, 13, 4], [6, 15, 9, 13, 4, 1], [6, 15, 9, 13, 4, 1, 5]]
But my problem is that I don't need paths that are not full (not going to the ending node), my list should have only full paths:
[6, 7]
[6, 15, 9, 13, 4, 1, 5]
[6, 15, 16, 21]
My idea is to check if the node has both neighbours, and if it doesn't than add path to list but I am not sure how to implement this as I am fairly new to python.