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 #11285

Closed
wants to merge 17 commits into from
Closed
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
259 changes: 244 additions & 15 deletions sorts/topological_sort.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,243 @@
"""Topological Sort."""

# a
# / \
# b c
# / \
# d e
edges: dict[str, list[str]] = {
"a": ["c", "b"],
"b": ["d", "e"],
"c": [],
"d": [],
"e": [],
}
vertices: list[str] = ["a", "b", "c", "d", "e"]
test_cases: list[dict[str, list]] = [
{
# a
# / \
# b c
# / \
# d e
"edges": {
"a": ["c", "b"],
"b": ["d", "e"],
"c": [],
"d": [],
"e": [],
},
"vertices": ["a", "b", "c", "d", "e"],
},
{
# a
# / | \
# b c f
# / \ / \
# d e g h
# /
# i
"edges": {
"a": ["b", "c", "f"],
"b": ["d", "e"],
"c": [],
"d": [],
"e": [],
"f": ["g", "h"],
"g": ["i"],
"h": [],
"i": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g", "h", "i"],
},
{
# a
# / \
# b c
# / / \
# d e f
# /
# g
# /
# h
# /
# i
# /
# j
# /
# k
"edges": {
"a": ["b", "c"],
"b": ["d"],
"c": ["e", "f"],
"d": ["g"],
"e": [],
"f": [],
"g": ["h"],
"h": ["i"],
"i": ["j"],
"j": ["k"],
"k": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"],
},
{
#
# a
# / \
# b c
# / \ / \
# d e f g
"edges": {
"a": ["b", "c"],
"b": ["d", "e"],
"c": ["f", "g"],
"d": [],
"e": [],
"f": [],
"g": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g"],
},
{
#
# a
# / \
# b c
# / \ / \
# d e f g
# / \ \ \
# h i j k
# / \
# l m
# /
# n
#
#
"edges": {
"a": ["b", "c"],
"b": ["d", "e"],
"c": ["f", "g"],
"d": ["h", "i"],
"e": ["j"],
"f": [],
"g": ["k"],
"h": ["l"],
"i": ["m"],
"j": [],
"k": [],
"l": ["n"],
"m": [],
"n": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g"],
},
{
# a
# /
# b
# /
# c
# /
# d
# /
# e
# /
# f
# /
# g
# /
# h
"edges": {
"a": ["b"],
"b": ["c"],
"c": ["d"],
"d": ["e"],
"e": ["f"],
"f": ["g"],
"g": ["h"],
"h": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g", "h"],
},
{
# a
# / | \
# b c d
# / \
# e f
# / \
# g h
# / \
# i j
# / \
# k l
"edges": {
"a": ["b", "c", "d"],
"b": ["e"],
"c": [],
"d": ["f"],
"e": ["g"],
"f": ["h"],
"g": ["i"],
"h": ["j"],
"i": ["k"],
"j": ["l"],
"k": [],
"l": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g", "h", "i"],
},
{
# a
# / \
# b c
# / \
# d e
"edges": {
"a": ["b", "c"],
"b": ["d"],
"c": ["e"],
"d": [],
"e": [],
},
"vertices": ["a", "b", "c", "d", "e"],
},
{
# a h
# / \ \
# b c i
# | | |
# d e j
# | |
# g f
"edges": {
"a": ["b", "c"],
"b": ["d"],
"c": ["e", "f"],
"d": ["g"],
"e": [],
"f": [],
"g": [],
"h": ["i"],
"i": ["j"],
"j": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
},
{
# a h k
# / \ \ \
# b c i l
# | | | |
# d e j m
# | |
# g f
"edges": {
"a": ["b", "c"],
"b": ["d"],
"c": ["e"],
"d": ["g"],
"e": ["f"],
"f": [],
"g": [],
"h": ["i"],
"i": ["j"],
"j": [],
"k": ["l"],
"l": ["m"],
"m": [],
},
"vertices": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"],
},
]


def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
Expand All @@ -37,5 +262,9 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st


if __name__ == "__main__":
sort = topological_sort("a", [], [])
print(sort)
for idx, test_case in enumerate(test_cases, start=1):
print(f"Test Case {idx}:")
edges = test_case["edges"]
vertices = test_case["vertices"]
sort = topological_sort("a", [], [])
print(sort)