Repository navigation
DOC: add gallery example for metric_closure - #8306
Conversation
| # 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) |
There was a problem hiding this comment.
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.
| # 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)" | ||
| ) |
There was a problem hiding this comment.
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.
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
|
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. |
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
| 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]) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Co-authored-by: Gilles Peiffer <[email protected]>
Co-authored-by: Gilles Peiffer <[email protected]>
This PR updates the new gallery example for the metric closure.
Clarifies the definition of the metric closure for both weighted and unweighted graphs:
weight along any path between two nodes.
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_lengthinstead.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