Example #1
0
def node_children(graph, nid, root, include_self=False):
    """
    Return the children of the source node.
    
    Traversal path is determined from the parent node with respect to the
    root via the source node to the children.
    
    Directed graphs and/or masked behaviour: masked child nodes or child
    nodes with directed connections from child to parent but not vice-versa
    will not be returned.
    
    :param graph:        Graph to perform calculation for
    :type graph:         Graph class instance
    :param nid:          source node to start search from
    :type nid:           :py:int
    :param root:         root node for the search
    :type root:          :py:int
    :param include_self: include source nid in results
    :type include_self:  :py:bool
    """

    # Children are all neighbors of the node except the parent
    p = node_parent(graph, nid, root)
    children = [n for n in node_neighbors(graph, nid) if not n == p]

    if include_self:
        children.insert(0, nid)

    return children
Example #2
0
def dfs_paths(graph, start, goal, method='dfs', cutoff=None):
    """
    Return all possible paths between two nodes.

    Setting method to 'bfs' returns the shortest path first

    :param graph:     graph to search
    :type graph:      graph class instance
    :param start:     root node to start the search from
    :type start:      :py:int
    :param goal:      target node
    :type goal:       :py:int
    :param method:    search method. Depth-first search (dfs, default)
                      and Breath-first search (bfs) supported.
    :type method:     :py:str
    :param cutoff:    only return paths with length up to cutoff
    :type cutoff:     :py:int

    :rtype:           generator object
    """

    # Define the search method
    stack_pop = -1
    if method == 'bfs':
        stack_pop = 0

    stack = [(start, [start])]
    while stack:
        (vertex, path) = stack.pop(stack_pop)
        neighbors = node_neighbors(graph, vertex)
        for next_node in set(neighbors) - set(path):
            if cutoff and len(path) > cutoff:
                break
            if next_node == goal:
                yield path + [next_node]
            else:
                stack.append((next_node, path + [next_node]))
def node_children(graph, nid, root, include_self=False, parent=None):
    """
    Return the children of the source node.

    Traversal path is determined from the parent node with respect to the
    root via the source node to the children.
    A speedup can be achieved by proving the known parent nid using the
    `parent` argument no longer requiring a call to `node_parent`.

    Directed graphs and/or masked behaviour: masked child nodes or child
    nodes with directed connections from child to parent but not vice-versa
    will not be returned.

    :param graph:        Graph to perform calculation for
    :type graph:         Graph class instance
    :param nid:          source node to start search from
    :type nid:           :py:int, :py:str
    :param root:         root node for the search
    :type root:          :py:int, :py:str
    :param include_self: include source nid in results
    :type include_self:  :py:bool
    :param parent:       parent nid if known
    :type parent:        :py:int, :py:str

    :return:             children node ID's
    :rtype:              :py:list
    """

    # Children are all neighbors of the node except the parent
    p = parent or node_parent(graph, nid, root)
    children = [n for n in node_neighbors(graph, nid) if not n == p]

    if include_self:
        children.insert(0, nid)

    return children
 def _walk(child_nid):
     ch = node_neighbors(graph, child_nid)
     for c in ch:
         if c not in start:
             start.append(c)
             _walk(c)