Python networkx.balanced_tree() Examples

The following are 24 code examples of networkx.balanced_tree(). 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: synthetic_structsim.py    From gnn-model-explainer with Apache License 2.0 6 votes vote down vote up
def tree(start, height, r=2, role_start=0):
    """Builds a balanced r-tree of height h
    INPUT:
    -------------
    start       :    starting index for the shape
    height      :    int height of the tree 
    r           :    int number of branches per node 
    role_start  :    starting index for the roles
    OUTPUT:
    -------------
    graph       :    a tree shape graph, with ids beginning at start
    roles       :    list of the roles of the nodes (indexed starting at role_start)
    """
    graph = nx.balanced_tree(r, height)
    roles = [0] * graph.number_of_nodes()
    return graph, roles 
Example #2
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 #3
Source File: test_closeness_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
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 #4
Source File: test_closeness_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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

        self.LM = nx.les_miserables_graph() 
Example #5
Source File: test_load_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):

        G = nx.Graph()
        G.add_edge(0, 1, weight=3)
        G.add_edge(0, 2, weight=2)
        G.add_edge(0, 3, weight=6)
        G.add_edge(0, 4, weight=4)
        G.add_edge(1, 3, weight=5)
        G.add_edge(1, 5, weight=5)
        G.add_edge(2, 4, weight=1)
        G.add_edge(3, 4, weight=2)
        G.add_edge(3, 5, weight=1)
        G.add_edge(4, 5, weight=4)
        self.G = G
        self.exact_weighted = {0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}
        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)])
        self.F = nx.florentine_families_graph()
        self.D = nx.cycle_graph(3, create_using=nx.DiGraph())
        self.D.add_edges_from([(3, 0), (4, 3)]) 
Example #6
Source File: test_decompositions.py    From strawberryfields with Apache License 2.0 5 votes vote down vote up
def test_real_degenerate(self):
        """Verify that the Takagi decomposition returns a matrix that is unitary and results in a
        correct decomposition when input a real but highly degenerate matrix. This test uses the
        adjacency matrix of a balanced tree graph."""
        g = nx.balanced_tree(2, 4)
        a = nx.to_numpy_array(g)
        rl, U = dec.takagi(a)
        assert np.allclose(U @ U.conj().T, np.eye(len(a)))
        assert np.allclose(U @ np.diag(rl) @ U.T, a) 
Example #7
Source File: test_betweenness_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_balanced_tree(self):
        """Edge betweenness centrality: balanced tree"""
        G=nx.balanced_tree(r=2,h=2)
        b=nx.edge_betweenness_centrality(G, weight=None, normalized=False)
        b_answer={(0, 1):12,(0, 2):12,
                  (1, 3):6,(1, 4):6,(2, 5):6,(2,6):6}
        for n in sorted(G.edges()):
            assert_almost_equal(b[n],b_answer[n]) 
Example #8
Source File: test_closeness_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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 #9
Source File: test_harmonic_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        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.C5 = nx.cycle_graph(5)


        self.T = nx.balanced_tree(r=2, h=2)

        self.Gb = nx.DiGraph()
        self.Gb.add_edges_from([(0, 1), (0, 2), (0, 4), (2, 1),
                                (2, 3), (4, 3)]) 
Example #10
Source File: test_richclub.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_rich_club_exception2():
    G = nx.MultiGraph()
    nx.rich_club_coefficient(G)


# def test_richclub2_normalized():
#    T = nx.balanced_tree(2,10)
#    rcNorm = nx.richclub.rich_club_coefficient(T,Q=2)
#    assert_true(rcNorm[0] ==1.0 and rcNorm[1] < 0.9 and rcNorm[2] < 0.9) 
Example #11
Source File: test_richclub.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_richclub2():
    T = nx.balanced_tree(2, 10)
    rc = nx.richclub.rich_club_coefficient(T, normalized=False)
    assert_equal(rc, {0: 4092 / (2047 * 2046.0),
                      1: (2044.0 / (1023 * 1022)),
                      2: (2040.0 / (1022 * 1021))}) 
Example #12
Source File: test_load_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):

        G = nx.Graph()
        G.add_edge(0, 1, weight=3)
        G.add_edge(0, 2, weight=2)
        G.add_edge(0, 3, weight=6)
        G.add_edge(0, 4, weight=4)
        G.add_edge(1, 3, weight=5)
        G.add_edge(1, 5, weight=5)
        G.add_edge(2, 4, weight=1)
        G.add_edge(3, 4, weight=2)
        G.add_edge(3, 5, weight=1)
        G.add_edge(4, 5, weight=4)
        self.G = G
        self.exact_weighted = {0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}
        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)])
        self.F = nx.florentine_families_graph()
        self.LM = nx.les_miserables_graph()
        self.D = nx.cycle_graph(3, create_using=nx.DiGraph())
        self.D.add_edges_from([(3, 0), (4, 3)]) 
