def topo_sort(self, edges): g = defaultdict(list) inDeg = defaultdict(int) for x, y in edges: inDeg[x] = 0
for x, y in edges: g[x].append(y) inDeg[y] += 1
q = deque([x for x, d in inDeg.items() if d == 0]) order = [] while q: x = q.popleft() order.append(x) for y in g[x]: inDeg[y] -= 1 if inDeg[y] == 0: q.append(y) if len(order) == len(inDeg): return order else: return None