New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Kirchhoff index / Effective graph resistance #6926
Conversation
Add the computation of the Kirchhoff index to networkx
Some benchmarks for comparison. Memory is measured using Both memory and speed are greatly improved for graphs with N=1000 nodes. Graph 1: ER graph with N = 100 nodes and link-connectivity probability 0.1
Graph 2: ER graph with N = 1,000 nodes and link-connectivity probability 0.05
Script used:import networkx as nx
import numpy as np
import time
import tracemalloc
def resistance_distance2(G, nodeA=None, nodeB=None, weight=None, invert_weight=True):
"""Returns the resistance distance between every pair of nodes on 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
----------
G : NetworkX graph
A graph
nodeA : node or None, optional (default=None)
A node within graph G.
If None, compute resistance distance using all nodes as source nodes.
nodeB : node or None, optional (default=None)
A node within graph G.
If None, compute resistance distance using all nodes as target nodes.
weight : string or None, optional (default=None)
The edge data key used to compute the resistance distance.
If None, then each edge has weight 1.
invert_weight : boolean (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
-------
rd : float (if `nodeA` and `nodeB` are given)
Resistance distance between `nodeA` and `nodeB`.
dictionary (if `nodeA` or `nodeB` is unspecified)
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`.
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
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] D. J. Klein and M. Randic.
Resistance distance.
J. of Math. Chem. 12:81-95, 1993.
"""
import numpy as np
if len(G) == 0:
raise nx.NetworkXError("Graph G must contain at least one node.")
if not nx.is_connected(G):
raise nx.NetworkXError("Graph G must be strongly connected.")
if nodeA is not None and nodeA not in G:
raise nx.NetworkXError("Node A is not in graph G.")
if nodeB is not None and nodeB not in G:
raise nx.NetworkXError("Node B is not in graph G.")
G = G.copy()
node_list = list(G)
# Invert weights
if invert_weight and weight is not None:
if G.is_multigraph():
for u, v, k, d in G.edges(keys=True, data=True):
d[weight] = 1 / d[weight]
else:
for u, v, d in G.edges(data=True):
d[weight] = 1 / d[weight]
# Replace with collapsing topology or approximated zero?
# Compute resistance distance using the Pseudo-inverse of the Laplacian
# Self-loops are ignored
L = nx.laplacian_matrix(G, weight=weight).todense()
Linv = np.linalg.pinv(L, hermitian=True)
# Return relevant distances
if nodeA is not None and nodeB is not None:
i = node_list.index(nodeA)
j = node_list.index(nodeB)
return Linv[i, i] + Linv[j, j] - Linv[i, j] - Linv[j, i]
elif nodeA is not None:
i = node_list.index(nodeA)
d = {}
for n in G:
j = node_list.index(n)
d[n] = Linv[i, i] + Linv[j, j] - Linv[i, j] - Linv[j, i]
return d
elif nodeB is not None:
j = node_list.index(nodeB)
d = {}
for n in G:
i = node_list.index(n)
d[n] = Linv[i, i] + Linv[j, j] - Linv[i, j] - Linv[j, i]
return d
else:
d = {}
for n in G:
i = node_list.index(n)
d[n] = {}
for n2 in G:
j = node_list.index(n2)
d[n][n2] = Linv[i, i] + Linv[j, j] - Linv[i, j] - Linv[j, i]
return d
def GetKirchhoffIndex1(G):
rd = resistance_distance2(G)
return sum([rd[key][key2] for key in rd for key2 in rd[key]])/2
def GetKirchhoffIndex2(G, weight=None, invert_weight=True):
"""Returns the Kirchhoff index of G.
Also known as the Effective graph resistance.
The Kirchhoff index is defined as the sum
of the resistance distance of every node pair in G [1]_.
If weight is not provided, then a weight of 1 is used for all edges.
The Kirchhoff index of a disconnected graph is infinite.
Parameters
----------
G : NetworkX graph
A graph
weight : string or None, optional (default=None)
The edge data key used to compute the resistance distance.
If None, then each edge has weight 1.
invert_weight : boolean (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
-------
Kf : float
The Kirchhoff index of `G`.
Raises
-------
NetworkXNotImplemented
If `G` is a directed graph.
NetworkXError
If `G` does not contain any nodes.
Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> round(nx.kirchhoff_index(G), 10)
10.25
Notes
-----
The implementation is based on Theorem 2.2 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] Wolfram
"Kirchhoff Index."
https://mathworld.wolfram.com/KirchhoffIndex.html
.. [2] W. Ellens, F. M. Spieksma, P. Van Mieghem, A. Jamakovic, R. E. Kooij.
Effective graph resistance.
Lin. Alg. Appl. 435:2491-2506, 2011.
"""
import numpy as np
if len(G) == 0:
raise nx.NetworkXError("Graph G must contain at least one node.")
# Disconnected graphs have infinite Kirchhoff index
if not nx.is_connected(G):
return np.inf
# Invert weights
G = G.copy()
if invert_weight and weight is not None:
if G.is_multigraph():
for u, v, k, d in G.edges(keys=True, data=True):
d[weight] = 1 / d[weight]
else:
for u, v, d in G.edges(data=True):
d[weight] = 1 / d[weight]
# Get Laplacian eigenvalues
mu = np.sort(nx.laplacian_spectrum(G, weight=weight))
# Compute Kirchhoff index based on spectrum of the Laplacian
# Self-loops are ignored
return np.sum(1 / mu[1:]) * G.number_of_nodes()
# Main
N = 1000 # nodes
M = 1 # number of repeated computations
G = nx.erdos_renyi_graph(N, 0.05) # graph
tracemalloc.start()
print('Memory', tracemalloc.get_traced_memory())
t0 = time.time()
for i in range(M):
Kf1 = GetKirchhoffIndex1(G)
print('Kirchhoff index v1:', Kf1)
print('Time:', round(time.time()-t0, 3), 's')
print('Memory', tracemalloc.get_traced_memory())
t0 = time.time()
for i in range(M):
Kf2 = GetKirchhoffIndex2(G)
print('Kirchhoff index v2:', Kf2)
print('Time:', round(time.time()-t0, 3), 's')
print('Memory', tracemalloc.get_traced_memory())
tracemalloc.stop() |
@dschult Sorry for the ping, I didn't see a better way of asking for a review... |
There are conflicts with the current branch, but I'm unsure how I should get rid of them without starting a new branch... |
Add the computation of the Kirchhoff index to networkx
8eedd21
to
6f3f238
Compare
I have tried to fix the conflicts (another new distance measure function has been merged into |
I would much prefer the name Thanks for this! |
@dschult |
@dschult If you have any time, your feedback is appreciated :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I would rearrange the test suite a bit for distance_measures but that could be a new PR.
Thanks @peijenburg!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks quite good. I only have one very nitty comment :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me!
Thanks!
* Add Kirchhoff index Add the computation of the Kirchhoff index to networkx * minor fixes * scipy not necessary * scipy not necessary * style fixes * small doc change * change digraph, add test * vectorise final computation * style fix * minor cleanup tests * Add Kirchhoff index Add the computation of the Kirchhoff index to networkx * minor fixes * scipy not necessary * scipy not necessary * style fixes * small doc change * change digraph, add test * vectorise final computation * style fix * minor cleanup tests * fix * change name * remove return var
The Kirchhoff index (also known as the effective graph resistance) is defined as the sum over the resistance distance of every pair of nodes.
The output of this function is equivalent to using resistance_distance and summing over all possible node pairs. However, this function is (much) faster on large graphs (1,000+ nodes) than using resistance_distance.
Implements #6847.