diff --git a/code_to_optimize/topological_sort.py b/code_to_optimize/topological_sort.py index 6d3fa457a..5d428992c 100644 --- a/code_to_optimize/topological_sort.py +++ b/code_to_optimize/topological_sort.py @@ -14,10 +14,10 @@ def topologicalSortUtil(self, v, visited, stack): visited[v] = True for i in self.graph[v]: - if visited[i] == False: + if not visited[i]: self.topologicalSortUtil(i, visited, stack) - stack.insert(0, v) + stack.append(v) # Changed from insert(0, v) to append for efficiency def topologicalSort(self): visited = [False] * self.V @@ -25,7 +25,8 @@ def topologicalSort(self): sorting_id = uuid.uuid4() for i in range(self.V): - if visited[i] == False: + if not visited[i]: self.topologicalSortUtil(i, visited, stack) + stack.reverse() # Reverse once for output order instead of inefficient insert(0, v) return stack, str(sorting_id)