Repository navigation
create new generator that returns a random k-lift of a d-regular graph - #8045
Conversation
|
Can you make this PRs title/subject line informative? |
|
Title updated. Regrarding the tests, I had a problem with my pre commit, that I now fixed. I will update the files and sorry for this (my first time contributing) |
|
Hi! Just a friendly reminder to take a look at this PR when you get the chance. Let me know if there’s anything I should adjust or clarify. Thanks! |
There was a problem hiding this comment.
I'm not familiar with k-lift graphs, not do I know if this would necessarily be a good fit for NetworkX; for that I defer to Ross and Dan. Here are some general comments (below and inline).
- The path to the new algorithm should be fixed. I'm not sure where this would best fit in NetworkX, but you can have a look at a recent PR that added a new algorithm: #7978. Regarding the choice of a location, I think this might fit in well with the random graph generator functions.
- The docstring would be a lot easier to read if you used backticks for variable names.
- Since this function uses randomness, it should have a seed parameter, and the tests should set this parameter to avoid spurious failures.
- Don't quote me on this, but I believe it's faster to add multiple nodes or edges at once (using
add_(node|edge)s_from) rather than callingadd_(edge|node)inside a loop.
|
Updated this PR to address the previous comments. The function has been moved to the generators module, and I’ll update the import statements accordingly once a final location is decided. If helpful, I can also add an nx.draw visualization to accompany the example for clarity. |
|
Hi! Just wanted to check in. Are there any further changes you'd like me to make (aside from the final placement), or at this point is it more a question of whether the PR fit within NetworkX? Happy to adjust if needed! |
|
Hello, we understand that review takes time, and we’re happy to respond to any questions or decisions you make. At the same time, we need to close our project within the next couple of weeks, so it would be very helpful to receive feedback on this PR, the k-lift, being the simplest one. If you’d like additional sources about k-lifts or expanders in general, we can provide them. We also have a project document that discusses these PRs and their evaluations in greater detail. |
Peiffap
left a comment
There was a problem hiding this comment.
I can't tell if this would be a good addition to NetworkX, but if so, these are some nits that would be good to fix.
f6ef7a9 to
c5ba690
Compare
Co-authored-by: Gilles Peiffer <[email protected]>
Co-authored-by: Gilles Peiffer <[email protected]>
|
I’ve fixed the nits, thanks again for the review! |
rossbar
left a comment
There was a problem hiding this comment.
Generally LGTM, though I wasn't able to determine what should happen with directed graphs!
| Parameters | ||
| ---------- | ||
| G : networkx.Graph | ||
| An undirected, degree-regular graph. |
There was a problem hiding this comment.
This brings up a good point: is this operation defined for directed graphs? There's nothing in the implementation to prevent running with a DiGraph - for example, this works fine:
>>> G = nx.complete_graph(5, create_using=nx.DiGraph)
>>> H = nx.random_k_lift(G, 5)... though of course H is undirected. How should directed graphs be handled?
There was a problem hiding this comment.
Good question. At the moment (despite the docstring) the code technically accepts directed graphs, but H is always created with nx.Graph(), so any edge directions in G are silently discarded. I see 2 options on how to handle this:
- Preserve directionality: Define H as a DiGraph when G is directed (H = nx.DiGraph() if G.is_directed() else nx.Graph()). This would keep the behavior consistent with the input type.
- Restrict input to undirected graphs: Use @not_implemented_for("directed") or raise a ValueError if G is directed, making the function’s expectations explicit.
k-lift main purpose is tied to expander graphs who are undirected. On the other hand, I’ve seen references suggesting random lifts can also be useful for directed graphs. Since this operation is made to be a more general one, would you like me to change the code to return directed H for directed G? or restrict it to undirected graphs?
There was a problem hiding this comment.
If the code is designed to work for either directed or undirected graphs (and there is an accepted way to handle directed graphs in the literature) then let's use H = G.__class__().
Similarly, does this "just work" for multigraphs? Probably so. Then nothing more is needed. Otherwise we should use the decorator @nx.utils.notimplementedfor("multigraph").
There was a problem hiding this comment.
Yes, it should work correctly for all standard NetworkX graph types, including directed and multigraph variants. Thanks for catching the definition of H, I’ll update that line to use H = G.class() and note the behavior in the docstring.
There was a problem hiding this comment.
The desired semantics for the directed case are still not quite clear to me. I tried the naive approach of simply extending the test_random_k_lift_size_and_structure test to included DiGraphs, and of course you run into other errors because the assertions are not relevant for directed graphs - e.g. checking for connectedness or k-regularity.
I understand that the procedure is straightforward enough and can be naively applied to directed graphs, but I'm curious whether the output of this procedure is actually meaningful. If not, my personal temptation would be to explicitly raise for directed graphs. If clear use-cases for a k-lift procedure crop up, it can always be added. Perhaps this approach is too conservative though!
There was a problem hiding this comment.
Here's a paper about directed k-lifts. A quick search turned up at least a few -- and they mostly are proving that random n-lifts have some property almost surely as n-> inf.
Also, this paper's abstract is helpful for us as it defines what a directed k-lift is and what a random directed k-lift would be.
I think the idea is that the nodes are treated just like for the undirected case (k-copies of the nodes), and the edges between lifted nodes follow the direction provided by the underlying digraph. So construction should be very similar, and maybe even the same code.
It would be good to have tests for the digraph versions of course. I wasn't able to find results about e.g. strongly connected. But it seems that the directed version is of interest, and there has been less research activity on it than for the undirected case.
Perhaps we should support directed (and multigraph) to let people play with it even if we only have tests for the undirected case. ???
There was a problem hiding this comment.
Indeed, that’s the same reference I saw on directed lifts, though I’m not deeply versed in the directed-graph literature either. It seems to me as well that strong connectivity is the most natural analog of connectivity for directed graphs.
Regarding tests: the current structure tests rely on random_regular_graph for easy generation and on a basic connectivity check, both of which are tied to the undirected expander use-case. Since constructing good directed or multigraph expanders is less trivial, I’d propose adding separate test functions with one small directed and one small multigraph example as simple sanity checks. These would just verify that the lift preserves the structure and works for these types (when it comes to multigraphs the test has all the normal checks and a type check, so is the test for the directed case with the exeption of strong connectivity that doesnt pass for the small k I used) without trying to cover arbitrary or large cases. Treating the more general test for simple graphs as representitive while still giving simple checks for directed and multigraph cases.
This keeps the tests lightweight but still demonstrates that the implementation handles all supported graph classes. would you like me to add those test functions (to give a bit of assurance for those who try to play with those types)?
There was a problem hiding this comment.
It might be that weakly_connected is the analog of connectivity in this case. And it might be that neither actually works well. :) In my head and with little experience, I don't see how the matching within fibers would preserve connectivity even in the undirected case -- so it must be related to the large n limit and having lots of connections is highly likely to connect everyone. That seems reasonable for directed case too -- though weakly connected might occur for smaller n than strongly connected. In the limit I would expect they both occur.
I'd rather stay away from large examples in the tests for directed as we don't know much about it. So I like your proposal @sean-chernov. Let's add some light-weight tests for directed and multigraph. Could you please add those? Thanks!
Updated so H will preserve the graph type of given base graph G
added simple tests for DiGraph and MultiGraph
| if not nx.is_regular(G): | ||
| raise ValueError("input graph must be degree-regular") |
There was a problem hiding this comment.
Do we need to restrict k-lift to regular graphs?
Why not allow it for all graphs?
There was a problem hiding this comment.
The majority of the literature I found had focused on d-regular graphs (as they are tied to expanders) and I am not sure when it comes to directed graphs (though the "d-regular" version of them also showed in/out degree preservation in the test). But with a farther research I did find a paper (https://arxiv.org/pdf/2401.01238) that generalizes lifts for non regular graphs and claims those lifts have the benfit of breaking short (small cycles). And also (possibly testable, though complicated) that the lifted graph H shares the degree disterbution of base graph G (a generalization of degree preservation in d-regular graphs I assume).
This paper in my opinion is a good justification to remove the limitation to only d-regular graphs (although I will note in the docstring that k-lifts are typically used with degree-regular graphs).
This raises two follow-up points:
- Tests - I’m leaning toward adding a simple test for a non-regular graph, similar to the existing multigraph and digraph tests. I’m unsure whether to include a degree-distribution check, as that might be unnecessarily complex.
- Docstring - Should I limit the description to the main undirected, degree-regular expander use case (and its spectral-gap properties) while simply noting that the operation also works for other graph types? Or should I also mention the additional properties found in more specialized cases and include references to those papers?
There was a problem hiding this comment.
I think a simple test for non-regular graphs is sufficient.
Also, I feel like the doc_string should focus on the regular undirected case. And has a note (maybe one paragraph for all of directed, multigraph, non-regular) that is works for these other types. And I would add references to the papers you have found -- maybe just one for directed and one for non-regular. References are cheap if you already know the paper and it can help someone in the future (maybe you?) who wants to start looking into that area.
:)
There was a problem hiding this comment.
I added a simple test for a non–degree-regular graph and removed the old ValueError test. I also removed the ValueError check for non–degree-regular graphs in the implementation itself.
Finally, I updated the docstring to reflect that all NetworkX graph types are supported and added three references:
- The two we discussed covering directed and non–degree-regular cases.
- Additional one covering expander graph and spectral-gap behavior in lifts.
replace raise error test with simple example test for non degree regular graph
removed raise on non degree regular base graphs and updated docstring to reflect changes. added 3 references 2 for the spacial cases for non degree regular or directed graphs and another for expander graph and spectral gap behaviour.
|
Hi, just checking in regarding this PR. |
|
Hello, I just wanted to follow up and mention that we’re hoping to finalize our project (including an answer regarding this PR) by the end of this month. |
|
Hi, just reminding you of the previous message |
|
Hi, just checking in to see whether there have been any updates or further thoughts on the review of this PR. |
dschult
left a comment
There was a problem hiding this comment.
I'm all good with what's in here now.
Thanks @sean-chernov !
|
Great, thank you! Once this PR is wrapped up, we’d be happy to discuss the remaining PRs as well. |
dschult
left a comment
There was a problem hiding this comment.
Am I correct that the in/out_degree of the nodes in k_lift(G) is the degree of their associated node in G for directed graphs. And for undirected graphs, the degree of nodes in H are twice the degree of the associated node in G?
Too many suggestions below. Might be easiest just to do the following on your local machine:
-
Use
nx.<graph type>(<list of edges>)to create the graphs. -
Put comments just before the line instead of at the end of a line
-
Create a variable
kto make it clear what the k value is. Use it in func calls and formulas for number of nodes and degree where relevant. e.g.assert H.number_of_nodes() == len(G) * k. That way the formula for how many nodes is clear. Similarly for the degree values. -
check line length of doc_strings. Should be max of 88 to match code.
Thanks!
|
Thanks for the detailed feedback! On the degree question: For directed graphs, yes, each lifted node preserves the in- and out-degree of its associated node in G. For undirected graphs, the degree is also preserved (not doubled), since each original edge induces a matching between copies. Considering the example in the directed test, the in/out degrees are 1 for G and also for H. For undirected graph based on the same list of edges we will get G with degree of 2 which will be preserved in H. I’ll also make the suggested cleanups locally: |
– Construct test graphs explicitly using nx.(edge_list). – Move comments to their own lines. – Introduce a variable k and use it consistently in assertions (e.g. len(G) * k).
dschult
left a comment
There was a problem hiding this comment.
This looks good! Thanks @sean-chernov !
This module implements a k-lift construction using random permutations for a given d-regular base graph. It supports applications in network topology design, such as the Xpander architecture, where high spectral expansion is desired.