def random_graphs(): print("Random graphs") print("fast GNP random graph") G = nx.fast_gnp_random_graph(n=9, p=0.4) draw_graph(G) print("GNP random graph") G = nx.gnp_random_graph(n=9, p=0.1) draw_graph(G) print("Dense GNM random graph") G = nx.dense_gnm_random_graph(n=19, m=28) draw_graph(G) print("GNM random graph") G = nx.gnm_random_graph(n=11, m=14) draw_graph(G) print("Erdős Rényi graph") G = nx.erdos_renyi_graph(n=11, p=0.4) draw_graph(G) print("Binomial graph") G = nx.binomial_graph(n=45, p=0.4) draw_graph(G) print("Newman Watts Strogatz") G = nx.newman_watts_strogatz_graph(n=9, k=5, p=0.4) draw_graph(G) print("Watts Strogatz") G = nx.watts_strogatz_graph(n=9, k=2, p=0.4) draw_graph(G) print("Watts Strogatz") G = nx.watts_strogatz_graph(n=9, k=2, p=0.4) draw_graph(G) print("Connected Watts Strogatz") G = nx.connected_watts_strogatz_graph(n=8, k=2, p=0.1) draw_graph(G) print("Random Regular Graph") G = nx.random_regular_graph(d=2, n=9) draw_graph(G) print("Barabasi Albert Graph") G = nx.barabasi_albert_graph(n=10, m=2) draw_graph(G) print("Powerlow Cluster Graph") G = nx.powerlaw_cluster_graph(n=10, m=2, p=0.2) draw_graph(G) print("Duplication Divergence Graph") G = nx.duplication_divergence_graph(n=10, p=0.2) draw_graph(G) print("Random lobster Graph") G = nx.random_lobster(n=10, p1=0.2, p2=0.8) draw_graph(G) print("Random shell Graph") constructor = [(10, 20, 0.8), (20, 40, 0.8)] G = nx.random_shell_graph(constructor) draw_graph(G) print("Random Powerlow Tree") G = nx.random_powerlaw_tree(n=24, gamma=3) draw_graph(G) print("Random Powerlow Tree Sequence") G = nx.random_powerlaw_tree(n=13, gamma=3) draw_graph(G)
def GenerateGraphs(size, base): numNodes = {"fast": [], "dupl": [], "comp": []} dic = GenerateDic(size, base) for i in range(1, 5): print("ciclo" + str(i)) for j in range(10): # G=AddEdges(nx.fast_gnp_random_graph(size, 0.5)) G = AddEdges( nx.dense_gnm_random_graph(size, int(size * size * 0.2))) numNodes["fast"].append([G.number_of_edges, size]) print("fast" + str(j)) for k in range(5): nodes = RandNodes(size - 1) for l in range(5): dic["Edmond" + str(size) + "fast"].append( Edmond(G, nodes[0], nodes[1])) dic["Din" + str(size) + "fast"].append( Din(G, nodes[0], nodes[1])) dic["Boyk" + str(size) + "fast"].append( Boyk(G, nodes[0], nodes[1])) G = AddEdges(nx.duplication_divergence_graph(size, 0.2)) numNodes["dupl"].append([G.number_of_edges, size]) print("dupl" + str(j)) for k in range(5): nodes = RandNodes(size - 1) for l in range(5): dic["Edmond" + str(size) + "dupl"].append( Edmond(G, nodes[0], nodes[1])) dic["Din" + str(size) + "dupl"].append( Din(G, nodes[0], nodes[1])) dic["Boyk" + str(size) + "dupl"].append( Boyk(G, nodes[0], nodes[1])) G = AddEdges(nx.complete_graph(size)) numNodes["comp"].append([G.number_of_edges, size]) print("comp" + str(j)) for k in range(5): nodes = RandNodes(size - 1) for l in range(5): dic["Edmond" + str(size) + "comp"].append( Edmond(G, nodes[0], nodes[1])) dic["Din" + str(size) + "comp"].append( Din(G, nodes[0], nodes[1])) dic["Boyk" + str(size) + "comp"].append( Boyk(G, nodes[0], nodes[1])) size *= base df = pd.DataFrame(dic) df.to_csv("matrix.csv") df = pd.DataFrame(numNodes) df.to_csv("Nodes.csv")
# write_json_graph(nx.dorogovtsev_goltsev_mendes_graph(2), 'dorogovtsev_goltsev_mendes_graph(2).json') # write_json_graph(nx.dorogovtsev_goltsev_mendes_graph(3), 'dorogovtsev_goltsev_mendes_graph(3).json') write_json_graph(nx.dorogovtsev_goltsev_mendes_graph(4), 'dorogovtsev_goltsev_mendes_graph(4).json') write_json_graph(nx.dorogovtsev_goltsev_mendes_graph(5), 'dorogovtsev_goltsev_mendes_graph(5).json') # write_json_graph(nx.navigable_small_world_graph(8), 'navigable_small_world_graph(8).json') # write_json_graph(nx.navigable_small_world_graph(20), 'navigable_small_world_graph(20).json') write_json_graph(nx.circular_ladder_graph(5), 'circular_ladder_graph(5).json') write_json_graph(nx.circular_ladder_graph(100), 'circular_ladder_graph(100).json') write_json_graph(nx.duplication_divergence_graph(50, 0.5, seed=0), 'duplication_divergence_graph(50,0_5).json') write_json_graph(nx.watts_strogatz_graph(100, 3, 0.05), 'watts_strogatz_graph(100,3,0.05).json') write_json_graph(nx.watts_strogatz_graph(100, 3, 0.25), 'watts_strogatz_graph(100,3,0.25).json') # write_json_graph(nx.watts_strogatz_graph(100,3,0.5), 'watts_strogatz_graph(100,3,0.5).json') write_json_graph(nx.watts_strogatz_graph(100, 4, 0.05), 'watts_strogatz_graph(100,4,0.05).json') write_json_graph(nx.watts_strogatz_graph(100, 4, 0.25), 'watts_strogatz_graph(100,4,0.25).json') # write_json_graph(nx.watts_strogatz_graph(100,4,0.5), 'watts_strogatz_graph(100,4,0.5).json') write_json_graph(nx.watts_strogatz_graph(100, 5, 0.05), 'watts_strogatz_graph(100,5,0.05).json') write_json_graph(nx.watts_strogatz_graph(100, 5, 0.25),
def create_graph(n): G = nx.duplication_divergence_graph(n, 0.5) return G
def gen_laplacian(data_num=DATA_NUM, opt=27, cache=False): label = None if cache: print('Loading cached graph') graph = pk.load(open('tmp/g.pk', 'rb')) else: print('Generating graph opt {}'.format(opt)) if 1 == opt: graph = gen_rand_graph(data_num=data_num) if 2 == opt: top_num = random.randint(1, data_num) bottom_num = data_num - top_num graph = nx.bipartite.random_graph(top_num, bottom_num, 0.9) label = [d['bipartite'] for n, d in graph.nodes(data=True)] elif 3 == opt: graph = nx.balanced_tree(4, 5) elif 4 == opt: graph = nx.complete_graph(data_num) elif 5 == opt: no1 = random.randint(1, data_num) no2 = random.randint(1, int(data_num / no1)) no3 = data_num / no1 / no2 graph = nx.complete_multipartite_graph(no1, no2, no3) elif 6 == opt: graph = nx.circular_ladder_graph(data_num) elif 7 == opt: graph = nx.cycle_graph(data_num) elif 8 == opt: graph = nx.dorogovtsev_goltsev_mendes_graph(5) elif 9 == opt: top_num = int(random.random() * data_num) bottom_num = data_num / top_num graph = nx.grid_2d_graph(top_num, bottom_num) elif 10 == opt: no1 = random.randint(1, data_num) no2 = random.randint(1, int(data_num / no1)) no3 = data_num / no1 / no2 graph = nx.grid_graph([no1, no2, no3]) elif 11 == opt: graph = nx.hypercube_graph(10) elif 12 == opt: graph = nx.ladder_graph(data_num) elif 13 == opt: top_num = int(random.random() * data_num) bottom_num = data_num - top_num graph = nx.lollipop_graph(top_num, bottom_num) elif 14 == opt: graph = nx.path_graph(data_num) elif 15 == opt: graph = nx.star_graph(data_num) elif 16 == opt: graph = nx.wheel_graph(data_num) elif 17 == opt: graph = nx.margulis_gabber_galil_graph(35) elif 18 == opt: graph = nx.chordal_cycle_graph(data_num) elif 19 == opt: graph = nx.fast_gnp_random_graph(data_num, random.random()) elif 20 == opt: # jump eigen value graph = nx.gnp_random_graph(data_num, random.random()) elif 21 == opt: # disconnected graph graph = nx.dense_gnm_random_graph(data_num, data_num / 2) elif 22 == opt: # disconnected graph graph = nx.gnm_random_graph(data_num, data_num / 2) elif 23 == opt: graph = nx.erdos_renyi_graph(data_num, data_num / 2) elif 24 == opt: graph = nx.binomial_graph(data_num, data_num / 2) elif 25 == opt: graph = nx.newman_watts_strogatz_graph(data_num, 5, random.random()) elif 26 == opt: graph = nx.watts_strogatz_graph(data_num, 5, random.random()) elif 26 == opt: # smooth eigen graph = nx.connected_watts_strogatz_graph(data_num, 5, random.random()) elif 27 == opt: # smooth eigen graph = nx.random_regular_graph(5, data_num) elif 28 == opt: # smooth eigen graph = nx.barabasi_albert_graph(data_num, 5) elif 29 == opt: # smooth eigen graph = nx.powerlaw_cluster_graph(data_num, 5, random.random()) elif 30 == opt: # smooth eigen graph = nx.duplication_divergence_graph(data_num, random.random()) elif 31 == opt: p = random.random() q = random.random() graph = nx.random_lobster(data_num, p, q) elif 32 == opt: p = random.random() q = random.random() k = random.random() graph = nx.random_shell_graph([(data_num / 3, 50, p), (data_num / 3, 40, q), (data_num / 3, 30, k)]) elif 33 == opt: # smooth eigen top_num = int(random.random() * data_num) bottom_num = data_num - top_num graph = nx.k_random_intersection_graph(top_num, bottom_num, 3) elif 34 == opt: graph = nx.random_geometric_graph(data_num, .1) elif 35 == opt: graph = nx.waxman_graph(data_num) elif 36 == opt: graph = nx.geographical_threshold_graph(data_num, .5) elif 37 == opt: top_num = int(random.random() * data_num) bottom_num = data_num - top_num graph = nx.uniform_random_intersection_graph( top_num, bottom_num, .5) elif 39 == opt: graph = nx.navigable_small_world_graph(data_num) elif 40 == opt: graph = nx.random_powerlaw_tree(data_num, tries=200) elif 41 == opt: graph = nx.karate_club_graph() elif 42 == opt: graph = nx.davis_southern_women_graph() elif 43 == opt: graph = nx.florentine_families_graph() elif 44 == opt: graph = nx.complete_multipartite_graph(data_num, data_num, data_num) # OPT 1 # norm_lap = nx.normalized_laplacian_matrix(graph).toarray() # OPT 2: renormalized # pk.dump(graph, open('tmp/g.pk', 'wb')) # plot_graph(graph, label) # note difference: normalized laplacian and normalzation by eigenvalue norm_lap, eigval, eigvec = normalize_lap(graph) return graph, norm_lap, eigval, eigvec
def duplication_divergence_graph(N, deg, dia, dim, domain): ''' Parameters of the graph: n (int) – The desired number of nodes in the graph. p (float) – The probability for retaining the edge of the replicated node. :return: Graph Object ''' strt_time = time() tolerance = 0.5 if deg == 4: lower_lim = 0.3 upper_lim = 0.4 bands = 10 elif deg == 6: lower_lim = 0.4 upper_lim = 0.6 bands = 15 elif deg == 8: lower_lim = 0.46 upper_lim = 0.60 bands = 15 elif deg == 10: lower_lim = 0.50 upper_lim = 0.65 bands = 15 elif deg == 12: lower_lim = 0.55 upper_lim = 0.68 bands = 15 flag = False curr_avg_deg_error = float('inf') while curr_avg_deg_error > tolerance: p_space = np.linspace(lower_lim, upper_lim, bands) avg_deg_err_list = [] p_gap = p_space[1] - p_space[0] for p_val in p_space: G = nx.duplication_divergence_graph(n=N, p=p_val) lcc, _ = graph_util.get_nk_lcc_undirected(G) curr_avg_deg = np.mean(list(dict(nx.degree(lcc)).values())) curr_avg_deg_error = abs(deg - curr_avg_deg) avg_deg_err_list.append((lcc, curr_avg_deg_error, p_val, curr_avg_deg)) if deg - curr_avg_deg < 0: break if curr_avg_deg_error <= tolerance: best_G = lcc best_avg_deg = curr_avg_deg best_diam = nx.algorithms.diameter(best_G) flag = True break if flag == True: break sorted_avg_err = sorted(avg_deg_err_list, key=lambda x: x[1]) curr_avg_deg_error = sorted_avg_err[0][1] if sorted_avg_err[0][1] <= tolerance: best_G = sorted_avg_err[0][0] best_avg_deg = sorted_avg_err[0][3] best_diam = nx.algorithms.diameter(best_G) break else: lower_lim = sorted_avg_err[0][2] - p_gap upper_lim = sorted_avg_err[0][2] + p_gap end_time = time() print('Graph_Name: duplication divergence graph') print('Num_Nodes: ', nx.number_of_nodes(best_G), ' Avg_Deg : ', best_avg_deg, ' Diameter: ', best_diam) print('TIME: ', end_time - strt_time) return best_G, best_avg_deg, best_diam
aggregated_results[func.__name__, 'yerr'] = timing.std(axis=1) return aggregated_results def plot_results(results): fig, ax = plt.subplots() x = [len(i) for i in results.index] for func in results.columns.levels[0]: y = results[func, 'avg'] yerr = results[func, 'yerr'] ax.errorbar(x, y, yerr=yerr, fmt='-o', label=func) ax.set_xlabel('number of nodes (nx.duplication_divergence_graph(n,0.5))') ax.set_ylabel('Time [s]') ax.set_yscale('log') ax.legend() return fig, ax G_5 = nx.duplication_divergence_graph(5, 0.5) G_50 = nx.duplication_divergence_graph(50, 0.5) G_100 = nx.duplication_divergence_graph(100, 0.5) G_500 = nx.duplication_divergence_graph(500, 0.5) G_1000 = nx.duplication_divergence_graph(1000, 0.5) G_2500 = nx.duplication_divergence_graph(4000, 0.5) # plot_times( [scalable_algo_sequential.seq_k_core_decompo, naive_algo.naive_approach], [G_5, G_50, G_100, G_500, G_1000, G_2500])
# Erdos-Renyi for prob in [0.2, 0.4, 0.5, 0.6, 0.7]: for size in [20, 40, 50, 70, 100]: file_name = './{}/erdos_renyi_{}_{}_{}.txt'.format( PATH, size, prob, seed) G = NX.erdos_renyi_graph(size, prob, seed) NX.write_edgelist(G, file_name) seed += 1 # Duplication-divergence for prob in [0.2, 0.4, 0.5, 0.6, 0.7, 0.8]: for size in [20, 40, 50, 70, 100]: file_name = './{}/duplication_divergence_{}_{}_{}.txt'.format( PATH, size, prob, seed) G = NX.duplication_divergence_graph(size, prob, seed) NX.write_edgelist(G, file_name) seed += 1 # GNM for size_to_edges in [lambda x: x // 2, lambda x: x, lambda x: x * 3 // 2]: for size in [20, 40, 50, 70, 100]: file_name = './{}/gnm_{}_{}_{}.txt'.format(PATH, size, size_to_edges(size), seed) G = NX.gnm_random_graph(size, size_to_edges(size), seed) NX.write_edgelist(G, file_name) seed += 1 for size_to_edges in [lambda x: x**(3 / 2), lambda x: x**2 // 2]: for size in [20, 40, 50, 70]: file_name = './{}/gnm_{}_{}_{}.txt'.format(PATH, size,
def duplication_divergence_graph(N, deg, dia, dim, domain): '''Returns an undirected graph using the duplication-divergence model. A graph of ``n`` nodes is created by duplicating the initial nodes and retaining edges incident to the original nodes with a retention probability ``p``. Parameters of the graph: n (int) – The desired number of nodes in the graph. p (float) – The probability for retaining the edge of the replicated node. Args: n (int or iterable) – Number of nodes or iterable of nodes dia (float) – Distance threshold value dim (int, optional): Dimension of the graph domain (str, optional): Domain of the graph Returns: Object: Best graph, beast average degree and best diameter. ''' strt_time = time() tolerance = 0.5 if deg == 4: lower_lim = 0.3 upper_lim = 0.4 bands = 10 elif deg == 6: lower_lim = 0.4 upper_lim = 0.6 bands = 15 elif deg == 8: lower_lim = 0.46 upper_lim = 0.60 bands = 15 elif deg == 10: lower_lim = 0.50 upper_lim = 0.65 bands = 15 elif deg == 12: lower_lim = 0.55 upper_lim = 0.68 bands = 15 flag = False curr_avg_deg_error = float('inf') while curr_avg_deg_error > tolerance: p_space = np.linspace(lower_lim, upper_lim, bands) avg_deg_err_list = [] p_gap = p_space[1] - p_space[0] for p_val in p_space: G = nx.duplication_divergence_graph(n=N, p=p_val) lcc, _ = graph_util.get_nk_lcc_undirected(G) curr_avg_deg = np.mean(list(dict(nx.degree(lcc)).values())) curr_avg_deg_error = abs(deg - curr_avg_deg) avg_deg_err_list.append((lcc, curr_avg_deg_error, p_val, curr_avg_deg)) if deg - curr_avg_deg < 0: break if curr_avg_deg_error <= tolerance: best_G = lcc best_avg_deg = curr_avg_deg best_diam = nx.algorithms.diameter(best_G) flag = True break if flag == True: break sorted_avg_err = sorted(avg_deg_err_list, key=lambda x: x[1]) curr_avg_deg_error = sorted_avg_err[0][1] if sorted_avg_err[0][1] <= tolerance: best_G = sorted_avg_err[0][0] best_avg_deg = sorted_avg_err[0][3] best_diam = nx.algorithms.diameter(best_G) break else: lower_lim = sorted_avg_err[0][2] - p_gap upper_lim = sorted_avg_err[0][2] + p_gap end_time = time() print('Graph_Name: duplication divergence graph') print('Num_Nodes: ', nx.number_of_nodes(best_G), ' Avg_Deg : ', best_avg_deg, ' Diameter: ', best_diam) print('TIME: ', end_time - strt_time) return best_G, best_avg_deg, best_diam
def get_duplication_graph(n, p, special_edge = None): G = nx.duplication_divergence_graph(n, p) if special_edge != None: G.add_edge(special_edge[0], special_edge[1]) return G
args=(nodes_split[t], G, counters, level)) thread_list.append(thread) thread_list[t].start() for thread in thread_list: thread.join() thread_list = [] level += 1 visited = counters.get_visited() deg = counters.deg.copy() return deg G_big = nx.duplication_divergence_graph(10000, 0.5) a = time.time() kcore_G_big_pkc = pkc(G_big, 9) b = time.time() - a # Test on toy graph # G_toy = nx.Graph([]) # G_toy.add_edges_from([(1,2), (1,8), (1,9), (5,2), (5,3), (5,4), (5,6), (6,7), (6,8), # (6,9), (7,8), (7,9), (8,9), (9,10), (10,11), (11,14), (11,15), # (11,12), (14,15), (15,12), (12,14)]) # # kcore_G_toy = pkc(G_toy, 7) # assert(nx.core_number(G_toy) == kcore_G_toy) # print(kcore_G_toy)
import numpy as np import networkx as nx import matplotlib.pyplot as plt import pkc_cython DTYPE = np.intc def plot_graph(G): nx.draw(G, with_labels=True) plt.show() G = nx.duplication_divergence_graph(10, 0.5, seed=10) # plot_graph(G) true_kcore = nx.core_number(G) # Extract nodes and lists of neighbors from G as numpy arrays n = G.number_of_nodes() nodes = np.arange(n, dtype=DTYPE) neighbors = [list(G.neighbors(n)) for n in nodes] deg_init = np.array([len(x) for x in neighbors], dtype=DTYPE) max_n_neighbors = max(len(x) for x in neighbors) neighbors = [x + [-1] * (max_n_neighbors - len(x)) for x in neighbors] neighbors = np.array(neighbors, dtype=DTYPE) pkc_out = pkc_cython.pkc(deg_init, deg_init, neighbors)