resistance_distance#

resistance_distance(G, nodeA=None, nodeB=None, weight=None, invert_weight=True)[source]#

Returns the resistance distance between pairs of nodes in graph G.

The resistance distance between two nodes of a graph is akin to treating the graph as a grid of resistors with a resistance equal to the provided weight [1], [2].

If weight is not provided, then a weight of 1 is used for all edges.

If two nodes are the same, the resistance distance is zero.

Parameters:
GNetworkX graph

A graph

nodeAnode or None, optional (default=None)

A node within graph G. If None, compute resistance distance using all nodes as source nodes.

nodeBnode or None, optional (default=None)

A node within graph G. If None, compute resistance distance using all nodes as target nodes.

weightstring or None, optional (default=None)

The edge data key used to compute the resistance distance. If None, then each edge has weight 1.

invert_weightboolean (default=True)

Proper calculation of resistance distance requires building the Laplacian matrix with the reciprocal of the weight. Not required if the weight is already inverted. Weight cannot be zero.

Returns:
rddict or float

If nodeA and nodeB are given, resistance distance between nodeA and nodeB. If nodeA or nodeB is unspecified (the default), a dictionary of nodes with resistance distances as the value.

Raises:
NetworkXNotImplemented

If G is a directed graph.

NetworkXError

If G is not connected, or contains no nodes, or nodeA is not in G or nodeB is not in G.

Notes

The implementation is based on Theorem A in [2]. Self-loops are ignored. Multi-edges are contracted in one edge with weight equal to the harmonic sum of the weights.

References

[1]

Wikipedia “Resistance distance.” https://en.wikipedia.org/wiki/Resistance_distance

[2] (1,2)

D. J. Klein and M. Randic. Resistance distance. J. of Math. Chem. 12:81-95, 1993.

Examples

>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> round(nx.resistance_distance(G, 1, 3), 10)
0.625