#NEWLINE# def topological_sort_recursive( G , nbunch = None ) : #NEWLINE# #INDENT# 'Return a list of nodes in topological sort order.\n\n    A topological sort is a nonunique permutation of the nodes such\n    that an edge from u to v implies that u appears before v in the\n    topological sort order.\n\n    Parameters\n    ----------\n    G : NetworkX digraph\n\n    nbunch : container of nodes (optional)\n       Explore graph in specified order given in nbunch\n\n    Raises\n    ------\n    NetworkXError\n       Topological sort is defined for directed graphs only. If the\n       graph G is undirected, a NetworkXError is raised.\n\n    NetworkXUnfeasible\n        If G is not a directed acyclic graph (DAG) no topological sort\n        exists and a NetworkXUnfeasible exception is raised.\n\n    Notes\n    -----\n    This is a recursive version of topological sort.\n\n    See also\n    --------\n    topological_sort\n    is_directed_acyclic_graph\n\n    ' #NEWLINE# if ( not G . is_directed ( ) ) : #NEWLINE# #INDENT# raise nx . NetworkXError ( 'Topological sort not defined on undirected graphs.' ) #NEWLINE# #UNINDENT# def _dfs( v ) : #NEWLINE# #INDENT# ancestors . add ( v ) #NEWLINE# for w in G [ v ] : #NEWLINE# #INDENT# if ( w in ancestors ) : #NEWLINE# #INDENT# raise nx . NetworkXUnfeasible ( 'Graph contains a cycle.' ) #NEWLINE# #UNINDENT# if ( w not in explored ) : #NEWLINE# #INDENT# _dfs ( w ) #NEWLINE# #UNINDENT# #UNINDENT# ancestors . remove ( v ) #NEWLINE# explored . add ( v ) #NEWLINE# order . append ( v ) #NEWLINE# #UNINDENT# ancestors = set ( ) #NEWLINE# explored = set ( ) #NEWLINE# order = [ ] #NEWLINE# if ( nbunch is None ) : #NEWLINE# #INDENT# nbunch = G . nodes_iter ( ) #NEWLINE# #UNINDENT# for v in ancestors : #NEWLINE# #INDENT# if ( v not in explored ) : #NEWLINE# #INDENT# _dfs ( v ) #NEWLINE# #UNINDENT# #UNINDENT# return list ( reversed ( order ) )