def fraction_connected(n, p, count=20): """Return fraction of RandomGraphs G(n,p) that are connected.""" vertices = make_vertices(n) connected = 0.0 for i in range(count): g = RandomGraph(vertices) g.add_random_edges(p) if g.is_connected(): connected += 1 return connected / count
def __init__(self, node_num, p, in_channels, out_channels, graph_mode, is_train, name): super(RandWire, self).__init__() self.node_num = node_num self.p = p self.in_channels = in_channels self.out_channels = out_channels self.graph_mode = graph_mode self.is_train = is_train self.name = name # get graph nodes and in edges graph_node = RandomGraph(self.node_num, self.p, graph_mode=graph_mode) if self.is_train is True: print("is_train: True") graph = graph_node.make_graph() self.nodes, self.in_edges = graph_node.get_graph_info(graph) graph_node.save_random_graph(graph, name) else: graph = graph_node.load_random_graph(name) self.nodes, self.in_edges = graph_node.get_graph_info(graph) # define input Node self.module_list = nn.ModuleList([ Node(self.in_edges[0], self.in_channels, self.out_channels, stride=2) ]) # define the rest Node self.module_list.extend([ Node(self.in_edges[node], self.out_channels, self.out_channels) for node in self.nodes if node > 0 ])
def test_connected_components(): graph = RandomGraph() number_cc = len(connected_components(graph)) graph_data = networkx.Graph(graph.graph_dict) test = networkx.is_connected(graph_data) test = number_cc == 1 if test else number_cc != 1 assert test
def test_mst_prim(): graph = RandomGraph() mst = mst_prim(graph, graph.nodes()[0]) nx_graph = networkx.Graph() for node in graph.nodes(): for neighbor in graph.get(node): if nx_graph.has_edge(node, neighbor) is False and nx_graph.has_edge( neighbor, node) is False: nx_graph.add_edge(node, neighbor, weight=graph.get(node, neighbor)) tree = [] for node in mst: if mst[node] is not None: tree.append((node, mst[node])) print(tree) T = networkx.minimum_spanning_tree(nx_graph) edges = T.edges(data=False) # Begin the test test = True # I need to check if (x,y) or (y,x) is in networkx generated mst for node in tree: x, y = node print(x, y) test = test and ((x, y) in edges or (y, x) in edges) assert test
steps = 200 full_graph = FullGraph(5) todo = [{ "msg": "Running IL", "name": "IL", "partners": 0, "n_samples": args.n_samples, "fun": lambda: LJALPart1(graph=Graph(5)).n_steps(steps) }, { "msg": "Running LJAL-2", "name": "LJAL-2", "partners": 2, "n_samples": args.n_samples, "fun": lambda: LJALPart1(graph=RandomGraph(5, 2)).n_steps(steps) }, { "msg": "Running LJAL-3", "name": "LJAL-3", "partners": 3, "n_samples": args.n_samples, "fun": lambda: LJALPart1(graph=RandomGraph(5, 3)).n_steps(steps) }, { "msg": "Running JAL", "name": "JAL", "partners": 4, "n_samples": args.n_samples, "fun": lambda: LJALPart1(graph=full_graph).n_steps(steps) }] tasks.run(todo, args.save_name)
def test_cc(): graph = RandomGraph() number_cc = len(connected_components(graph)) graph_data = networkx.Graph(graph.graph_dict) assert networkx.number_connected_components(graph_data) == number_cc
try: from matplotlib import pyplot as plt from matplotlib import patches as mpatches except ImportError: matplotlib_available = False N_MAX = 100 n_list = list( range( 1, N_MAX+1 ) ) max_queue_list = [] max_stack_list = [] for n in n_list: # Generate a random Graph # 25% = 1 75% = 0 G = RandomGraph( n_vertex = n, unconnected_probability = 100000000 ) # if the adj_matrix have the main diag set to 1 # and s is 0 # BFS lowest queue -> DFS max stack ''' s = G.get_first_vertex() G.set_matrix_sub_diag_to_1() ''' # if all s column is set to 1 # DFS lowest stack -> BSF max queue s = G.get_random_source_S() G.set_matrix_column_to_1( s.get_key() ) G.show_adj_matrix()
from graph import RandomGraph from graph_functions import BFS from graph_functions import DFS_recursive from graph_functions import DFS_iterative import sys G = RandomGraph( n_vertex=10, unconnected_probability=3 ) G.show_adj_matrix() sys.stdout.flush() print("\nObtaining random source S") s = G.get_random_source_S() print("S is: " + str( s.get_key() ) ) max_queue = BFS( G, s ) print("\nAll vertex after BFS:") for vertex in G: print( vertex.get_key(), end=" color: " ) print( vertex.get_color() ) G.delete_white_vertices() print("\nWhite verticies deleted." ) print("\nLaunch DFS_iterative:") DFS_iterative( G, s, verbose=True )
def random(vertices, p): p = float(p) graph = RandomGraph(vertices) graph.add_random_edges(p) return graph
#!/usr/bin/env python from __future__ import print_function from graph import Graph, RandomGraph, Vertex import draw_graph as dg def test(): print('connected', g.is_connected()) g = RandomGraph([Vertex(i) for i in range(80)]) g.add_random_edges(.1) # g.add_regular_edges(2) if __name__ == '__main__': import timeit print(timeit.timeit("test()", number=10, setup="from __main__ import test;")) dg.draw(g, 1000)
def build_graph(filename, p): for i in range(start, end, offset): RandomGraph(list(range(i)), p).save_graph(filename + format_p(p) + str(i))
from graph import RandomGraph from random import randint from graph_functions import BFS G = RandomGraph( 10 ) G.generate_vertex_and_random_adj_matrix() G.show_adj_matrix() for vertex in G: if randint( 0,1 ) is 0: vertex.set_color("GREY") #G.delete_white_vertices() for vertex in G: print( vertex.get_key(), end=" color: " ) print( vertex.get_color() ) s = G.get_random_source_S() print( s.get_key() ) neighbors_set = G.get_neighbors_of_vertex( s ) print("neighbors are:") for neighbor in neighbors_set: print(neighbor.get_color())