本文整理汇总了Python中networkx.cycle_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.cycle_graph方法的具体用法?Python networkx.cycle_graph怎么用?Python networkx.cycle_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.cycle_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_annotator
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_annotator():
# making graph
graph = nx.cycle_graph(5)
graph.add_edge(3, 6)
for n, d in graph.nodes(data=True):
d['label'] = 'X'
# annotate
a = Annotator()
graph = a.transform([graph], part_name='name', part_id='id').next()
# test annotation
cyc = 0
other = 0
ids = set()
for n, d in graph.nodes(data=True):
ids.add(d['id'][0]) # unpack because list unhashable
if d['name'] == ['XXXXX']:
cyc += 1
if d['name'] == ['X']:
other += 1
assert cyc == 5
assert other == 1
assert len(ids) == 2
示例2: test_energies_discard
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_energies_discard(self):
h = {'a': .1, 'b': 0, 'c': 0}
J = {('a', 'b'): 1, ('b', 'c'): 1.3, ('a', 'c'): -1}
bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=1.3)
embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)
embedded_response = dimod.ExactSolver().sample(embedded_bqm)
chain_break_method = dwave.embedding.discard
response = dwave.embedding.unembed_sampleset(embedded_response, embedding, bqm,
chain_break_method=chain_break_method)
self.assertEqual(len(embedded_response) / 2, len(response)) # half chains should be broken
for sample, energy in response.data(['sample', 'energy']):
self.assertEqual(bqm.energy(sample), energy)
示例3: test_unembed_sampleset_with_discard_matrix_typical
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_unembed_sampleset_with_discard_matrix_typical(self):
h = {'a': .1, 'b': 0, 'c': 0}
J = {('a', 'b'): 1, ('b', 'c'): 1.3, ('a', 'c'): -1}
bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=1.3)
embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)
embedded_response = dimod.ExactSolver().sample(embedded_bqm)
chain_break_method = dwave.embedding.discard
response = dwave.embedding.unembed_sampleset(embedded_response, embedding, bqm,
chain_break_method=dwave.embedding.discard)
self.assertEqual(len(embedded_response) / 2, len(response)) # half chains should be broken
for sample, energy in response.data(['sample', 'energy']):
self.assertEqual(bqm.energy(sample), energy)
示例4: test_embed_bqm_NAE3SAT_to_square
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_embed_bqm_NAE3SAT_to_square(self):
h = {'a': 0, 'b': 0, 'c': 0}
J = {('a', 'b'): 1, ('b', 'c'): 1, ('a', 'c'): 1}
bqm = dimod.BinaryQuadraticModel.from_ising(h, J)
embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)
self.assertEqual(embedded_bqm,
dimod.BinaryQuadraticModel({0: 0, 1: 0, 2: 0, 3: 0},
{(0, 1): 1, (1, 2): 1, (2, 3): -1, (0, 3): 1},
1.0, # offset the energy from satisfying chains
dimod.SPIN))
# check that the energy has been preserved
for config in itertools.product((-1, 1), repeat=3):
sample = dict(zip(('a', 'b', 'c'), config))
target_sample = {u: sample[v] for v, chain in embedding.items() for u in chain} # no chains broken
self.assertAlmostEqual(bqm.energy(sample), embedded_bqm.energy(target_sample))
示例5: test_energy_range_embedding
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_energy_range_embedding(self):
# start with an Ising
bqm = dimod.BinaryQuadraticModel.from_ising({}, {(0, 1): 1, (1, 2): 1, (0, 2): 1})
# convert to BINARY
bqm.change_vartype(dimod.BINARY, inplace=True)
# embedding
embedding = {0: ['a', 'b'], 1: ['c'], 2: ['d']}
graph = nx.cycle_graph(['a', 'b', 'c', 'd'])
graph.add_edge('a', 'c')
# embed
embedded = dwave.embedding.embed_bqm(bqm, embedding, graph, chain_strength=1, smear_vartype=dimod.SPIN)
preferred = dimod.BinaryQuadraticModel({'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.0},
{('a', 'c'): 0.5, ('b', 'c'): 0.5, ('c', 'd'): 1.0,
('a', 'd'): 1.0, ('a', 'b'): -1.0}, 1.0, dimod.SPIN)
self.assertEqual(embedded.spin, preferred)
示例6: test_graphs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_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)
示例7: test_negative_weight_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_negative_weight_cycle(self):
G = nx.cycle_graph(5, create_using=nx.DiGraph())
G.add_edge(1, 2, weight=-7)
for i in range(5):
assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, i)
assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
G = nx.cycle_graph(5) # undirected Graph
G.add_edge(1, 2, weight=-3)
for i in range(5):
assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, i)
assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
G = nx.DiGraph([(1, 1, {'weight': -1})])
assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, 1)
assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, 1)
# no negative cycle but negative weight
G = nx.cycle_graph(5, create_using=nx.DiGraph())
G.add_edge(1, 2, weight=-3)
assert_equal(nx.bellman_ford(G, 0),
({0: None, 1: 0, 2: 1, 3: 2, 4: 3},
{0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
assert_equal(nx.goldberg_radzik(G, 0),
({0: None, 1: 0, 2: 1, 3: 2, 4: 3},
{0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
示例8: test_directed_edge_connectivity
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_directed_edge_connectivity():
G = nx.cycle_graph(10, create_using=nx.DiGraph()) # only one direction
D = nx.cycle_graph(10).to_directed() # 2 reciprocal edges
for flow_func in flow_funcs:
assert_equal(1, nx.edge_connectivity(G, flow_func=flow_func),
msg=msg.format(flow_func.__name__))
assert_equal(1, local_edge_connectivity(G, 1, 4, flow_func=flow_func),
msg=msg.format(flow_func.__name__))
assert_equal(1, nx.edge_connectivity(G, 1, 4, flow_func=flow_func),
msg=msg.format(flow_func.__name__))
assert_equal(2, nx.edge_connectivity(D, flow_func=flow_func),
msg=msg.format(flow_func.__name__))
assert_equal(2, local_edge_connectivity(D, 1, 4, flow_func=flow_func),
msg=msg.format(flow_func.__name__))
assert_equal(2, nx.edge_connectivity(D, 1, 4, flow_func=flow_func),
msg=msg.format(flow_func.__name__))
示例9: test_eulerian_circuit_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_eulerian_circuit_cycle(self):
G=nx.cycle_graph(4)
edges=list(eulerian_circuit(G,source=0))
nodes=[u for u,v in edges]
assert_equal(nodes,[0,3,2,1])
assert_equal(edges,[(0,3),(3,2),(2,1),(1,0)])
edges=list(eulerian_circuit(G,source=1))
nodes=[u for u,v in edges]
assert_equal(nodes,[1,2,3,0])
assert_equal(edges,[(1,2),(2,3),(3,0),(0,1)])
G=nx.complete_graph(3)
edges=list(eulerian_circuit(G,source=0))
nodes=[u for u,v in edges]
assert_equal(nodes,[0,2,1])
assert_equal(edges,[(0,2),(2,1),(1,0)])
edges=list(eulerian_circuit(G,source=1))
nodes=[u for u,v in edges]
assert_equal(nodes,[1,2,0])
assert_equal(edges,[(1,2),(2,0),(0,1)])
示例10: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def setUp(self):
self.K = nx.krackhardt_kite_graph()
self.P3 = nx.path_graph(3)
self.P4 = nx.path_graph(4)
self.K5 = nx.complete_graph(5)
self.C4=nx.cycle_graph(4)
self.T=nx.balanced_tree(r=2, h=2)
self.Gb = nx.Graph()
self.Gb.add_edges_from([(0,1), (0,2), (1,3), (2,3),
(2,4), (4,5), (3,5)])
F = nx.florentine_families_graph()
self.F = F
示例11: test_from_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_from_edgelist(self):
# Pandas DataFrame
g = nx.cycle_graph(10)
G = nx.Graph()
G.add_nodes_from(g)
G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
edgelist = nx.to_edgelist(G)
source = [s for s, t, d in edgelist]
target = [t for s, t, d in edgelist]
weight = [d['weight'] for s, t, d in edgelist]
edges = pd.DataFrame({'source': source,
'target': target,
'weight': weight})
GG = nx.from_pandas_edgelist(edges, edge_attr='weight')
assert_nodes_equal(G.nodes(), GG.nodes())
assert_edges_equal(G.edges(), GG.edges())
GW = nx.to_networkx_graph(edges, create_using=nx.Graph)
assert_nodes_equal(G.nodes(), GW.nodes())
assert_edges_equal(G.edges(), GW.edges())
示例12: _gen_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def _gen_cycle(self, n):
for _ in range(n):
num_v = np.random.randint(self.min_num_v, self.max_num_v)
g = nx.cycle_graph(num_v)
self.graphs.append(g)
self.labels.append(0)
示例13: test_cycle_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_cycle_graph(self, dim):
"""Tests that the length of edges in a circular cycle graph are all of equal length."""
graph = nx.cycle_graph(dim)
layout = nx.kamada_kawai_layout(graph)
coords = plot._edge_coords(graph, layout)
x = coords["x"]
y = coords["y"]
dists = [
np.sqrt((x[3 * k] - x[3 * k + 1]) ** 2 + (y[3 * k] - y[3 * k + 1]) ** 2)
for k in range(dim)
]
first_dist = np.sqrt((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2)
assert np.allclose(dists, first_dist)
示例14: test_energies_functional
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_energies_functional(self):
h = {'a': .1, 'b': 0, 'c': 0}
J = {('a', 'b'): 1, ('b', 'c'): 1.3, ('a', 'c'): -1}
bqm = dimod.BinaryQuadraticModel.from_ising(h, J, offset=1.3)
embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)
embedded_response = dimod.ExactSolver().sample(embedded_bqm)
response = dwave.embedding.unembed_sampleset(embedded_response, embedding, bqm)
for sample, energy in response.data(['sample', 'energy']):
self.assertAlmostEqual(bqm.energy(sample), energy)
示例15: test_embed_bqm_BINARY
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_graph [as 别名]
def test_embed_bqm_BINARY(self):
Q = {('a', 'a'): 0, ('a', 'b'): -1, ('b', 'b'): 0, ('c', 'c'): 0, ('b', 'c'): -1}
bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
embedding = {'a': {0}, 'b': {1}, 'c': {2, 3}}
embedded_bqm = dwave.embedding.embed_bqm(bqm, embedding, nx.cycle_graph(4), chain_strength=1)
# check that the energy has been preserved
for config in itertools.product((0, 1), repeat=3):
sample = dict(zip(('a', 'b', 'c'), config))
target_sample = {u: sample[v] for v, chain in embedding.items() for u in chain} # no chains broken
self.assertAlmostEqual(bqm.energy(sample), embedded_bqm.energy(target_sample))