Python networkx.barbell_graph() Examples

The following are 30 code examples of networkx.barbell_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_ramsey.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
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!") 
Example #2
Source File: test_biconnected.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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}) 
Example #3
Source File: test_specification.py    From penaltymodel with Apache License 2.0 6 votes vote down vote up
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]) 
Example #4
Source File: test_specification.py    From penaltymodel with Apache License 2.0 6 votes vote down vote up
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]) 
Example #5
Source File: test_chains.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #6
Source File: test_elimination_ordering.py    From dwave_networkx with Apache License 2.0 6 votes vote down vote up
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 #7
Source File: test_embedding_utils.py    From dwave-system with Apache License 2.0 6 votes vote down vote up
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) 
Example #8
Source File: test_clique.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
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!") 
Example #9
Source File: graph_gens.py    From GEM-Benchmark with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)

########################################################################## 
Example #10
Source File: test_bqm.py    From dimod with Apache License 2.0 6 votes vote down vote up
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']) 
Example #11
Source File: transition.py    From edge2vec with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #12
Source File: test_chains.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
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) 
Example #13
Source File: test_quality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_good_partition(self):
        """Tests that a good partition has a high performance measure.

        """
        G = barbell_graph(3, 0)
        partition = [{0, 1, 2}, {3, 4, 5}]
        assert_almost_equal(14 / 15, performance(G, partition)) 
Example #14
Source File: test_quality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_bad_partition(self):
        """Tests that a poor partition has a low performance measure."""
        G = barbell_graph(3, 0)
        partition = [{0, 1, 4}, {2, 3, 5}]
        assert_almost_equal(8 / 15, performance(G, partition)) 
Example #15
Source File: test_cuts.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_directed(self):
        """Tests that each directed edge is counted once in the cut."""
        G = nx.barbell_graph(3, 0).to_directed()
        S = {0, 1, 2}
        T = {3, 4, 5}
        assert_equal(nx.cut_size(G, S, T), 2)
        assert_equal(nx.cut_size(G, T, S), 2) 
Example #16
Source File: test_pylab.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.G = nx.barbell_graph(4, 6) 
Example #17
Source File: test_cuts.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_single_edge(self):
        """Tests for a cut of a single edge."""
        G = nx.barbell_graph(3, 0)
        S = {0, 1, 2}
        T = {3, 4, 5}
        assert_equal(nx.cut_size(G, S, T), 1)
        assert_equal(nx.cut_size(G, T, S), 1) 
Example #18
Source File: test_agraph.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_view_pygraphviz(self):
        G = nx.Graph()  # "An empty graph cannot be drawn."
        assert_raises(nx.NetworkXException, nx.nx_agraph.view_pygraphviz, G)
        G = nx.barbell_graph(4, 6)
        nx.nx_agraph.view_pygraphviz(G) 
Example #19
Source File: test_cuts.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_directed_symmetric(self):
        """Tests that a cut in a directed graph is symmetric."""
        G = nx.barbell_graph(3, 0).to_directed()
        S = {0, 1, 4}
        T = {2, 3, 5}
        assert_equal(nx.cut_size(G, S, T), 8)
        assert_equal(nx.cut_size(G, T, S), 8) 
Example #20
Source File: test_kernighan_lin.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_partition():
    G = nx.barbell_graph(3, 0)
    C = kernighan_lin_bisection(G)
    assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}]) 
Example #21
Source File: test_clique.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_barbell_graph(self):
        G = nx.barbell_graph(10, 5)
        independent_set, cliques = clique_removal(G)
        assert_true(is_independent_set(G, independent_set))
        assert_true(all(is_clique(G, clique) for clique in cliques)) 
Example #22
Source File: test_chains.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_disconnected_graph_root_node(self):
        """Test for a single component of a disconnected graph."""
        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, root='a'))
        expected = [
            [('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) 
Example #23
Source File: test_chains.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_barbell_graph(self):
        # The (3, 0) barbell graph has two triangles joined by a single edge.
        G = nx.barbell_graph(3, 0)
        chains = list(nx.chain_decomposition(G, root=0))
        expected = [
            [(0, 1), (1, 2), (2, 0)],
            [(3, 4), (4, 5), (5, 3)],
        ]
        self.assertEqual(len(chains), len(expected))
        for chain in chains:
            self.assertContainsChain(chain, expected) 
Example #24
Source File: test_cuts.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_graph(self):
        G = nx.barbell_graph(5, 0)
        S = set(range(5))
        T = set(G) - S
        expansion = nx.mixing_expansion(G, S, T)
        # There is one cut edge, and the total number of edges in the
        # graph is twice the total number of edges in a clique of size
        # five, plus one more for the bridge.
        expected = 1 / (2 * (5 * 4 + 1))
        assert_equal(expected, expansion) 
Example #25
Source File: test_cuts.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_graph(self):
        G = nx.barbell_graph(5, 0)
        # Consider the singleton sets containing the "bridge" nodes.
        # There is only one cut edge, and each set has volume five.
        S = {4}
        T = {5}
        conductance = nx.conductance(G, S, T)
        expected = 1 / 5
        assert_equal(expected, conductance) 
Example #26
Source File: test_cuts.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_directed_symmetric(self):
        """Tests that a cut in a directed graph is symmetric."""
        G = nx.barbell_graph(3, 0).to_directed()
        S = {0, 1, 4}
        T = {2, 3, 5}
        assert_equal(nx.cut_size(G, S, T), 8)
        assert_equal(nx.cut_size(G, T, S), 8) 
Example #27
Source File: test_cuts.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_directed(self):
        """Tests that each directed edge is counted once in the cut."""
        G = nx.barbell_graph(3, 0).to_directed()
        S = {0, 1, 2}
        T = {3, 4, 5}
        assert_equal(nx.cut_size(G, S, T), 2)
        assert_equal(nx.cut_size(G, T, S), 2) 
Example #28
Source File: test_cuts.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_single_edge(self):
        """Tests for a cut of a single edge."""
        G = nx.barbell_graph(3, 0)
        S = {0, 1, 2}
        T = {3, 4, 5}
        assert_equal(nx.cut_size(G, S, T), 1)
        assert_equal(nx.cut_size(G, T, S), 1) 
Example #29
Source File: test_cuts.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_symmetric(self):
        """Tests that the cut size is symmetric."""
        G = nx.barbell_graph(3, 0)
        S = {0, 1, 4}
        T = {2, 3, 5}
        assert_equal(nx.cut_size(G, S, T), 4)
        assert_equal(nx.cut_size(G, T, S), 4) 
Example #30
Source File: test_bridges.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_barbell_graph(self):
        # The (3, 0) barbell graph has two triangles joined by a single edge.
        G = nx.barbell_graph(3, 0)
        source = 0
        bridges = list(nx.bridges(G, source))
        self.assertEqual(bridges, [(2, 3)])