Skip to content

DOC: rework betweenness centrality docstrings - #8264

Merged
dschult merged 14 commits into
networkx:mainfrom
Peiffap:doc/bc
Oct 10, 2025
Merged

dschult merged 14 commits into
networkx:mainfrom
Peiffap:doc/bc

Conversation

@Peiffap

@Peiffap Peiffap commented Sep 13, 2025

Copy link
Copy Markdown
Member

Fix #8263.

This PR addresses some of the points in the linked issue. Currently, I've only included an example for betweenness_centrality. If that one seems reasonable enough, I'll adapt it for the other 3 functions.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good... some suggestions below...

And a nit: looking at the docs, and at the doc_strings (which I like to keep readable too), I'd be in favor of removing the \text{} around the subscript B. I think it makes the html docs very slightly worse (the B touches the c it subscripts). But it makes the raw latex code in the doc_string much less readable.
Similarly, I'd prefer to use | instead of \mid. The whitespace in the rendered docs is slightly annoying, but more importantly the TeX command \mid really has no obvious connection with the vertical bar symbol. IMO | looks better in both the rendered html and in the ascii doc_strings. But these are very minor. Do what you prefer.

@Peiffap

Peiffap commented Sep 13, 2025

Copy link
Copy Markdown
Member Author

@dschult I've updated the docstring for betweenness_centrality to align it with your comments. If this looks good to you, I'll add examples for the other functions too. In particular, I'm curious to hear what you think about the section in the Notes about normalization, and about the worked example.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is nice. I'm still finding it fills my brain to think about endpoints and normalization together and sometimes I get confused whether the discussion is about the example or is a general result. So I have some suggestions that try to separate those. See what you think... I'm not saying this language is needed... just some way to separate the (many) ideas a little. :)

@Peiffap

Peiffap commented Sep 13, 2025

Copy link
Copy Markdown
Member Author

Thanks for the discussion, Dan! I really like your suggestion to separate discussing endpoints and normalization. I've updated the text a bit to hopefully make that distinction more clear. I believe I've also addressed your other comments. I'll push this up one more time for a final check before making changes to other functions.

@Peiffap

Peiffap commented Sep 13, 2025

Copy link
Copy Markdown
Member Author

Actually, we should include a discussion of setting an explicit k in the example too...

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a couple of minor rewording suggestions. Nothing to stop continuation to other doc_strings.
And I see your point that we should probably include an example where k is prescribed. Perhaps after the current example, we could use the same graph, but choose k=2 and suppose that the two nodes chosen are {0, 2}. (or make then {0, 1} if that makes things clearer.

@Peiffap

Peiffap commented Sep 15, 2025

Copy link
Copy Markdown
Member Author

Thanks for the suggestions Dan! They improve the flow of the docstring.
I believe 10a20a7 should address your comments; a653e84 has the additional example for k=2.

I'm purposely leaving out the case when setting k with endpoints=False as it's really quite involved: source nodes and nonsource nodes are treated differently, and then you add normalization on top of that... At that point, I think it's unfortunately best for users to go and just figure it out themselves from reading the source code. Thankfully, Erik has gone through quite a bit of effort to make sure it's nicely documented :)

I'll work on the remaining docstrings tomorrow when I have some time.

@Peiffap
Peiffap marked this pull request as ready for review September 15, 2025 16:39
@Peiffap

Peiffap commented Sep 15, 2025 •

Copy link
Copy Markdown
Member Author

I'm going to mark this as ready for review since I want to propose some changes to the subset centralities that would require changing the docs anyways:

  • Adding an endpoints parameter for node subset BC.
  • Adapting the normalization to agree with the main betweenness centralities (right now we always use $n(n-1)$ so subset BC and full BC agree when all nodes are given as sources and targets, but I believe it would make more sense to scale these with the sizes of sources and targets, while still maintaining that property).

The motivation for those changes is being able to make full BC a wrapper around subset BC.

Another thing I noticed that I'm mentioning in passing: the default value of normalized differs between the full BCs and subset BCs. Perhaps it would make sense to deprecate one of the two (set default to None, and if None is passed, warn that the default will change in a future version). That's completely separate from everything else though!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, thanks @Peiffap ! The examples are quite detailed, but then again there is a lot to consider with BC, especially with the combinations of parameters (k, endpoints) and graph properties (directedness) that all dictate semantics. I think the proposed example does a good job of capturing the important bits! I went through and compared the examples with the implementation, and AFAICT the explanations are all aligned.

