Saurabh Verma
Saurabh Verma

Reputation: 6728

Multiple Nodes graph to single node graph in python

I have a Directed Acyclic graph, something like this:

[a, b, c] --> [p, q] --> [p, a, c] --> [x, y, z]

Here, each of the alphabets is a node. From the above graph, I want the list of all the graphs having single nodes. eg One graph can be: a --> p --> a --> y Another can be: b --> p --> p --> z etc.

I can do a depth-first search (DFS) on the main graph to traverse it, but I'm not sure how to do multiple DFS to extract every graph having single character

Upvotes: 0

Views: 136

Answers (1)

Tom Ron
Tom Ron

Reputation: 6181

If you have a list of the node sets you can do something like this -

from itertools import product
s =[[1, 2], [3, 4, 5], [6, 7]]
list(product(*s))

output -

[(1, 3, 6), (1, 3, 7), (1, 4, 6), (1, 4, 7), (1, 5, 6), (1, 5, 7), (2, 3, 6), (2, 3, 7), (2, 4, 6), (2, 4, 7), (2, 5, 6), (2, 5, 7)]

Upvotes: 1

Related Questions