Skip to content

Collect label propagation communities in one go - #4573

Merged
stefanv merged 5 commits into
networkx:masterfrom
mariokostelac:mario/improve_label_prop_collection
Feb 12, 2021
Merged

stefanv merged 5 commits into
networkx:masterfrom
mariokostelac:mario/improve_label_prop_collection

Conversation

@mariokostelac

@mariokostelac mariokostelac commented Jan 27, 2021 •

Copy link
Copy Markdown
Contributor

This change makes sure that we collect communities in one go, instead of performing len(communities) iterations over labeling list. It reduces the complexity from O(nodes^2) to O(nodes) at the expense of memory, since we us a dict to collect results.

I've discovered this by running label propagation on a big graph (1M nodes), which took 8h. Applying this change reduced the runtime to 6 minutes.

If this is not an acceptable tradeoff, please tell me, I have other ideas how to make it faster.

This change makes sure that we collect communities in one go, instead of performing len(communities) iterations over labeling list.
It reduces the complexity from O(nodes^2) to O(nodes) at the expense of memory, since we us a dict to collect results.

I've discovered this by running label propagation on a big graph (1M nodes), which took 8h. Applying this change reduced the runtime to 6 mintues.
@stefanv

stefanv commented Jan 27, 2021

Copy link
Copy Markdown
Contributor

Hi @mariokostelac; thanks for this optimization.

I am curious what you mean by "trade-off"—this simply looks like the better way of calculating the same thing.

Here's another way of writing the inverse mapping (not sure if it has any impact on time):

clusters = {} 
for k, v in labeling.items(): 
    clusters.setdefault(v, set()).add(k) 

@mariokostelac

Copy link
Copy Markdown
Contributor Author

I am curious what you mean by "trade-off"—this simply looks like the better way of calculating the same thing.

The old implementation creates a generator, and the producer (our function) does not hold more than one community in memory.

Let's imagine a consumer like

for community in nx.label_propagation_communities(G):
    print(len(community))

The old implementation would not hold in memory more than the current community because it's lazily generated in the loop.

My implementation creates additional dict of sets (kept in clusters var). If we had 1M communities, it'd create 1M additional sets in memory, regardless of what the consumer does. I assume it does not matter, but just wanted to make sure I'm not breaking some implicit agreement that it does not allocate extra memory for results collection.

Here's another way of writing the inverse mapping (not sure if it has any impact on time):

Without measuring, I'd say that the difference is negligible. defaultdict should be doing exactly that - on first value retrieval, if the key does not exist, create a default value with given function (in our case set function that creates an empty set).

@mariokostelac

Copy link
Copy Markdown
Contributor Author

@stefanv If you need any other help to get this one merged, just ask.

@stefanv

stefanv commented Jan 28, 2021

Copy link
Copy Markdown
Contributor

@dschult @jarrodmillman Do we have any guidelines on whether queries like the above should preferentially return generators? As Mario explains above, there is a big speed/memory trade-off here.

@dschult

dschult commented Jan 29, 2021 •

Copy link
Copy Markdown
Member

This memory cost is minimal. The usual guidelines are that additional data structures on the order of the number of nodes are fine. But data structures on the order of the number of edges can be costly and should be avoided if not needed. However, in either case, if the returned structure can be generated per node, or per edge, that is preferred to returning a collection of all the results. If it saves time to create the collection, then the collection should be returned.

In this case, however, the number of sets is smaller than the number of nodes. So it is quite unlikely to cause a memory crunch. And the speed-up is reportedly quite dramatic.

It looks like the speed trouble is that we used to iterate over all nodes for each label. Now we iterate over all nodes once. Notice also that we do construct all sets simultaneously, so the number of sets held in memory is the number of labels instead of 1. The size of those sets is constrained so that the total number of elements across all sets is the number of nodes.

I definitely approve this nice speedup. Thank you!

Next question --> this changes the output from a generator to a dict_values object. Should the output be clusters? the values object clusters.values()? or an iterator over the sets like iter(clusters.values())?

I can see reasons for changing it to be the whole dict. I can see reasons (backward compatibility) for returning an iterator . We could also deprecate the current behavior by returning iter(clusters.values()) and say that the change will occur in v3.0. What is the right choice here?

