def postorder_3color(graph, root):
 color = dict()
 order = []
 def dfs_walk(node):
  color[node] = 'grey'
  for succ in graph.successors(node):
   if (color.get(succ) == 'grey'):
    print 'CYCLE: {0}-->{1}'.format(node, succ)
   if (succ not in color):
    dfs_walk(succ)
  order.append(node)
  color[node] = 'black'
 dfs_walk(root)
 return order

def postorder_3color ( graph , root ) : color = dict ( ) order = [ ] def dfs_walk ( node ) : color [ node ] = 'grey' for succ in graph . successors ( node ) : if ( color . get ( succ ) == 'grey' ) : print 'CYCLE: {0}-->{1}' . format ( node , succ ) if ( succ not in color ) : dfs_walk ( succ ) order . append ( node ) color [ node ] = 'black' dfs_walk ( root ) return order
return a post-order ordering of nodes in the graph .