Skip to content

Commit a0a8209

Browse files
author
Kcstring
committed
Fix topological sort order
1 parent 791deb4 commit a0a8209

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

sorts/topological_sort.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
2525
# if neighbor not in visited, visit
2626
if neighbor not in visited:
2727
sort = topological_sort(neighbor, visited, sort)
28-
# if all neighbors visited add current to sort
29-
sort.append(current)
28+
# if all neighbors visited add current before its descendants
29+
sort.insert(0, current)
3030
# if all vertices haven't been visited select a new one to visit
3131
if len(visited) != len(vertices):
3232
for vertice in vertices:

0 commit comments

Comments
 (0)