Time Complexity: O(∣E∣+∣V∣) where |V| is the number. of courses, and |E| is the number of dependencies.
As in the previous algorithm, it would take us |E| time complexity to build a graph in the first step. Similar with the above postorder DFS traversal, we would visit each vertex and each edge once and only once in the worst case, i.e. |E| + |V|∣E∣+∣V∣.As a result, the overall time complexity of the algorithm would be O(2|E| + |V|) = O(|E| + |V|).
Space Complexity: O(∣E∣+∣V∣), with the same denotation as in the above time complexity.
We built a graph data structure in the algorithm, which would consume∣E∣+∣V∣ space. In addition, we use a container to keep track of the courses that have no prerequisite, and the size of the container would be bounded by |V|. As a result, the overall space complexity of the algorithm would be O(∣E∣+2⋅∣V∣)=O(∣E∣+∣V∣).