MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / topological_generations

Function topological_generations

easygraph/classes/operation.py:260–282  ·  view source on GitHub ↗
(G)

Source from the content-addressed store, hash-verified

258
259
260def topological_generations(G):
261 if not G.is_directed():
262 raise AssertionError("Topological sort not defined on undirected graphs.")
263 indegree_map = {v: d for v, d in G.in_degree() if d > 0}
264 zero_indegree = [v for v, d in G.in_degree() if d == 0]
265 while zero_indegree:
266 this_generation = zero_indegree
267 zero_indegree = []
268 for node in this_generation:
269 if node not in G:
270 raise RuntimeError("Graph changed during iteration")
271 for child in G.neighbors(node):
272 try:
273 indegree_map[child] -= 1
274 except KeyError as err:
275 raise RuntimeError("Graph changed during iteration") from err
276 if indegree_map[child] == 0:
277 zero_indegree.append(child)
278 del indegree_map[child]
279 yield this_generation
280
281 if indegree_map:
282 raise AssertionError("Graph contains a cycle or graph changed during iteration")
283
284
285def topological_sort(G):

Callers

nothing calls this directly

Calls 4

appendMethod · 0.80
is_directedMethod · 0.45
in_degreeMethod · 0.45
neighborsMethod · 0.45

Tested by

no test coverage detected