def read_graph(): ''' Reads the input network in networkx. ''' if args.weighted: G = nx.read_edgelist(args.input, nodetype=int, data=(('weight', float), ), create_using=nx.DiGraph()) else: G = nx.read_edgelist(args.input, nodetype=int, create_using=nx.DiGraph()) for edge in G.edges(): G[edge[0]][edge[1]]['weight'] = 1 if not args.directed: G = G.to_undirected() return G
import itertools import operator from networkx import nx import matplotlib.pyplot as plt import statistics import community import networkx as nx G = nx.read_edgelist("30temple83Rel.txt") totalcentra = nx.degree_centrality(G) {k: v for k, v in sorted(totalcentra.items(), key=lambda item: item[1])} for x in list(reversed(list(totalcentra)))[0:5]: print (totalcentra[x])
window=window_size, min_count=0, sg=1, workers=8, iter=1) model.wv.save(output) return model.wv #G0, Polyid, Polysynset, _,_,_ = generate_word_graph(True, False, False, 0) #snap.SaveEdgeList(G0, "G0.txt", "") #G1, Polyid, Polysynset, _,_,_ = generate_word_graph(True, False, False, 1) #snap.SaveEdgeList(G1, "G1.txt", "") #G2, Polyid, Polysynset, _,_,_ = generate_word_graph(True, False, False, 2) #snap.SaveEdgeList(G2, "G2.txt", "") G0 = nx.read_edgelist("G0.txt", nodetype=int) for edge in G0.edges(): G0[edge[0]][edge[1]]['weight'] = 1.0 G1 = nx.read_edgelist("G1.txt", nodetype=int, create_using=nx.DiGraph()) for edge in G1.edges(): G1[edge[0]][edge[1]]['weight'] = 1.0 G2 = nx.read_edgelist("G2.txt", nodetype=int, create_using=nx.DiGraph()) for edge in G2.edges(): G2[edge[0]][edge[1]]['weight'] = 1.0 p = 100000 q = 1 walk_length = 20 window_size = 20 num_walks = 100 dimension = 50 G1 = node2vec.Graph(G1, False, p, q)
from networkx import nx from random import randint import json input_folder = "data" num_node_network = 10 path_random_network = input_folder+"/random_network_"+str(num_node_network)+".csv" input_random_network = open(path_random_network) graph = nx.read_edgelist(input_random_network, delimiter=',', create_using=nx.DiGraph(), nodetype=int, data=(('cost', float), ('capacity', float))) # read configuration file with orders parameter for any order of our problem orders_configuration_file = open("Config/orders_config.json").read() orders_configuration_parameters = json.loads(orders_configuration_file) # assign orders parameter into variable min_capacity = orders_configuration_parameters["min_capacity"] # capacity minima ( = capacity truck) min_len_shortest_path = orders_configuration_parameters["min_len_shortest_path"] # lunghezza minima shortest path num_max_orders = orders_configuration_parameters["num_max_orders"] # numero massimo di ordini, solo se limited_num_orders = true limited_num_orders = True # se = True => vengono salvati al max num_max_orders ordini unique_sources = orders_configuration_parameters["unique_sources"] # se = True => i nodi partenza degli ordini saranno tutti diversi nodes = graph.nodes() orders = [] # lista di ordini order_count = 0 for n1 in nodes: tmp = list(nodes) tmp.remove(n1) for n2 in tmp: if limited_num_orders and len(orders) == num_max_orders: # vengono fatti comunque dei cicli inutili, ma pazienza
write_score_file('SI', si_res_list) write_score_file('SIR', sir_res_list) if __name__ == "__main__": import time start = time.clock() file_name = "../dataset/facebook_combined.txt" #file_name = "../dataset/CA-GrQc_nor.txt" ; #file_name = "../dataset/karate.txt" ; #file_name = "../dataset/friendships-hamster_new.txt" ; #file_name = "../dataset/hamster_full_new.txt" ; #file_name = "../dataset/blog_edges.txt" ; G = nx.read_edgelist(file_name) seedList = [3] run_num = 200 f = open(output_file, 'w') f.close() for seed in seedList: f = open(output_file, 'a') print >> f, "***** seed_num=", seed, "*****" f.close() print "***** seed_num=", seed, "*****" my_run(G, seed, run_num) f = open(output_file, 'a') print >> f, "\n" f.close()
def activate_nodes(fileN, p, beginRange, endRange, outFileName): # READ GRAPH FROM FILE g = nx.read_edgelist(fileN) #g = nx.read_weighted_edgelist(fileN) # SORT BY DEGREE sorted(g.degree, key=lambda x: x[1], reverse=True) # SET ATTRIBUTE 0 TO ALL NODES attr = 0 nx.set_node_attributes(g, attr, 'attr') att = nx.get_node_attributes(g, 'attr') #print att numNodes = 0 for k, v in att.items(): numNodes = numNodes + 1 #print("Key : {0}, Value : {1}".format(k, v)) #numNodes = number_of_nodes(g) if (p == 0.3): # RANGE ADJUST a = 100 - beginRange b = 100 - endRange beginAtt = (a * numNodes) / 100 endAtt = (b * numNodes) / 100 # FINAL POSITIONS OF RANGE ON DICTIONARY beginAtt = numNodes - beginAtt endAtt = numNodes - endAtt while (beginAtt <= endAtt): att[att.keys()[beginAtt]] = 1 beginAtt = beginAtt + 1 #for k, v in att.items(): #print("Key : {0}, Value : {1}".format(k, v)) if (p == 0.2): # RANGE ADJUST a = 100 - beginRange b = 100 - (endRange - 10) beginAtt = (a * numNodes) / 100 endAtt = (b * numNodes) / 100 # FINAL POSITIONS OF RANGE ON DICTIONARY beginAtt = numNodes - beginAtt endAtt = numNodes - endAtt while (beginAtt <= endAtt): att[att.keys()[beginAtt]] = 1 beginAtt = beginAtt + 1 #for k, v in att.items(): #print("Key : {0}, Value : {1}".format(k, v)) if (p == 0.1): # RANGE ADJUST a = 100 - beginRange b = 100 - (endRange - 20) beginAtt = (a * numNodes) / 100 endAtt = (b * numNodes) / 100 # FINAL POSITIONS OF RANGE ON DICTIONARY beginAtt = numNodes - beginAtt endAtt = numNodes - endAtt while (beginAtt <= endAtt): att[att.keys()[beginAtt]] = 1 beginAtt = beginAtt + 1 #for k, v in att.items(): #print("Key : {0}, Value : {1}".format(k, v)) if (p == 0.05): # RANGE ADJUST a = 100 - beginRange b = 100 - (endRange - 25) beginAtt = (a * numNodes) / 100 endAtt = (b * numNodes) / 100 # FINAL POSITIONS OF RANGE ON DICTIONARY beginAtt = numNodes - beginAtt endAtt = numNodes - endAtt while (beginAtt <= endAtt): att[att.keys()[beginAtt]] = 1 beginAtt = beginAtt + 1 #for k, v in att.items(): #print("Key : {0}, Value : {1}".format(k, v)) nx.set_node_attributes(g, att, 'attr') nx.write_edgelist(g, outFileName+".elist.txt", data=['attr']) #att = nx.get_node_attributes(g, 'attr') #for k, v in att.items(): # print("Key : {0}, Value : {1}".format(k, v)) measure_MI(g)
# Original data from http://www.sociopatterns.org/datasets/high-school-contact-and-friendship-networks/ import matplotlib.pyplot as plt from networkx import nx G = nx.read_edgelist('Facebook-known-pairs_data_2013_cleaned.csv', delimiter=',') options = {'node_color': 'blue', 'node_size': 10, 'line_color': 'grey', 'linewidths': 0, 'width': 0.1} plt.figure(figsize=(8.8, 3.5)) nx.draw_spring(G, **options) plt.show()