def test_binomial_tree(self): graphs = (None, nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph) for create_using in graphs: for n in range(0, 4): b = nx.binomial_tree(n, create_using) assert nx.number_of_nodes(b) == 2**n assert nx.number_of_edges(b) == (2**n - 1)
def get_test_graphs() -> List[nx.Graph]: # return nx.graph_atlas_g()[1:100] names = [ "Path graph (10)", "Complete graph (30)", "Balanced Tree (2, 3)", "Barbell graph (5, 1)", "Binomial tree (4)", "Circular ladder graph (5)", "Cycle graph (10)", "Star graph (10)", "Wheel graph (6)" ] optimal_colorings = [2, 30, 2, 5, 2, 3, 2, 2, 4] graphs = [ nx.path_graph(10), nx.complete_graph(30), nx.balanced_tree(2, 3), nx.barbell_graph(5, 1), nx.binomial_tree(4), nx.circular_ladder_graph(5), nx.cycle_graph(10), nx.star_graph(10), nx.wheel_graph(6) ] # load graph instances for file in os.listdir("./instances"): new_graph = nx.Graph() with open(os.path.join("instances", file), "r") as f: # data = f.read() edges = [] line = f.readline() line.strip() while line: if " " not in line: break # need indexes to be from 0 edges.append([int(x) - 1 for x in line.split(" ")]) line = f.readline() line.strip() # last line is the optimal coloring if line == '?': continue # ignore graphs for which we don't know the optimal coloring new_graph.add_edges_from(edges) if len(new_graph.nodes) > 200 or len(new_graph.edges) > 5000: continue names.append(file) optimal_colorings.append(line) graphs.append(new_graph) for i, g in enumerate(graphs): g.name = names[i] return graphs, optimal_colorings
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 test_binomial_tree(self): for n in range(0, 4): b = nx.binomial_tree(n) assert nx.number_of_nodes(b) == 2**n assert nx.number_of_edges(b) == (2**n - 1)
I = 'I' S = 'S' R = 'R' def color_estado(estado): if estado == S: return 'blue' if estado == I: return 'red' if estado == R: return 'green' G = nx.binomial_tree(5) for n in G.nodes: G.nodes[n]['probabilidad'] = 0 G.nodes[n]['estado'] = S if random.random() < 0.5 else I G.nodes[n]['Ti'] = 4 if G.nodes[n]['estado'] == I else 0 G.nodes[n]['Ri'] = 0 G.graph['colores'] = [color_estado(G.nodes[n]['estado']) for n in G.nodes] #nx.draw_circular(G, node_color=G.graph['colores'], with_labels=True) plt.show() def contagiarse(probabilidad): return random.random() < probabilidad def calcularProbabilidad(grafo, nodo):
'args': ('m1', 'm2'), 'argtypes': (int, int), 'argvals': (3, 3), 'gen': lambda m1, m2: nx.barbell_graph(m1, m2), 'description_fn': 'barbell_graph(m1, m2)', 'description': 'Returns the Barbell Graph: two complete graphs connected by a path.' }, 'binomial_tree': { 'name': 'Binomial Tree', 'args': ('n', ), 'argtypes': (int, ), 'argvals': (3, ), 'gen': lambda n: nx.binomial_tree(n), 'description_fn': 'binomial_tree(n)', 'description': 'Returns the Binomial Tree of order n.', }, 'circular_ladder_graph': { 'name': 'Circular Ladder Graph', 'args': ('n', ), 'argtypes': (int, ), 'argvals': (3, ), 'gen': lambda n: nx.circular_ladder_graph(n), 'description_fn': 'circular_ladder_graph(n)', 'description': 'Returns the circular ladder graph CLn of length n.' }, 'cycle_graph': { 'name': 'Cycle Graph', 'args': ('n', ),
# Daniel Hammer # Import the necessary package 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)
import networkx as nx import matplotlib.pyplot as plt import algorithms # graph1 = nx.Graph() # graph1.add_nodes_from([i for i in range(5)]) # graph1.add_edges_from([(0, 1), (1, 2), (1, 4), (2, 3), (2, 4)]) graph2 = nx.binomial_tree(5) pos = nx.shell_layout(graph2) # nx.draw(graph2, with_labels=True) # plt.show() def bfs_demo(): # pos = nx.kamada_kawai_layout(graph1) # plt.subplot(221) # nx.draw(graph1, with_labels=True, pos=pos) # plt.subplot(222) # res_graph = algorithms.path_to_graph(graph1, # algorithms.bfs(graph1, 0)) # edge_labels = {(v, u): res_graph.get_edge_data(v, u).get('weight') # for (v, u) in res_graph.edges # if res_graph.get_edge_data(v, u).get('weight') is not None} # nx.draw_networkx_edge_labels(res_graph, pos, edge_labels=edge_labels) # nx.draw(res_graph, with_labels=True, pos=pos) pos = nx.spring_layout(graph2) plt.subplot(121) nx.draw(graph2, pos=pos, with_labels=True)