Skip to content

projective polarity graph generator - #8066

Open
sean-chernov wants to merge 15 commits into
networkx:mainfrom
sean-chernov:er_polarity_graph
Open

sean-chernov wants to merge 15 commits into
networkx:mainfrom
sean-chernov:er_polarity_graph

Conversation

@sean-chernov

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

Copy link
Copy Markdown
Contributor

This PR adds a new module for generating polarity graphs based on the finite projective space PG(m, q) over GF(q) including the ER polarity graph based on PG(2, q). These graphs are known for their low diameter (2 for ER polarity), near-regularity, and strong connectivity while ER is also useful for modeling deterministic low-diameter network topologies such as PolarFly.

@dschult

dschult commented Jun 1, 2025

Copy link
Copy Markdown
Member

This looks like a new graph generator of networkx that are at least close to some used in router network topology design. It is dependent on an extension package of NumPy to finite fields called galois. The code is actually pretty short -- using helper functions that are even shorter. At first read, it is not clear that the use of galois is needed beyond working mod q which I guess saves using mod on some numpy arrays.

I wonder if the name er_polarity is sufficiently known/used to describe this graph. It doesn't seem to appear in the polarfly paper. You should describe what the name refers to, or match a known name for this type of graph. Can you explain why you chose this name?

There are a number of bookkeepping tasks to make this work smoothly with NetworkX. If that is too daunting we can probably morph this into a PR that passes tests. Or we can help you do it.

  • the new module should be located in the networkx/generators directory with test file in networkx/generstors/test_...
  • the module/file name should not be identically the same as the function name (to avoid name collisions). It's also not great that we have a new module for just one function. Is there a category of graph generators that this fits with? Can you find a name of a type of graph this fits with that other folks could add to? Like low_diameter.py only better... :)
  • the helper functions should start with the _ character to be easily identified as private functions (and excluded from tab-completion tools). The main function should be included in a variable named __all__ at the top of the file.
  • We need to connect this file to the package in the networkx/generator/__init__.py file.
  • We need to connect this file to the documentation using something similar to what you'll find in doc/reference/generator.rst
  • NetworkX is built so it works even on systems without numpy installed. So you'll need to move the import numpy and import galois inside the functions. And for tests, you'll need to include an "import or skip" check to skip the tests whenever numpy is not present. And you should add the function name to the list in networkx/conftest.py defined as needs_numpy.

I'm not sure about the galois extension. How much do you gain from that? Could we remove it as a dependency by using the numpy's mod functionality? How big an impact does that have?

Finally for now, what got you into this kind of work? What problems is this useful for? What motivated you to code it up and submit it to NetworkX? Thanks!

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hi, thank you for the detailed feedback and the thoughtful review.

My partner (Shai Reuven) and I are electrical engineering students working on our final bachelor's project in the NSSL (Networked Software Systems Lab). The focus of our project is implementing and evaluating graph constructions relevant to network topology design, particularly those that are not yet available in NetworkX. Specifically, we’re contributing implementations for three graph models: Xpander via k-lift, ER polarity graphs (used in PolarFly), and SlimFly. This PR and the earlier k-lift one are our first contributions.

  1. Use of galois and Prime Power Fields
    Initially, I tried to implement the ER polarity graph without galois, relying solely on modular arithmetic (mod q) using NumPy. That version works when q is a prime number — as used in the examples in the PolarFly paper.

However, upon reading further, we noted that the paper explicitly states the construction is valid for prime powers (i.e., q = p^k). In those cases, the field GF(q) becomes a finite field extension, and arithmetic involves polynomials modulo an irreducible polynomial over GF(p). These operations are mathematically more complex and aren't feasible to handle correctly using just NumPy’s mod functionality.

That’s what motivated the use of the galois library: it provides a clean and efficient interface for handling arithmetic in extended finite fields without manually managing polynomial arithmetic. We understand this introduces an external dependency and would be happy to help refactor the code to isolate that dependency as needed.

  1. Naming: "ER Polarity Graph"
    You're right that the term "ER polarity graph" is not emphasized as a formal graph class name in the PolarFly paper. The name was based on references in pages 3–4, which describe the underlying construction used for PolarFly, where nodes are derived from projective geometry (PG(m, q)), and edges are based on orthogonality under a polarity transformation.

