graphit/graph_algorithms.py
def dfs(graph, root, method='dfs', max_depth=10000):
    """
    General implementation of depth-first-search algorithm.
    
    The real power of the dfs method is combining it with the
    graph query methods. These allow sub graphs to be selected
    based on node or edge attributes such as graph directionality
    or edge weight.
    
    :param graph:     graph to search
    :type graph:      graph class instance
    :param root:      root node to start the search from
    :type root:       node ID or Node object
    :param method:    search method. Depth-first search (dfs, default)
                      and Breath-first search (bfs) supported.
    :type method:     :py:str
    :param max_depth: maximum search depth
    :type max_depth:  int, default equals 10000
    """
    
    # Get node object from node ID
    root = graph.getnodes(root)
    
    # Define the search method
    stack_pop = -1
    if method == 'bfs':
        stack_pop = 0
    
    visited = []
    stack = [root.nid]
    depth = 0
    
    while stack or depth == max_depth:
        node = stack.pop(stack_pop)
        
        if node not in visited:
            visited.append(node)
            stack.extend(
                [x for x in node_neighbors(graph, node) if x not in visited])
            depth += 1
    
    return visited

    General implementation of depth-first-search algorithm.
    
    The real power of the dfs method is combining it with the
    graph query methods. These allow sub graphs to be selected
    based on node or edge attributes such as graph directionality
    or edge weight.
    
    :param graph:     graph to search
    :type graph:      graph class instance
    :param root:      root node to start the search from
    :type root:       node ID or Node object
    :param method:    search method. Depth-first search (dfs, default)
                      and Breath-first search (bfs) supported.
    :type method:     :py:str
    :param max_depth: maximum search depth
    :type max_depth:  int, default equals 10000
    
General implementation of depth-first-search algorithm.
The real power of the dfs method is combining it with the
graph query methods. These allow sub graphs to be selected
based on node or edge attributes such as graph directionality
or edge weight.
General implementation of depth - first - search algorithm . The real power of the dfs method is combining it with the graph query methods . These allow sub graphs to be selected based on node or edge attributes such as graph directionality or edge weight .
def dfs(graph, root, method='dfs', max_depth=10000):
    root = graph.getnodes(root)
    stack_pop = -1
    if method == 'bfs':
        stack_pop = 0
    visited = []
    stack = [root.nid]
    depth = 0
    while stack or depth == max_depth:
        node = stack.pop(stack_pop)
        if node not in visited:
            visited.append(node)
            stack.extend(
                [x for x in node_neighbors(graph, node) if x not in visited])
            depth += 1
    return visited
def dfs ( graph , root , method = 'dfs' , max_depth = 10000 ) : root = graph . getnodes ( root ) stack_pop = - 1 if method == 'bfs' : stack_pop = 0 visited = [ ] stack = [ root . nid ] depth = 0 while stack or depth == max_depth : node = stack . pop ( stack_pop ) if node not in visited : visited . append ( node ) stack . extend ( [ x for x in node_neighbors ( graph , node ) if x not in visited ] ) depth += 1 return visited
General implementation of depth-first-search algorithm.
General implementation of depth - first - search algorithm .
"""
    General implementation of depth-first-search algorithm.
    
    The real power of the dfs method is combining it with the
    graph query methods. These allow sub graphs to be selected
    based on node or edge attributes such as graph directionality
    or edge weight.
    
    :param graph:     graph to search
    :type graph:      graph class instance
    :param root:      root node to start the search from
    :type root:       node ID or Node object
    :param method:    search method. Depth-first search (dfs, default)
                      and Breath-first search (bfs) supported.
    :type method:     :py:str
    :param max_depth: maximum search depth
    :type max_depth:  int, default equals 10000
    """ # Get node object from node ID # Define the search method
graph
graph to search
graph to search
root
root node to start the search from
root node to start the search from
method
search method. Depth-first search (dfs, default)
and Breath-first search (bfs) supported.
search method . Depth - first search ( dfs default ) and Breath - first search ( bfs ) supported .
max_depth
maximum search depth
maximum search depth