I am generating a network of size N with a power-law degree distribution using the so-called configuration_model algorithm. The resulting graph allows multiple edges, I have to count them but I cannot figure out how. Here is a possible solution:
import networkx as nx
import numpy as np
from networkx.utils import (powerlaw_sequence, create_degree_sequence)
deg_sequence = create_degree_sequence(num, powerlaw_sequence, exponent=2.2)
graph=nx.configuration_model(deg_sequence)
num_par = sum(len(graph[node][neigh]) for node in graph for neigh in graph.neighbors_iter(node)) // 2
print ('Graph has %d multi-links' %num_par)
self_loops=len(graph.selfloop_edges())
print ('Graph has %d self-loops' %self_loops)
edges=len(graph.edges())
print edges
I borrowed the sixth line of code somewhere on the internet but frankly I can't quite understand how it works. In particular I can't understand what is (graph[node][neigh]) of which it calculates the length. To me, it does not seem like a list either, but I am sure it's me not getting the idea here. Btw as a result of this code I get a very high percentage of multiple edges, more than 98% of the total number of links, and it does not seem very good to me. Is this way of calculating parallel edges correct? If so, would you explain me how it works? Do you know any other way of doing it?
