def _kahn_adjacency_list(graph: Graph) -> list: S = Queue() in_degree = {u: 0 for u in graph.vertices} for u in graph.vertices: for v in graph.neighbors(u): in_degree[v.name] += 1 for u in graph.vertices: if in_degree[u] == 0: S.append(u) in_degree.pop(u) L = [] while S: n = S.popleft() L.append(n) for m in graph.neighbors(n): graph.remove_edge(n, m.name) in_degree[m.name] -= 1 if in_degree[m.name] == 0: S.append(m.name) in_degree.pop(m.name) if in_degree: raise ValueError("Graph is not acyclic.") return L
def _job(graph: Graph, u: str): for v in graph.neighbors(u): graph.remove_edge(u, v.name)