effective_resistance

effective_resistance#

graph_tool.centrality.effective_resistance(g, weight=None, pairs=None)[git][source]#

Compute the effective resistance between vertices.

Parameters:
gGraph

Graph to be used. If it is directed, it is treated as undirected.

weightEdgePropertyMap (optional, default: None)

Edge conductances (inverse resistances). If not supplied, every edge has unit conductance.

pairsiterable of pairs of int (optional, default: None)

Pairs of vertices for which the resistance is computed. If not given, it is computed for all pairs.

Returns:
Rnumpy.ndarray

If pairs is None, the matrix of effective resistances between all pairs of vertices (indexed by vertex index), otherwise the resistances of the given pairs. Vertices in different components have infinite resistance.

See also

kirchhoff_index

Sum of the effective resistances

current_flow_closeness

Closeness based on effective resistances

current_flow_betweenness

Random-walk betweenness centrality

graph_tool.spectral.laplacian

Laplacian matrix

Notes

The effective resistance (or resistance distance) [klein-resistance-1993] between \(s\) and \(t\) is the voltage between them when a unit current is injected at \(s\) and extracted at \(t\), with every edge acting as a resistor of conductance \(w_e\),

\[R_{st} = (\boldsymbol e_s - \boldsymbol e_t)^\top \boldsymbol L^{+} (\boldsymbol e_s - \boldsymbol e_t),\]

where \(\boldsymbol e_s\) is the unit vector of vertex \(s\), and \(\boldsymbol L^{+}\) is the pseudo-inverse of the weighted Laplacian \(\boldsymbol L = \boldsymbol D - \boldsymbol A\), with \(A_{ij}\) the total conductance of the edges between \(i\) and \(j\) (so that parallel edges act as resistors in parallel, and self-loops are ignored) and \(\boldsymbol D\) the diagonal matrix of the weighted degrees \(d_i = \sum_jA_{ij}\). It is a metric, and it is proportional to the commute time of the random walk between the two vertices, \(2WR_{st}\), where \(W\) is the total edge weight. Two quantities that follow from it are the Kirchhoff index \(\sum_{s<t}R_{st}\), and the Kemeny constant of the random walk, \(\boldsymbol k^\top\boldsymbol R\boldsymbol k/4W\), where \(\boldsymbol k\) is the vector of weighted degrees [kemeny-finite-1960].

The Laplacian of each connected component, with one vertex removed, is factorized with a sparse LU decomposition, and inverted, which takes \(O(V^2)\) memory and at least \(O(V^2)\) time.

References

[klein-resistance-1993]

D. J. Klein and M. Randić, “Resistance distance”, Journal of Mathematical Chemistry 12, 81-95 (1993), DOI: 10.1007/BF01164627 [sci-hub, @tor]

[kemeny-finite-1960]

John G. Kemeny and J. Laurie Snell, “Finite Markov Chains”, Van Nostrand (1960).

Examples

>>> g = gt.collection.data["karate"]
>>> R = gt.effective_resistance(g)
>>> print(R[0, 33], R.sum() / 2)
0.253802298... 470.268184984...

The resistances from one vertex to all others can be visualized by drawing the network with vertex colors and sizes given by them:

>>> r = g.new_vp("double", vals=R[0, :])
>>> cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
...     "gray-blue", ["#d9d9d9", "#7dabd1"])
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=r, vcmap=cmap,
...               vertex_size=gt.prop_to_size(r, mi=5, ma=15),
...               output="effective_resistance-karate.pdf")
<...>
../_images/effective_resistance-karate.png

Effective resistances from vertex 0 to the other vertices of Zachary’s karate club network. The vertex colors, from gray to blue, and the vertex sizes increase with the value.#