def sort_topologically(dag):
    dag = copy.deepcopy(dag)
    sorted_nodes = []
    independent_nodes = deque(get_independent_nodes(dag))
    while independent_nodes:
        node = independent_nodes.popleft()
        sorted_nodes.append(node)
        downstream_nodes = dag[node]
        while downstream_nodes:
            downstream_node = downstream_nodes.pop(0)
            if downstream_node not in dag:
                continue
            if not has_dependencies(downstream_node, dag):
                independent_nodes.append(downstream_node)
    if len(sorted_nodes) != len(dag.keys()):
        raise ValueError('graph is not acyclic')
    return sorted_nodes