def generate_random_uniform(N, T, G_method, seed=1234): rng = np.random.RandomState(seed) N_ = 4 if N >= 5 else N - 1 switcher = { 'complete': nx.complete_graph(N), 'path': nx.path_graph(N), 'cycle': nx.cycle_graph(N), 'regular': nx.random_regular_graph(N_, N, seed=rng), 'wheel': nx.wheel_graph(N), 'tree': nx.random_tree(N, seed=rng), 'chordal': nx.Graph(nx.chordal_cycle_graph(N)), } G = switcher.get(G_method) player_list = [] for n in range(N): p = Player(x=rng.uniform(3, -3, T), sm=13.5, s0=0, ram=5, ec=0.9, ed=0.9) player_list.append(p) buying_price = np.ones(T) * 3.0 selling_price = np.ones(T) * 1.0 game = Game(player_list, buying_price, selling_price, G) game.graphtype = G_method game.seed = seed return game
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 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 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 pytest import os from allegedb import ORM import networkx as nx scalefreestart = nx.MultiDiGraph(name='scale_free_graph_5') scalefreestart.add_edges_from([(0, 1), (1, 2), (2, 0)]) testgraphs = [ nx.chvatal_graph(), nx.scale_free_graph(5, create_using=scalefreestart), nx.chordal_cycle_graph( 5, create_using=nx.MultiGraph(name='chordal_cycle_graph_5')), ] # have to name it after creation because it clears the create_using path_graph_9 = nx.path_graph(9) path_graph_9.name = 'path_graph_9' testgraphs.append(path_graph_9) @pytest.fixture def db(): name = 'allegedb_load_test.db' if os.path.exists(name): os.remove(name) with ORM('sqlite:///' + name) as orm: for graph in testgraphs: { nx.Graph: orm.new_graph, nx.DiGraph: orm.new_digraph, nx.MultiGraph: orm.new_multigraph,
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): #greedy_coloring = nx.coloring.greedy_color(G, strategy='largest_first')