def dfs_labeled_edges(G, source=None):
 if (source is None):
  nodes = G
 else:
  nodes = [source]
 visited = set()
 for start in nodes:
  if (start in visited):
   continue
  (yield (start, start, 'forward'))
  visited.add(start)
  stack = [(start, iter(G[start]))]
  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 (start, start, 'reverse'))

def dfs_labeled_edges ( G , source = None ) : if ( source is None ) : nodes = G else : nodes = [ source ] visited = set ( ) for start in nodes : if ( start in visited ) : continue ( yield ( start , start , 'forward' ) ) visited . add ( start ) stack = [ ( start , iter ( G [ start ] ) ) ] 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 ( start , start , 'reverse' ) )
produce edges in a depth-first-search labeled by type .