def test(): n = random.randint(10, 200) seq = nx.random_powerlaw_tree_sequence(n, 3, None, 500) g = nx.degree_sequence_tree(seq) sig = greedy.normal_greedy(g, random.randint(0, n)) cos = cost.cost_function(g, sig) print n print "Installation order is %s \n" % sig print "The cost of this order is %s \n" % cos
def test_degree_sequence_tree(): z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4] G = nx.degree_sequence_tree(z) assert_equal(len(G), len(z)) assert_true(len(list(G.edges())) == sum(z) / 2) assert_raises(nx.NetworkXError, nx.degree_sequence_tree, z, create_using=nx.DiGraph()) z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4] assert_raises(nx.NetworkXError, nx.degree_sequence_tree, z)
def test_degree_sequence_tree(): z=[1, 1, 1, 1, 1, 2, 2, 2, 3, 4] G=nx.degree_sequence_tree(z) assert_equal(len(G), len(z)) assert_true(len(list(G.edges())) == sum(z)/2) assert_raises(nx.NetworkXError, nx.degree_sequence_tree, z, create_using=nx.DiGraph()) z=[1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4] assert_raises(nx.NetworkXError, nx.degree_sequence_tree, z)
def test_degree_sequence_tree(): z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4] G = nx.degree_sequence_tree(z) assert len(G) == len(z) assert len(list(G.edges())) == sum(z) / 2 pytest.raises( nx.NetworkXError, nx.degree_sequence_tree, z, create_using=nx.DiGraph() ) z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4] pytest.raises(nx.NetworkXError, nx.degree_sequence_tree, z)
def deg_seq_tree(N): degree_sequence = [ randint(1,6) for i in range(N) ] if sum(degree_sequence) % 2 == 1: degree_sequence[0] += 1 while len(degree_sequence)-sum(degree_sequence)//2 != 1: degree_sequence.append(0) _G = nx.degree_sequence_tree(degree_sequence) return _G
def testJ1(): n = random.randint(10, 50) seq = nx.random_powerlaw_tree_sequence(n, 3, None, 500) tree = nx.degree_sequence_tree(seq) a = random.randint(0, n) b = random.randint(0, n) while (b == a or tree.has_edge(a, b)): b = random.randint(0, n) tree.add_edge(a, b) print "running on a", n, "node tree with (", a, ",", b, ") added\n" blackstart = random.randint(0, n - 1) sig1 = greedy.normal_greedy(tree, blackstart) cos1 = cost.cost_function(tree, sig1) sig2 = greedy.percentage_greedy(tree, blackstart) cos2 = cost.cost_function(tree, sig2) print "Greedy cost for distinct cycle is", cos1
def degree_sequence_graphs(): print("Degree sequence") print("Configuration model") z = nx.utils.create_degree_sequence(30, powerlaw_sequence) G = nx.configuration_model(z) draw_graph(G) # to remove paralel edges G = nx.Graph(G) draw_graph(G) # to remove self loops G.remove_edges_from(G.selfloop_edges()) draw_graph(G) print("Directed configuration model") D = nx.DiGraph([(0, 1), (1, 2), (2, 3), (1, 3)]) # directed path graph din = list(D.in_degree().values()) dout = list(D.out_degree().values()) din.append(1) dout[0] = 2 D = nx.directed_configuration_model(din, dout) D = nx.DiGraph(D) # to remove paralell edges D.remove_edges_from(D.selfloop_edges()) # to remove self loops draw_graph(D) print("Expected degree graphs") z = [i for i in range(10)] G = nx.expected_degree_graph(z) draw_graph(G) print("Havel Hakimi graphs") z = [2, 4, 5, 6, 7, 3] G = nx.havel_hakimi_graph(z) draw_graph(G) print("Directed Havel Hakimi graphs") inds = [2, 4, 5, 6, 7, 3] outds = [2, 4, 5, 6, 7, 3] G = nx.directed_havel_hakimi_graph(in_deg_sequence=inds, out_deg_sequence=outds) draw_graph(G) print("Degree Sequence Tree") dg = [1, 2, 3, 4, 2, 3] G = nx.degree_sequence_tree(dg) draw_graph(G) print("Random Degree Sequence") sequence = [1, 2, 2, 3] G = nx.random_degree_sequence_graph(sequence) sorted(G.degree().values()) draw_graph(G)
def generate_tree(self, vertex_count, maximal_children_count): if vertex_count == 1: return nx.empty_graph(1) if vertex_count == 0 or maximal_children_count == 0: return nx.Graph() degree_sequence = [1] * vertex_count degree_sum = 2 * (vertex_count - 1) - vertex_count i = 0 while degree_sum > 0 and i < vertex_count - 2: degree = random.randint(1, min(maximal_children_count, degree_sum)) degree_sequence[i] += degree degree_sum -= degree i += 1 t = nx.degree_sequence_tree(degree_sequence) return t
def DegreeSequenceTree(deg_sequence): """ Returns a tree with the given degree sequence. Raises a NetworkX error if the proposed degree sequence cannot be that of a tree. Since every tree has one more vertex than edge, the degree sequence must satisfy len(deg_sequence) - sum(deg_sequence)/2 == 1. INPUT: - ``deg_sequence`` - a list of integers with each entry corresponding to the expected degree of a different vertex. EXAMPLE:: sage: G = graphs.DegreeSequenceTree([3,1,3,3,1,1,1,2,1]) sage: G.show() # long time """ import networkx return graph.Graph(networkx.degree_sequence_tree([int(i) for i in deg_sequence]))
def DegreeSequenceTree(deg_sequence): """ Returns a tree with the given degree sequence. Raises a NetworkX error if the proposed degree sequence cannot be that of a tree. Since every tree has one more vertex than edge, the degree sequence must satisfy len(deg_sequence) - sum(deg_sequence)/2 == 1. INPUT: - ``deg_sequence`` - a list of integers with each entry corresponding to the expected degree of a different vertex. EXAMPLES:: sage: G = graphs.DegreeSequenceTree([3,1,3,3,1,1,1,2,1]) sage: G.show() # long time """ import networkx return Graph(networkx.degree_sequence_tree([int(i) for i in deg_sequence]))