Fix vertex filtering in layered topological sort

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-29 18:36:42 -03:00
commit 2305806c40

View file

@ -631,7 +631,7 @@ class Graph:
vertices: List[Vertex],
) -> List[List[str]]:
"""Performs a layered topological sort of the vertices in the graph."""
vertices_ids = [vertex.id for vertex in vertices]
vertices_ids = {vertex.id for vertex in vertices}
# Queue for vertices with no incoming edges
queue = deque(
vertex.id for vertex in vertices if self.in_degree_map[vertex.id] == 0
@ -646,7 +646,10 @@ class Graph:
vertex_id = queue.popleft()
layers[current_layer].append(vertex_id)
for neighbor in self.successor_map[vertex_id]:
# only vertices in `vertices` are considered
# only vertices in `vertices_ids` should be considered
# because vertices by have been filtered out
# in a previous step. All dependencies of theirs
# will be built automatically if required
if neighbor not in vertices_ids:
continue