Skip to content

07 counts cuts - #106

Open
eriknw wants to merge 11 commits into
python-graphblas:06-clusterfrom
eriknw:07-counts-cuts
Open

eriknw wants to merge 11 commits into
python-graphblas:06-clusterfrom
eriknw:07-counts-cuts

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026

Copy link
Copy Markdown
Member

Count self-loops and undirected edges the way NetworkX does in the degree, regularity, reciprocity, cut and community-edge names, pass weight through the cut family, and treat the caller's sets as NetworkX does. The NetworkX test suite also becomes a required CI step.

  • is_regular raises NetworkXPointlessConcept for the null graph, and is_regular/is_k_regular treat a node with no edges as degree 0
  • is_regular, is_k_regular, degree_centrality, volume, s_metric and the cut measures count a self-loop at both ends in an undirected graph; directed s_metric returns a float
  • inter_community_edges and intra_community_edges were twice NetworkX's count on undirected graphs; inter_community_edges validates its partition
  • normalized_cut_size, conductance, edge_expansion and mixing_expansion honor weight; expansion measures divide by the caller's set sizes; volume counts a repeated member each time
  • reciprocity raises NetworkXNotImplemented for an undirected graph on a direct call, and reports None for an isolated node
  • node_expansion raises NetworkXError for an absent member
  • cut_size and volume raise OverflowError where weights could pass 2**63

Stacked on #105.

is_regular and is_k_regular read the degree vector, where a node with no
edges has no entry, and took a missing entry to mean "not regular". A
graph of one node, or of isolated nodes only, was not regular here and
is for NetworkX, and is_k_regular(G, 0) of an edgeless graph was False.
The null graph raised IndexError; NetworkX raises
NetworkXPointlessConcept, and so does this now.

Both also counted a self-loop once. NetworkX's degree counts it at both
ends of its node, and the matrix holds one diagonal entry for it, so a
triangle beside a node with a self-loop was not regular here, and a
graph of two self-loops was 1-regular instead of 2-regular. The
undirected degrees now add the diagonal back. A directed self-loop is
one in-edge and one out-edge, which the matrix already counts.
The matrix holds an undirected edge at both (u, v) and (v, u), and a
self-loop at one diagonal entry; these names counted as if only one of
the two held.

- degree_centrality counted a self-loop once; NetworkX's degree counts
  it at both ends of its node. On a path of three nodes with a self-loop
  at one end, that end scored 1.0 where NetworkX gives 1.5.
- inter_community_edges and intra_community_edges returned twice
  NetworkX's count on every undirected graph. Halving alone would halve
  the self-loops too, so intra counts them twice first.
- volume summed one end of a self-loop (2 where NetworkX gives 3), and
  normalized_cut_size and conductance inherit volume.
- mixing_expansion divided by the stored entries, not by twice the edge
  count, so it was twice NetworkX's answer on every directed graph and
  too large on an undirected graph with a self-loop.
not_implemented_for("undirected", "multigraph") refuses a graph that is
both, so a plain undirected graph passed the guard on reciprocity and
overall_reciprocity and died in to_directed_graph with an empty
TypeError. Two decorators refuse either, with NetworkXNotImplemented.
Dispatch already declines an undirected graph, so NetworkX still
answers those calls; this is for a direct call.

reciprocity over a list of nodes left out an isolated one, which
NetworkX reports with None; a numeric vector has no entry for it, so
the answer is a dict over the nodes asked for. The nodes are read with
nbunch_keys: an absent member, or a string (a sequence of characters),
raised KeyError, and a non-iterable absent node or an unhashable member
raised TypeError, where NetworkX skips absent members and raises
NetworkXError for the other two.
s_metric sums degree(u) * degree(v) over the edges. The kernel summed
over the stored matrix entries and halved, which undoes the double
count of an undirected edge; a directed edge is stored once, so every
directed answer was half of NetworkX's. The directed sum is no longer
halved, and is returned as a float, as NetworkX returns it.

An undirected self-loop was wrong twice: its degree counted it once
where NetworkX counts both ends, and its one diagonal entry was halved
with the rest. A lone self-loop gave 0.5 where NetworkX gives 4.0. The
degrees now count both ends, and each self-loop's term is added once
more before the halving.
normalized_cut_size, conductance, edge_expansion and mixing_expansion
converted the graph with the caller's weight and then called a kernel
that never received it, so a weighted call answered the unweighted
number. On a path weighted 2, 5, 1 with a self-loop of 3, with sets
{0, 1} and {2, 3}, conductance was 0.333 where NetworkX gives 0.714,
and edge_expansion 0.5 against 2.5.

Weighted volume summed the in-weights of the set (A @ S); NetworkX sums
out_degree on a directed graph. The two agree when A is symmetric, so
only a directed graph showed it: 6 for a set NetworkX gives 10.

mixing_expansion keeps an edge count in its denominator for a weighted
call, as NetworkX does.

