Python networkx.is_tree() Examples

The following are 30 code examples of networkx.is_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: utility.py    From ohmnet with MIT License 6 votes vote down vote up
def read_hierarchy(fname, log):
    # format directed edge list (parent, child)
    # parent, child are nodes:
    # -- a network name if leaf node
    # -- arbitrary name if internal node
    # nodes must have unique names
    # graph should be a tree
    G = nx.DiGraph()
    with open(fname) as fin:
        # print 'Reading: %s' % fname
        for line in fin:
            p, c = line.strip().split()
            p = p.replace('/', '_')
            c = c.replace('/', '_')
            G.add_edge(p, c)
    assert nx.is_tree(G), 'Hierarchy should be a tree'
    log.info('Hierarchy nodes: %s' % ', '.join(G.nodes()))
    return G 
Example #2
Source File: entangled_states.py    From forest-benchmarking with Apache License 2.0 6 votes vote down vote up
def create_ghz_program(tree: nx.DiGraph):
    """
    Create a Bell/GHZ state with CNOTs described by tree.

    :param tree: A tree that describes the CNOTs to perform to create a bell/GHZ state.
    :return: the program
    """
    assert nx.is_tree(tree), 'Needs to be a tree'
    nodes = list(nx.topological_sort(tree))
    n_qubits = len(nodes)
    program = Program(H(nodes[0]))

    for node in nodes:
        for child in tree.successors(node):
            program += CNOT(node, child)

    ro = program.declare('ro', 'BIT', n_qubits)
    for i, q in enumerate(nodes):
        program += MEASURE(q, ro[i])

    return program 
Example #3
Source File: test_gomory_hu.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_les_miserables_graph_cutset(self):
        G = nx.les_miserables_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v),
                             cut_value) 
Example #4
Source File: test_gomory_hu.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_davis_southern_women_graph(self):
        G = nx.davis_southern_women_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v),
                             cut_value) 
Example #5
Source File: test_gomory_hu.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_wikipedia_example(self):
        # Example from https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree
        G = nx.Graph()
        G.add_weighted_edges_from((
            (0, 1, 1), (0, 2, 7), (1, 2, 1),
            (1, 3, 3), (1, 4, 2), (2, 4, 4),
            (3, 4, 1), (3, 5, 6), (4, 5, 2),
        ))
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, capacity='weight', flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v, capacity='weight'),
                             cut_value) 
Example #6
Source File: test_trees.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_random_tree():
    """Tests that a random tree is in fact a tree."""
    T = nx.random_tree(10, seed=1234)
    assert_true(nx.is_tree(T)) 
Example #7
Source File: test_recognition.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_null_tree(self):
        nx.is_tree(self.graph())
        nx.is_tree(self.multigraph()) 
Example #8
Source File: test_recognition.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_is_tree(self):
        assert_true(nx.is_tree(self.T2))
        assert_true(nx.is_tree(self.T3))
        assert_true(nx.is_tree(self.T5)) 
Example #9
Source File: test_recognition.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_is_not_tree(self):
        assert_false(nx.is_tree(self.N4))
        assert_false(nx.is_tree(self.N5))
        assert_false(nx.is_tree(self.N6)) 
Example #10
Source File: test_recognition.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_disconnected_graph():
    # https://github.com/networkx/networkx/issues/1144
    G = nx.Graph()
    G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
    assert_false(nx.is_tree(G))

    G = nx.DiGraph()
    G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
    assert_false(nx.is_tree(G)) 
Example #11
Source File: test_recognition.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_dag_nontree():
    G = nx.DiGraph()
    G.add_edges_from([(0,1), (0,2), (1,2)])
    assert_false(nx.is_tree(G))
    assert_true(nx.is_directed_acyclic_graph(G)) 
Example #12
Source File: test_gomory_hu.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_default_flow_function_karate_club_graph(self):
        G = nx.karate_club_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        T = nx.gomory_hu_tree(G)
        assert_true(nx.is_tree(T))
        for u, v in combinations(G, 2):
            cut_value, edge = self.minimum_edge_weight(T, u, v)
            assert_equal(nx.minimum_cut_value(G, u, v),
                         cut_value) 
Example #13
Source File: test_gomory_hu.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_karate_club_graph(self):
        G = nx.karate_club_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v),
                             cut_value) 
Example #14
Source File: test_gomory_hu.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_davis_southern_women_graph(self):
        G = nx.davis_southern_women_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v),
                             cut_value) 
Example #15
Source File: test_gomory_hu.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_florentine_families_graph(self):
        G = nx.florentine_families_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v),
                             cut_value) 
Example #16
Source File: test_gomory_hu.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_karate_club_graph_cutset(self):
        G = nx.karate_club_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        T = nx.gomory_hu_tree(G)
        assert_true(nx.is_tree(T))
        u, v = 0, 33
        cut_value, edge = self.minimum_edge_weight(T, u, v)
        cutset = self.compute_cutset(G, T, edge)
        assert_equal(cut_value, len(cutset)) 
