(G)
Generate sequence of triples (v,w,edgetype) for DFS of graph G.
    The subsequence for each root of each tree in the DFS forest starts
    with (root,root,forward) and ends with (root,root,reverse).
    If the initial vertex is given, it is used as the root and vertices
    not reachable from it are not searched.
Generate sequence of triples (v,w,edgetype) for DFS of graph G.
    The subsequence for each root of each tree in the DFS forest starts
    with (root,root,forward) and ends with (root,root,reverse).
    If the initial vertex is given, it is used as the root and vertices
    not reachable from it are not searched.
Generate sequence of triples ( v w edgetype ) for DFS of graph G . The subsequence for each root of each tree in the DFS forest starts with ( root root forward ) and ends with ( root root reverse ) . If the initial vertex is given it is used as the root and vertices not reachable from it are not searched .
def DFS_search(G):
    """
    Generate sequence of triples (v,w,edgetype) for DFS of graph G.
    The subsequence for each root of each tree in the DFS forest starts
    with (root,root,forward) and ends with (root,root,reverse).
    If the initial vertex is given, it is used as the root and vertices
    not reachable from it are not searched.
    """
    visited = set()
    initials = G

    for v in initials:
        if v not in visited:
            yield v,v,forward
            visited.add(v)
            stack = [(v, iter(G[v]))]
            while stack:
                parent,children = stack[-1]
                try:
                    child = next(children)
                    if child in visited:
                        yield parent,child,nontree
                    else:
                        yield parent,child,forward
                        visited.add(child)
                        stack.append((child,iter(G[child])))
                except StopIteration:
                    stack.pop()
                    if stack:
                        yield stack[-1][0],parent,reverse

            yield v,v,reverse
def DFS_search ( G ) : visited = set ( ) initials = G for v in initials : if v not in visited : yield v , v , forward visited . add ( v ) stack = [ ( v , iter ( G [ v ] ) ) ] while stack : parent , children = stack [ - 1 ] try : child = next ( children ) if child in visited : yield parent , child , nontree else : yield parent , child , forward visited . add ( child ) stack . append ( ( child , iter ( G [ child ] ) ) ) except StopIteration : stack . pop ( ) if stack : yield stack [ - 1 ] [ 0 ] , parent , reverse yield v , v , reverse