The measures whose answers are floats add weights in floating point:
an integer cut or volume wraps past 2**63, as on a star of three
edges weighing 2**62. cut_size and volume, which answer integers,
still add in the matrix's own type.
NetworkX divides edge_expansion by min(len(S), len(T)), and
boundary_expansion and node_expansion by len(S), counting the sets as
they were passed: a member the graph does not have, or a node named
twice, is part of the count. These took the counts off the converted
vectors, which drop both, so the answer came back too large. On a
square with one chord and sets {0, 1, "absent"} and {2, 3, "gone"},
edge_expansion was 1.5 where NetworkX gives 1.0, and boundary_expansion
1.0 against 2/3; node_expansion of [0, 0, 1] was 2.0 against 4/3.

The kernels take the sizes as optional arguments, defaulting to the
vectors', and the wrappers pass the caller's. An omitted T counts the
nodes of the graph not in S, as NetworkX fills it.
volume sums a degree view over its nbunch, so NetworkX skips a member
the graph does not have and counts a member named twice each time. This
looked each member up and raised KeyError for an absent one (and for a
string, a sequence of characters), raised TypeError for a non-iterable
absent node where NetworkX raises NetworkXError, and counted a repeat
once: volume(G, [0, 0]) was the degree of node 0, not twice it.
normalized_cut_size and conductance take the volumes of S and T as
given, so they dropped repeats too: on a path of four nodes,
normalized_cut_size(G, [0, 0]) was 1.2 where NetworkX gives 0.7. The
wrappers now read each set with nbunch_keys into a vector that counts
its members, and the volume kernel weighs each degree by that count.

node_expansion reads each member's neighbors, which raises for an
absent member rather than skipping it. It raised KeyError; it now
raises NetworkXError, with NetworkX's message ("graph" or "digraph").
NetworkX fills an omitted T with the nodes not in S before it counts.
edge_expansion passed None to the kernel instead, and on a directed
graph the cut also counts the edges from T to S, which reads T as the
first set of edge_boundary; only a second set can be filled there, so
the call raised TypeError from the matrix layer where NetworkX answers
(1.0 for node 0 of a directed path of four nodes). The wrapper fills T,
as conductance and normalized_cut_size already do. Undirected graphs
were not affected.
NetworkX counts the edges of a quotient graph, which it builds on the
graph restricted to the union of the blocks, and then requires the
blocks to split that union exactly. So an edge to a node no block holds
is not counted, and two blocks sharing a node, a node twice in a block,
or a block naming a node the graph does not have raise
NetworkXException. This counted every edge leaving a block and dropped
absent nodes: on a triangle with the one block {0, 1}, it counted 1
where NetworkX counts 0, and it answered overlapping blocks.

The wrapper checks the partition, and the kernel counts an edge only
when its other end is in another block.
NetworkX's own suite, run through this backend, now ends in passes and
NetworkX's own expected failures on both NetworkX versions CI tests, so
its job stops being advisory: the suite step loses continue-on-error,
and the summary step reports a failed suite as an error, not a warning.

A failure has to be reproducible from its log, so the suite runs in
NetworkX's order (-p no:randomly): with -q, a shuffled run never prints
its seed. The summary step names the installed NetworkX release rather
than the matrix label "latest", and runs on any outcome but a cancel,
since the counts matter most on a red run; the coverage upload is
skipped when the suite fails.

The xfail table in on_start_tests is empty, and its docstring now says
so and why: a call this backend would answer wrongly is declined in
can_run, so NetworkX answers it, rather than marked as an expected
failure.
NetworkX adds integer weights as Python integers, which never overflow.
cut_size and volume added them in the matrix's own type, so they
wrapped silently: on a star of three edges weighing 2**62 both answered
about -4.6e18 where NetworkX gives 1.38e19, and a graph built from an
INT8 Matrix wrapped at 127.

They now add integer weights in 64 bits and raise OverflowError where
the absolute values of the weights sum to 2**63 or more, which bounds
every partial sum. For volume, whose set may name a node more than once
as NetworkX counts it, that sum is multiplied by the most times it
does. The graph's least and greatest values, which it caches, usually
settle it; the absolute sum is computed, and cached, only when they do
not. The backend info says when each raises.

The cut measures whose answers are floats add in floating point
already, and never raise.
@eriknw eriknw mentioned this pull request Dec 2, 2026
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.39142% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.87%. Comparing base (8cabadb) to head (54ac506).

Additional details and impacted files
@@              Coverage Diff               @@
##           06-cluster     #106      +/-   ##
==============================================
+ Coverage       84.73%   85.87%   +1.13%     
==============================================
  Files             133      138       +5     
  Lines            6094     6405     +311     
  Branches          881      898      +17     
==============================================
+ Hits             5164     5500     +336     
+ Misses            675      654      -21     
+ Partials          255      251       -4     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant