本文整理汇总了Python中networkx.powerlaw_cluster_graph函数的典型用法代码示例。如果您正苦于以下问题:Python powerlaw_cluster_graph函数的具体用法?Python powerlaw_cluster_graph怎么用?Python powerlaw_cluster_graph使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了powerlaw_cluster_graph函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize
def initialize():
global g
global wr
global f
global initialConditions
initialConditions=0
# Inicialite file for saving data
cola= dt.datetime.now().strftime('%H%M')
outputfilename = 'competicion_01_{}.csv'.format( cola)
fileOut = open(outputfilename, 'w')
wr = csv.writer(fileOut,quoting = csv.QUOTE_NONE)
wr.writerow(['Tipo 1','Tipo 2','Tipo 3','Patogeno'])
f = open( 'competicion_01_{}.ini'.format( cola), "w")
# g = nx.karate_club_graph()
g=nx.powerlaw_cluster_graph(100,3,0.3)
g.pos = nx.spring_layout(g)
for i in g.nodes_iter():
if random() < .33:
g.node[i]['tipo'] = 0
elif random() < .33:
g.node[i]['tipo'] = 1
elif random() < .33:
g.node[i]['tipo'] = 2
elif random() < .33:
g.node[i]['tipo'] = 3
else:
g.node[i]['tipo'] = 4
示例2: get_graph
def get_graph(objects, properties):
graph_type = properties['graph_type']
n = len(objects)-1
if 'num_nodes_to_attach' in properties.keys():
k = properties['num_nodes_to_attach']
else:
k = 3
r = properties['connection_probability']
tries = 0
while(True):
if graph_type == 'random':
x = nx.fast_gnp_random_graph(n,r)
elif graph_type == 'erdos_renyi_graph':
x = nx.erdos_renyi_graph(n,r)
elif graph_type == 'watts_strogatz_graph':
x = nx.watts_strogatz_graph(n, k, r)
elif graph_type == 'newman_watts_strogatz_graph':
x = nx.newman_watts_strogatz_graph(n, k, r)
elif graph_type == 'barabasi_albert_graph':
x = nx.barabasi_albert_graph(n, k, r)
elif graph_type == 'powerlaw_cluster_graph':
x = nx.powerlaw_cluster_graph(n, k, r)
elif graph_type == 'cycle_graph':
x = nx.cycle_graph(n)
else: ##Star by default
x = nx.star_graph(len(objects)-1)
tries += 1
cc_conn = nx.connected_components(x)
if len(cc_conn) == 1 or tries > 5:
##best effort to create a connected graph!
break
return x, cc_conn
示例3: fromPowerLaw
def fromPowerLaw(cls, specs):
'''
:param specs: a tupple containing (n, m, p, seed, bw, delay, cpu)
:return: a substrate
'''
n, m, p, seed, bw, delay, cpu = specs
n = int(n)
m = int(m)
p = float(p)
seed = int(seed)
g = nx.powerlaw_cluster_graph(n, m, p, seed)
session = Session()
nodes = [Node(name=str(n), cpu_capacity=cpu) for n in g.nodes()]
session.add_all(nodes)
session.flush()
edges = [Edge
(node_1=session.query(Node).filter(Node.name == str(e[0])).one(),
node_2=session.query(Node).filter(Node.name == str(e[1])).one(),
bandwidth=bw,
delay=delay
)
for e in g.edges()
]
session.add_all(edges)
session.flush()
return cls(edges, nodes
)
示例4: __update_structure
def __update_structure(self):
self.structure = nx.powerlaw_cluster_graph(
self.num_nodes,
self.node_degree,
self.prob_triad,
self.seed
)
if nx.is_connected(self.structure):
return
components = nx.connected_components(self.structure)
biggest_comp = []
comp_index = -1
for i, component in enumerate(components):
if len(component) > len(biggest_comp):
biggest_comp = component
comp_index = i
if self.seed:
random.seed(self.seed)
del components[comp_index]
for component in components:
for left_node in component:
right_node = random.choice(biggest_comp)
self.structure.add_edge(left_node, right_node)
示例5: add_edges_to_groups
def add_edges_to_groups(output_graph, groups_list, edges_to_add, prob, level):
global template_created
total_groups = len(groups_list)
edges_per_node = max((3 - level), 1)
triangle_prob = 0.1*level
if False:
random_graph = nx.random_regular_graph(int(total_groups/3), total_groups)
else:
random_graph = nx.powerlaw_cluster_graph(total_groups, edges_per_node, triangle_prob, random.random()*10)
if template_created:
template_created = False
plt.axis('off')
position = nx.graphviz_layout(random_graph, prog='sfdp')
nx.draw_networkx_nodes(random_graph, position, node_size=30, node_color='r') #output_graph.degree().values())
nx.draw_networkx_edges(random_graph, position, alpha=0.3)
plt.savefig(dataset_name2 +"/"+ "template_" + image_name, bbox_inches='tight', dpi=500)
print "plot saved as ", image_name
random_edges = random_graph.edges()
for edge in random_edges:
e0 = edge[0]
e1 = edge[1]
if random.random() > 0.3:
e0, e1 = e1, e0
print("adding level{} edges between group{} and group{}".format(level, e0, e1))
add_edges_to_two_groups(output_graph, groups_list[e0], groups_list[e1], edges_to_add, prob)
示例6: test_pagerank
def test_pagerank(self):
size = 1000
g = nx.DiGraph(nx.powerlaw_cluster_graph(size, 3, 0.001))
N = len(g.nodes())
tmp_file = tempfile.NamedTemporaryFile(delete=False)
for node in g.nodes():
outlinks = g.out_edges(nbunch=[node])
outlinks = map(str, [n2 for n1, n2 in outlinks])
if not outlinks:
value = 'pr_results,%s,%s' % (1.0/N, N)
tmp_file.write('%s\t%s\n' % (node, value))
else:
outlinks_str = ','.join(outlinks)
value = 'pr_results,%s,%s,' % (1.0/N, N)
value += outlinks_str
tmp_file.write('%s\t%s\n' % (node, value))
tmp_file.flush()
input_path = tmp_file.name
job_id = 'unittest'
sorted_ids = pagerank(job_id, self.iter_count, input_path, self.top_n)
fs = HadoopFS()
fs.rmr('%s/hat_results' % job_id)
if self.top_n <= size:
self.assertEqual(len(sorted_ids), self.top_n, 'some ids is missing')
id_ranges = range(0, 1000)
for _id in sorted_ids:
self.assertIn(int(_id), id_ranges, 'node should in graph')
示例7: createGraphsAndCommunities
def createGraphsAndCommunities():
g = nx.scale_free_graph(500, alpha=0.40, beta=0.40, gamma=0.20)
g1 = nx.powerlaw_cluster_graph(500, 10, 0.2)
g2 = nx.barabasi_albert_graph(500, 10)
g3 = nx.newman_watts_strogatz_graph(500, 10, 0.2)
nx.write_graphml (g, direc+"sfg.graphml")
nx.write_graphml(g1, direc+"pcg.graphml")
nx.write_graphml(g2, direc+"bag.graphml")
nx.write_graphml(g3, direc+"nwsg.graphml")
graphs = {}
graphs["sfg"] = graph_tool.load_graph(direc+"sfg.graphml")
graphs["pcg"] = graph_tool.load_graph(direc+"pcg.graphml")
graphs["bag"] = graph_tool.load_graph(direc+"bag.graphml")
graphs["nwsg"] = graph_tool.load_graph(direc+"nwsg.graphml")
graphs["price"] = graph_tool.generation.price_network(1000)
for i,h in graphs.iteritems():
s = graph_tool.community.minimize_blockmodel_dl(h)
b = s.b
graph_tool.draw.graph_draw(h, vertex_fill_color=b, vertex_shape=b, output=direc+"block"+str(i)+".pdf")
com = graph_tool.community.community_structure(h, 10000, 20)
graph_tool.draw.graph_draw(h, vertex_fill_color=com, vertex_shape=com, output=direc+"community"+str(i)+".pdf")
state = graph_tool.community.minimize_nested_blockmodel_dl(h)
graph_tool.draw.draw_hierarchy(state, output=direc+"nestedblock"+str(i)+".pdf")
pagerank = graph_tool.centrality.pagerank(h)
graph_tool.draw.graph_draw(h, vertex_fill_color=pagerank, vertex_size = graph_tool.draw.prop_to_size(pagerank, mi=5, ma=15), vorder=pagerank, output=direc+"pagerank"+str(i)+".pdf")
h.set_reversed(is_reversed=True)
pagerank = graph_tool.centrality.pagerank(h)
graph_tool.draw.graph_draw(h, vertex_fill_color=pagerank, vertex_size = graph_tool.draw.prop_to_size(pagerank, mi=5, ma=15), vorder=pagerank, output=direc+"reversed_pagerank"+str(i)+".pdf")
示例8: RandomHolmeKim
def RandomHolmeKim(n, m, p, seed=None):
"""
Returns a random graph generated by the Holme and Kim algorithm for
graphs with power law degree distribution and approximate average
clustering.
INPUT:
- ``n`` - number of vertices.
- ``m`` - number of random edges to add for each new
node.
- ``p`` - probability of adding a triangle after
adding a random edge.
- ``seed`` - for the random number generator.
From the NetworkX documentation: The average clustering has a hard
time getting above a certain cutoff that depends on m. This cutoff
is often quite low. Note that the transitivity (fraction of
triangles to possible triangles) seems to go down with network
size. It is essentially the Barabasi-Albert growth model with an
extra step that each random edge is followed by a chance of making
an edge to one of its neighbors too (and thus a triangle). This
algorithm improves on B-A in the sense that it enables a higher
average clustering to be attained if desired. It seems possible to
have a disconnected graph with this algorithm since the initial m
nodes may not be all linked to a new node on the first iteration
like the BA model.
EXAMPLE: We show the edge list of a random graph on 8 nodes with 2
random edges per node and a probability `p = 0.5` of
forming triangles.
::
sage: graphs.RandomHolmeKim(8, 2, 0.5).edges(labels=False)
[(0, 2), (0, 5), (1, 2), (1, 3), (2, 3), (2, 4), (2, 6), (2, 7),
(3, 4), (3, 6), (3, 7), (4, 5)]
::
sage: G = graphs.RandomHolmeKim(12, 3, .3)
sage: G.show() # long time
REFERENCE:
- [1] Holme, P. and Kim, B.J. Growing scale-free networks with
tunable clustering, Phys. Rev. E (2002). vol 65, no 2,
026107.
"""
if seed is None:
seed = current_randstate().long_seed()
import networkx
return graph.Graph(networkx.powerlaw_cluster_graph(n, m, p, seed=seed))
示例9: generate_topo
def generate_topo(n):
topo = nx.powerlaw_cluster_graph(n,2,0.08)
# topo = fnss.waxman_1_topology(n=50,alpha=0.6,beta=0.3)
# topo = fnss.fat_tree_topology(n)
fnss.set_weights_constant(topo,1)
fnss.set_delays_constant(topo, 1, 'ms')
fnss.set_capacities_edge_betweenness(topo,[100,500,1000],'Mbps')
fnss.write_topology(topo,'topo_pl_50.xml')
示例10: generate_graph
def generate_graph():
# Try these included graphs! Play around with the constants!
# Feel free to define your own graph for testing.
#return nx.random_regular_graph(5, GRAPH_SIZE, seed=GRAPH_SEED)
#return nx.barabasi_albert_graph(GRAPH_SIZE, 5)
#return grid_graph()
return nx.powerlaw_cluster_graph(GRAPH_SIZE, 5, 0.7)
示例11: graph_space_iter
def graph_space_iter():
i = 20
n = 0
while n < 1000000:
G = nx.powerlaw_cluster_graph(i, 10, 0.1)
G = nx.convert_node_labels_to_integers(G)
n = G.number_of_edges()
i *= 2
yield G.number_of_nodes(), G
示例12: pl_cluster_new
def pl_cluster_new(n, m, p, random_seed=None):
Gw=nx.powerlaw_cluster_graph(n, m, p, seed=random_seed)
Gs=nx.Graph()
Gs.add_nodes_from(Gw.nodes(),state=1.0)
Gs.add_edges_from(Gw.edges(),weight=1.0)
##remove self-edges
Gs.remove_edges_from(Gs.selfloop_edges())
return Gs
示例13: create_scale_free_graph
def create_scale_free_graph(N_nodes,p_edge,n_infected):
#scale free and small world
#Growing Scale-Free Networks with Tunable Clustering
n = N_nodes
m = int(0.5*p_edge*N_nodes)
p = 1.0
#Random graph
#p_coop is the fraction of cooperators
G = nx.powerlaw_cluster_graph(n,m,p)
return set_graph_strategies(G, n_infected)
示例14: topo_pl
def topo_pl(nodes):
bws = [1,2,3,4,5,6,7,8,9]
network = nx.powerlaw_cluster_graph(nodes,2,0.13)
g = nx.Graph()
g.add_nodes_from(network.nodes())
for link in network.edges():
bw = random.choice(bws)
g.add_edge(link[0],link[1],{'weight':bw})
g.add_edge(link[1],link[0],{'weight':bw})
return g
示例15: initialize
def initialize():
global g
# g = nx.karate_club_graph()
g=nx.powerlaw_cluster_graph(100,3,0.3)
g.pos = nx.spring_layout(g)
for i in g.nodes_iter():
if random() < .3:
g.node[i]['state'] = 1
elif random() < .3:
g.node[i]['state'] = 2
elif random() < .3:
g.node[i]['state'] = 3
else:
g.node[i]['state'] = 0