Skip to content

Densest Subgraph Problem: Greedy Peeling and Greedy++ Implementations - #7731

Merged
dschult merged 17 commits into
networkx:mainfrom
FaroukY:implement-densest-subgraph-algorithms
Dec 13, 2024
Merged

dschult merged 17 commits into
networkx:mainfrom
FaroukY:implement-densest-subgraph-algorithms

Conversation

@FaroukY

@FaroukY FaroukY commented Nov 22, 2024

Copy link
Copy Markdown
Contributor

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:

  1. Greedy Peeling: Based on Charikar's classical algorithm [1], this is a simple and effective approach to approximating the densest subgraph and is a 1/2 approximation.
  2. Greedy++: From a more recent work [2], this method builds on Greedy Peeling and achieves significantly better performance on large-scale graphs. The algorithm is a $(1-\epsilon)$ approximation after $O(\Delta(G)\log n/\epsilon^2)$ iterations. In practice, it converges quite fast. See for example [3].

Future Work:

  1. Implement the algorithm from [3], which offers further improvements in scalability and runtime.
  2. Optimize Greedy++ by implementing a linked list-based approach, enabling key operations to be performed in $O(1)$ time instead of $O(\log n)$ time. While the heap-based approach used here is efficient, the constant factor difference between linked lists and heaps becomes significant for massive graphs (e.g., billions of edges).
  3. Implement the exact algorithm (Goldberg's algorithm).

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

[1] Charikar, Moses. "Greedy approximation algorithms for finding dense components in a graph." 
    In International workshop on approximation algorithms for combinatorial optimization, pp. 84-95. 
    Berlin, Heidelberg: Springer Berlin Heidelberg, 2000.

[2] Boob, Digvijay, Yu Gao, Richard Peng, Saurabh Sawlani, Charalampos Tsourakakis, Di Wang, 
    and Junxing Wang. "Flowless: Extracting densest subgraphs without flow computations." 
    In Proceedings of The Web Conference 2020, pp. 573-583. 2020.

[3] Harb, Elfarouk, Kent Quanrud, and Chandra Chekuri. 
    "Faster and scalable algorithms for densest subgraph and decomposition."
    Advances in Neural Information Processing Systems 35 (2022): 26966-26979.

Elfarouk Harb added 3 commits November 22, 2024 00:16
…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 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 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!

@FaroukY

FaroukY commented Nov 26, 2024

Copy link
Copy Markdown
Contributor Author

Thanks @rossbar ! I've implemented your recommended changes.

@rossbar
rossbar dismissed their stale review November 27, 2024 00:02

Requested changes have been made!

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

@FaroukY

FaroukY commented Nov 29, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for the comments @rossbar. I'm planning to have a look at them very soon and do the changes, thanks!

@FaroukY

FaroukY commented Dec 2, 2024

Copy link
Copy Markdown
Contributor Author

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?

@rossbar

rossbar commented Dec 4, 2024

Copy link
Copy Markdown
Contributor

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 iterations keyword-only, so that it is independent of ordering in the signature. WDYT? Either way, no need to stress about the finer points of API at this stage!

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

Also - I meant to just go ahead and approve this... the API/method stuff isn't a blocker and can be iterated on in subsequent PRs. Thanks again @FaroukY !

@dschult

dschult commented Dec 6, 2024

Copy link
Copy Markdown
Member

OK... I think we have agreement on changing the module name to density.py, the test module to tests/test_density.py. We also have agreement on a long term plan for what the main function will be with private helper functions for the different methods.

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 greedy_plus_plus and wait two release cycles, etc. If that timing is OK, function names are fine. If that might be iffy, can we go ahead and change the function name here and add the "method" keyword arg?

Either way, can we change the module names now so we don't have those changes baked into the commit history forever?

Thanks!

Elfarouk Harb added 2 commits December 6, 2024 15:28
…erent algorithms), and restructured algorithms and tests around the renaming
@FaroukY

FaroukY commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

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.

@FaroukY

FaroukY commented Dec 12, 2024

Copy link
Copy Markdown
Contributor Author

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

@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 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 written m/(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.

@FaroukY

FaroukY commented Dec 13, 2024

Copy link
Copy Markdown
Contributor Author

Hi @dschult,

Thank you for the review! I’ve addressed all your (minor) comments. Regarding your main comments/questions:

  1. On “density”: Unfortunately, the term has an ambiguous history. In earlier papers from the 1970s and 1980s, density was commonly defined as the number of edges divided by the number of vertices, $m / n$. Over time, as definitions became more standardized, it began to refer to the number of edges divided by the number of possible edges, $2m / (n(n-1))$, which matches your definition. Researchers in densest subgraph discovery still use the original definition from when the problem was first introduced in 1982, contributing to the confusion. You’re not missing anything—this is just a historical inconsistency. I’ve added a note about this in the Notes section.
  2. Polynomial time solvability: What I meant is that given a graph with $n$ vertices and $m$ edges, we can construct $O(\log(n))$ instances of a Max-Flow network (each of size $O(m + n)$), which, as we know, is solvable in polynomial time. This allows us to extract the solution to the densest subgraph problem. Specifically, if solving Max-Flow on a graph with $n$ nodes and $m$ edges takes $T(n, m)$ time, then the densest subgraph problem can be solved in $O(\log(n)) \cdot T(m + n+2, 2m)$.
  3. Algorithm bottlenecks: There are two main bottlenecks in the algorithm:
    • The priority queue's minimum degree extraction during each “peel.” This can be replaced with a linked list, though the improvement only becomes noticeable for larger graphs.
    • The graph copy step, which seems expensive in NetworkX. In my latest commit above, I’ve implemented a modified version of the algorithm (slightly deviating from the original description) that avoids the graph copy in each iteration (but still needs to copy the nodes). Additionally, the FISTA-based algorithm I plan to push in the next PR eliminates the need for graph copies per iteration while incurring minimal memory overhead.



Let me know if you’d like further refinements!

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

@FaroukY

FaroukY commented Dec 13, 2024

Copy link
Copy Markdown
Contributor Author

Thanks @dschult ! Got it regarding perf difference between .degree iterator vs .degree[u]. Should be fixed now.

@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! Thanks @FaroukY !!

@dschult
dschult merged commit 2b72abb into networkx:main Dec 13, 2024
@jarrodmillman jarrodmillman added this to the 3.5 milestone Dec 13, 2024
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