diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index efce8165fcac..fc638cbfd5a0 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -17,16 +17,17 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]: """Perform topological sort on a directed acyclic graph.""" + # consider edge direction from top to bottom current = start # add current to visited visited.append(current) neighbors = edges[current] + # as the current node encounter add it to the topo sort list + sort.append(current) for neighbor in neighbors: # if neighbor not in visited, visit if neighbor not in visited: sort = topological_sort(neighbor, visited, sort) - # if all neighbors visited add current to sort - sort.append(current) # if all vertices haven't been visited select a new one to visit if len(visited) != len(vertices): for vertice in vertices: