Skip to content

Add: Shrikhande graph - #8884

Merged
MridulS merged 7 commits into
networkx:mainfrom
satemochi:add-for-shrikhande-graph
Sep 11, 2026
Merged

MridulS merged 7 commits into
networkx:mainfrom
satemochi:add-for-shrikhande-graph

Conversation

@satemochi

Copy link
Copy Markdown
Contributor

Hi, NetworkX maintainers and NetworkX community,

I recommend adding the Shrikhande graph to the list of named graphs.
If this graph is not impactful or if the list is complete then please reject this PR immediately.

The Shrikhande graph is an important graph in spectral graph theory, similar to nx.paley_graph(13).They are both graceful graphs.

I wrote another snippet of code that can be found here. This code gives two major layouts with the minimum coloring for testing correctness as follows:
shrikhande

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

The proposal seems reasonable to me - the Wikipedia article is interesting and the named graph seems to be well-established and discoverable.

Some high-level questions:

  • Why the tuple nodes - are the tuples significant in some way?
  • Almost certainly related: is there a procedure for generating this graph that doesn't involve hard-coded nodes? If so, the procedure itself could be more desirable than the hard-coded version. Similarly, if the procedure is a general one (i.e. capable of producing other graphs from a set of parameters) the question becomes whether it wouldn't be better to add that and include the construction of the Shrikhande graph as an example.
  • Is there a natural concept of a "directed" Shrikhande graph? If users pass in create_using=nx.DiGraph they will indeed get a graph with directed edges from this formulation. Are the edge directions meaningful?

@satemochi

Copy link
Copy Markdown
Contributor Author

@rossbar Thank you for your thoughtful reply.

  • Why the tuple nodes - are the tuples significant in some way?

It follows the definition provided in the Wikipedia article: $V = \mathbb{Z}_4 \times \mathbb{Z}_4 = {(0, 0), (0, 1), \ldots, (3, 3)}$. However, the SageMath code has selected (octal) int type. If this is preferred, I will make the correction.

  • Almost certainly related: is there a procedure for generating this graph that doesn't involve hard-coded nodes? If so, the procedure itself could be more desirable than the hard-coded version. Similarly, if the procedure is a general one (i.e. capable of producing other graphs from a set of parameters) the question becomes whether it wouldn't be better to add that and include the construction of the Shrikhande graph as an example.

Yes, I will rewrite as follows.

@_raise_on_directed
@nx._dispatchable(graphs=None, returns_graph=True)
def shrikhande_graph(create_using=None):
    r"""
    Returns the Shrikhane Graph.

    The Shrikhande graph has 16 nodes and 48 edges.
    This graph can be constructed as a Cayley graph [1]_.
    The vertex set is $\mathbb{Z}_4 \times \mathbb{Z}_4$.
    Two vertices are adjacent if and only if the difference is in
    $\{\pm(1, 0), \pm(0, 1), \pm(1, 1)\}$.

    Parameters
    ----------
    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Returns
    -------
    G : networkx Graph
        Shrikhande Graph with 16 nodes and 48 edges

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Shrikhande_graph
    """
    from itertools import product
    _ = [(1, 0), (0, 1), (1, 1), (-1, 0), (0, -1), (-1, -1)]
    G = nx.Graph((((u, v), ((u+w) % 4, (v+x) % 4))
                 for (u, v), (w, x) in product(product(range(4), range(4)), _)),
                 create_using=create_using,)
    G.name = "Shrikhande Graph"
    return G

Since general constructions follow the Cayley graph, this may present a challenge. This lies at the intersection of graph theory and algebra (group theory).

  • Is there a natural concept of a "directed" Shrikhande graph? If users pass in create_using=nx.DiGraph they will indeed get a graph with directed edges from this formulation. Are the edge directions meaningful?

You are asking whether you should use @_raise_on_directed for decoration, or it can be removed, correct? In fact, if an edge $((a, b), (c, d))$ is in the Shrikhande graph, then $|a - c| \leq 1$ and $|b -d| \leq 1$ hold under $\mathbb{Z}_4$. Taking the absolute value implies undirected. Therefore, edge direction is not that important.

