Search code examples
pythongraphgraph-tool

Python, Generating Random Graphs with Graph-tool


So I'm trying to generate a random directed graph such that each vertex has 3 in-nodes and 1 outnode. But graph tool seems to be getting stuck in the deg_sampler() function.

from graph_tool.all import *

def deg_sampler():
    return 1,2
g = random_graph(1000,deg_sampler,verbose=True)

I get this error after running the code

adding vertices: 1000 of 1000 (100%)
fixing average degrees. Total degree difference: 1000^CTraceback (most recent call last):
  File "code.py", line 6, in <module>
    g = random_graph(1000,deg_sampler,verbose=True)
  File "/usr/lib/python2.7/dist-packages/graph_tool/generation/__init__.py", line 384, in random_graph
    _get_rng(), verbose, True)
  File "/usr/lib/python2.7/dist-packages/graph_tool/generation/__init__.py", line 379, in <lambda>
    sampler = lambda i: deg_sampler()
KeyboardInterrupt

Solution

  • The degree sampler function should return the in- and out-degrees of the nodes. In your implementation, each node has an in-degree of 1 and out-degree of 2. It is, of course, impossible to construct a graph with this degree sequence, since the average in- and out-degrees must identical. This is why the algorithm gets stuck in the "fixing average degrees" phase.