Skip to content

feat: add directed star graph - #8151

Merged
rossbar merged 6 commits into
networkx:mainfrom
Peiffap:feat/directed_star_graph
Jul 30, 2025
Merged

rossbar merged 6 commits into
networkx:mainfrom
Peiffap:feat/directed_star_graph

Conversation

@Peiffap

@Peiffap Peiffap commented Jul 14, 2025

Copy link
Copy Markdown
Member

While working on #8148, directed star graphs would have been useful to easily created directed graphs with many "leaf nodes" in their DFS tree. This PR implements adds that possibility and adds a test for it.

Comment thread networkx/generators/classic.py Resolved
Comment thread networkx/generators/classic.py Resolved

@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 important question here is one of semantics. To my mind, there are three possible interpretations of a directed star graph:

  1. Directed edges originating from the "root" (what's proposed in this PR)
  2. Directed edges originating from the "leaves" pointed towards the root
  3. Bidirectional edges

Without any concrete background in graph theory, I have no idea if any of these is the "accepted" definition. Note that NetworkX does different things for different graphs with create_using=DiGraph. For example, path_graph creates directional paths from "start" to "finish" while complete_graph creates bidirectional edges.

So the question is, do we want to make star_graph(n, create_using=nx.DiGraph) mean option 1. If the graph theory literature agrees on this definition, I'm +0.5 - else I'm -1. I'd generally tend towards explicitness: it's pretty straightforward (and only a couple characters longer) to replace nx.star_graph(n, create_using=nx.DiGraph) with nx.DiGraph([(0, n) for n in range(1, n + 1)]).

@Peiffap

Peiffap commented Jul 15, 2025

Copy link
Copy Markdown
Member Author

I think this is actually option 3 from your list, because that's the one that made sense in my use case, and because it's the one that's "hardest" to write.
Your point is valid though. I don't expect there to be an agreed upon standard; if anything, option 1 from your list seems like it's the most common choice.

  • In [1], the following definition is given:

    An (s, t)-directed star is a directed graph with s + t + 1 vertices and s + t arcs; s vertices have indegree zero and outdegree one, t have indegree one and outdegree zero, and one has indegree s and outdegree t.

    The NetworkX definition of a star graph is the (0, n) case.

  • In the Wolfram language implementation, the visualization shows a directed star graph with edges from the hub to the spokes.

  • igraph actually has all three possibilities:

    IGRAPH_STAR_OUT | directed star graph, edges point from the center to the other vertices.
    IGRAPH_STAR_IN | directed star graph, edges point to the center from the other vertices.
    IGRAPH_STAR_MUTUAL | directed star graph with mutual edges.
    IGRAPH_STAR_UNDIRECTED | an undirected star graph is created.

@rossbar

rossbar commented Jul 15, 2025

Copy link
Copy Markdown
Contributor

I'd definitely not want to pollute the API with functions for each option. I'd be much more inclined to add docstring examples.

@dschult

dschult commented Jul 15, 2025

Copy link
Copy Markdown
Member

I believe the bidirectional case is DG = nx.star_graph(5).todirected().
Outward could be done using DG = nx.DiGraph(nx.star_graph(5).edges)
Inward could be DG = nx.DiGraph(nx.star_graph(5).edges).reverse() or DG = nx.DiGraph([(v, u) for u, v in nx.star_graph(5).edges])

This type of thing is a motivating example for the NXEP we explored a while back involving making versions of the generators that return the edges and nodes rather than the full graph. But docstring examples might do the trick here. I don't think there is much difference in performance either but I haven't checked.

@Peiffap

Peiffap commented Jul 15, 2025

Copy link
Copy Markdown
Member Author

Okay, how about this:

  • Make option 1 the default: directed edges from hub to spokes. This seems to be what's most common in the literature, and it requires the least code churn from the current version on main.
  • Add docstring examples for how to get the other two, based on Dan's comment.

@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 like a reasonable approach. +1 for the discussion and examples in the doc_string. And this adds functionality (can be directed which used to raise) so I believe no deprecation is needed.

I approve this PR

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

Sounds good to me - I trust everyone's opinion about what makes the most sense from a definition standpoint and have no strong sense of my own! Implementation-wise everything looks good!

@rossbar
rossbar merged commit 01705c4 into networkx:main Jul 30, 2025
@Peiffap
Peiffap deleted the feat/directed_star_graph branch July 30, 2025 17:25
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