Skip to content

Commit 52ace6c

Browse files
committed
docs(readme): update time and space complexity`
1 parent baefd0f commit 52ace6c

File tree

1 file changed

+3
-10
lines changed
  • algorithms/graphs/sort_items_by_group

1 file changed

+3
-10
lines changed

algorithms/graphs/sort_items_by_group/README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,9 @@ topological order using Kahn’s algorithm. It performs the following steps:
139139

140140
### Time Complexity
141141

142-
The solution’s time complexity is O(n.2^n) because in the worst case, when the array contains
143-
n distinct elements, the recursion tree generates all 2^n possible subsets, resulting in 2^n recursive calls. In each call,
144-
we create a copy of the current subset, which can take up to O(n) time when the subset is at its largest. Therefore, the
145-
overall time complexity of this approach is O(n.2^n).
142+
Time: O(n + m + E) where E is the total number of edges (sum of all beforeItems[i] lengths), since we process each node
143+
and edge once in both graphs.
146144

147145
### Space Complexity
148146

149-
The space complexity of the sorting step depends on the specific sorting algorithm used by the language or library, but
150-
most standard comparison-based sorts require O(log(n)) auxiliary space. The recursive backtracking process uses up to
151-
O(n) space on the call stack, as the maximum depth of recursion corresponds to building subsets of length n. The space
152-
used to store the final list of subsets is not counted toward auxiliary space.
153-
154-
Therefore, the overall auxiliary space complexity of this approach is O(n).
147+
O(n + m + E) for storing the adjacency lists, in-degree arrays, and the queue used in topological sort.

0 commit comments

Comments
 (0)