original_docstring: Creates a top sort of a directed graph. This is an unstable sorting!
  
          The function returns the sorted nodes as well as the edges that need
          to be removed from the graph to make it acyclic (and hence, sortable).
  
          The graph should be represented as a dictionary, e.g.:
  
          >>> example_graph = {
          ...         "a": set("b", "c", "d"),
          ...         "b": set(),
          ...         "c": set("d"),
          ...         "d": set(),
          ...         "e": set("f"),
          ...         "f": set()}
          >>> StoryGraph.topological_sort(example_graph)
          (deque([u'e', u'f', u'a', u'c', u'd', u'b']), [])
          
docstring: Creates a top sort of a directed graph. This is an unstable sorting.
  The function returns the sorted nodes as well as the edges that need
  to be removed from the graph to make it acyclic (and hence, sortable).
  
  The graph should be represented as a dictionary, e.g..
  
  
docstring_tokens: Creates a top sort of a directed graph . This is an unstable sorting . The function returns the sorted nodes as well as the edges that need to be removed from the graph to make it acyclic ( and hence sortable ) . The graph should be represented as a dictionary e . g ..
code: def topological_sort(
              graph  # type: Dict[Text, Set[Text]]
      ):
          # type: (...) -> Tuple[deque, Set[Tuple[Text, Text]]]
          """Creates a top sort of a directed graph. This is an unstable sorting!
  
          The function returns the sorted nodes as well as the edges that need
          to be removed from the graph to make it acyclic (and hence, sortable).
  
          The graph should be represented as a dictionary, e.g.:
  
          >>> example_graph = {
          ...         "a": set("b", "c", "d"),
          ...         "b": set(),
          ...         "c": set("d"),
          ...         "d": set(),
          ...         "e": set("f"),
          ...         "f": set()}
          >>> StoryGraph.topological_sort(example_graph)
          (deque([u'e', u'f', u'a', u'c', u'd', u'b']), [])
          """
  
          # noinspection PyPep8Naming
          GRAY, BLACK = 0, 1
  
          ordered = deque()
          unprocessed = set(graph)
          visited_nodes = {}
  
          removed_edges = set()
  
          def dfs(node):
              visited_nodes[node] = GRAY
              for k in graph.get(node, set()):
                  sk = visited_nodes.get(k, None)
                  if sk == GRAY:
                      removed_edges.add((node, k))
                      continue
                  if sk == BLACK:
                      continue
                  unprocessed.discard(k)
                  dfs(k)
              ordered.appendleft(node)
              visited_nodes[node] = BLACK
  
          while unprocessed:
              dfs(unprocessed.pop())
          return ordered, removed_edges
code_tokens: def topological_sort ( graph ) : GRAY , BLACK = 0 , 1 ordered = deque ( ) unprocessed = set ( graph ) visited_nodes = { } removed_edges = set ( ) def dfs ( node ) : visited_nodes [ node ] = GRAY for k in graph . get ( node , set ( ) ) : sk = visited_nodes . get ( k , None ) if sk == GRAY : removed_edges . add ( ( node , k ) ) continue if sk == BLACK : continue unprocessed . discard ( k ) dfs ( k ) ordered . appendleft ( node ) visited_nodes [ node ] = BLACK while unprocessed : dfs ( unprocessed . pop ( ) ) return ordered , removed_edges
short_docstring: Creates a top sort of a directed graph.
short_docstring_tokens: Creates a top sort of a directed graph .
comment: # type: Dict[Text, Set[Text]]$SEP$# type: (...) -> Tuple[deque, Set[Tuple[Text, Text]]]$SEP$"""Creates a top sort of a directed graph. This is an unstable sorting!
  
          The function returns the sorted nodes as well as the edges that need
          to be removed from the graph to make it acyclic (and hence, sortable).
  
          The graph should be represented as a dictionary, e.g.:
  
          >>> example_graph = {
          ...         "a": set("b", "c", "d"),
          ...         "b": set(),
          ...         "c": set("d"),
          ...         "d": set(),
          ...         "e": set("f"),
          ...         "f": set()}
          >>> StoryGraph.topological_sort(example_graph)
          (deque([u'e', u'f', u'a', u'c', u'd', u'b']), [])
          """$SEP$# noinspection PyPep8Naming