networkx.algorithms.minors.equivalence_classes¶
- equivalence_classes(iterable, relation)[source]¶
Returns equivalence classes of
relationwhen applied toiterable.The equivalence classes, or blocks, consist of objects from
iterablewhich are all equivalent. They are defined to be equivalent if therelationfunction returnsTruewhen passed any two objects from that class, andFalseotherwise. To define an equivalence relation the function must be reflexive, symmetric and transitive.- Parameters
- iterablelist, tuple, or set
An iterable of elements/nodes.
- relationfunction
A Boolean-valued function that implements an equivalence relation (reflexive, symmetric, transitive binary relation) on the elements of
iterable- it must take two elements and returnTrueif they are related, orFalseif not.
- Returns
- set of frozensets
A set of frozensets representing the partition induced by the equivalence relation function
relationon the elements ofiterable. Each member set in the return set represents an equivalence class, or block, of the partition.Duplicate elements will be ignored so it makes the most sense for
iterableto be aset.
Notes
This function does not check that
relationrepresents an equivalence relation. You can check that your equivalence classes provide a partition usingis_partition.Examples
Let
Xbe the set of integers from0to9, and consider an equivalence relationRonXof congruence modulo3: this means that two integersxandyinXare equivalent underRif they leave the same remainder when divided by3, i.e.(x - y) mod 3 = 0.The equivalence classes of this relation are
{0, 3, 6, 9},{1, 4, 7},{2, 5, 8}:0,3,6,9are all divisible by3and leave zero remainder;1,4,7leave remainder1; while2,5and8leave remainder2. We can see this by callingequivalence_classeswithXand a function implementation ofR.>>> X = set(range(10)) >>> def mod3(x, y): return (x - y) % 3 == 0 >>> equivalence_classes(X, mod3) {frozenset({1, 4, 7}), frozenset({8, 2, 5}), frozenset({0, 9, 3, 6})}