I have a data structure that looks like a graph of rather isolated undirected "subgraphs" and would like to get those subgraphs that have less than N edges, in this case only those with one or two edges. I'm a bit lost here and am not sure how to go with it. Actualy, the way I stored the data might not even be the best approach for this problem.
As an example, for graph
below I would like to retrieve the nodes A-B
, H-J
and K-M
, but not C-G
because that has more than 2 edges.
graph = { # using sets
# single (A-B)
'A': {'B'},
'B': {'A'},
# complex (C-G)
'C': {'D'},
'D': {'C', 'E'},
'E': {'D', 'F'},
'F': {'G', 'E'},
'G': {'F'},
# digraph1 (H-J)
'H': {'J', 'I'},
'I': {'H'},
'J': {'H'},
# digraph2 (K-M)
'K': {'L'},
'L': {'K', 'M'},
'M': {'L'},
}
Any ideas or suggestions on how to tackle this problem in Python?