The code in the PR is based on the Petersen graph code. On the other hand, the Frucht graph and the Paley graph does not have @_raise_on_directed... I will follow the maintainer's instructions.

@dschult

dschult commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

How about the following code (trying to be clear -- ignoring optimization cuz its so small.)

def shrikhande_graph():
    <docs>
    # basic 2d 4x4 periodic grid but only connect to the East, North and NE.
    # Missing edges are SE and NW. The others get filled in by wraparound.
    #    |/|/|/|/
    #    O-O-O-O-
    #    |/|/|/|/        Circle is a node.
    #    O-O-O-O-        Wrap is shown
    #    |/|/|/|/        using lines heading
    #    O-O-O-O-        to the right and up.
    #    |/|/|/|/
    #    O-O-O-O-
     rows = cols = range(4)
    plus_one = [1, 2, 3, 0]
    G = nx.Graph()
    G.add_edges_from(((x, y), (plus_one[x], y)) for x in rows for y in cols)
    G.add_edges_from(((x, y), (x, plus_one[y])) for x in rows for y in cols)
    G.add_edges_from(((x, y), (plus_one[x], plus_one[y])) for x in rows for y in cols)
    return G

Generalizations could also be based off the 2d-grid approach
including stuff like "make it a 5x5 grid".
But I don't know if that is an interesting graph.
Who knows, maybe we could write a paper about it. :}

I think directedness should be handled like Petersen graph.
These are specific graphs, famous for specific properties.
Only famous for undirected case.

@satemochi

Copy link
Copy Markdown
Contributor Author

@dschult Thank you for your quick reply.

Clearly, your code is better than mine. It is more readable, intuitive and insightful.

Starting with a 4x4 grid shows that the Shrikhande graph is a toroidal (genus one) graph. This information is not included in the Wikipedia article on the Shrikhande graph. However, there is a short script in MathWorld which suggests that E. Weisstein has also recently noticed this. I added the MathWorld reference to <docs>.

I think directedness should be handled like Petersen graph. These are specific graphs, famous for specific properties. Only famous for undirected case.

Yes, I do.

Comment thread networkx/generators/small.py Resolved

@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've gone through this one more time. I think we should add a down-to-earth description of the graph as a first paragraph -- then have the paragraph describing the properties of the graph (related to Cayley graphs, the formula for which nodes connectm etc).

Notes: we should

  • change "vertex" to "node" in the description to match the networkx preferred term.
  • add a link/hook in the docs so the function shows in the docs (in the "small" sectin of doc/reference/generators.rst
  • maybe remove the words "with 16 nodes and 48 edges" from the "Returns" section of the doc-string.

I made a suggestion for the paragraph, but feel free to change that as you think appropriate.

Comment thread networkx/generators/small.py Resolved
@satemochi

satemochi commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you @dschult for suggestions.

  • change "vertex" to "node" in the description to match the networkx preferred term.

I checked and corrected. In the hoffman_singleton_graph(), the terminology vertex is used. I hesitated briefly, but decided not to make any corrections. If it would be better to make corrections for Hoffman-Singleton graph, I will do so.

  • add a link/hook in the docs so the function shows in the docs (in the "small" section of doc/reference/generators.rst

I checked and added entry shrikhande_graph.

  • maybe remove the words "with 16 nodes and 48 edges" from the "Returns" section of the doc-string.

I checked and remove "with 16 nodes and 48 edges", however, this sentence was written in reference to Frucht graph. If it would be better to make corrections for Frucht graph, I will do so.

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

Haha -- yes, it looks like about half the graphs in small.py add the number of nodes and edges to the description of what is returned. And the other half don't have that info. Let's leave it as you've got it.

I'd rather leave the hoffman_singleton_graph and frucht_graph as they are -- We could easily get sucked into docs changes for consistency sake and this could become a large PR indeed. :)

I approve this PR :} Thanks @satemochi

@MridulS
MridulS merged commit 9ec50e8 into networkx:main Sep 11, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Sep 11, 2026
@satemochi
satemochi deleted the add-for-shrikhande-graph branch September 11, 2026 13:40
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