def sort_topologically(dag):
dag = copy.deepcopy(dag)<EOL>sorted_nodes = []<EOL>independent_nodes = deque(get_independent_nodes(dag))<EOL>while independent_nodes:<EOL><INDENT>node = independent_nodes.popleft()<EOL>sorted_nodes.append(node)<EOL>downstream_nodes = dag[node]<EOL>while downstream_nodes:<EOL><INDENT>downstream_node = downstream_nodes.pop(<NUM_LIT:0>)<EOL>if downstream_node not in dag:<EOL><INDENT>continue<EOL><DEDENT>if not has_dependencies(downstream_node, dag):<EOL><INDENT>independent_nodes.append(downstream_node)<EOL><DEDENT><DEDENT><DEDENT>if len(sorted_nodes) != len(dag.keys()):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return sorted_nodes<EOL>
Sort the dag breath first topologically.

    Only the nodes inside the dag are returned, i.e. the nodes that are also keys.

    Returns:
         a topological ordering of the DAG.
    Raises:
         an error if this is not possible (graph is not valid).