Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated topological_sort.py #11290

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9b7e9e1
Update topological_sort.py
ShengLiuDev Feb 12, 2024
fe22fb2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
ad74538
Update topological_sort.py
ShengLiuDev Feb 12, 2024
2d1622f
Merge branch 'master' of https://github.com/ShengLiuDev/TheAlgorithms…
ShengLiuDev Feb 12, 2024
0a8383b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
fecee82
Update topological_sort.py
ShengLiuDev Feb 12, 2024
047ddd2
Merge branch 'master' of https://github.com/ShengLiuDev/TheAlgorithms…
ShengLiuDev Feb 12, 2024
029c12f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
96232dd
Fixed Camel Casing
ShengLiuDev Feb 12, 2024
aace5a0
Merge branch 'master' of https://github.com/ShengLiuDev/TheAlgorithms…
ShengLiuDev Feb 12, 2024
ed0e152
Fixed capitalization
ShengLiuDev Feb 12, 2024
b3e467c
Added as keyword
ShengLiuDev Feb 12, 2024
81bbdd3
got rid of import
ShengLiuDev Feb 12, 2024
c5fc2a1
Any import
ShengLiuDev Feb 12, 2024
d2eafae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
ae7ae96
removed any
ShengLiuDev Feb 12, 2024
05eb625
Merge branch 'master' of https://github.com/ShengLiuDev/TheAlgorithms…
ShengLiuDev Feb 12, 2024
78af49e
rewrote to list
ShengLiuDev Feb 12, 2024
505e448
Update topological_sort.py
ShengLiuDev Feb 12, 2024
fae8f10
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
d8a8dbb
updated line length
ShengLiuDev Feb 12, 2024
de11626
Merge branch 'master' of https://github.com/ShengLiuDev/TheAlgorithms…
ShengLiuDev Feb 12, 2024
60ac77c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
1fad900
changed sort and visited types
ShengLiuDev Feb 12, 2024
eaaa7ca
Merge branch 'master' of https://github.com/ShengLiuDev/TheAlgorithms…
ShengLiuDev Feb 12, 2024
1648a87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 50 additions & 8 deletions sorts/topological_sort.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Topological Sort."""

# Test cases:

# a
# / \
# b c
Expand All @@ -15,27 +17,67 @@
vertices: list[str] = ["a", "b", "c", "d", "e"]


def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
"""Perform topological sort on a directed acyclic graph."""
def topological_sort_util(
start: str, edges: dict[str, list[str]], visited: list[str], sort: list[str]
) -> list[str]:
""""""
"""
Examples:
>>> topological_sort(['a', 'b', 'c', 'd'], {"a": ["b", "c"], "b": ["d"],
"c": [], "d": []})
['d', 'c', 'b', 'a']
>>> topological_sort(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], {"a": ["b"],
"b": ["c"], "c": ["d"], "d": ["e"], "e": ["f"], "f": ["g"], "g": ["h"], "h": []})
['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> topological_sort(['a', 'b', 'c', 'd', 'e', 'f', 'g'], {"a": ["b", "c"],
"b": ["d", "e"], "c": ["f", "g"], "d": [], "f": [], "h": []})
['d', 'f', 'e', 'g', 'c', 'b', 'a']
>>> topological_sort([], {})
['']
"""

current = start
# add current to visited
visited.append(current)
neighbors = edges[current]
for neighbor in neighbors:
# if neighbor not in visited, visit
if neighbor not in visited:
sort = topological_sort(neighbor, visited, sort)
sort = topological_sort_util(neighbor, edges, 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:
if vertice not in visited:
sort = topological_sort(vertice, visited, sort)
if len(visited) != len(edges):
for vertex in edges:
if vertex not in visited:
sort = topological_sort_util(vertex, edges, visited, sort)
# return sort
return sort


def topological_sort(vertices: list[str], edges: dict[str, list[str]]) -> list[str]:
sort: list[str] = []
visited: list[str] = []
for vertex in vertices:
if vertex not in visited:
sort = topological_sort_util(vertex, edges, visited, sort)
return sort


if __name__ == "__main__":
sort = topological_sort("a", [], [])
# Get vertices from the user
vertices_input = input("Please enter the vertices separated by commas: ").strip()
vertices = [vertex.strip() for vertex in vertices_input.split(",")]
# Initialize an empty dictionary for edges
edges = {}
# Iterate over each vertex to get its connected edges
for vertex in vertices:
edges_input = input(
f'Please enter the edges connected to vertex "{vertex}" '
"separated by commas (leave empty to finish): "
).strip()
edges[vertex] = [
edge.strip() for edge in edges_input.split(",") if edge.strip()
]
sort = topological_sort(vertices, edges)
print(sort)