Esempio n. 1
0
 def random_init(self, deg_sampler, args):
     """
     Initialize random graph with custom 
     degree sampler.
     """
     self.g = gt.random_graph(N=self.N,
                              deg_sampler=lambda: deg_sampler(**args),
                              directed=False,
                              parallel_edges=False,
                              random=True)
def generuj(n, rand_edges, num_colors):
    if rand_edges < 3: rand_edges = 3
    g = random_graph(n, (lambda: (randint(0, rand_edges), randint(1, rand_edges))), parallel_edges=False)
    g.set_directed(False)
    remove_parallel_edges(g)

    g.vertex_properties['liczba_kolorow'] = g.new_vertex_property('int')
    for v in g.vertices():
        g.vertex_properties['liczba_kolorow'][v] = randint(1, num_colors + 1)

    return g
Esempio n. 3
0
def generuj(n, rand_edges, num_colors):
    if rand_edges < 3: rand_edges = 3
    g = random_graph(n, (lambda:
                         (randint(0, rand_edges), randint(1, rand_edges))),
                     parallel_edges=False)
    g.set_directed(False)
    remove_parallel_edges(g)

    g.vertex_properties['liczba_kolorow'] = g.new_vertex_property('int')
    for v in g.vertices():
        g.vertex_properties['liczba_kolorow'][v] = randint(1, num_colors + 1)

    return g
def work(verts, deg):
  if ~hasattr(options, 'filename'):
    options.filename = str(verts) + '_' + str(deg)
  print(options.filename)
  g = gt.random_graph(int(verts), lambda: sample_k(int(deg)), model="probabilistic",
             vertex_corr=lambda i, k: 1.0 / (1 + abs(i - k)), directed=False,
             n_iter=100)
  dotfile = os.getcwd() + '/'+options.filename+'.dot'
  imgfile = os.getcwd() + '/'+options.filename+'.'+options.output_format
  g.save(dotfile, 'dot')
  subprocess.Popen([options.layout, "-T", options.output_format], stdin=open(dotfile, 'r'), stdout=open(imgfile, 'a')).communicate()
  if options.show:
    subprocess.Popen(['display', imgfile])
Esempio n. 5
0
import graph_tool
import scipy
from numpy.random import poisson,randint
import time
import math

start = time.time()
def corr(a,b):                                                                                                                                         
    if a==b:
        return 0.999
    else:
        return 0.001
        
g, bm = gt.random_graph(100000, 
                        lambda: poisson(10), 
                        directed=False,
                        block_membership=lambda: randint(50),
                        vertex_corr=corr)
                        
                        
print(g.num_vertices(), g.num_edges())
pagerank = graph_tool.centrality.betweenness(g)


diff = time.time() - start

nodes = g.num_vertices()
edges = g.num_edges()

filename = 'betweenness_undirected_%dkN_%dkE' % (nodes/1000,edges/1000)
with open(filename+'.txt','w+') as f:
block_size_heterogeneity = 1  # 3; # larger means the block sizes are more uneven
block_distribution = np.random.dirichlet(
    np.ones(num_blocks) * 10 / block_size_heterogeneity, 1)[0]
print('Block distribution: {}'.format(block_distribution))

# draw block membership for each node
block_membership_vector = np.where(
    np.random.multinomial(n=1, size=N, pvals=block_distribution))[1]
true_partition = block_membership_vector

# generate the graph
if (float(gt.__version__[0:4]) >= 2.20
    ):  # specify inter-block strength through edge_probs in later versions
    g_sample, block_membership = gt.random_graph(N, lambda: degree_distribution_function(rv_indegree, rv_outdegree), \
                                                 directed=True, model="blockmodel",
                                                 block_membership=block_membership_vector,
                                                 edge_probs=inter_block_strength, n_iter=10, verbose=False)
else:  # specify inter-block strength through vertex_corr in earlier versions
    g_sample, block_membership = gt.random_graph(N, lambda: degree_distribution_function(rv_indegree, rv_outdegree), \
                                                 directed=True, model="blockmodel",
                                                 block_membership=block_membership_vector,
                                                 vertex_corr=inter_block_strength, n_iter=10, verbose=False)

# remove (1-density) percent of the edges
edge_filter = g_sample.new_edge_property('bool')
edge_filter.a = stats.bernoulli.rvs(density, size=edge_filter.a.shape)
g_sample.set_edge_filter(edge_filter)
g_sample.purge_edges()

# store the nodal block memberships in a vertex property
g_sample.vertex_properties["block_membership"] = block_membership
import numpy as np

# Number of vertices
nb_vertices = 750

# Expected average degree
expected_average_degree = 5


# Sampler for the degree of the vertices
def sample_deg():
    return np.random.poisson(expected_average_degree)


# Generates a graph.
g = gt.random_graph(nb_vertices, sample_deg, directed=False)
g = gt.extract_largest_component(g, prune=True)

# Saves the graph as an edgelist.
with open("validation_edgelist.dat", "wb") as f:
    np.savetxt(f,
               g.get_edges(),
               fmt="%15s",
               delimiter=" ",
               header=" SourceVertex    TargetVertex")

with open("validation_raw_results.dat", "w") as f:

    # g.set_fast_edge_removal(True)
    f.write(
        '#   nb_vertices        nb_edges        size_1st        size_2nd         nb_comp\n'
Esempio n. 8
0
import topology_utils as tu
import graph_tool.all as gt

g=gt.random_graph(1000000, lambda: (3, 3))
g2=tu.compute_transitive_closure(g)

print(g2.num_edges(), g2.num_vertices(), g.num_edges())
directed = True

spacing = 15
print 'nodes | iteration | deg sampler | bias'.rjust(20), '|', 'eigenvec status'.rjust(
    spacing), '|', 'compare status'.rjust(spacing), '| abstol', '| maxdiff'
print '#' * 120
for iteration in range(iterations):
    deg_sample_type = random.sample(deg_samplers, 1)[0]
    if deg_sample_type == 'unif':
        helper = lambda: int(random.random() * num_nodes)
    elif deg_sample_type == 'exp':
        helper = lambda: np.random.exponential(scale=0.06) * num_nodes
    deg_sample = lambda: (helper(), helper())
    # print deg_sample()
    while True:
        g = gt.random_graph(num_nodes, deg_sampler=deg_sample, directed=directed)
        use_weights = random.random() > 0.5
        if use_weights:
            weights = g.new_vertex_property('float')
            weights.a = np.array(
                [(1. + random.random() * 9) if i else 1. for i in (np.random.random(size=g.num_vertices()) > 0.5)])
        else:
            weights = None

        lcc = gt.label_largest_component(g)
        g.set_vertex_filter(lcc)
        g.purge_vertices()
        g.purge_edges()
        g.clear_filters()
        if g.num_vertices() > 5 and check_aperiodic(g):
            break