Python networkx.dfs_tree() Examples

The following are 12 code examples of networkx.dfs_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: depth_first_search.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def dfs_tree(G, source):
    """Return oriented tree constructed from a depth-first-search from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search.

    Returns
    -------
    T : NetworkX DiGraph
       An oriented tree

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> T = nx.dfs_tree(G,0)
    >>> print(T.edges())
    [(0, 1), (1, 2)]
    """
    T = nx.DiGraph()
    if source is None:
        T.add_nodes_from(G)
    else:
        T.add_node(source)
    T.add_edges_from(dfs_edges(G,source))
    return T 
Example #2
Source File: test_dfs.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_dfs_tree(self):
        T=nx.dfs_tree(self.G,source=0)
        assert_equal(sorted(T.nodes()),sorted(self.G.nodes()))
        assert_equal(sorted(T.edges()),[(0, 1), (1, 2), (2, 4), (4, 3)]) 
Example #3
Source File: test_dfs.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_dfs_tree_isolates(self):
        G = nx.Graph()
        G.add_node(1)
        G.add_node(2)
        T=nx.dfs_tree(G,source=1)
        assert_equal(sorted(T.nodes()),[1])
        assert_equal(sorted(T.edges()),[])
        T=nx.dfs_tree(G,source=None)
        assert_equal(sorted(T.nodes()),[1, 2])
        assert_equal(sorted(T.edges()),[]) 
Example #4
Source File: cliquetree.py    From pyBN with MIT License 5 votes vote down vote up
def dfs_postorder(self, root):
        G = nx.Graph(self.E)
        tree_graph = nx.dfs_tree(G,root)
        clique_ordering = list(nx.dfs_postorder_nodes(tree_graph,root))
        return clique_ordering 
Example #5
Source File: depth_first_search.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dfs_tree(G, source=None, depth_limit=None):
    """Returns oriented tree constructed from a depth-first-search from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search.

    depth_limit : int, optional (default=len(G))
       Specify the maximum search depth.

    Returns
    -------
    T : NetworkX DiGraph
       An oriented tree

    Examples
    --------
    >>> G = nx.path_graph(5)
    >>> T = nx.dfs_tree(G, source=0, depth_limit=2)
    >>> list(T.edges())
    [(0, 1), (1, 2)]
    >>> T = nx.dfs_tree(G, source=0)
    >>> list(T.edges())
    [(0, 1), (1, 2), (2, 3), (3, 4)]

    """
    T = nx.DiGraph()
    if source is None:
        T.add_nodes_from(G)
    else:
        T.add_node(source)
    T.add_edges_from(dfs_edges(G, source, depth_limit))
    return T 
Example #6
Source File: test_dfs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_dfs_tree(self):
        exp_nodes = sorted(self.G.nodes())
        exp_edges = [(0, 1), (1, 2), (2, 4), (4, 3)]
        # Search from first node
        T = nx.dfs_tree(self.G, source=0)
        assert_equal(sorted(T.nodes()), exp_nodes)
        assert_equal(sorted(T.edges()), exp_edges)
        # Check source=None
        T = nx.dfs_tree(self.G, source=None)
        assert_equal(sorted(T.nodes()), exp_nodes)
        assert_equal(sorted(T.edges()), exp_edges)
        # Check source=None is the default
        T = nx.dfs_tree(self.G)
        assert_equal(sorted(T.nodes()), exp_nodes)
        assert_equal(sorted(T.edges()), exp_edges) 
Example #7
Source File: test_dfs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_dfs_tree_isolates(self):
        G = nx.Graph()
        G.add_node(1)
        G.add_node(2)
        T = nx.dfs_tree(G, source=1)
        assert_equal(sorted(T.nodes()), [1])
        assert_equal(sorted(T.edges()), [])
        T = nx.dfs_tree(G, source=None)
        assert_equal(sorted(T.nodes()), [1, 2])
        assert_equal(sorted(T.edges()), []) 
Example #8
Source File: test_dfs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_dls_tree(self):
        T = nx.dfs_tree(self.G, source=3, depth_limit=1)
        assert_equal(sorted(T.edges()), [(3, 2), (3, 4)]) 
Example #9
Source File: depth_first_search.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def dfs_tree(G, source=None, depth_limit=None):
    """Return oriented tree constructed from a depth-first-search from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search.

    depth_limit : int, optional (default=len(G))
       Specify the maximum search depth.

    Returns
    -------
    T : NetworkX DiGraph
       An oriented tree

    Examples
    --------
    >>> G = nx.path_graph(5)
    >>> T = nx.dfs_tree(G, source=0, depth_limit=2)
    >>> list(T.edges())
    [(0, 1), (1, 2)]
    >>> T = nx.dfs_tree(G, source=0)
    >>> list(T.edges())
    [(0, 1), (1, 2), (2, 3), (3, 4)]

    """
    T = nx.DiGraph()
    if source is None:
        T.add_nodes_from(G)
    else:
        T.add_node(source)
    T.add_edges_from(dfs_edges(G, source, depth_limit))
    return T 
Example #10
Source File: test_dfs.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_dfs_tree(self):
        exp_nodes = sorted(self.G.nodes())
        exp_edges = [(0, 1), (1, 2), (2, 4), (4, 3)]
        # Search from first node
        T=nx.dfs_tree(self.G,source=0)
        assert_equal(sorted(T.nodes()), exp_nodes)
        assert_equal(sorted(T.edges()), exp_edges)
        # Check source=None
        T = nx.dfs_tree(self.G, source=None)
        assert_equal(sorted(T.nodes()), exp_nodes)
        assert_equal(sorted(T.edges()), exp_edges)
        # Check source=None is the default
        T = nx.dfs_tree(self.G)
        assert_equal(sorted(T.nodes()), exp_nodes)
        assert_equal(sorted(T.edges()), exp_edges) 
Example #11
Source File: test_dfs.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_dfs_tree_isolates(self):
        G = nx.Graph()
        G.add_node(1)
        G.add_node(2)
        T=nx.dfs_tree(G,source=1)
        assert_equal(sorted(T.nodes()),[1])
        assert_equal(sorted(T.edges()),[])
        T=nx.dfs_tree(G,source=None)
        assert_equal(sorted(T.nodes()),[1, 2])
        assert_equal(sorted(T.edges()),[]) 
Example #12
Source File: test_dfs.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_dls_tree(self):
        T = nx.dfs_tree(self.G, source=3, depth_limit=1)
        assert_equal(sorted(T.edges()), [(3, 2), (3, 4)])