Skip to content

DOC: add gallery example for metric_closure - #8306

Merged
rossbar merged 12 commits into
networkx:mainfrom
Aka2210:doc-metric-closure-gallery
Oct 3, 2025
Merged

rossbar merged 12 commits into
networkx:mainfrom
Aka2210:doc-metric-closure-gallery

Conversation

@Aka2210

@Aka2210 Aka2210 commented Oct 1, 2025 •

Copy link
Copy Markdown
Contributor

This PR updates the new gallery example for the metric closure.

  • Clarifies the definition of the metric closure for both weighted and unweighted graphs:

    • For weighted graphs, edge weights in the closure correspond to the minimum total
      weight along any path between two nodes.
    • For unweighted graphs, all edges are assumed to have weight 1, so closure weights
      correspond to the minimum number of hops.
  • Adjusts the example to demonstrate the unweighted version using
    :func:networkx.all_pairs_shortest_path_length.

  • Notes in the docstring that the weighted version can be obtained by using
    :func:networkx.all_pairs_dijkstra_path_length instead.

The goal is to reduce potential confusion around whether "shortest path" refers to hop
count or weighted distance, and to provide a clear demonstration in the simplest (unweighted)
case, with guidance for extending to weighted graphs.

closes to #8302

@Aka2210 Aka2210 changed the title DOC: clarify metric closure for weighted vs unweighted graphs, use unweighted example DOC: add gallery example for metric_closure Oct 1, 2025

@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, thanks @Aka2210 !

I left a few suggestions which you can feel free to take or leave, not blockers!

Comment on lines +23 to +35
# Creating five nodes from index 0 to 4
G = nx.Graph()
G.add_nodes_from(range(5))
G.add_edge(0, 1)
G.add_edge(0, 4)
G.add_edge(1, 3)
G.add_edge(1, 5)
G.add_edge(2, 3)
G.add_edge(2, 4)
G.add_edge(2, 5)
G.add_edge(3, 4)
G.add_edge(3, 5)
G.add_edge(4, 5)

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.

For the sake of reducing cognitive load, perhaps a built-in example could work? E.g. nx.house_graph, which gives a very similar result.

Comment on lines +37 to +45
# Find the all-pairs shortest distance
# all_pairs_shortest_path_length returns a one-time generator,
# so wrap with dict() to materialize results for reuse.
paths = dict(nx.all_pairs_shortest_path_length(G))
for u, dist_dict in paths.items():
print(
f"From node {u}, distances to all other nodes: {dist_dict} "
"(keys = target nodes, values = shortest-path distances)"
)

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.

I'd just as soon leave this out, or at least simplify the printing with e.g. pprint:

from pprint import pprint
pprint(paths)

Again this reduces the amount of "custom" code folks have to read to help focus on the underlying example.

Another minor note: since this is an example I'd vote to be a little more precise with the variable name, i.e. path_lengths or pl instead of paths.

@Aka2210

Aka2210 commented Oct 2, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions! They all make sense — I’ll update the example later to use nx.house_graph, simplify the printing with pprint, and adjust the variable name to be more precise.

@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 @Aka2210 !

I've approved it -- and have two minor suggestions.
One is white-space, and the other is about positions for the house_graph that make it look like a house rather than using the circle layout.

Aka2210 and others added 2 commits October 3, 2025 02:24

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

Thanks @Aka2210!

Below are some nits and a question for Ross/Dan.

fig, axes = plt.subplots(1, 2, figsize=(10, 4))

pos = {0: (0, 0), 1: (1, 0), 2: (0, 1), 3: (1, 1), 4: (0.5, 2.0)}
nx.draw(G, pos, with_labels=True, ax=axes[0])

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.

A question for other reviewers: with the new display interface replacing the draw interface, should we try to use display in new examples? Or is there no plan to ever have display truly supersede draw?

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.

with the new display interface replacing the draw interface, should we try to use display in new examples? Or is there no plan to ever have display truly supersede draw?

Updating the gallery examples (separate from this PR) is a worthwhile exercise to stress-test display and ID differences in both the rendered output and user experience. draw will always be available though, so there's no requirement for new examples to use it.

@Peiffap

Peiffap commented Oct 2, 2025

Copy link
Copy Markdown
Member

You can also add a "closes #8302" keyword to the PR body to automatically link to (and close) the related issue (which I think is appropriate now that the metric_closure function has been deprecated in #8304).

@rossbar
rossbar merged commit 1c0cfec into networkx:main Oct 3, 2025
@dschult dschult added this to the 3.6 milestone Oct 3, 2025
@Aka2210
Aka2210 deleted the doc-metric-closure-gallery branch October 4, 2025 12:02
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