Skip to content

Update approx current_flow betweenness to use k directly - #8007

Merged
dschult merged 16 commits into
networkx:mainfrom
mrecachinas:replace-epsilon-in-current-flow
Jun 26, 2025
Merged

dschult merged 16 commits into
networkx:mainfrom
mrecachinas:replace-epsilon-in-current-flow

Conversation

@mrecachinas

Copy link
Copy Markdown
Contributor

Rough pass at a fix for #6188.

@@ -119,10 +124,13 @@ def approximate_current_flow_betweenness_centrality(
nb = (n - 1.0) * (n - 2.0) # normalization factor
cstar = n * (n - 1) / nb
l = 1 # parameter in approximation, adjustable

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.

This l is also odd. Should we make it a parameter?

@dschult dschult May 1, 2025 •

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.

If we do make it a parameter (and even if we don't) can we change this variable name? Even spelling el is easier to read than l which can look like a capital i or a 1 depending on font.

Would also be fine with me to remove that parameter. It currently has no effect and it is not clear what it is doing or how that affects things.

@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.

Thanks for putting this together @mrecachinas - seeing the changes to the test suite make me less enthused about the "get rid of epsilon" approach. Given this, I'm leaning more towards docstring updates as the best way to resolve #6188: essentially improving the description for both epsilon and kmax and mentioning the relationship (as you had originally proposed @mrecachinas !). WDYT?

Also on an unrelated note - I see the old normalization factor (see L119) - pinging @eriknw for an extra set of eyes given the work to fix bc in #7908 . There's no endpoints in this case, but there is k!

@eriknw

eriknw commented May 2, 2025

Copy link
Copy Markdown
Contributor

Also on an unrelated note - I see the old normalization factor (see L119) - pinging @eriknw for an extra set of eyes given the work to fix bc in #7908 . There's no endpoints in this case, but there is k!

Oh boy! I'll need to read up on this algorithm to have any degree of confidence. But, if this is similar to regular BC, the normalization might be something like (k - 1) * (n - 2) instead of (n - 1) * (n - 2).

Also, there may be nuance when normalized=False. What is expected to be calculated: an estimate of the "full" nonnormalized centrality, or the partial "k" centrality? For the former, the results may need to be scaled by e.g. N / (k - 1) (depending on the definition of the centrality and what exactly is being estimated).

Comment on lines 32 to 33
solver="full",
epsilon=0.5,
kmax=10000,
k=None,
seed=None,

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.

Let's make the new keyword arg keyword only. And we should have seed keyword only also -- because it changes position in the function signature when we replace epsilon and kmax with k. So, anyone using position args will need to change their code -- and they should make the call use keywords.

So, I think something like this should work:

*, 
k=None,
seed=None,

Also -- changing the argument structure will require that the random_state decorator get updated too.

@py_random_state("seed")

@dschult

dschult commented May 3, 2025

Copy link
Copy Markdown
Member

I think this is different than the other BC function. First and most importantly, this doesn't have the endpoints kwarg. Next, it was already set up to handle the number of sample pairs k. And finally, there wasn't any dependence on kmax or epsilon before. So there shouldn't be any normalization dependence on k now.

@mrecachinas

mrecachinas commented May 3, 2025 •

Copy link
Copy Markdown
Contributor Author

Happy to remove my most recent changes (saw them after @dschult's review). I reverted the change to k, updated epsilon's description, and pulled l out to sample_weight, which seems appropriate since it's scaling the sampling process that yields k (i.e., sample node pairs). 🤔

@mrecachinas
mrecachinas marked this pull request as ready for review May 3, 2025 17:20

@amcandio amcandio 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.

Looks good to me. Many of the inputs such as n, sample size of epsilon could cause zero division errors. We might want to add some validations on the input so the error is clear for the user

@mrecachinas

Copy link
Copy Markdown
Contributor Author

Not sure why the label check is failing. Should it have been auto-added?

@dschult

dschult commented May 9, 2025

Copy link
Copy Markdown
Member

Tha label has to be added manually by a core dev. (the name says "Labels / reviewer label?" which is not clear partly cuz it has a length restriction. But I think/hope that clicking on "view details" give a better message. :) Anyway, this just means one of us should look at your PR. :)

@mrecachinas

Copy link
Copy Markdown
Contributor Author

@dschult How is this one looking?

@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.

This looks like the right approach to me. One comment about the kwarg-only specification but otherwise looks good!

@Schefflera-Arboricola Schefflera-Arboricola 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 @mrecachinas ! Consider adding tests for the new sample_weigh parameter and the error messages.

@mrecachinas
mrecachinas requested review from dschult and rossbar June 22, 2025 12:32
@mrecachinas

Copy link
Copy Markdown
Contributor Author

@dschult anything else remaining here?

@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.

LGTM, thanks @mrecachinas

I left a couple final docstring formatting nits. Normally I'd just push them up myself but it looks like you have the "maintainers may edit this branch" button disabled. Sorry for the nits!

@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.

This looks good. I've suggested a linting fix below. And I approve this PR once we get a green check. :)

@dschult
dschult merged commit c24a661 into networkx:main Jun 26, 2025
@mrecachinas
mrecachinas deleted the replace-epsilon-in-current-flow branch June 26, 2025 19:06
amcandio pushed a commit to amcandio/networkx that referenced this pull request Jul 20, 2025
* Update approx current_flow betweenness to use k directly

Fix latex

* Revert "Update approx current_flow betweenness to use k directly"

This reverts commit 88acca7.

* Update docstring for epsilon

* Extract `l` to a param and rename to `sample_weight`

* Address review feedback on param arrangement

* Add some param validations

* Move the kmax check below other checks

* Reorder the kwarg-only delimiter

* Add tests for sample_weight and errors

* Make approximate_current_flow_betweenness_centrality match other centrality algorithms

* Add tests for centrality edge cases

* Apply suggestions from code review

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

* Update networkx/algorithms/centrality/current_flow_betweenness.py

---------

Co-authored-by: Mridul Seth <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
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.

7 participants