I have a directed graph and would like to get all subgraphs with conditions as follow:
For each subgraph:
- all nodes have exactly 1 edge in
- all nodes have exactly 1 edge out
- sum of 'durations' (a given attribute) per subgraph is under (<=) a given value K
Here is a small example:
import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([
(1, {"name": "node 1", "duration": 30}),
(2, {"name": "node 2", "duration": 40}),
(3, {"name": "node 3", "duration": 20}),
(4, {"name": "node 4", "duration": 10}),
(5, {"name": "node 5", "duration": 30}),
(6, {"name": "node 6", "duration": 20}),
(7, {"name": "node 7", "duration": 50}),
(8, {"name": "node 8", "duration": 40}),
])
links = ( (1,2), (2,3), (3,4), (4,5), (1,8), (8,7), (7,6), (6,5))
G.add_edges_from(links)
The expected solution is (for example, as there are multiple options):
( (2,3,4), (6,7), (8) )