1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Efficiently converting edgelist to distance matrix in Python

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    R user learning Python here. I have an undirected unweighted edgelist and I want a distance matrix, showing the shortest path between all my nodes. There are about 30000 nodes and 40000 edges, and my goal is a 30000x30000 matrix where entry ij shows distance between node i and node j.

    On my i7 16GB RAM PC I can achieve the result in 59 seconds in R 4.3.2

    library(igraph)
    g <- graph_from_data_frame(el, directed = F)
    dm <- distances(g)


    But neither attempt 1 nor 2 below finished after 10 minutes of running in Python 3.12.4 (Spyder 5.5.1):

    import networkx as nx
    import pandas as pd

    g = nx.from_pandas_edgelist(el, 'mid', 'cid')

    #Attempt 1
    dm = nx.floyd_warshall_numpy(g)

    #Attempt 2
    gdict= dict(nx.all_pairs_shortest_path_length(g))
    dm = pd.Dataframe(gdict)


    Where el is a pandas Dataframe and mid and cid are uint16 columns.

    Am I doing something wrong here, or is Python just very slow in shortest path algorithms compared to R? Alternatively, how can I create a distance matrix from an edgelist in Python efficiently?

    My data looks something like this:

    data = {
    'mid': [1, 1, 2, 2, 3, 4],
    'cid': [2, 3, 3, 4, 5, 5]
    }
    el = pd.DataFrame(data)

    Continue reading...

Compartilhe esta Página