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

Update topological_sort.py #11278

Closed
wants to merge 1 commit 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
241 changes: 226 additions & 15 deletions sorts/topological_sort.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,226 @@
"""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"]
testCases = [

Check failure on line 3 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N816)

sorts/topological_sort.py:3:1: N816 Variable `testCases` in global scope should not be mixedCase
{
# 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"],

Check failure on line 60 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W191)

sorts/topological_sort.py:60:1: W191 Indentation contains tabs

Check failure on line 60 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E101)

sorts/topological_sort.py:60:1: E101 Indentation contains mixed spaces and tabs
"i": ["j"],
"j": ["k"],
"k": [],

Check failure on line 63 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W191)

sorts/topological_sort.py:63:1: W191 Indentation contains tabs

Check failure on line 63 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E101)

sorts/topological_sort.py:63:1: E101 Indentation contains mixed spaces and tabs
}, "vertices": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]},
{
#
# a
# / \

Check failure on line 68 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/topological_sort.py:68:31: W291 Trailing whitespace
# b c
# / \ / \
# d e f g
"edges":

Check failure on line 72 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/topological_sort.py:72:17: W291 Trailing whitespace
{
"a": ["b", "c"],
"b": ["d", "e"],
"c": ["f", "g"],
"d": [],
"e": [],
"f": [],
"g": [],
}, "vertices": ["a", "b", "c", "d", "e", "f", "g"]},
{
#
# a
# / \

Check failure on line 85 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/topological_sort.py:85:31: W291 Trailing whitespace
# b c
# / \ / \
# d e f g
# / \ \ \
# h i j k
# / \
# l m
# /
# n
#

Check failure on line 95 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/topological_sort.py:95:10: W291 Trailing whitespace
#

Check failure on line 96 in sorts/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/topological_sort.py:96:10: W291 Trailing whitespace
"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]:
"""Perform topological sort on a directed acyclic graph."""
Expand All @@ -37,5 +244,9 @@


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