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
andnodeB
are given, resistance distance betweennodeA
andnodeB
. IfnodeA
ornodeB
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, ornodeA
is not inG
ornodeB
is not inG
.
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
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