def stable_reverse_topological_sort(graph):
    if not graph.is_directed():
        raise networkx.NetworkXError(
            'Topological sort not defined on undirected graphs.')
    seen = set()
    explored = set()
    for v in sorted(graph.nodes()):
        if v in explored:
            continue
        fringe = [v]
        while fringe:
            w = fringe[-1]
            if w in explored:
                fringe.pop()
                continue
            seen.add(w)
            new_nodes = []
            for n in sorted(graph[w]):
                if n not in explored:
                    if n in seen:
                        raise networkx.NetworkXUnfeasible(
                            'Graph contains a cycle.')
                    new_nodes.append(n)
            if new_nodes:
                fringe.extend(new_nodes)
            else:
                explored.add(w)
                yield w
                fringe.pop()