Python networkx.cycle_graph() Examples
The following are 30
code examples of networkx.cycle_graph().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: test_euler.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
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)])
Example #2
Source File: test_annotate_cycle.py From EDeN with MIT License | 6 votes |
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
Example #3
Source File: test_embedding_transforms.py From dwave-system with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_embedding_transforms.py From dwave-system with Apache License 2.0 | 6 votes |
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)
Example #5
Source File: test_embedding_transforms.py From dwave-system with Apache License 2.0 | 6 votes |
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))
Example #6
Source File: test_connectivity.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
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__))
Example #7
Source File: test_convert_pandas.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
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())
Example #8
Source File: test_embedding_transforms.py From dwave-system with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: test_weighted.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
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}))
Example #10
Source File: test_elimination_ordering.py From dwave_networkx with Apache License 2.0 | 6 votes |
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)
Example #11
Source File: test_closeness_centrality.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
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
Example #12
Source File: test_cluster.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_average_clustering(): G=nx.cycle_graph(3) G.add_edge(2,3) assert_equal(nx.average_clustering(G),(1+1+1/3.0)/4.0) assert_equal(nx.average_clustering(G,count_zeros=True),(1+1+1/3.0)/4.0) assert_equal(nx.average_clustering(G,count_zeros=False),(1+1+1/3.0)/3.0)
Example #13
Source File: test_line.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_cycle(self): G = nx.cycle_graph(5) L = nx.line_graph(G) assert_true(nx.is_isomorphic(L, G))
Example #14
Source File: test_dominating.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_wikipedia_is_dominating_set(): """Example from http://en.wikipedia.org/wiki/Dominating_set """ G = nx.cycle_graph(4) G.add_edges_from([(0, 4), (1, 4), (2,5)]) assert_true(nx.is_dominating_set(G, set([4, 3, 5]))) assert_true(nx.is_dominating_set(G, set([0, 2]))) assert_true(nx.is_dominating_set(G, set([1, 2])))
Example #15
Source File: test_connectivity.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_directed_node_connectivity(): G = nx.cycle_graph(10, create_using=nx.DiGraph()) # only one direction D = nx.cycle_graph(10).to_directed() # 2 reciprocal edges assert_equal(1, approx.node_connectivity(G)) assert_equal(1, approx.node_connectivity(G, 1, 4)) assert_equal(2, approx.node_connectivity(D)) assert_equal(2, approx.node_connectivity(D, 1, 4))
Example #16
Source File: test_convert.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_digraphs(self): for dest, source in [(to_dict_of_dicts, from_dict_of_dicts), (to_dict_of_lists, from_dict_of_lists)]: G = cycle_graph(10) # Dict of [dicts, lists] dod = dest(G) GG = source(dod) assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert_edges_equal(sorted(G.edges()), sorted(GG.edges())) GW = to_networkx_graph(dod) assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert_edges_equal(sorted(G.edges()), sorted(GW.edges())) GI = nx.Graph(dod) assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) assert_edges_equal(sorted(G.edges()), sorted(GI.edges())) G = cycle_graph(10, create_using=nx.DiGraph) dod = dest(G) GG = source(dod, create_using=nx.DiGraph) assert_equal(sorted(G.nodes()), sorted(GG.nodes())) assert_equal(sorted(G.edges()), sorted(GG.edges())) GW = to_networkx_graph(dod, create_using=nx.DiGraph) assert_equal(sorted(G.nodes()), sorted(GW.nodes())) assert_equal(sorted(G.edges()), sorted(GW.edges())) GI = nx.DiGraph(dod) assert_equal(sorted(G.nodes()), sorted(GI.nodes())) assert_equal(sorted(G.edges()), sorted(GI.edges()))
Example #17
Source File: test_convert_numpy.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self): self.G1 = barbell_graph(10, 3) self.G2 = cycle_graph(10, create_using=nx.DiGraph) self.G3 = self.create_weighted(nx.Graph()) self.G4 = self.create_weighted(nx.DiGraph())
Example #18
Source File: test_convert.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_graph(self): 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()) # Dict of dicts dod = to_dict_of_dicts(G) GG = from_dict_of_dicts(dod, create_using=nx.Graph) assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert_edges_equal(sorted(G.edges()), sorted(GG.edges())) GW = to_networkx_graph(dod, create_using=nx.Graph) assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert_edges_equal(sorted(G.edges()), sorted(GW.edges())) GI = nx.Graph(dod) assert_equal(sorted(G.nodes()), sorted(GI.nodes())) assert_equal(sorted(G.edges()), sorted(GI.edges())) # Dict of lists dol = to_dict_of_lists(G) GG = from_dict_of_lists(dol, create_using=nx.Graph) # dict of lists throws away edge data so set it to none enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)] assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert_edges_equal(enone, sorted(GG.edges(data=True))) GW = to_networkx_graph(dol, create_using=nx.Graph) assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert_edges_equal(enone, sorted(GW.edges(data=True))) GI = nx.Graph(dol) assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) assert_edges_equal(enone, sorted(GI.edges(data=True)))
Example #19
Source File: test_convert_numpy.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def create_weighted(self, G): g = cycle_graph(4) G.add_nodes_from(g) G.add_weighted_edges_from((u, v, 10 + u) for u, v in g.edges()) return G
Example #20
Source File: test_current_flow_betweenness_centrality.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_C4(self): """Edge flow betweenness centrality: C4""" G=networkx.cycle_graph(4) b=edge_current_flow(G,normalized=False) b_answer={(0, 1):1.25,(0, 3):1.25, (1, 2):1.25, (2, 3): 1.25} for (s,t),v1 in b_answer.items(): v2=b.get((s,t),b.get((t,s))) assert_almost_equal(v1,v2)
Example #21
Source File: test_simple_paths.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_bidirectional_shortest_path_restricted(): grid = cnlti(nx.grid_2d_graph(4,4), first_label=1, ordering="sorted") cycle = nx.cycle_graph(7) directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph()) length, path = _bidirectional_shortest_path(cycle, 0, 3) assert_equal(path, [0, 1, 2, 3]) length, path = _bidirectional_shortest_path(cycle, 0, 3, ignore_nodes=[1]) assert_equal(path, [0, 6, 5, 4, 3]) length, path = _bidirectional_shortest_path(grid, 1, 12) assert_equal(path, [1, 2, 3, 4, 8, 12]) length, path = _bidirectional_shortest_path(grid, 1, 12, ignore_nodes=[2]) assert_equal(path, [1, 5, 6, 10, 11, 12]) length, path = _bidirectional_shortest_path(grid, 1, 12, ignore_nodes=[2, 6]) assert_equal(path, [1, 5, 9, 10, 11, 12]) length, path = _bidirectional_shortest_path(grid, 1, 12, ignore_nodes=[2, 6], ignore_edges=[(10, 11)]) assert_equal(path, [1, 5, 9, 10, 14, 15, 16, 12]) length, path = _bidirectional_shortest_path(directed_cycle, 0, 3) assert_equal(path, [0, 1, 2, 3]) assert_raises( nx.NetworkXNoPath, _bidirectional_shortest_path, directed_cycle, 0, 3, ignore_nodes=[1], ) length, path = _bidirectional_shortest_path(directed_cycle, 0, 3, ignore_edges=[(2, 1)]) assert_equal(path, [0, 1, 2, 3]) assert_raises( nx.NetworkXNoPath, _bidirectional_shortest_path, directed_cycle, 0, 3, ignore_edges=[(1, 2)], )
Example #22
Source File: test_simple_paths.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_shortest_simple_paths_directed(): G = nx.cycle_graph(7, create_using=nx.DiGraph()) paths = nx.shortest_simple_paths(G, 0, 3) assert_equal([path for path in paths], [[0, 1, 2, 3]])
Example #23
Source File: test_distance_regular.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_intersection_array(self): b,c=nx.intersection_array(nx.cycle_graph(5)) assert_equal(b,[2, 1]) assert_equal(c,[1, 1]) b,c=nx.intersection_array(nx.dodecahedral_graph()) assert_equal(b,[3, 2, 1, 1, 1]) assert_equal(c,[1, 1, 1, 2, 3]) b,c=nx.intersection_array(nx.icosahedral_graph()) assert_equal(b,[5, 2, 1]) assert_equal(c,[1, 2, 5])
Example #24
Source File: test_distance_regular.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_global_parameters(self): b,c=nx.intersection_array(nx.cycle_graph(5)) g=nx.global_parameters(b,c) assert_equal(list(g),[(0, 0, 2), (1, 0, 1), (1, 1, 0)]) b,c=nx.intersection_array(nx.cycle_graph(3)) g=nx.global_parameters(b,c) assert_equal(list(g),[(0, 0, 2), (1, 1, 0)])
Example #25
Source File: test_distance_regular.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_not_connected(self): G=nx.cycle_graph(4) G.add_cycle([5,6,7]) assert_false(nx.is_distance_regular(G))
Example #26
Source File: test_hierarchy.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_hierarchy_cycle(): G = nx.cycle_graph(5,create_using=nx.DiGraph()) assert_equal(nx.flow_hierarchy(G),0.0)
Example #27
Source File: test_hierarchy.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_hierarchy_exception(): G = nx.cycle_graph(5) assert_raises(nx.NetworkXError,nx.flow_hierarchy,G)
Example #28
Source File: test_dominance.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_cycle(self): n = 5 G = nx.cycle_graph(n, create_using=nx.DiGraph()) assert_equal(nx.dominance_frontiers(G, 0), {i: [] for i in range(n)})
Example #29
Source File: test_dominance.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_cycle(self): n = 5 G = nx.cycle_graph(n, create_using=nx.DiGraph()) assert_equal(nx.immediate_dominators(G, 0), {i: max(i - 1, 0) for i in range(n)})
Example #30
Source File: test_minors.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_nonexistent_edge(self): """Tests that attempting to contract a non-existent edge raises an exception. """ G = nx.cycle_graph(4) nx.contracted_edge(G, (0, 2))