Пример #1
0
def random_powerlaw_tree_sequence(n, gamma=3, seed=None, tries=100):
    # get trial sequence
    z = powerlaw_sequence(n, exponent=gamma, seed=seed)
    # round to integer values in the range [0,n]
    zseq = [min(n, max(int(round(s)), 0)) for s in z]

    # another sequence to swap values from
    z = powerlaw_sequence(tries, exponent=gamma, seed=seed)
    # round to integer values in the range [0,n]
    swap = [min(n, max(int(round(s)), 0)) for s in z]

    for deg in swap:
        # If this degree sequence can be the degree sequence of a tree, return
        # it. It can be a tree if the number of edges is one fewer than the
        # number of nodes, or in other words, `n - sum(zseq) / 2 == 1`. We
        # use an equivalent condition below that avoids floating point
        # operations.
        if 2 * n - sum(zseq) == 2:
            return zseq
        index = seed.randint(0, n - 1)
        zseq[index] = swap.pop()

    raise nx.NetworkXError(
        "Exceeded max (%d) attempts for a valid tree" " sequence." % tries
    )
Пример #2
0
def random_powerlaw_tree_sequence(n, gamma=3, seed=None, tries=100):
    """Returns a degree sequence for a tree with a power law distribution.

    Parameters
    ----------
    n : int,
        The number of nodes.
    gamma : float
        Exponent of the power law.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    tries : int
        Number of attempts to adjust the sequence to make it a tree.

    Raises
    ------
    NetworkXError
        If no valid sequence is found within the maximum number of
        attempts.

    Notes
    -----
    A trial power law degree sequence is chosen and then elements are
    swapped with new elements from a power law distribution until
    the sequence makes a tree (by checking, for example, that the number of
    edges is one smaller than the number of nodes).

    """
    # get trial sequence
    z = powerlaw_sequence(n, exponent=gamma, seed=seed)
    # round to integer values in the range [0,n]
    zseq = [min(n, max(int(round(s)), 0)) for s in z]

    # another sequence to swap values from
    z = powerlaw_sequence(tries, exponent=gamma, seed=seed)
    # round to integer values in the range [0,n]
    swap = [min(n, max(int(round(s)), 0)) for s in z]

    for deg in swap:
        # If this degree sequence can be the degree sequence of a tree, return
        # it. It can be a tree if the number of edges is one fewer than the
        # number of nodes, or in other words, `n - sum(zseq) / 2 == 1`. We
        # use an equivalent condition below that avoids floating point
        # operations.
        if 2 * n - sum(zseq) == 2:
            return zseq
        index = seed.randint(0, n - 1)
        zseq[index] = swap.pop()

    raise nx.NetworkXError("Exceeded max (%d) attempts for a valid tree"
                           " sequence." % tries)
Пример #3
0
def generate_DAG(p, m=4, prob=0., type_='config_model'):
    if type_ == 'config_model':
        z = [int(e) for e in powerlaw_sequence(p)]
        if np.sum(z) % 2 != 0:
            z[0] += 1
        G = nx.configuration_model(z)
    elif type_ == 'barabasi':
        G = nx.barabasi_albert_graph(p, m)
    elif type_ == 'small_world':
        G = nx.watts_strogatz_graph(p, m, prob)
    elif type_ == 'chain':
        source_node = int(np.ceil(p / 2)) - 1
        arcs = {(i + 1, i)
                for i in range(source_node)
                } | {(i, i + 1)
                     for i in range(source_node, p - 1)}
        print(source_node, arcs)
        return cd.DAG(nodes=set(range(p)), arcs=arcs)
    elif type_ == 'chain_one_direction':
        return cd.DAG(nodes=set(range(p)),
                      arcs={(i, i + 1)
                            for i in range(p - 1)})
    else:
        raise Exception('Not a graph type')
    G = nx.Graph(G)
    dag = cd.DAG(nodes=set(range(p)))
    for i, j in G.edges:
        if i != j:
            dag.add_arc(*sorted((i, j)))
    return dag
Пример #4
0
def powerLawArray(n, beta, mean_degree):
    powerLawSequence = nu.powerlaw_sequence(n, beta)
    powerLawArr = np.array(powerLawSequence)
    initialSum = np.sum(powerLawArr)
    expectedSum = mean_degree * n
    for i in xrange(n):
        powerLawArr[i] = powerLawArr[i] * 1.0 * expectedSum / initialSum
    return powerLawArr
Пример #5
0
def test_random_number_distribution():
    # smoke test only
    z = powerlaw_sequence(20, exponent=2.5)
    z = discrete_sequence(20, distribution=[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3])
Пример #6
0
# random number generator
from datetime import datetime
random.seed(datetime.now())

# %% [markdown]
# ## Create Scale-free graph

# %%
num_nodes = 1000  # number of nodes
exp = 2.2  # exponent of power law distribution

# random power law degree sequence
sequence = []
while len(sequence) < num_nodes:
    nextval = int(powerlaw_sequence(1, exp)[0])
    # we throw away nodes with degree more than num_nodes/2
    # also we set min degree to 2. because this methods creates too many parallel edges
    # that we need to remove
    if 2 <= nextval <= (num_nodes // 2):
        sequence.append(nextval)

# sum of degrees must even
if sum(sequence) % 2 == 1:
    sequence[0] += 1

# create multigraph
graph = nx.configuration_model(sequence)
# remove parallel edges
graph = nx.Graph(graph)
# remove self loops
Пример #7
0
def pesudo_power_sequence_alt(n, gamma):
    unround_sequence = powerlaw_sequence(n, exponent=gamma)
    current_degree_sequence=[min(n, max( int(round(unround_sequence)),0)) for s in unround_sequence]
    return sorted(current_degree_sequence)
Пример #8
0
def test_degree_sequences():
    seq = powerlaw_sequence(10, seed=1)
    seq = powerlaw_sequence(10)
    assert len(seq) == 10
def test_degree_sequences():
    seq = powerlaw_sequence(10)
    assert_equal(len(seq), 10)
from networkx.utils import powerlaw_sequence
import sys, math

nNodos = int(sys.argv[1])
nComunidades = int(nNodos * 0.01)
exp = float(sys.argv[2])

sequence = powerlaw_sequence(nComunidades, exponent = exp)
sequence = map(lambda x : int(math.fmod(4 + int(x), nNodos)), sequence)

for i in sequence:
	print i