def topological_order(graph):
    """Topological sorting by maintaining indegree

    :param graph: directed graph in listlist format, cannot be listdict
    :returns: list of vertices in order
    :complexity: `O(|V|+|E|)`
    """
    V = range(len(graph))
    indeg = [0 for _ in V]
    for node in V:            # compute indegree
        for neighbor in graph[node]:
            indeg[neighbor] += 1
    Q = [node for node in V if indeg[node] == 0]
    order = []
    while Q:
        node = Q.pop()        # node without incoming arrows
        order.append(node)
        for neighbor in graph[node]:
            indeg[neighbor] -= 1
            if indeg[neighbor] == 0:
                Q.append(neighbor)
    return order
def topological_order ( graph ) : V = range ( len ( graph ) ) indeg = [ 0 for _ in V ] for node in V : for neighbor in graph [ node ] : indeg [ neighbor ] += 1 Q = [ node for node in V if indeg [ node ] == 0 ] order = [ ] while Q : node = Q . pop ( ) order . append ( node ) for neighbor in graph [ node ] : indeg [ neighbor ] -= 1 if indeg [ neighbor ] == 0 : Q . append ( neighbor ) return order
Topological sorting by maintaining indegree

    :param graph: directed graph in listlist format, cannot be listdict
    :returns: list of vertices in order
    :complexity: `O(|V|+|E|)`