Returns the Wiener index of the given graph.
The Wiener index of a graph is the sum of the shortest-path distances between each pair of reachable nodes. For pairs of nodes in undirected graphs, only one orientation of the pair is counted.
G (NetworkX graph)
weight (object) – The edge attribute to use as distance when computing shortest-path distances. This is passed directly to the
The Wiener index of the graph
- Return type
NetworkXError – If the graph
Gis not connected.
If a pair of nodes is not reachable, the distance is assumed to be infinity. This means that for graphs that are not strongly-connected, this function returns
The Wiener index is not usually defined for directed graphs, however this function uses the natural generalization of the Wiener index to directed graphs.
The Wiener index of the (unweighted) complete graph on n nodes equals the number of pairs of the n nodes, since each pair of nodes is at distance one:
>>> import networkx as nx >>> n = 10 >>> G = nx.complete_graph(n) >>> nx.wiener_index(G) == n * (n - 1) / 2 True
Graphs that are not strongly-connected have infinite Wiener index:
>>> G = nx.empty_graph(2) >>> nx.wiener_index(G) inf