Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions sorts/topological_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@


def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
"""Perform topological sort on a directed acyclic graph."""
"""
Perform topological sort on a directed acyclic graph.

>>> topological_sort("a", [], [])
['a', 'b', 'e', 'd', 'c']
"""
current = start
# add current to visited
visited.append(current)
Expand All @@ -25,8 +30,8 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
# 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 neighbors visited add current before its neighbors in sort
sort.insert(0, current)
# if all vertices haven't been visited select a new one to visit
if len(visited) != len(vertices):
for vertice in vertices:
Expand Down