Example #17
Source File: test_tree.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_random_tree():
    """Tests that a random tree is in fact a tree."""
    T = nx.random_tree(10, seed=1234)
    assert_true(nx.is_tree(T)) 
Example #18
Source File: mst.py    From PyRate with Apache License 2.0 5 votes vote down vote up
def mst_from_ifgs(ifgs):
    """
    Returns a Minimum Spanning Tree (MST) network from the given interferograms
    using Kruskal's algorithm. The MST is calculated using a weighting based
    on the number of NaN cells in the phase band.

    :param list ifgs: List of interferogram objects (Ifg class)

    :return: edges: The number of network connections
    :rtype: int
    :return: is_tree: A boolean that is True if network is a tree
    :rtype: bool
    :return: ntrees: The number of disconnected trees
    :rtype: int
    :return: mst_ifgs: Minimum Spanning Tree network of interferograms
    :rtype: list
    """

    edges_with_weights_for_networkx = [(i.master, i.slave, i.nan_fraction)
                                       for i in ifgs]
    g_nx = _build_graph_networkx(edges_with_weights_for_networkx)
    mst = nx.minimum_spanning_tree(g_nx)
    # mst_edges, is tree?, number of trees
    edges = mst.edges()
    ifg_sub = [ifg_date_index_lookup(ifgs, d) for d in edges]
    mst_ifgs = [i for k, i in enumerate(ifgs) if k in ifg_sub]
    return mst.edges(), nx.is_tree(mst), \
        nx.number_connected_components(mst), mst_ifgs 
Example #19
Source File: test_recognition.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_null_tree(self):
        nx.is_tree(self.graph())
        nx.is_tree(self.multigraph()) 
Example #20
Source File: test_tree.py    From fragile with MIT License 5 votes vote down vote up
def test_prune_dead_branches(self, tree):
        leafs = tree.get_leaf_nodes()
        tree.prune_dead_branches(dead_leafs=set(leafs[:2]), alive_leafs=set(leafs[2:]))
        for le in leafs[:2]:
            assert le not in tree.leafs
            assert le not in tree.data

        assert networkx.is_tree(tree.data) 
Example #21
Source File: test_tree.py    From ScaffoldGraph with MIT License 5 votes vote down vote up
def test_tree_from_sdf(sdf_file):
    tree = sg.ScaffoldTree.from_sdf(sdf_file)
    assert tree.num_scaffold_nodes == 5
    assert tree.num_molecule_nodes == 2
    assert nx.is_tree(tree) 
Example #22
Source File: test_tree.py    From ScaffoldGraph with MIT License 5 votes vote down vote up
def test_tree_from_smiles(smiles_file):
    tree = sg.ScaffoldTree.from_smiles_file(smiles_file)
    assert tree.num_scaffold_nodes == 5
    assert tree.num_molecule_nodes == 2
    assert nx.is_tree(tree) 
Example #23
Source File: test_recognition.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_null_tree(self):
        nx.is_tree(self.graph())
        nx.is_tree(self.multigraph()) 
Example #24
Source File: test_recognition.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_is_tree(self):
        assert_true(nx.is_tree(self.T2))
        assert_true(nx.is_tree(self.T3))
        assert_true(nx.is_tree(self.T5)) 
Example #25
Source File: test_recognition.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_is_not_tree(self):
        assert_false(nx.is_tree(self.N4))
        assert_false(nx.is_tree(self.N5))
        assert_false(nx.is_tree(self.N6)) 
Example #26
Source File: test_recognition.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_disconnected_graph():
    # https://github.com/networkx/networkx/issues/1144
    G = nx.Graph()
    G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
    assert_false(nx.is_tree(G))

    G = nx.DiGraph()
    G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
    assert_false(nx.is_tree(G)) 
Example #27
Source File: test_recognition.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_dag_nontree():
    G = nx.DiGraph()
    G.add_edges_from([(0,1), (0,2), (1,2)])
    assert_false(nx.is_tree(G))
    assert_true(nx.is_directed_acyclic_graph(G)) 
Example #28
Source File: test_gomory_hu.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_florentine_families_graph(self):
        G = nx.florentine_families_graph()
        nx.set_edge_attributes(G, 1, 'capacity')
        for flow_func in flow_funcs:
            T = nx.gomory_hu_tree(G, flow_func=flow_func)
            assert_true(nx.is_tree(T))
            for u, v in combinations(G, 2):
                cut_value, edge = self.minimum_edge_weight(T, u, v)
                assert_equal(nx.minimum_cut_value(G, u, v),
                             cut_value) 
Example #29
Source File: test_recognition.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_is_tree(self):
        assert_true(nx.is_tree(self.T2))
        assert_true(nx.is_tree(self.T3))
        assert_true(nx.is_tree(self.T5)) 
Example #30
Source File: test_recognition.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_is_not_tree(self):
        assert_false(nx.is_tree(self.N4))
        assert_false(nx.is_tree(self.N5))
        assert_false(nx.is_tree(self.N6))