Repository navigation
Collect label propagation communities in one go - #4573
Conversation
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.
|
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): |
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
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 |
|
@stefanv If you need any other help to get this one merged, just ask. |
|
@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. |
|
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 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 |
|
@rossbar Dan's first paragraph is useful to know; maybe an item for our forthcoming core contributor guide? |
|
@dschult Since Maybe we can merge this and open an issue to consider the suggested API change? |
|
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. |
|
I guess that's the question. Is changing an iterator to an iterable a small enough change to declare it acceptable. |
|
I added a release note announcing the api change without deprecation. |
|
@dschult The documentation currently states |
This changes the doc_string to show an iterable instead of a generator and it addes a test to verify this behavior.
|
I see |
|
|
rossbar
left a comment
There was a problem hiding this comment.
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!
|
Hey all, is there anything else I can do to help getting this accepted/merged? |
|
Thanks @mariokostelac! |
|
Thank you for spending time to maintain this library :). |
* 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]>
* 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]>
This change makes sure that we collect communities in one go, instead of performing
len(communities)iterations overlabelinglist. It reduces the complexity fromO(nodes^2)toO(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.