courseSchedule.md
October 16, 2024 ยท View on GitHub
Problem statement:
Given an array prerequisites course pairs where prerequisites[i] = [a, b] indicates that you must take course b first if you want to take course a. There are a total of numCourses courses you are required to take, labeled from 0 to numCourses - 1.
For example, the pair [0, 1], indicates that must take course 1 before taking course 0.
Return true if it is possible to finish all courses, otherwise return false.
Examples:
Example1:
Input: numCourses = 2, prerequisites = [[0,1]]
Output: true
Example2:
Input: numCourses = 2, prerequisites = [[0,1],[1,0]]
Output: false
Algorithmic Steps This problem is solved by DFS using recursion. The algorithmic approach can be summarized as follows:
-
Create a function(
canFinish1) to determine all courses can be finished or not with number of courses(numCourses) and prerequisite courses(prerequisites) as their properties. -
Create a prerequisite map property(
prereqMap) with key as a course name and value as list of prerequistes for the given course -
Iterate over all the courses and update the prerequisite map values as empty array.
-
Iterate over the prerequistes and update the prerequisite map with their respective given prerequisite courses.
-
Create a dfs function(
dfs) to determine whether any cycle/loop exists with the given prerequisites or not. If the courses forms a cycle, all the courses cannot be completed.- This function accepts current course, prerequisite map and visit course set as input parameters.
- If the course already present in visit set, return
falseindicating that all courses cannot be finished due to a cycle. - Return
trueif there are no prerequiste for the given course. - Add the current course to the visit set since the course is processed.
- Iterate over each prerequiste course and perform DFS function on each course. Return
falseif dfs function returnfalsefor any course. - Remove the current course from the visit set because the current path is traversed.
- Reset the prerequiste map for given course to empty array. This is because all prerequiste courses are processed.
- At the end, Return
truefrom dfs function indicating that all courses can be finished.
-
Create a visit course set to track the already enrolled courses along the DFS path or not.
-
Invoke the dfs function for each course and return false if any course path cannot be finished.
-
As a last step, return
trueindicating that all courses can be completed.
Time and Space complexity:
This algorithm has a time complexity of O(V+E), where V is the number of courses in graph and E is the number of edges. This is because each course is visited exactly once during the traversal of dfs function, results in V time complexity. While visiting each course, all of its prerequistes(edges) are iterated to find any possible cycles, contributing to E time complexity. Hence, the overall time complexity is O(V+E).
The canFinish1 function requires O(V+E) space complexity, where V is the number of courses in the graph and E is the number of edges. This is because the prequisite map(or adjacency map) dominates the memory to store the nodes and their prerequisites.