python-graphblas / graphblas-algorithms

Graph algorithms written in GraphBLAS
Apache License 2.0
71 stars 4 forks source link

Degree Centrality Calculation Differs From Networkx #96

Open davidsilveiro opened 3 months ago

davidsilveiro commented 3 months ago

A few days ago a user posted an issue describing calculation's from NetworkX not matching against Graphblas's implementation on SO. Thought i'd pass on the details along as another kind gent has described where he believes the issue may be and I'm unable to verify if they're on the right track.

Original post: https://stackoverflow.com/questions/78383991/why-does-graphblas-return-different-results-as-networkx/78402677#78402677

import networkx as nx
import graphblas_algorithms as ga

G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3)])
nx.in_degree_centrality(G)
{0: 0.0, 1: 0.3333333333333333, 2: 0.6666666666666666, 3: 0.6666666666666666}

GG = ga.Graph.from_networkx(G)
ga.in_degree_centrality(GG)
0   1     2 3
1.0 0.666667 
eriknw commented 3 months ago

Thanks for the report and sharing the issue on StackOverflow @davidsilveiro!

In the example above G is directed (as is required for in_degree_centrality), and GG is undirected. So, to fix the example, change GG = ga.Graph.from_networkx(G) to GG = ga.DiGraph.from_networkx(G):

- GG = ga.Graph.from_networkx(G)
+ GG = ga.DiGraph.from_networkx(G)

I would also suggest considering using the networkx API for dispatching:

nx.in_degree_centrality(GG)

or

nx.in_degree_centrality(G, backend="graphblas")

The latter converts the input graph G to a graphblas graph. This conversion can be cached in NetworkX 3.3 via first setting a configuration:

nx.config.cache_converted_graphs = True

Finally, ga.in_degree_centrality should probably raise if the input is an undirected graph, so this is a valid bug report. Using ga.nxapi.in_degree_centrality(GG) raises as expected if GG is undirected. I think having functions appear in both ga and ga.nxapi namespaces may be a little unclear, and it would be better if instead we moved the networkx backend API to e.g. nx_graphblas.