I left a few minor suggestions re: wording and/or example ordering but none of these are blockers. Note also that I only left the explicit comments in the first functionn, so if you decide to go with them, it'd be good to apply the changes to the other modified docstrings too.

Co-authored-by: Ross Barnowski <[email protected]>
@Peiffap

Peiffap commented Sep 15, 2025 •

Copy link
Copy Markdown
Member Author

I believe 8c05d37 addresses your comments; I had to rewrite things slightly to take the final comment into account, but I believe the average distance from explanation to example is now shorter :)

@Peiffap Peiffap mentioned this pull request Sep 29, 2025

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much @Peiffap !
I have a bunch of minor wording tweaks. Push back if they don't make sense or seem wrong.

Otherwise holds the name of the edge attribute used as weight.
Weights are used to calculate weighted shortest paths, so they are
interpreted as distances.
If `None`, all edge weights are considered equal.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If `None`, all edge weights are considered equal.
If `None`, all edge weights are considered equal and treated as 1.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter what the weights are as long as they're all the same, right? I'd slightly prefer to keep it generic if so; personally ("describe the behavior, not the implementation"). Let me know if you have a strong preference!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... Why not describe both the behavior and the implementation if its easy to do so?

We use this idiom of weight is None to mean all edge weights are 1 in many places, and we describe the edge weight as defaulting to 1 in many places (probably all the same places -- but maybe not?? -- do we ever have weight is None mean all edge weights are 1 while also allowing the user to pick a default edge weight?) The answer is not relevant really. The point is that we describe this situation in many places and this change is quite different from the other descriptions. That's why I suggested this change.

Also, is the fact that they are treated as equal the main idea of unweighted treatments? The equality is a side-effect -- not the main effect IMO. I mean, unweighted shortest paths count the number of "hops" across edges in the network. Counting is the same as summing 1s. I've never seen this described as treating all edges as equal. The text just talks about counting instead of summing -- or it talks about edge weights being 1. Indeed, if we set the weights to be all equal to 10, we would not find the length to be the number of hops across edges.

(we're in the weeds here, and that's fine -- I like weeds as well as nits) I guess I'm pushing pack on the notion that equal edge weights is the behavior. In fact the behavior is counting edges along the path (or hops across edges) and that is equivalent to considering edge weights as 1.
My preferred language would be: ... all edge weights are 1. But my suggestion includes both perspectives.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the additional context. My issue is with having both; to me that obfuscates things. I've amended the PR to have your "preferred language" instead; I'm happy with this too. :) (Additionally, "considered equal" doesn't work for some pathological weights anyways so this is actually a meaningful nit.)

in each direction, as the shortest paths can differ by direction.
However, in undirected graphs, each pair of nodes is considered only once,
as the shortest paths are symmetric.
This means the normalization factor to divide by is $N(N-1)$ for directed graphs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normalization factor appears in the formula as sigma right? And the formula is the normalized BC value. Maybe we should add that somewhere near the formula. Something like:

The denominator $\sigma(s, t)$ is a normalization factor that can be turned off to get the 
raw path counts.

Should we also use \sigma in these expressions? Not sure...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also my interpretation of the formula, and it would track with normalized defaulting to True. I've added the suggested blurb where appropriate.

Note that for the subset betweenness centralities, the parameter defaults to False... But it still has the normalization as part of the formula. Nothing major, but that's an annoying inconsistency that we might want to change at some point...

I think we shouldn't use \sigma outside of the formula.

@Peiffap

Peiffap commented Oct 8, 2025

Copy link
Copy Markdown
Member Author

Thanks for the suggestions, @dschult! I think they help clarify some of the stuff re: normalization for approximated centrality, which is probably the hardest part to grok of it all.
I've applied most of them verbatim, some with minor rewording but trying to keep the spirit of what you suggested, and left comments for those I want to push back on. Let me know if you have other remarks!

@dschult
dschult merged commit 1cb76db into networkx:main Oct 10, 2025
@dschult dschult added this to the 3.6 milestone Oct 10, 2025
@Peiffap
Peiffap deleted the doc/bc branch October 10, 2025 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

DOC: update docs for betweenness centrality functions

3 participants