Course Schedule II

Course Schedule II – Medium Level Problem

You are given an array prerequisites where each element prerequisites [i] = [a, b] specifies that course b must be completed before you can take course a.

For instance, the pair [0, 1] means you need to complete course 1 before enrolling in course 0.

The total number of courses is numCourses, labeled from 0 to numCourses – 1.

Your task is to return a valid order in which you can complete all the courses. If multiple valid orders exist, return any one of them. If it is not possible to complete all courses, return an empty array.

Course Schedule II Problem

Examples related to Course Schedule II Problem

Example 1:

Example 2:

Constraints:

  • 1 <= numCourses <= 1000
  • 0 <= prerequisites.length <= 1000
  • All prerequisite pairs are unique.

Hints to solve Course Schedule II Problem

Recommended Time & Space Complexity

The ideal solution should have a time complexity of O(V + E) and a space complexity of O(V + E), where V represents the number of courses (nodes), and E represents the number of prerequisites (edges).

Hint 1:

Think of this problem as a graph, where:

  • Courses are the nodes.
  • Each prerequisites[i] = [a, b] represents a directed edge from b to a.

Challenge is to check whether this graph contains a cycle. Why? A cycle means it is impossible to complete the courses involved in the cycle.

Can you identify an algorithm to detect cycles in a graph while also finding a valid ordering of the nodes if no cycle exists?

Hint 2:

  • One way to detect cycles is by using DFS (Depth First Search). While doing this, we can also find a valid course ordering.

  • Alternatively, use Topological Sort, which is specifically designed to determine the ordering of nodes in a Directed Acyclic Graph (DAG).

  • Graph must be acyclic for all courses to be completed, and each prerequisite acts as the parent of a dependent course.

  • Can you figure out how to implement this?

Hint 3:

To implement Topological Sort:

  • Compute the indegree (number of incoming edges) for all nodes.
  • Perform BFS (Breadth First Search) starting from nodes with zero indegree (indegree[node] == 0).
  • For each visited node: Decrease the indegree of its neighbors (child nodes).Add child nodes with zero indegree to the queue.
  • Add each visited node to the result array.

Finally, if the length of the result array matches the total number of courses, return the array.

Otherwise, return an empty array, indicating that completing all courses is impossible.

Methods to Solve Course Schedule II Problem

There are mainly 3 approaches to solve Solve Course Schedule II Problem:

  1. Cycle Detection (DFS)
  2. Topological Sort (Kahn’s Algorithm)
  3. Topological Sort (DFS)

1. Cycle Detection (DFS) Method

This method uses Depth First Search (DFS) to check if there is a cycle in the course dependency graph. If a cycle is detected, completing all courses is impossible; otherwise, we return the course order.

  • Time complexity: O(V+E)
  • Space complexity: O(V+E)

Where V is the number of vertices and E is the number of edges.

Code:

2. Topological Sort (Kahn’s Algorithm)

Kahn’s Algorithm is a BFS-based approach where we process nodes with zero in-degree (no prerequisites). It builds the course order step by step while detecting cycles if any nodes remain unprocessed.

  • Time complexity: O(V + E)
  • Space complexity: O(V + E)

Where V is the number of vertices and E is the number of edges.

Code:

3. Topological Sort (DFS) Method

This method uses DFS to visit nodes in the graph, adding them to the course order in reverse as they finish processing. It ensures that prerequisites are processed first and detects cycles along the way.

  • Time complexity: O(V+E)
  • Space complexity: O(V+E)

Where V is the number of vertices and E is the number of edges.

Code:

More Articles