@stefanv

stefanv commented Jan 29, 2021

Copy link
Copy Markdown
Contributor

@rossbar Dan's first paragraph is useful to know; maybe an item for our forthcoming core contributor guide?

@stefanv

stefanv commented Jan 29, 2021

Copy link
Copy Markdown
Contributor

@dschult Since dict_values are iterable, I think this PR preserves API well enough.

Maybe we can merge this and open an issue to consider the suggested API change?

@mariokostelac

Copy link
Copy Markdown
Contributor Author

I've tried to preserve the API as much as possible so I've returned something iterable. Ofc, since it's not explicitly defined with types or tests, some consumers might rely on result being a generator. If you think that's the case, we can make sure we still return a generator.

@dschult

dschult commented Jan 29, 2021

Copy link
Copy Markdown
Member

I guess that's the question. Is changing an iterator to an iterable a small enough change to declare it acceptable.
Historically we have made changes like that fairly often -- but we're trying to deprecate these days and I don't know where the boundary is for what should be deprecated. I'm fine with accepting as is, especially if we open an issue to consider whether iterator -> iterable is an OK API change to avoid deprecation.

@dschult

dschult commented Jan 29, 2021

Copy link
Copy Markdown
Member

I added a release note announcing the api change without deprecation.
This should be ready to merge.

@stefanv

stefanv commented Jan 29, 2021

Copy link
Copy Markdown
Contributor

@dschult The documentation currently states generator to describe the return parameter; if we change that to iterable we should be good to go. I wonder if this pattern is more widespread. I don't think we should ever guarantee that we'll return a generator: iterator gives us more freedom?

This changes the doc_string to show an iterable instead of a generator
and it addes a test to verify this behavior.
@dschult

dschult commented Jan 29, 2021

Copy link
Copy Markdown
Member

I see iterator and generator as very similar. But iterator and iterable are different in that one is exhaustible and the other can be reused. And Yes, we need to change the docs as well.

@stefanv

stefanv commented Jan 29, 2021

Copy link
Copy Markdown
Contributor

iterable is exactly right—sorry!

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

I agree with everyone re: both the performance v. memory tradeoff and that a full deprecation is not necessary in this case. Just one final nit in the docstring, other than that this looks ready to go!

@mariokostelac

Copy link
Copy Markdown
Contributor Author

Hey all, is there anything else I can do to help getting this accepted/merged?

@dschult dschult added this to the networkx-2.6 milestone Feb 12, 2021
@stefanv
stefanv merged commit 744cbe6 into networkx:master Feb 12, 2021
@stefanv

stefanv commented Feb 12, 2021

Copy link
Copy Markdown
Contributor

Thanks @mariokostelac!

@mariokostelac

Copy link
Copy Markdown
Contributor Author

Thank you for spending time to maintain this library :).

@mariokostelac
mariokostelac deleted the mario/improve_label_prop_collection branch February 13, 2021 10:29
robertclancy added a commit to intercom-archive/networkx that referenced this pull request Mar 8, 2021
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* Collect label propagation communities in one go

This change makes sure that we collect communities in one go, instead of performing len(communities) iterations over labeling list.
It reduces the complexity from O(nodes^2) to O(nodes) at the expense of memory, since we us a dict to collect results.

I've discovered this by running label propagation on a big graph (1M nodes), which took 8h. Applying this change reduced the runtime to 6 mintues.

* Add api change to release notes

* Changed doc_string and added test

This changes the doc_string to show an iterable instead of a generator
and it addes a test to verify this behavior.

* Tweak docs as per suggestion in PR

Co-authored-by: Dan Schult <[email protected]>
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* Collect label propagation communities in one go

This change makes sure that we collect communities in one go, instead of performing len(communities) iterations over labeling list.
It reduces the complexity from O(nodes^2) to O(nodes) at the expense of memory, since we us a dict to collect results.

I've discovered this by running label propagation on a big graph (1M nodes), which took 8h. Applying this change reduced the runtime to 6 mintues.

* Add api change to release notes

* Changed doc_string and added test

This changes the doc_string to show an iterable instead of a generator
and it addes a test to verify this behavior.

* Tweak docs as per suggestion in PR

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.

4 participants