Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a85256

Browse files
authoredOct 23, 2024
Original code
1 parent d400b63 commit 7a85256

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed
 

‎sorts/topological_sort.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
vertices: list[str] = ["a", "b", "c", "d", "e"]
1616

1717

18+
1819
def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
1920
"""Perform topological sort on a directed acyclic graph."""
20-
# consider edge direction from top to bottom
2121
current = start
2222
# add current to visited
2323
visited.append(current)
2424
neighbors = edges[current]
25-
# as the current node encounter add it to the topo sort list
26-
sort.append(current)
2725
for neighbor in neighbors:
2826
# if neighbor not in visited, visit
2927
if neighbor not in visited:
3028
sort = topological_sort(neighbor, visited, sort)
29+
# if all neighbors visited add current to sort
30+
sort.append(current)
3131
# if all vertices haven't been visited select a new one to visit
3232
if len(visited) != len(vertices):
3333
for vertice in vertices:

0 commit comments

Comments
 (0)
Please sign in to comment.