summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2021-09-14 22:41:33 -0500
committerGitHub <noreply@github.com>2021-09-14 23:41:33 -0400
commitd6be5c440728cf56106cf06668090504406bbde5 (patch)
treeda9ab9c8c98727bb9961049c772f56ff658ffe80
parentd7fc10144c624c4ae7fc1c3bc9df7d7a3c563984 (diff)
downloadnetworkx-d6be5c440728cf56106cf06668090504406bbde5.tar.gz
Document `geometric_edges` and add it to main namespace (#5045)
* Expose geometric_edges from top-level namespace. * Beef up geometric_edges docstring. * Add geometric_edges to reference guide.
-rw-r--r--doc/reference/generators.rst9
-rw-r--r--networkx/generators/geometric.py49
2 files changed, 51 insertions, 7 deletions
diff --git a/doc/reference/generators.rst b/doc/reference/generators.rst
index e05e89f7..0985a0b4 100644
--- a/doc/reference/generators.rst
+++ b/doc/reference/generators.rst
@@ -182,12 +182,13 @@ Geometric
.. autosummary::
:toctree: generated/
- random_geometric_graph
- soft_random_geometric_graph
+ geometric_edges
geographical_threshold_graph
- waxman_graph
navigable_small_world_graph
+ random_geometric_graph
+ soft_random_geometric_graph
thresholded_random_geometric_graph
+ waxman_graph
Line Graph
----------
@@ -358,4 +359,4 @@ Sudoku
.. autosummary::
:toctree: generated/
- sudoku_graph \ No newline at end of file
+ sudoku_graph
diff --git a/networkx/generators/geometric.py b/networkx/generators/geometric.py
index d5e83c57..d843a9df 100644
--- a/networkx/generators/geometric.py
+++ b/networkx/generators/geometric.py
@@ -10,12 +10,13 @@ import networkx as nx
from networkx.utils import nodes_or_number, py_random_state
__all__ = [
+ "geometric_edges",
"geographical_threshold_graph",
- "waxman_graph",
"navigable_small_world_graph",
"random_geometric_graph",
"soft_random_geometric_graph",
"thresholded_random_geometric_graph",
+ "waxman_graph",
]
@@ -30,10 +31,52 @@ def euclidean(x, y):
def geometric_edges(G, radius, p):
- """Returns edge list of node pairs within `radius` of each other
+ """Returns edge list of node pairs within `radius` of each other.
+ Parameters
+ ----------
+ G : networkx graph
+ The graph from which to generate the edge list. The nodes in `G` should
+ have an attribute ``pos`` corresponding to the node position, which is
+ used to compute the distance to other nodes.
+ radius : scalar
+ The distance threshold. Edges are included in the edge list if the
+ distance between the two nodes is less than `radius`.
+ p : scalar
+ The `Minkowski distance metric
+ <https://en.wikipedia.org/wiki/Minkowski_distance>`_ use to compute
+ distances.
+
+ Returns
+ -------
+ edges : list
+ List of edges whose distances are less than `radius`
+
+ Notes
+ -----
Radius uses Minkowski distance metric `p`.
- If scipy available, use scipy cKDTree to speed computation.
+ If scipy is available, `scipy.spatial.cKDTree` is used to speed computation.
+
+ Examples
+ --------
+ Create a graph with nodes that have a "pos" attribute representing 2D
+ coordinates.
+
+ >>> G = nx.Graph()
+ >>> G.add_nodes_from([
+ ... (0, {"pos": (0, 0)}),
+ ... (1, {"pos": (3, 0)}),
+ ... (2, {"pos": (8, 0)}),
+ ... ])
+ >>> p = 2 # Euclidean distance
+ >>> nx.geometric_edges(G, radius=1, p=p)
+ []
+ >>> nx.geometric_edges(G, radius=4, p=p)
+ [(0, 1)]
+ >>> nx.geometric_edges(G, radius=6, p=p)
+ [(0, 1), (1, 2)]
+ >>> nx.geometric_edges(G, radius=9, p=p)
+ [(0, 1), (0, 2), (1, 2)]
"""
nodes_pos = G.nodes(data="pos")
try: