我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用networkx.barabasi_albert_graph()。
def SF_Generateor(N=1000, m=3): G = nx.barabasi_albert_graph(N, m) #component of the network if nx.is_connected(G) == False: # Get the Greatest Component of Networks ##### components = sorted(nx.connected_components(G), key = len, reverse=True) print "Component Number of the Generated Network:", len(components) for i in range(1, len(components)): for node in components[i]: G.remove_node(node) #end for print nx.is_connected(G) #endif return G #************************************************************************
def save_topology(adj, path, graph_name, link_possibility): graph = nx.Graph() if not os.path.isdir(path): os.makedirs(path) # 1. transfer adj to nx adj_list = list(np.squeeze(adj[0,:,:,:])) for src in range(len(adj_list)): graph.add_node(src) for dst in range(len(adj_list[src])): if adj_list[src][dst] >= link_possibility: # ?? sample ?? ?? [0,1]??? graph.add_edge(src,dst) # 2. read position pos_file = glob.glob(path+'*.pos') if pos_file == []: node_size = len(graph.nodes()) tmp_graph = nx.barabasi_albert_graph(node_size,2) pos = nx.spring_layout(tmp_graph) pickle.dump(pos, open(path+'graph.pos','wb')) else: pos = pickle.load(open(pos_file[0],'rb')) # 3. draw graph nx.draw_networkx_nodes(graph, pos, node_size=300, node_color='b', alpha=0.8) nx.draw_networkx_edges(graph, pos, width=1.5, alpha=0.8) nx.draw_networkx_labels(graph, pos, font_color='w') plt.savefig(path+'/'+graph_name+'.png') plt.savefig(path+'/'+graph_name+'.pdf') # plt.show() plt.clf() # 4. store graph pickle.dump(graph, open(path+graph_name+'.graph','wb')) # ------------------------------ # show_all_variables() # @purpose: ??TF?????? # ------------------------------