Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to run pagerank algorithm on NetworkX's MultiGraph?

I'm working on a graph with multiple edges between the same nodes (edges are having different values). In order to model this graph I need to use MultiGraph instead of normal Graph. Unfortunately, it's not possible to run PageRank algo on it.

Any workarounds known ?

NetworkXNotImplemented: not implemented for multigraph type

like image 327
grechut Avatar asked Jan 08 '23 21:01

grechut


1 Answers

You could create make a graph without parallel edges and then run pagerank. Here is an example of summing edge weights of parallel edges to make a simple graph:

import networkx as nx
G = nx.MultiGraph()
G.add_edge(1,2,weight=7)
G.add_edge(1,2,weight=10)
G.add_edge(2,3,weight=9)

# make new graph with sum of weights on each edge
H = nx.Graph()
for u,v,d in G.edges(data=True):
    w = d['weight']
    if H.has_edge(u,v):
        H[u][v]['weight'] += w
    else:
        H.add_edge(u,v,weight=w)

print H.edges(data=True)
#[(1, 2, {'weight': 17}), (2, 3, {'weight': 9})]
print nx.pagerank(H)
#{1: 0.32037465332634, 2: 0.4864858243244209, 3: 0.1931395223492388}
like image 139
Aric Avatar answered Jan 16 '23 20:01

Aric