Python networkx.bfs_successors() Examples

The following are 12 code examples of networkx.bfs_successors(). 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: data.py    From graph-generation with MIT License 6 votes vote down vote up
def bfs_seq(G, start_id):
    '''
    get a bfs node sequence
    :param G:
    :param start_id:
    :return:
    '''
    dictionary = dict(nx.bfs_successors(G, start_id))
    start = [start_id]
    output = [start_id]
    while len(start) > 0:
        next = []
        while len(start) > 0:
            current = start.pop(0)
            neighbor = dictionary.get(current)
            if neighbor is not None:
                #### a wrong example, should not permute here!
                # shuffle(neighbor)
                next = next + neighbor
        output = output + next
        start = next
    return output 
Example #2
Source File: data.py    From GraphRNN with MIT License 6 votes vote down vote up
def bfs_seq(G, start_id):
    '''
    get a bfs node sequence
    :param G:
    :param start_id:
    :return:
    '''
    dictionary = dict(nx.bfs_successors(G, start_id))
    start = [start_id]
    output = [start_id]
    while len(start) > 0:
        next = []
        while len(start) > 0:
            current = start.pop(0)
            neighbor = dictionary.get(current)
            if neighbor is not None:
                #### a wrong example, should not permute here!
                # shuffle(neighbor)
                next = next + neighbor
        output = output + next
        start = next
    return output 
Example #3
Source File: config.py    From partridge with MIT License 5 votes vote down vote up
def reroot_graph(G: nx.DiGraph, node: str) -> nx.DiGraph:
    """Return a copy of the graph rooted at the given node"""
    G = G.copy()
    for n, successors in list(nx.bfs_successors(G, source=node)):
        for s in successors:
            G.add_edge(s, n, **G.edges[n, s])
            G.remove_edge(n, s)
    return G 
Example #4
Source File: networkx_dagcircuit.py    From qiskit-terra with Apache License 2.0 5 votes vote down vote up
def bfs_successors(self, node):
        """
        Returns an iterator of tuples of (DAGNode, [DAGNodes]) where the DAGNode is the current node
        and [DAGNode] is its successors in  BFS order.
        """
        return ((self._id_to_node[idx], [self._id_to_node[succ] for succ in succ_list])
                for idx, succ_list in nx.bfs_successors(self._multi_graph, node._node_id)) 
Example #5
Source File: breadth_first_search.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def bfs_successors(G, source):
    """Return dictionary of successors in breadth-first-search from source.

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

    source : node
       Specify starting node for breadth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    succ: dict
       A dictionary with nodes as keys and list of succssors nodes as values.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> print(nx.bfs_successors(G,0))
    {0: [1], 1: [2]}

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
    by D. Eppstein, July 2004.
    """
    d = defaultdict(list)
    for s,t in bfs_edges(G,source):
        d[s].append(t)
    return dict(d) 
Example #6
Source File: test_bfs.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_successor(self):
        assert_equal(nx.bfs_successors(self.G, source=0),
                     {0: [1], 1: [2, 3], 2: [4]}) 
Example #7
Source File: test_bfs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_successor(self):
        assert_equal(dict(nx.bfs_successors(self.G, source=0)),
                     {0: [1], 1: [2, 3], 2: [4]}) 
Example #8
Source File: test_bfs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def bfs_test_successor(self):
        assert_equal(dict(nx.bfs_successors(self.G, source=1, depth_limit=3)),
                     {1: [0, 2], 2: [3, 7], 3: [4], 7: [8]})
        result = {n: sorted(s) for n, s in nx.bfs_successors(self.D, source=7,
                                                             depth_limit=2)}
        assert_equal(result, {8: [9], 2: [3], 7: [2, 8]}) 
Example #9
Source File: test_bfs.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_successor(self):
        assert_equal(dict(nx.bfs_successors(self.G, source=0)),
                     {0: [1], 1: [2, 3], 2: [4]}) 