We’re happy to revise the name if there’s a more standardized or widely recognized term for this class of graphs. One alternative could be “projective_orthogonal_graph”, or grouping it under a broader category like finite_geometry.py if there are future related contributions.

Thanks for outlining the integration requirements clearly. We'll follow the steps you suggested.
We’ll update the PR progressively and let you know if any blockers arise.
Thanks again for the feedback, and also for the opportunity to contribute. We’ve really enjoyed working on this and hope it’s helpful to the community.

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

IMO the galois dependency is a hard blocker, especially since galois itself looks like it depends on numba.

Have you considered publishing a separate package? That would probably be the easiest way to get this code out there without having to worry about dependencies!

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. I’ll definitely consider publishing the more general Galois-based version as a separate package.

In the meantime, would you be interested in me updating this pull request to use a simplified version of the code that only supports prime values of q? This version avoids the galois dependency entirely and uses a standard modular dot product. It covers many practical use cases and follows the form shown in typical examples (like those in the PolarFly paper), though with the limitation that q must be a prime number.

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

In the meantime, would you be interested in me updating this pull request to use a simplified version of the code that only supports prime values of q?

It would be interesting to me personally, but I'm not at all familiar with the subdomain so I have no sense at all whether or not this is a good fit for networkx! The only thing I can say definitively is that galois is a hard-blocker for me!

@sean-chernov

Copy link
Copy Markdown
Contributor Author

updated the PR to use a simplified version with no Galois dependency

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Updated this PR to pass all checks. The main issues were:
Adjusting the import to reflect the correct location within networkx/generators/.
Ensuring the projective point coordinates are int types instead of numpy.int64.

@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, For the sake of accuracy, I am changing the name of this PR (and its main function) to "Projective Polarity Graph generator". This is to avoid confusion, since "ER polarity graph" refers specifically to polarity graphs based on PG(2, q), which approach the Moore bound.

@sean-chernov sean-chernov changed the title Er polarity graph generator projective polarity graph generator Aug 18, 2025
@sean-chernov

sean-chernov commented Nov 5, 2025 •

Copy link
Copy Markdown
Contributor Author

Hello,
Since the random-k-lift PR is currently under discussion, I’ll focus on updating our other PRs (being the low-diameter topologies).

For this PR, I plan to make the following adjustments:
-Remove the m=1 case (which is not meaningful) and the corresponding connectivity check. Instead, add a validation step to raise an error for m < 2 or non-integer values.
-Add a reference that discusses projective polarity graphs based on PG(m, q) for m > 2.
-Refine the docstring notes, primarily emphasizing the properties of the Erdős–Rényi polarity graph (m=2 case) and its advantages, while briefly mentioning the generalized m>2 cases and their relevance.
-I will remove the m > 2 diameter-2 checks from the test suite.
While small instances of PG(m, q) with m > 2 often happen to have diameter 2 due to high connectivity, this property is not guaranteed in general for larger parameters.
To avoid misleading assumptions or brittle tests, the strict diameter = 2 assertion is now kept only for the proven m = 2 (Erdős–Rényi polarity) case. And merge the other structure tests

-remove m=1 case and connectivity raise. add instead raise for m<2 (or non integer)
-update docstring and refernces
-merge structure tests
-diameter 2 test only for m=2 case (ER polarity)
-add test for invalid m value
@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hello, now that the random k-lift PR has been successfully merged, we’re ready to focus on the remaining low-diameter topology PRs: #8066 and #8118 (Slim Fly ring).

Do you have any outstanding questions or concerns about either of these PRs? Also, would you prefer that we keep them as separate PRs, or would it make sense to consolidate them (for example, under a shared low-diameter module)?

@sean-chernov

Copy link
Copy Markdown
Contributor Author

Hello, any updates on wheter you think those PRs fit the library and which direction you want to take with them?

This branch has not been deployed

No deployments
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.

3 participants