def expanders_graphs(): print("Margulis - Gabber - Galil Graph") MGGG = nx.margulis_gabber_galil_graph(n=3) draw_graph(MGGG) print("Chordal - Cycle Graph") MGGG = nx.chordal_cycle_graph(8) draw_graph(MGGG)
def GenerateGossipMatrix(NoAgents,Topology): if(Topology == "Grid"): Graph_Adj = nx.adj_matrix(nx.grid_graph([int(np.sqrt(NoAgents)),int(np.sqrt(NoAgents))],periodic=True)).toarray() elif(Topology=="Cycle"): Graph_Adj = nx.adj_matrix(nx.grid_graph([NoAgents],periodic=True)).toarray() elif(Topology=="Expander"): Graph_Adj = nx.adj_matrix(nx.margulis_gabber_galil_graph(int(np.sqrt(NoAgents)))).toarray() Graph_Gossip = np.identity(NoAgents) - (np.diag(Graph_Adj.sum(axis=0)) - Graph_Adj )/(Graph_Adj.sum(axis=0).max() + 1) if( np.sum(Graph_Gossip.sum(0) != 1.) > 0 or np.sum(Graph_Gossip.sum(1) != 1.) ): print("Gossip Matrix not doubly stochastic! ") return(Graph_Gossip)
def expander(weight: Tensor, expander_name: str = "paley"): expanders = { "mgg": lambda n: nx.margulis_gabber_galil_graph(int(math.sqrt(n))), # margulis_gabber_galil_graph - weight size must have integer square root "chordal": nx. chordal_cycle_graph, # chordal_cycle_graph - weight size must be prime number "paley": nx.paley_graph # paley_graph - weight size must be prime number } G = expanders[expander_name](weight.size(0)) mask = nx.linalg.graphmatrix.adjacency_matrix(G).todense() return weight * torch.from_numpy(mask)
def __init__(self, n=DEFAULT_N, seed=None): self.G = nx.margulis_gabber_galil_graph(n) self.pos = None #class Expander2(Graph): # __type__ = 'expander2' # __expected__ = (100, 716) # # DEFAULT_P = 100 # # def __init__(self, p = DEFAULT_P, seed = None): # self.G = nx.chordal_cycle_graph(p) self.pos = None
def make_graph(g_name, n): switcher = { "path": nx.path_graph(n), "complete": nx.complete_graph(n), "binomial tree": nx.binomial_tree(n), "circular ladder": nx.circular_ladder_graph(n), "cycle": nx.cycle_graph(n), "dorogovtsev": nx.dorogovtsev_goltsev_mendes_graph(n), "ladder": nx.ladder_graph(n), "star": nx.star_graph(n), "wheel": nx.wheel_graph(n), "margulis gabber galil": nx.margulis_gabber_galil_graph(n), "chordal cycle": nx.chordal_cycle_graph(n), "hypercube": nx.hypercube_graph(n), "mycielski": nx.mycielski_graph(n) } return switcher.get(g_name)
def graphGenerator(): if args.graph_type == "erdos_renyi": return networkx.erdos_renyi_graph(args.graph_size,args.graph_p) if args.graph_type == "balanced_tree": ndim = int(np.ceil(np.log(args.graph_size)/np.log(args.graph_degree))) return networkx.balanced_tree(args.graph_degree,ndim) if args.graph_type == "cicular_ladder": ndim = int(np.ceil(args.graph_size*0.5)) return networkx.circular_ladder_graph(ndim) if args.graph_type == "cycle": return networkx.cycle_graph(args.graph_size) if args.graph_type == 'grid_2d': ndim = int(np.ceil(np.sqrt(args.graph_size))) return networkx.grid_2d_graph(ndim,ndim) if args.graph_type == 'lollipop': ndim = int(np.ceil(args.graph_size*0.5)) return networkx.lollipop_graph(ndim,ndim) if args.graph_type =='expander': ndim = int(np.ceil(np.sqrt(args.graph_size))) return networkx.margulis_gabber_galil_graph(ndim) if args.graph_type =="hypercube": ndim = int(np.ceil(np.log(args.graph_size)/np.log(2.0))) return networkx.hypercube_graph(ndim) if args.graph_type =="star": ndim = args.graph_size-1 return networkx.star_graph(ndim) if args.graph_type =='barabasi_albert': return networkx.barabasi_albert_graph(args.graph_size,args.graph_degree) if args.graph_type =='watts_strogatz': return networkx.connected_watts_strogatz_graph(args.graph_size,args.graph_degree,args.graph_p) if args.graph_type =='regular': return networkx.random_regular_graph(args.graph_degree,args.graph_size) if args.graph_type =='powerlaw_tree': return networkx.random_powerlaw_tree(args.graph_size) if args.graph_type =='small_world': ndim = int(np.ceil(np.sqrt(args.graph_size))) return networkx.navigable_small_world_graph(ndim) if args.graph_type =='geant': return topologies.GEANT() if args.graph_type =='dtelekom': return topologies.Dtelekom() if args.graph_type =='abilene': return topologies.Abilene() if args.graph_type =='servicenetwork': return topologies.ServiceNetwork()
def test_modularity(): G = nx.barbell_graph(3, 0) C = [{0, 1, 4}, {2, 3, 5}] assert almost_equal(-16 / (14**2), modularity(G, C)) C = [{0, 1, 2}, {3, 4, 5}] assert almost_equal((35 * 2) / (14**2), modularity(G, C)) n = 1000 G = nx.erdos_renyi_graph(n, 0.09, seed=42, directed=True) C = [set(range(n // 2)), set(range(n // 2, n))] assert almost_equal(0.00017154251389292754, modularity(G, C)) G = nx.margulis_gabber_galil_graph(10) mid_value = G.number_of_nodes() // 2 nodes = list(G.nodes) C = [set(nodes[:mid_value]), set(nodes[mid_value:])] assert almost_equal(0.13, modularity(G, C)) G = nx.DiGraph() G.add_edges_from([(2, 1), (2, 3), (3, 4)]) C = [{1, 2}, {3, 4}] assert almost_equal(2 / 9, modularity(G, C))
import math import json settings.init() # Loads all the data from settings models.init() # Loads the models and network variables #################### # Network creation # #################### if settings.network_type == 0: G = nx.complete_graph(settings.number_of_nodes) if settings.network_type == 1: G = nx.barabasi_albert_graph(settings.number_of_nodes, 10) if settings.network_type == 2: G = nx.margulis_gabber_galil_graph(settings.number_of_nodes, None) # More types of networks can be added here ############## # Simulation # ############## sim = NetworkSimulation(topology=G, states=init_states, agent_type=ControlModelM2, max_time=settings.max_time, num_trials=settings.num_trials, logging_interval=1.0) sim.run_simulation()
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
import networkx as nx n = 4 #def graph_caller(self, graph_name): switcher = { "path": nx.path_graph(n), "complete": nx.complete_graph(n), "binomial tree": nx.binomial_tree(n), "circular ladder": nx.circular_ladder_graph(n), "cycle": nx.cycle_graph(n), "dorogovtsev": nx.dorogovtsev_goltsev_mendes_graph(n), "ladder": nx.ladder_graph(n), "star": nx.star_graph(n), "wheel": nx.wheel_graph(n), "margulis gabber galil": nx.margulis_gabber_galil_graph(n), "chordal cycle": nx.chordal_cycle_graph(n), "hypercube": nx.hypercube_graph(n), "mycielski": nx.mycielski_graph(n) } # return switcher.get(graph_name,"Invalid choice") graph_name = input("Enter a graph name to generate\n") print(graph_name) #G = graph_caller(graph_name) G = switcher.get(graph_name) for k in range(3,11): for x in range(3,11):
#write_graph(nx.barbell_graph(10, 1), f) # # #f = open("barbell1000-1000.graph", "w+") #write_graph(nx.barbell_graph(1000, 1), f) #f = open("barbell10000-10000.graph", "w+") #write_graph(nx.barbell_graph(10000, 10000), f) #f = open("barbell100000-100000.graph", "w+") #write_graph(nx.barbell_graph(100000, 100000), f) #for i in [10, 100, 1000]: # f = open(f"barbell{i}-{i}.graph", "w+") # write_graph(nx.barbell_graph(i, 0), f) #for i in [10, 100, 1000]: # f = open(f"complete{i}.graph", "w+") # write_graph(nx.complete_graph(i), f) for i in [2]: f = open(f"expander{i**2}.graph", "w+") write_graph(nx.margulis_gabber_galil_graph(i), f)