Example #10
Source File: adsorption.py    From CatKit with GNU General Public License v3.0 4 votes vote down vote up
def _single_adsorption(
            self,
            adsorbate,
            bond,
            slab=None,
            site_index=0,
            auto_construct=True,
            symmetric=True):
        """Bond and adsorbate by a single atom."""
        if slab is None:
            slab = self.slab.copy()
        atoms = adsorbate.copy()
        atoms.set_cell(slab.cell)

        if symmetric:
            ind = self.get_symmetric_sites()[site_index]
            vector = self.get_adsorption_vectors()[site_index]
        else:
            ind = self.get_periodic_sites()[site_index]
            vector = self.get_adsorption_vectors(unique=False)[site_index]

        # Improved position estimate for site.
        u = self.r1_topology[ind]
        r = radii[slab[self.index[u]].numbers]
        top_sites = self.coordinates[self.connectivity == 1]

        numbers = atoms.numbers[bond]
        R = radii[numbers]
        base_position = utils.trilaterate(top_sites[u], r + R, vector)

        branches = nx.bfs_successors(atoms.graph, bond)
        atoms.translate(-atoms.positions[bond])

        if auto_construct:
            atoms = catkit.gen.molecules.get_3D_positions(atoms, bond)

            # Align with the adsorption vector
            atoms.rotate([0, 0, 1], vector)

        atoms.translate(base_position)
        n = len(slab)
        slab += atoms

        # Add graph connections
        for metal_index in self.index[u]:
            slab.graph.add_edge(metal_index, bond + n)

        return slab 
Example #11
Source File: breadth_first_search.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def bfs_successors(G, source, depth_limit=None):
    """Returns an iterator of successors in breadth-first-search from source.

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

    source : node
       Specify starting node for breadth-first search and return edges in
       the component reachable from source.

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

    Returns
    -------
    succ: iterator
       (node, successors) iterator where successors is the list of
       successors of the node.

    Examples
    --------
    >>> G = nx.path_graph(3)
    >>> print(dict(nx.bfs_successors(G,0)))
    {0: [1], 1: [2]}
    >>> H = nx.Graph()
    >>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
    >>> print(dict(nx.bfs_successors(H, 0)))
    {0: [1, 2], 1: [3, 4], 2: [5, 6]}
    >>> G = nx.Graph()
    >>> nx.add_path(G, [0, 1, 2, 3, 4, 5, 6])
    >>> nx.add_path(G, [2, 7, 8, 9, 10])
    >>> print(dict(nx.bfs_successors(G, source=1, depth_limit=3)))
    {1: [0, 2], 2: [3, 7], 3: [4], 7: [8]}


    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
    by D. Eppstein, July 2004.The modifications
    to allow depth limits based on the Wikipedia article
    "`Depth-limited-search`_".

    .. _Depth-limited-search: https://en.wikipedia.org/wiki/Depth-limited_search
    """
    parent = source
    children = []
    for p, c in bfs_edges(G, source, depth_limit=depth_limit):
        if p == parent:
            children.append(c)
            continue
        yield (parent, children)
        children = [c]
        parent = p
    yield (parent, children) 
Example #12
Source File: breadth_first_search.py    From aws-kube-codesuite with Apache License 2.0 4 votes vote down vote up
def bfs_successors(G, source):
    """Returns an iterator of successors in breadth-first-search from source.

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

    source : node
       Specify starting node for breadth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    succ: iterator
       (node, successors) iterator where successors is the list of
       successors of the node.

    Examples
    --------
    >>> G = nx.path_graph(3)
    >>> print(dict(nx.bfs_successors(G,0)))
    {0: [1], 1: [2]}
    >>> H = nx.Graph()
    >>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
    >>> dict(nx.bfs_successors(H, 0))
    {0: [1, 2], 1: [3, 4], 2: [5, 6]}


    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py
    by D. Eppstein, July 2004.
    """
    parent = source
    children = []
    for p, c in bfs_edges(G, source):
        if p == parent:
            children.append(c)
            continue
        yield (parent, children)
        children = [c]
        parent = p
    yield (parent, children)