def find_order(graph):
    '''
    Do a topological sort on the dependency graph dict.
    '''
    while graph:
        leftmost = [l for l, s in graph.items() if not s]
        if not leftmost:
            raise ValueError('Dependency cycle detected! %s' % graph)
        leftmost.sort()
        for result in leftmost:
            yield result
            graph.pop(result)
            for bset in graph.values():
                bset.discard(result)