diff options
author | Dan Schult <dschult@colgate.edu> | 2021-06-28 09:54:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-28 09:54:03 -0400 |
commit | 3146c56f0fe942f71784d5ee03aa65aec845859e (patch) | |
tree | 72d3a030dc5006ca2056bc9525aa740ec77d189c | |
parent | df483f7749501fe5348dfe2945632ace1df98f6b (diff) | |
download | networkx-3146c56f0fe942f71784d5ee03aa65aec845859e.tar.gz |
move partition checking outside private _quotient_graph function (#4931)
* move partition checking outside private _quotient_graph function
* fix typo
-rw-r--r-- | networkx/algorithms/minors/contraction.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/networkx/algorithms/minors/contraction.py b/networkx/algorithms/minors/contraction.py index bca9008c..199c2272 100644 --- a/networkx/algorithms/minors/contraction.py +++ b/networkx/algorithms/minors/contraction.py @@ -51,6 +51,12 @@ def equivalence_classes(iterable, relation): Duplicate elements will be ignored so it makes the most sense for `iterable` to be a :class:`set`. + Notes + ----- + This function does not check that `relation` represents an equivalence + relation. You can check that your equivalence classes provide a partition + using `is_partition`. + Examples -------- Let `X` be the set of integers from `0` to `9`, and consider an equivalence @@ -295,6 +301,10 @@ def quotient_graph( if callable(partition): # equivalence_classes always return partition of whole G. partition = equivalence_classes(G, partition) + if not nx.community.is_partition(G, partition): + raise nx.NetworkXException( + "Input `partition` is not an equivalence relation for nodes of G" + ) return _quotient_graph( G, partition, edge_relation, node_data, edge_data, relabel, create_using ) @@ -310,6 +320,9 @@ def quotient_graph( partition_nodes = set().union(*partition) if len(partition_nodes) != len(G): G = G.subgraph(partition_nodes) + # Each node in the graph/subgraph must be in exactly one block. + if not nx.community.is_partition(G, partition): + raise NetworkXException("each node must be in exactly one part of `partition`") return _quotient_graph( G, partition, edge_relation, node_data, edge_data, relabel, create_using ) @@ -324,9 +337,7 @@ def _quotient_graph( relabel=False, create_using=None, ): - # Each node in the graph must be in exactly one block. - if any(sum(1 for b in partition if v in b) != 1 for v in G): - raise NetworkXException("each node must be in exactly one block") + """Construct the quotient graph assuming input has been checked""" if create_using is None: H = G.__class__() else: |