Skip to content
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

Merged
merged 25 commits into from Dec 9, 2023

Conversation

ghost
Copy link

@ghost ghost commented Sep 13, 2023

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.

@ghost
Copy link
Author

ghost commented Sep 27, 2023

Some benchmarks for comparison. Memory is measured using tracemalloc and time using the time module. Memory shows current vs maximum memory used and is averaged over multiple runs.

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

Kirchhoff index v1: 1328.721854594229
Time: 0.153 s
Memory (5420029, 6250704)

Kirchhoff index v2: 1328.7218545942321
Time: 0.012 s
Memory (5418027, 5542378)

Graph 2: ER graph with N = 1,000 nodes and link-connectivity probability 0.05

Kirchhoff index v1: 21031.780201753903
Time: 9.48 s
Memory (13466175, 86475722)

Kirchhoff index v2: 21031.78020175406
Time: 0.273 s
Memory (13506311, 25813676)
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()

@ghost
Copy link
Author

ghost commented Oct 2, 2023

@dschult Sorry for the ping, I didn't see a better way of asking for a review...

@ghost
Copy link
Author

ghost commented Oct 23, 2023

There are conflicts with the current branch, but I'm unsure how I should get rid of them without starting a new branch...

@dschult
Copy link
Member

dschult commented Oct 23, 2023

I have tried to fix the conflicts (another new distance measure function has been merged into main in the same lines of the file that you put this new function. My pushing these changes means that your local repo will need to pull the changes down from your github account before you can make more changes.

@dschult
Copy link
Member

dschult commented Oct 23, 2023

I would much prefer the name effective_graph_resistance over kirchhoff_index. There are so many things named after Kirchhoff it is hard to know what that refers to and is harder to pick up in a search based on the term. (I'm also biased against naming ideas after the creator instead of using an expression for what it means. But I know I'm biased that way so push back if you prefer kirchhoff_index and I'll likely back off.)

Thanks for this!

@ghost
Copy link
Author

ghost commented Oct 24, 2023

@dschult
Thanks a lot for fixing the conflicts, all should be good now!
As you suggested, I have renamed the function to effective_graph_resistance,
but have kept Kirchhoff index in the documentation (so that it can be found while searching).
Looking forward to the rest of the review and thanks for your time!

@ghost
Copy link
Author

ghost commented Dec 6, 2023

@dschult If you have any time, your feedback is appreciated :)

Copy link
Member

@MridulS MridulS left a 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!

Copy link
Member

@dschult dschult left a 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 :)

networkx/algorithms/distance_measures.py Show resolved Hide resolved
@ghost ghost requested a review from dschult December 8, 2023 08:56
Copy link
Member

@dschult dschult left a 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!

@dschult dschult merged commit 9cc8b42 into networkx:main Dec 9, 2023
39 checks passed
@jarrodmillman jarrodmillman added this to the 3.3 milestone Dec 9, 2023
@ghost ghost deleted the Add-Kirchhoff-index branch December 12, 2023 10:35
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants