diff --git a/graphs/kahns_algorithm_topo.py b/graphs/kahns_algorithm_topo.py index c956cf9f48fd..df5d1d72b9b8 100644 --- a/graphs/kahns_algorithm_topo.py +++ b/graphs/kahns_algorithm_topo.py @@ -1,3 +1,6 @@ +from collections import deque + + def topological_sort(graph: dict[int, list[int]]) -> list[int] | None: """ Perform topological sorting of a Directed Acyclic Graph (DAG) @@ -21,10 +24,17 @@ def topological_sort(graph: dict[int, list[int]]) -> list[int] | None: >>> graph_with_cycle = {0: [1], 1: [2], 2: [0]} >>> topological_sort(graph_with_cycle) + + >>> sparse_graph = {10: [20], 20: []} + >>> topological_sort(sparse_graph) + [10, 20] + + >>> sparse_cycle = {10: [20], 20: [10]} + >>> topological_sort(sparse_cycle) """ - indegree = [0] * len(graph) - queue = [] + indegree = dict.fromkeys(graph, 0) + queue: deque[int] = deque() topo_order = [] processed_vertices_count = 0 @@ -34,13 +44,13 @@ def topological_sort(graph: dict[int, list[int]]) -> list[int] | None: indegree[i] += 1 # Add all vertices with 0 indegree to the queue - for i in range(len(indegree)): - if indegree[i] == 0: - queue.append(i) + for vertex, count in indegree.items(): + if count == 0: + queue.append(vertex) # Perform BFS while queue: - vertex = queue.pop(0) + vertex = queue.popleft() processed_vertices_count += 1 topo_order.append(vertex) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py index 15b0c96e0f07..bb88e52f3f38 100644 --- a/maths/matrix_exponentiation.py +++ b/maths/matrix_exponentiation.py @@ -1,7 +1,3 @@ -"""Matrix Exponentiation""" - -import timeit - """ Matrix Exponentiation is a technique to solve linear recurrences in logarithmic time. You read more about it here: @@ -9,6 +5,8 @@ https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/ """ +import timeit + class Matrix: def __init__(self, arg: list[list] | int) -> None: