本文整理汇总了Python中networkx.configuration_model方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.configuration_model方法的具体用法?Python networkx.configuration_model怎么用?Python networkx.configuration_model使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.configuration_model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_network
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def generate_network(Pk, N, ntries = 100):
r'''Generates an N-node random network whose degree distribution is given by Pk'''
counter = 0
while counter< ntries:
counter += 1
ks = []
for ctr in range(N):
ks.append(Pk())
if sum(ks)%2 == 0:
break
if sum(ks)%2 ==1:
raise EoN.EoNError("cannot generate even degree sum")
G = nx.configuration_model(ks)
return G
#An erdos-renyi network has a Poisson degree distribution.
示例2: test_SIS_simulations
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_SIS_simulations(self):
print("test_SIS_simulations")
plt.clf()
tau = 0.1
gamma = 0.3
G = nx.configuration_model([1, 5, 10] * 100000)
G = nx.Graph(G)
G.remove_edges_from(G.selfloop_edges())
N = G.order()
initial_size = 5000
for counter in range(10):
print('fast_SIS')
t, S, I = EoN.fast_SIS(G, tau, gamma, initial_infecteds=range(initial_size), tmax=20)
plt.plot(t, S, '-.', color='b', alpha=0.3)
plt.plot(t, I, '-.', color='b', alpha=0.3)
print('Gillespie_SIS')
t, S, I = EoN.Gillespie_SIS(G, tau, gamma, initial_infecteds=range(initial_size), tmax=20)
plt.plot(t, S, '--', color='r', alpha=0.3)
plt.plot(t, I, '--', color='r', alpha=0.3)
plt.title('curves should overlie to show event driven and gillespie agree')
plt.savefig('SIS_sims')
示例3: EBCM_pref_mix_discrete_from_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def EBCM_pref_mix_discrete_from_graph(G, p, rho = None, tmin = 0, tmax = 100, return_full_data=False):
'''
Takes a given graph, finds degree correlations, and calls EBCM_pref_mix_discrete
:SAMPLE USE:
::
import networkx as nx
import EoN
import matplotlib.pyplot as plt
G = nx.bipartite.configuration_model([5]*300000, [2]*750000)
t, S, I, R = EoN.basic_discrete_SIR(G, 0.6, rho = 0.002)
tx, Sx, Ix, Rx = EoN.EBCM_pref_mix_discrete_from_graph(G, 0.6, rho=0.002, tmax=t[-1])
plt.plot(t, I, label = 'simulation')
plt.plot(tx, Ix, '--', label = 'analytic prediction')
plt.legend(loc='upper right')
plt.show()
'''
N = G.order()
Pk = get_Pk(G)
Pnk = get_Pnk(G)
return EBCM_pref_mix_discrete(N, Pk, Pnk, p, rho=rho, tmin=tmin, tmax=tmax, return_full_data=return_full_data)
示例4: test_havel_hakimi_construction
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_havel_hakimi_construction():
G = nx.havel_hakimi_graph([])
assert_equal(len(G), 0)
z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
z = [5, 4, 3, 3, 3, 2, 2, 2]
G = nx.havel_hakimi_graph(z)
G = nx.configuration_model(z)
z = [6, 5, 4, 4, 2, 1, 1, 1]
assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]
G = nx.havel_hakimi_graph(z)
assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z,
create_using=nx.DiGraph())
示例5: get_G
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def get_G(N, Pk):
while True:
ks = []
for ctr in range(N):
r = random.random()
for k in Pk:
if r<Pk[k]:
break
else:
r-= Pk[k]
ks.append(k)
if sum(ks)%2==0:
break
G = nx.configuration_model(ks)
return G
示例6: regular_graph_generation
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def regular_graph_generation(N, kave):
return nx.configuration_model([kave]*N)
示例7: generateFrom
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def generateFrom(N, p, maxdeg=100):
# construct degrees according to the distribution given
# by the model function
ns = []
t = 0
for i in range(N):
while True:
k = rng.integers(1, maxdeg)
if rng.random() < p(k):
ns.append(k)
t += k
break
# the final sequence of degrees has to sum to an even number, as
# each edge has two endpoints
# if the sequence is odd, remove an element and draw another from
# the distribution, repeating until the overall sequence is even
while t % 2 != 0:
# pick a node at random
i = rng.integers(0, len(ns) - 1)
# remove it from the sequence and from the total
t -= ns[i]
del ns[i]
# draw a new degree from the distribution
while True:
k = rng.integers(1, maxdeg)
if rng.random() < p(k):
# add new node to the sequence
ns.append(k)
t += k
break
# populate the network using the configuration
# model with the given degree distribution
g = networkx.configuration_model(ns, create_using=networkx.Graph())
return g
示例8: test_empty_degree_sequence
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_empty_degree_sequence(self):
"""Tests that an empty degree sequence yields the null graph."""
G = nx.configuration_model([])
assert_equal(len(G), 0)
示例9: test_degree_zero
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_degree_zero(self):
"""Tests that a degree sequence of all zeros yields the empty
graph.
"""
G = nx.configuration_model([0, 0, 0])
assert_equal(len(G), 3)
assert_equal(G.number_of_edges(), 0)
示例10: test_degree_sequence
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_degree_sequence(self):
"""Tests that the degree sequence of the generated graph matches
the input degree sequence.
"""
deg_seq = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
G = nx.configuration_model(deg_seq, seed=12345678)
assert_equal(sorted((d for n, d in G.degree()), reverse=True),
[5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1])
assert_equal(sorted((d for n, d in G.degree(range(len(deg_seq)))),
reverse=True),
[5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1])
示例11: test_random_seed
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_random_seed(self):
"""Tests that each call with the same random seed generates the
same graph.
"""
deg_seq = [3] * 12
G1 = nx.configuration_model(deg_seq, seed=1000)
G2 = nx.configuration_model(deg_seq, seed=1000)
assert_true(nx.is_isomorphic(G1, G2))
G1 = nx.configuration_model(deg_seq, seed=10)
G2 = nx.configuration_model(deg_seq, seed=10)
assert_true(nx.is_isomorphic(G1, G2))
示例12: test_odd_degree_sum
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_odd_degree_sum(self):
"""Tests that a degree sequence whose sum is odd yields an
exception.
"""
nx.configuration_model([1, 2])
示例13: test_Gillespie_complex_contagion
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def test_Gillespie_complex_contagion(self):
def transition_rate(G, node, status, parameters):
# this function needs to return the rate at which ``node`` changes status
#
r = parameters[0]
if status[node] == 'S' and len([nbr for nbr in G.neighbors(node) if status[nbr] == 'I']) > 1:
return 1
else: # status[node] might be 0 or length might be 0 or 1.
return 0
def transition_choice(G, node, status, parameters):
# this function needs to return the new status of node. We assume going
# in that we have already calculated it is changing status.
#
# this function could be more elaborate if there were different
# possible transitions that could happen. However, for this model,
# the 'I' nodes aren't changing status, and the 'S' ones are changing to 'I'
# So if we're in this function, the node must be 'S' and becoming 'I'
#
return 'I'
def get_influence_set(G, node, status, parameters):
# this function needs to return any node whose rates might change
# because ``node`` has just changed status. That is, which nodes
# might ``node`` influence?
#
# For our models the only nodes a node might affect are the susceptible neighbors.
return {nbr for nbr in G.neighbors(node) if status[nbr] == 'S'}
parameters = (2,) # this is the threshold. Note the comma. It is needed
# for python to realize this is a 1-tuple, not just a number.
# ``parameters`` is sent as a tuple so we need the comma.
N = 60000
deg_dist = [2, 4, 6] * int(N / 3)
G = nx.configuration_model(deg_dist)
for rho in np.linspace(3. / 80, 7. / 80, 8): # 8 values from 3/80 to 7/80.
print(rho)
IC = defaultdict(lambda: 'S')
for node in G.nodes():
if np.random.random() < rho: # there are faster ways to do this random selection
IC[node] = 'I'
t, S, I = EoN.Gillespie_complex_contagion(G, transition_rate, transition_choice,
get_influence_set, IC, return_statuses=('S', 'I'),
parameters=parameters)
plt.plot(t, I)
plt.savefig('test_Gillespie_complex_contagion')
示例14: random_degree_sequence_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def random_degree_sequence_graph(sequence, seed=None, tries=10):
r"""Return a simple random graph with the given degree sequence.
If the maximum degree `d_m` in the sequence is `O(m^{1/4})` then the
algorithm produces almost uniform random graphs in `O(m d_m)` time
where `m` is the number of edges.
Parameters
----------
sequence : list of integers
Sequence of degrees
seed : hashable object, optional
Seed for random number generator
tries : int, optional
Maximum number of tries to create a graph
Returns
-------
G : Graph
A graph with the specified degree sequence.
Nodes are labeled starting at 0 with an index
corresponding to the position in the sequence.
Raises
------
NetworkXUnfeasible
If the degree sequence is not graphical.
NetworkXError
If a graph is not produced in specified number of tries
See Also
--------
is_valid_degree_sequence, configuration_model
Notes
-----
The generator algorithm [1]_ is not guaranteed to produce a graph.
References
----------
.. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi,
A sequential algorithm for generating random graphs.
Algorithmica, Volume 58, Number 4, 860-910,
DOI: 10.1007/s00453-009-9340-1
Examples
--------
>>> sequence = [1, 2, 2, 3]
>>> G = nx.random_degree_sequence_graph(sequence)
>>> sorted(G.degree().values())
[1, 2, 2, 3]
"""
DSRG = DegreeSequenceRandomGraph(sequence, seed=seed)
for try_n in range(tries):
try:
return DSRG.generate()
except nx.NetworkXUnfeasible:
pass
raise nx.NetworkXError('failed to generate graph in %d tries'%tries)
示例15: random_degree_sequence_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import configuration_model [as 别名]
def random_degree_sequence_graph(sequence, seed=None, tries=10):
r"""Returns a simple random graph with the given degree sequence.
If the maximum degree $d_m$ in the sequence is $O(m^{1/4})$ then the
algorithm produces almost uniform random graphs in $O(m d_m)$ time
where $m$ is the number of edges.
Parameters
----------
sequence : list of integers
Sequence of degrees
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
tries : int, optional
Maximum number of tries to create a graph
Returns
-------
G : Graph
A graph with the specified degree sequence.
Nodes are labeled starting at 0 with an index
corresponding to the position in the sequence.
Raises
------
NetworkXUnfeasible
If the degree sequence is not graphical.
NetworkXError
If a graph is not produced in specified number of tries
See Also
--------
is_graphical, configuration_model
Notes
-----
The generator algorithm [1]_ is not guaranteed to produce a graph.
References
----------
.. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi,
A sequential algorithm for generating random graphs.
Algorithmica, Volume 58, Number 4, 860-910,
DOI: 10.1007/s00453-009-9340-1
Examples
--------
>>> sequence = [1, 2, 2, 3]
>>> G = nx.random_degree_sequence_graph(sequence)
>>> sorted(d for n, d in G.degree())
[1, 2, 2, 3]
"""
DSRG = DegreeSequenceRandomGraph(sequence, seed)
for try_n in range(tries):
try:
return DSRG.generate()
except nx.NetworkXUnfeasible:
pass
raise nx.NetworkXError('failed to generate graph in %d tries' % tries)