I write this code to generate scale free network with power law degree distribution.
import networkx as nx
import random
N = 1000
exponent = 2.2
test = [int(random.paretovariate(exponent-1)) for i in range(N)]
graph = nx.configuration_model(test)
print("number of self-loops : ", graph.number_of_selfloops())
but I face this error:
AttributeError: 'MultiGraph' object has no attribute 'number_of_selfloops'
I can't understand what is the problem and how can I fix it. Is there any other ways to generate such network with networkX? (I don't want self-loops and multi-links to be removed)
As stated in the documentation of
configuration_model
this function returns aMultiGraph
, which does not have the methodnumber_of_selfloops
, but you can still use thenx.number_of_selfloops
method, which also works forMultiGraph
(NetworkX changed how they want to have the call for such methods) or simply create a usual graph withnon_multi_graph = nx.Graph(graph)
.