diameter#
- diameter(G, e=None, usebounds=False, weight=None)[source]#
Returns the diameter of the graph G.
The diameter is the maximum eccentricity.
- Parameters:
- GNetworkX graph
A graph
- eeccentricity dictionary, optional
A precomputed dictionary of eccentricities.
- useboundsbool, optional
If
True
, use extrema bounding (see Notes) when computing the diameter for undirected graphs. Extrema bounding may accelerate the distance calculation for some graphs.usebounds
is ignored ifG
is directed or ife
is notNone
. Default isFalse
.- weightstring, function, or None
If this is a string, then edge weights will be accessed via the edge attribute with this key (that is, the weight of the edge joining
u
tov
will beG.edges[u, v][weight]
). If no such edge attribute exists, the weight of the edge is assumed to be one.If this is a function, the weight of an edge is the value returned by the function. The function must accept exactly three positional arguments: the two endpoints of an edge and the dictionary of edge attributes for that edge. The function must return a number.
If this is None, every edge has weight/distance/cost 1.
Weights stored as floating point values can lead to small round-off errors in distances. Use integer weights to avoid this.
Weights should be positive, since they are distances.
- Returns:
- dinteger
Diameter of graph
See also
Notes
When
usebounds=True
, the computation makes use of smart lower and upper bounds and is often linear in the number of nodes, rather than quadratic (except for some border cases such as complete graphs or circle shaped-graphs).Examples
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)]) >>> nx.diameter(G) 3