本文整理汇总了Python中networkx.barbell_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.barbell_graph方法的具体用法?Python networkx.barbell_graph怎么用?Python networkx.barbell_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.barbell_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def main(args):
# print "------begin to write graph---------"
# generate_graph_write_edgelist(args.m1,args.m2,args.input)
print "begin to initialize transition matrix"
trans_matrix = initialize_edge_type_matrix(args.type_size)
print trans_matrix
print "------begin to read graph---------"
G = read_graph(args.input,args.weighted,args.directed)
# print G.edges(data=True)
# nodes = list(G.nodes)
# print G.number_of_edges(),nodes,[n for n in G.neighbors('3')]
# # G=nx.barbell_graph(17,1)
# # draw_graph(G)
print "------begin to simulate walk---------"
for i in range(args.em_iteration):
walks = simulate_walks(G,args.num_walks, args.walk_length,trans_matrix,args.directed,args.p,args.q)#M step
print str(i), "th iteration for Upating transition matrix!"
trans_matrix = update_trans_matrix(walks,args.type_size,args.e_step)#E step
print "trans_matrix: ",trans_matrix
# print walks
print "------finish!---------"
np.savetxt(args.output, trans_matrix)
示例2: test_to_networkx_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_to_networkx_graph(self):
import networkx as nx
graph = nx.barbell_graph(7, 6)
# build a BQM
model = dimod.BinaryQuadraticModel({v: -.1 for v in graph},
{edge: -.4 for edge in graph.edges},
1.3,
vartype=dimod.SPIN)
# get the graph
BQM = model.to_networkx_graph()
self.assertEqual(set(graph), set(BQM))
for u, v in graph.edges:
self.assertIn(u, BQM[v])
for v, bias in model.linear.items():
self.assertEqual(bias, BQM.nodes[v]['bias'])
示例3: barbell_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def barbell_graph(m1,m2):
graph = nx.barbell_graph(m1,m2)
## for com_nc, one hot
#onehot_com = np.array([[1,0,0]]*m1+[[0,1,0]]*m2+[[0,0,1]]*m1) is slower when num of nodes > 2000
node_labels_com = np.zeros(m1*2+m2).astype(int)
node_labels_com[m1:m1+m2] = 2
node_labels_com[m1+m2:] = 1
## one hot
onehot_com = np.zeros((m1*2+m2,3)).astype(int)
onehot_com[np.arange(m1*2+m2), node_labels_com] = 1
## for role_nc, one hot
node_labels_role = np.zeros(m1*2+m2).astype(int)
p,q = divmod(m2, 2)
for i in range(p+1):
node_labels_role[[m1-1+i,m1+m2-i]] = i+1
if q:
node_labels_role[m1+p] = p+2
onehot_role = np.zeros((m1*2+m2,p+q+2)).astype(int)
onehot_role[np.arange(m1*2+m2), node_labels_role] = 1
return graph, scipy.sparse.csr_matrix(onehot_com), scipy.sparse.csr_matrix(onehot_role)
##########################################################################
示例4: test_typical
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_typical(self):
graph = nx.barbell_graph(17, 8)
edgelist = set(graph.edges())
adj = dwave.embedding.utils.edgelist_to_adjacency(edgelist)
# test that they're equal
for u, v in edgelist:
self.assertIn(u, adj)
self.assertIn(v, adj)
self.assertIn(u, adj[v])
self.assertIn(v, adj[u])
for u in adj:
for v in adj[u]:
self.assertTrue((u, v) in edgelist or (v, u) in edgelist)
self.assertFalse((u, v) in edgelist and (v, u) in edgelist)
示例5: test_graphs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_graphs(self):
H = nx.complete_graph(2)
H.add_edge(2, 3)
graphs = [nx.complete_graph(7),
dnx.chimera_graph(2, 1, 3),
nx.balanced_tree(5, 3),
nx.barbell_graph(8, 11),
nx.cycle_graph(5),
H]
for G in graphs:
tw, order = dnx.treewidth_branch_and_bound(G)
self.assertEqual(dnx.elimination_order_width(G, order), tw)
tw, order = dnx.min_width_heuristic(G)
self.assertEqual(dnx.elimination_order_width(G, order), tw)
tw, order = dnx.min_fill_heuristic(G)
self.assertEqual(dnx.elimination_order_width(G, order), tw)
tw, order = dnx.max_cardinality_heuristic(G)
self.assertEqual(dnx.elimination_order_width(G, order), tw)
示例6: test_linear_ranges_specified
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_linear_ranges_specified(self):
graph = nx.barbell_graph(4, 16)
decision_variables = (0, 4, 3)
feasible_configurations = {(0, 0, 1): 0.}
spec = pm.Specification(graph, decision_variables, feasible_configurations,
ising_linear_ranges={v: [-v, 2] for v in graph},
vartype=dimod.BINARY)
# check default energy ranges
for v in graph:
self.assertEqual(spec.ising_linear_ranges[v], [-v, 2])
spec = pm.Specification(graph, decision_variables, feasible_configurations,
ising_linear_ranges={v: (-v, 2) for v in graph},
vartype=dimod.BINARY)
# check default energy ranges
for v in graph:
self.assertEqual(spec.ising_linear_ranges[v], [-v, 2])
示例7: test_quadratic_ranges_partially_specified
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_quadratic_ranges_partially_specified(self):
graph = nx.barbell_graph(4, 16)
decision_variables = (0, 4, 3)
feasible_configurations = {(0, 0, 1): 0.}
spec = pm.Specification(graph, decision_variables, feasible_configurations,
ising_quadratic_ranges={0: {1: [0, 1], 2: [-1, 0]}, 2: {0: [-1, 0]}},
vartype=dimod.BINARY)
ising_quadratic_ranges = spec.ising_quadratic_ranges
for u in ising_quadratic_ranges:
for v in ising_quadratic_ranges[u]:
self.assertIs(ising_quadratic_ranges[u][v], ising_quadratic_ranges[v][u])
for u, v in graph.edges:
self.assertIn(v, ising_quadratic_ranges[u])
self.assertIn(u, ising_quadratic_ranges[v])
self.assertEqual(ising_quadratic_ranges[0][1], [0, 1])
示例8: test_clique_removal
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_clique_removal():
graph = nx.complete_graph(10)
i, cs = apxa.clique_removal(graph)
idens = nx.density(graph.subgraph(i))
eq_(idens, 0.0, "i-set not found by clique_removal!")
for clique in cs:
cdens = nx.density(graph.subgraph(clique))
eq_(cdens, 1.0, "clique not found by clique_removal!")
graph = nx.trivial_graph(nx.Graph())
i, cs = apxa.clique_removal(graph)
idens = nx.density(graph.subgraph(i))
eq_(idens, 0.0, "i-set not found by ramsey!")
# we should only have 1-cliques. Just singleton nodes.
for clique in cs:
cdens = nx.density(graph.subgraph(clique))
eq_(cdens, 0.0, "clique not found by clique_removal!")
graph = nx.barbell_graph(10, 5, nx.Graph())
i, cs = apxa.clique_removal(graph)
idens = nx.density(graph.subgraph(i))
eq_(idens, 0.0, "i-set not found by ramsey!")
for clique in cs:
cdens = nx.density(graph.subgraph(clique))
eq_(cdens, 1.0, "clique not found by clique_removal!")
示例9: test_ramsey
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_ramsey():
# this should only find the complete graph
graph = nx.complete_graph(10)
c, i = apxa.ramsey_R2(graph)
cdens = nx.density(graph.subgraph(c))
eq_(cdens, 1.0, "clique not found by ramsey!")
idens = nx.density(graph.subgraph(i))
eq_(idens, 0.0, "i-set not found by ramsey!")
# this trival graph has no cliques. should just find i-sets
graph = nx.trivial_graph(nx.Graph())
c, i = apxa.ramsey_R2(graph)
cdens = nx.density(graph.subgraph(c))
eq_(cdens, 0.0, "clique not found by ramsey!")
idens = nx.density(graph.subgraph(i))
eq_(idens, 0.0, "i-set not found by ramsey!")
graph = nx.barbell_graph(10, 5, nx.Graph())
c, i = apxa.ramsey_R2(graph)
cdens = nx.density(graph.subgraph(c))
eq_(cdens, 1.0, "clique not found by ramsey!")
idens = nx.density(graph.subgraph(i))
eq_(idens, 0.0, "i-set not found by ramsey!")
示例10: test_disconnected_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_disconnected_graph(self):
"""Test for a graph with multiple connected components."""
G = nx.barbell_graph(3, 0)
H = nx.barbell_graph(3, 0)
mapping = dict(zip(range(6), 'abcdef'))
nx.relabel_nodes(H, mapping, copy=False)
G = nx.union(G, H)
chains = list(nx.chain_decomposition(G))
expected = [
[(0, 1), (1, 2), (2, 0)],
[(3, 4), (4, 5), (5, 3)],
[('a', 'b'), ('b', 'c'), ('c', 'a')],
[('d', 'e'), ('e', 'f'), ('f', 'd')],
]
self.assertEqual(len(chains), len(expected))
for chain in chains:
self.assertContainsChain(chain, expected)
示例11: test_barbell
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_barbell():
G = nx.barbell_graph(8, 4)
nx.add_path(G, [7, 20, 21, 22])
nx.add_cycle(G, [22, 23, 24, 25])
pts = set(nx.articulation_points(G))
assert_equal(pts, {7, 8, 9, 10, 11, 12, 20, 21, 22})
answer = [
{12, 13, 14, 15, 16, 17, 18, 19},
{0, 1, 2, 3, 4, 5, 6, 7},
{22, 23, 24, 25},
{11, 12},
{10, 11},
{9, 10},
{8, 9},
{7, 8},
{21, 22},
{20, 21},
{7, 20},
]
assert_components_equal(list(nx.biconnected_components(G)), answer)
G.add_edge(2, 17)
pts = set(nx.articulation_points(G))
assert_equal(pts, {7, 20, 21, 22})
示例12: test_grow_maximal
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_grow_maximal(self, dim):
"""Test if function grows to expected maximal graph and then stops. The chosen graph is
composed of two fully connected graphs joined together at one node. Starting from the
first node, ``grow`` is expected to grow to be the first fully connected graph."""
graph = nx.barbell_graph(dim, 0)
s = [0]
assert set(clique.grow(s, graph)) == set(range(dim))
示例13: test_bad_node_select
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_bad_node_select(self, dim):
"""Tests if function raises a ``ValueError`` when input an invalid ``node_select``
argument"""
graph = nx.barbell_graph(dim, 0)
s = [0]
with pytest.raises(ValueError, match="Node selection method not recognized"):
clique.grow(s, graph, node_select="")
示例14: test_weight_based_ties
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def test_weight_based_ties(self, dim):
"""Test that the function returns the correct clique when using weight-based node
selection to settle ties. The starting graph is a barbell graph and the subgraph is taken
to be the whole graph. The weights of the first clique in the barbell are set to be less
than the weights of the second, so that we expect the function to return the second
clique."""
graph = nx.barbell_graph(dim, 0)
subgraph = graph.nodes()
weights = [1] * dim + [2] * dim
c = clique.shrink(subgraph, graph, node_select=weights)
assert c == list(range(dim, 2 * dim))
示例15: is_k_edge_connected
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import barbell_graph [as 别名]
def is_k_edge_connected(G, k):
"""
Tests to see if a graph is k-edge-connected
See Also
--------
is_locally_k_edge_connected
Example
-------
>>> G = nx.barbell_graph(10, 0)
>>> is_k_edge_connected(G, k=1)
True
>>> is_k_edge_connected(G, k=2)
False
"""
if k < 1:
raise ValueError('k must be positive, not {}'.format(k))
# First try to quickly determine if G is not k-edge-connected
if G.number_of_nodes() < k + 1:
return False
elif any(d < k for n, d in G.degree()):
return False
else:
# Otherwise perform the full check
if k == 1:
return nx.is_connected(G)
elif k == 2:
return not nx.has_bridges(G)
else:
# return nx.edge_connectivity(G, cutoff=k) >= k
return nx.edge_connectivity(G) >= k