Repository navigation
Densest Subgraph Problem: Greedy Peeling and Greedy++ Implementations - #7731
Conversation
…to NetworkX. Adds it to the eexisting module approximation and adds some super light weight tests for the two algorithms implemented.
…tests from the literature to improve test coverage.
…d changed reference/background strings to r-strings
rossbar
left a comment
There was a problem hiding this comment.
I appreciate the meta-hacking with __doc__ in an attempt to reuse documentation for multiple functions, but before this will be considered it should be structured in the standard way.
The simplest thing to do is to get rid of the greedy_peeled alias - no need to have two names for the same thing. Let's start with one name with an embedded docstring so that any code review can focus on the algorithm itself!
|
Thanks @rossbar ! I've implemented your recommended changes. |
rossbar
left a comment
There was a problem hiding this comment.
Thanks @FaroukY , this looks great! I'm not familiar with the densest subgraph problem, but a quick scan of the docstring+references provided a lot of nice context. This is very clearly written and the test cases are great, so I think this is a good fit!
I haven't had a chance to go through it in detail yet but will try to do so soon!
rossbar
left a comment
There was a problem hiding this comment.
Thanks very much for this @FaroukY , it's quite interesting!
I took the liberty of pushing up a few minor changes (docstring formatting, changing "vertex" -> "node" [the preferred nomenclature in networkx], etc.) so please be sure to pull!
I've also left a few minor suggestions but they're all just nits so if you don't have the interest/bandwidth just ignore them!
The one thing that does warrant discussion at this stage is what the module/function naming scheme should be. Though greedy_plus_plus is plenty descriptive in the context of the subgraph density problem, users generally won't have that context when exploring the API/calling the function (e.g. when accessing the function from the approximation package directly, as in: nx.approximation.greedy_plus_plus).
I'd propose the following: rename the module densest_subgraph -> density (just to avoid any duplicate naming with the underlying function) then name greedy_plus_plus -> densest_subgraph. WDYT?
|
Thanks for the comments @rossbar. I'm planning to have a look at them very soon and do the changes, thanks! |
|
Thank you for the insightful feedback, @rossbar. I’ve addressed all your comments. Module Renaming: I’m happy to rename the module from densest_subgraph to density as you suggested. Function Naming and Future Algorithms: Regarding the naming of the underlying functions, I plan to implement two additional promising algorithms for densest subgraph (as mentioned in the "Future Work" section of this PR) once this is merged. Since there isn’t a single "best" algorithm and their performance can vary depending on the graph, renaming greedy_plus_plus to densest_subgraph could become problematic as we add more algorithms. Proposed Solution: One option is to introduce a method parameter to the densest_subgraph function, defaulting to Greedy++ (or whichever performs best on standard benchmarks). This approach would be similar to scipy.optimize.minimize, which allows selecting different minimization methods. By doing this, we can easily add new algorithms in the future by specifying options like method="Greedy++" or method="FISTA". What do you think about this approach? |
I agree, I think this is the best way forward (and what I was going to suggest!) So the API (for users) will look something like: >>> SG = nx.approxmiation.densest_subgraph(G, method="greedy++", iterations=3)In that case, it may also be worth making |
|
OK... I think we have agreement on changing the module name to I agree with @rossbar that the function names can be changed in later PRs which introduce the new methods. But I would very much prefer that this happen before we make a release (which is scheduled for late March/early April). Otherwise we have to make a deprecation for Either way, can we change the module names now so we don't have those changes baked into the commit history forever? Thanks! |
…nsest-subgraph-algorithms
…erent algorithms), and restructured algorithms and tests around the renaming
|
Thank you, @rossbar and @dschult! Regarding the timeline, I’m confident I can complete the implementation of the other two algorithms (along with the review process) by March/April 2025, so there shouldn’t be any issues on that front. I’ve updated the module name to density.py and renamed the function to densest_subgraph. The corresponding docstring and tests have also been updated. This PR is now ready for review (and hopefully for merging). Once this is merged (if all looks good), I’ll submit a PR for the next algorithm, which I’ve already implemented on a local branch. |
|
@dschult Following up - I've renamed the modules as requested (density.py and tests/test_density.py). Since I'm confident about implementing the other algorithms before the March/April release, would you have time to review/approve the current changes? |
There was a problem hiding this comment.
Thanks for refactoring this. It helps quite a bit with review!
I have some minor notes/suggestions below. You don't need to make the changes through the github interface -- you can do them locally if you prefer. I just find it easier to make suggestions this way on github. Feel free to improve my suggestions -- I'm not locked in to any specific wording.
More importantly, I have some questions about the concept of the problem. :)
- the "density" as defined here is the average degree of the induced subgraph G[S]. Typically in graph theory, the "density of a graph" is defined as the fraction of possible edges present in a graph
(|E| / (|V|*(|V|-1)/2)or often writtenm/(n*(n-1)/2). Note that while the "density of the densest subgraph" is proportional to the "average degree of the induced subgraph", they are not the same. Maximizing either give the same set of nodes. So I can see why people focus on the average degree when finding the set which induces the densest subgraph. But it seems like we should not be calling the value returned here a "density" of the subgraph. Does this make sense? Am I missing something here? - When you say that the algorithm is polynomial time solvable, can you specify with respect to what parameter(s)? I guess it would be number of nodes -- but doesn't it depend on the density of G too? What is being held constant in the asymptotic limit being taken? (and maybe nothing is being held constant -- it just isn't clear to me what polynomial time means here)
- Do you have an idea of where the bottlenecks of this algorithm lie? It seems a shame to have to copy the graph on each iteration. I'm envisioning working with a dict of degrees instead of G_iter. But maybe that is negligible time overall.
…nsest-subgraph-algorithms
|
Hi @dschult, Thank you for the review! I’ve addressed all your (minor) comments. Regarding your main comments/questions:
Let me know if you’d like further refinements! |
dschult
left a comment
There was a problem hiding this comment.
Looks good!
My last two comments have to do with the annoying fact that G.degree as an iterator over (node, degree) is enough faster than G.degree[node] for node in G that I would recommend using the iteration when possible. The two places are not in the inner loop, so it won't make a huge difference, but it'll help some, and in my opinion it makes the code more readable too. But that is subjective.
This is really nice stuff. And I really appreciate your answers to my questions. It helps to have someone who has studied the problem and knows the history. Thanks!
|
Thanks @dschult ! Got it regarding perf difference between |
This PR introduces two popular algorithms for solving the Densest Subgraph Problem (DSG), which is a foundational problem in graph theory with a wide range of applications. Dense subgraphs often provide insights into network structures, such as community detection, and can serve as a key subroutine in unsupervised clustering techniques.
While the problem is exactly solvable in Polynomial Time via a reduction to maximum flow (commonly referred to as Goldberg's algorithm), the algorithm is quite involved and not scalable for large-scale graphs. For example, authors from [2] noted that the Goldberg’s algorithm failed on many large graphs even though they used a highly optimized maximum flow library. Due to the importance of the problem and solving it on large graphs, there are several known approximation algorithms for the problem.
Algorithms Implemented:
Future Work:
Notes
These implementations focus on correctness and scalability, with Greedy++ already demonstrating excellent performance on large-scale graphs. In subsequent PRs, additional optimizations and advanced algorithms will be included, ensuring support for even larger datasets and more efficient runtime.
References