|
| 1 | +""" |
| 2 | +Topological Sort implementation using: |
| 3 | +1. DFS-based approach |
| 4 | +2. Kahn's Algorithm (BFS-based) |
| 5 | +
|
| 6 | +Topological sorting is applicable only for Directed Acyclic Graphs (DAGs). |
| 7 | +""" |
| 8 | + |
| 9 | +from collections import deque, defaultdict |
| 10 | +from typing import List |
| 11 | + |
| 12 | + |
| 13 | +def topological_sort_dfs(vertices: int, edges: List[List[int]]) -> List[int]: |
| 14 | + """ |
| 15 | + Perform topological sort using DFS. |
| 16 | +
|
| 17 | + :param vertices: Number of vertices in the graph |
| 18 | + :param edges: List of directed edges [u, v] where u -> v |
| 19 | + :return: A list representing topological order |
| 20 | + :raises ValueError: If a cycle is detected |
| 21 | + """ |
| 22 | + graph = defaultdict(list) |
| 23 | + for u, v in edges: |
| 24 | + graph[u].append(v) |
| 25 | + |
| 26 | + visited = [0] * vertices # 0 = unvisited, 1 = visiting, 2 = visited |
| 27 | + stack = [] |
| 28 | + |
| 29 | + def dfs(node: int): |
| 30 | + if visited[node] == 1: |
| 31 | + raise ValueError("Graph contains a cycle") |
| 32 | + if visited[node] == 2: |
| 33 | + return |
| 34 | + |
| 35 | + visited[node] = 1 |
| 36 | + for neighbor in graph[node]: |
| 37 | + dfs(neighbor) |
| 38 | + visited[node] = 2 |
| 39 | + stack.append(node) |
| 40 | + |
| 41 | + for v in range(vertices): |
| 42 | + if visited[v] == 0: |
| 43 | + dfs(v) |
| 44 | + |
| 45 | + return stack[::-1] |
| 46 | + |
| 47 | + |
| 48 | +def topological_sort_kahn(vertices: int, edges: List[List[int]]) -> List[int]: |
| 49 | + """ |
| 50 | + Perform topological sort using Kahn's Algorithm (BFS). |
| 51 | +
|
| 52 | + :param vertices: Number of vertices in the graph |
| 53 | + :param edges: List of directed edges [u, v] where u -> v |
| 54 | + :return: A list representing topological order |
| 55 | + :raises ValueError: If a cycle is detected |
| 56 | + """ |
| 57 | + graph = defaultdict(list) |
| 58 | + in_degree = [0] * vertices |
| 59 | + |
| 60 | + for u, v in edges: |
| 61 | + graph[u].append(v) |
| 62 | + in_degree[v] += 1 |
| 63 | + |
| 64 | + queue = deque([i for i in range(vertices) if in_degree[i] == 0]) |
| 65 | + topo_order = [] |
| 66 | + |
| 67 | + while queue: |
| 68 | + node = queue.popleft() |
| 69 | + topo_order.append(node) |
| 70 | + for neighbor in graph[node]: |
| 71 | + in_degree[neighbor] -= 1 |
| 72 | + if in_degree[neighbor] == 0: |
| 73 | + queue.append(neighbor) |
| 74 | + |
| 75 | + if len(topo_order) != vertices: |
| 76 | + raise ValueError("Graph contains a cycle") |
| 77 | + |
| 78 | + return topo_order |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + vertices = 6 |
| 83 | + edges = [ |
| 84 | + [5, 2], |
| 85 | + [5, 0], |
| 86 | + [4, 0], |
| 87 | + [4, 1], |
| 88 | + [2, 3], |
| 89 | + [3, 1], |
| 90 | + ] |
| 91 | + |
| 92 | + print("DFS-based Topological Sort:") |
| 93 | + print(topological_sort_dfs(vertices, edges)) |
| 94 | + |
| 95 | + print("\nKahn's Algorithm Topological Sort:") |
| 96 | + print(topological_sort_kahn(vertices, edges)) |
0 commit comments