Skip to content

create new generator that returns a random k-lift of a d-regular graph - #8045

Merged
dschult merged 66 commits into
networkx:mainfrom
sean-chernov:k_lift
Dec 20, 2025
Merged

dschult merged 66 commits into
networkx:mainfrom
sean-chernov:k_lift

Conversation

@sean-chernov

@sean-chernov sean-chernov commented May 13, 2025 •

Copy link
Copy Markdown
Contributor

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.

@dschult

dschult commented May 26, 2025

Copy link
Copy Markdown
Member

Can you make this PRs title/subject line informative?

@sean-chernov sean-chernov changed the title added my test and module files k-lift May 26, 2025
@sean-chernov

sean-chernov commented May 26, 2025 •

Copy link
Copy Markdown
Contributor Author

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)

@sean-chernov

Copy link
Copy Markdown
Contributor Author

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!

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

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 calling add_(edge|node) inside a loop.

Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/networkx/module/k_lift.py Resolved
Comment thread path/to/tests/test_k_lift.py Resolved
@sean-chernov

Copy link
Copy Markdown
Contributor Author

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.

@sean-chernov

Copy link
Copy Markdown
Contributor Author

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!

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hello,

I'm just checking in on this PR and the related PRs #8066 and #8118.

I'm available to address any feedback, make necessary changes, or answer any questions you might have. Please let me know what the next steps are to help move this forward.

Thank you!

@sean-chernov

Copy link
Copy Markdown
Contributor Author

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

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.

Comment thread networkx/generators/k_lift.py Resolved
Comment thread networkx/generators/k_lift.py Resolved
Comment thread networkx/generators/k_lift.py Resolved
@sean-chernov
sean-chernov force-pushed the k_lift branch 2 times, most recently from f6ef7a9 to c5ba690 Compare August 20, 2025 12:53
@sean-chernov

Copy link
Copy Markdown
Contributor Author

I’ve fixed the nits, thanks again for the review!

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

Generally LGTM, though I wasn't able to determine what should happen with directed graphs!

Comment thread networkx/generators/random_graphs.py Outdated
Parameters
----------
G : networkx.Graph
An undirected, degree-regular graph.

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.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. 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?

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.

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").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

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!

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.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.

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!

Comment thread networkx/generators/random_graphs.py Outdated
Comment on lines +1498 to +1499
if not nx.is_regular(G):
raise ValueError("input graph must be degree-regular")

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.

Do we need to restrict k-lift to regular graphs?
Why not allow it for all graphs?

@sean-chernov sean-chernov Sep 23, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. 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?

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.

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.

:)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@dschult
dschult dismissed their stale review September 25, 2025 00:47

Lots of new material in this PR

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hi, just checking in regarding this PR.
It’s been about three weeks since the generalization updates (allowing all graph types and adding the relevant references), so I wanted to ask whether there’s been any further discussion or a conclusion regarding the new data and changes.
Please let me know if there’s anything else I can clarify or adjust to help move it forward.

@sean-chernov

Copy link
Copy Markdown
Contributor Author

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.

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hi, just reminding you of the previous message

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hi, just checking in to see whether there have been any updates or further thoughts on the review of this PR.

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

I'm all good with what's in here now.
Thanks @sean-chernov !

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Great, thank you!
I’d like to mention that this PR, together with PR #8066 and PR #8118, is part of a project I worked on with Shai Reuven at the NSSL Laboratory, Technion.
Thank you for the opportunity to contribute to NetworkX.

Once this PR is wrapped up, we’d be happy to discuss the remaining PRs as well.

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

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 k to 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!

@sean-chernov

Copy link
Copy Markdown
Contributor Author

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).
– Ensure docstrings respect the 88-character line limit.

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

This looks good! Thanks @sean-chernov !

@dschult
dschult merged commit aa66764 into networkx:main Dec 20, 2025
@dschult dschult added this to the 3.6 milestone Dec 20, 2025
@sean-chernov
sean-chernov deleted the k_lift branch January 9, 2026 15:11
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