Example #13
Source File: test_betweenness_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_balanced_tree(self):
        """Edge betweenness centrality: balanced tree"""
        G = nx.balanced_tree(r=2, h=2)
        b = nx.edge_betweenness_centrality(G, weight='weight', normalized=False)
        b_answer = {(0, 1): 12, (0, 2): 12,
                    (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6}
        for n in sorted(G.edges()):
            assert_almost_equal(b[n], b_answer[n]) 
Example #14
Source File: test_betweenness_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_balanced_tree(self):
        """Edge betweenness centrality: balanced tree"""
        G = nx.balanced_tree(r=2, h=2)
        b = nx.edge_betweenness_centrality(G, weight=None, normalized=False)
        b_answer = {(0, 1): 12, (0, 2): 12,
                    (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6}
        for n in sorted(G.edges()):
            assert_almost_equal(b[n], b_answer[n]) 
Example #15
Source File: test_cycles.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_tree_graph(self):
        tg = nx.balanced_tree(3, 3)
        assert_false(minimum_cycle_basis(tg)) 
Example #16
Source File: test_richclub.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_rich_club_exception2():
    G = nx.MultiGraph()
    nx.rich_club_coefficient(G)


# def test_richclub2_normalized():
#    T = nx.balanced_tree(2,10)
#    rcNorm = nx.richclub.rich_club_coefficient(T,Q=2)
#    assert_true(rcNorm[0] ==1.0 and rcNorm[1] < 0.9 and rcNorm[2] < 0.9) 
Example #17
Source File: test_richclub.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_richclub2():
    T = nx.balanced_tree(2, 10)
    rc = nx.richclub.rich_club_coefficient(T, normalized=False)
    assert_equal(rc, {0: 4092 / (2047 * 2046.0),
                      1: (2044.0 / (1023 * 1022)),
                      2: (2040.0 / (1022 * 1021))}) 
Example #18
Source File: test_dag.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_already_branching(self):
        """Tests that a directed acyclic graph that is already a
        branching produces an isomorphic branching as output.

        """
        T1 = nx.balanced_tree(2, 2, create_using=nx.DiGraph())
        T2 = nx.balanced_tree(2, 2, create_using=nx.DiGraph())
        G = nx.disjoint_union(T1, T2)
        B = nx.dag_to_branching(G)
        assert_true(nx.is_isomorphic(G, B)) 
Example #19
Source File: test_dag.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_already_arborescence(self):
        """Tests that a directed acyclic graph that is already an
        arborescence produces an isomorphic arborescence as output.

        """
        A = nx.balanced_tree(2, 2, create_using=nx.DiGraph())
        B = nx.dag_to_branching(A)
        assert_true(nx.is_isomorphic(A, B)) 
Example #20
Source File: test_betweenness_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_balanced_tree(self):
        """Edge betweenness centrality: balanced tree"""
        G=nx.balanced_tree(r=2,h=2)
        b=nx.edge_betweenness_centrality(G, weight='weight', normalized=False)
        b_answer={(0, 1):12,(0, 2):12,
                  (1, 3):6,(1, 4):6,(2, 5):6,(2,6):6}
        for n in sorted(G.edges()):
            assert_almost_equal(b[n],b_answer[n]) 
Example #21
Source File: test_betweenness_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_balanced_tree(self):
        """Edge betweenness centrality: balanced tree"""
        G=nx.balanced_tree(r=2,h=2)
        b=nx.edge_betweenness_centrality(G, weight=None, normalized=False)
        b_answer={(0, 1):12,(0, 2):12,
                  (1, 3):6,(1, 4):6,(2, 5):6,(2,6):6}
        for n in sorted(G.edges()):
            assert_almost_equal(b[n],b_answer[n]) 
Example #22
Source File: test_harmonic_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        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.C5 = nx.cycle_graph(5)


        self.T = nx.balanced_tree(r=2, h=2)

        self.Gb = nx.DiGraph()
        self.Gb.add_edges_from([(0, 1), (0, 2), (0, 4), (2, 1),
                                (2, 3), (4, 3)]) 
Example #23
Source File: test_richclub.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_richclub2():
    T = nx.balanced_tree(2,10)
    rc = nx.richclub.rich_club_coefficient(T,normalized=False)
    assert_equal(rc,{0:4092/(2047*2046.0),
                     1:(2044.0/(1023*1022)),
                     2:(2040.0/(1022*1021))})

#def test_richclub2_normalized():
#    T = nx.balanced_tree(2,10)
#    rcNorm = nx.richclub.rich_club_coefficient(T,Q=2)
#    assert_true(rcNorm[0] ==1.0 and rcNorm[1] < 0.9 and rcNorm[2] < 0.9) 
Example #24
Source File: dag.py    From clusim with MIT License 5 votes vote down vote up
def make_complete_rary_tree(self, h=2, r=2):
        self.add_edges_from(nx.balanced_tree(h=h, r=r,
                            create_using=nx.DiGraph()).edges)
        self.linkage_dist = {n: d for n, (d, _) in
                             self.maxdist_from_roots().items()}