-
-
Notifications
You must be signed in to change notification settings - Fork 49.9k
Add topological sort using DFS and Kahn's algorithm #14086
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
base: master
Are you sure you want to change the base?
Changes from all commits
02b6e20
be33ac2
f50a3f8
2427e79
8bb8fe7
ec3a574
b0c4729
3fa277d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """ | ||
| Topological Sort implementation using: | ||
| 1. DFS-based approach | ||
| 2. Kahn's Algorithm (BFS-based approach) | ||
|
|
||
| Topological sorting is applicable only for Directed Acyclic Graphs (DAGs). | ||
|
|
||
| Reference: | ||
| https://en.wikipedia.org/wiki/Topological_sorting | ||
| """ | ||
|
|
||
| from collections import deque | ||
|
|
||
|
|
||
| def _dfs( | ||
| node: int, | ||
| graph: list[list[int]], | ||
| visited: list[int], | ||
| result: list[int], | ||
| ) -> None: | ||
| """ | ||
| Helper DFS function for topological sorting. | ||
| """ | ||
| if visited[node] == 1: | ||
| raise ValueError("Graph contains a cycle") | ||
| if visited[node] == 2: | ||
| return | ||
|
|
||
| visited[node] = 1 | ||
| for neighbor in graph[node]: | ||
| _dfs(neighbor, graph, visited, result) | ||
| visited[node] = 2 | ||
| result.append(node) | ||
|
|
||
|
|
||
| def topological_sort_dfs(vertices: int, edges: list[list[int]]) -> list[int]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
| """ | ||
| Perform topological sort using DFS. | ||
|
|
||
| Example: | ||
| vertices = 6 | ||
| edges = [[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]] | ||
| order = topological_sort_dfs(vertices, edges) | ||
| """ | ||
| graph: list[list[int]] = [[] for _ in range(vertices)] | ||
| for u, v in edges: | ||
| graph[u].append(v) | ||
|
|
||
| visited = [0] * vertices | ||
| result: list[int] = [] | ||
|
|
||
| for vertex in range(vertices): | ||
| if visited[vertex] == 0: | ||
| _dfs(vertex, graph, visited, result) | ||
|
|
||
| return result[::-1] | ||
|
|
||
|
|
||
| def topological_sort_kahn(vertices: int, edges: list[list[int]]) -> list[int]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
| """ | ||
| Perform topological sort using Kahn's Algorithm. | ||
|
|
||
| Example: | ||
| vertices = 6 | ||
| edges = [[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]] | ||
| order = topological_sort_kahn(vertices, edges) | ||
| """ | ||
| graph: list[list[int]] = [[] for _ in range(vertices)] | ||
| in_degree = [0] * vertices | ||
|
|
||
| for u, v in edges: | ||
| graph[u].append(v) | ||
| in_degree[v] += 1 | ||
|
|
||
| queue = deque(i for i in range(vertices) if in_degree[i] == 0) | ||
| topo_order: list[int] = [] | ||
|
|
||
| while queue: | ||
| node = queue.popleft() | ||
| topo_order.append(node) | ||
| for neighbor in graph[node]: | ||
| in_degree[neighbor] -= 1 | ||
| if in_degree[neighbor] == 0: | ||
| queue.append(neighbor) | ||
|
|
||
| if len(topo_order) != vertices: | ||
| raise ValueError("Graph contains a cycle") | ||
|
|
||
| return topo_order | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/sort/topological_sort.py, please provide doctest for the function_dfs