function topological_sort(graph): # Kahn's algorithm
   in_degree = count_incoming_edges(graph)

   queue = [v for v in graph if in_degree[v] == 0]
   order = []
   while queue:
      u = queue.remove_first()
      order.append(u)

      for v in graph[u]:
        in_degree[v] -= 1
        if in_degree[v] == 0:
           queue.append(v)
   if length(order) < length(graph):

      report an unsupported case # graph has a cycle
   return order
