-
-
Notifications
You must be signed in to change notification settings - Fork 100
207. Course Schedule #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
207. Course Schedule #191
Conversation
Reviewer's GuideThis PR introduces a new C++ solution for the Course Schedule problem using Kahn’s algorithm: it constructs a directed graph from prerequisites, tracks in-degrees, and performs a BFS-based topological sort to detect cycles and determine if all courses can be completed. Class diagram for the new Solution class (Course Schedule)classDiagram
class Solution {
+bool canFinish(int numCourses, vector<vector<int>>& prerequisites)
}
class graph {
}
class inDegree {
}
class q {
}
Solution "canFinish()" --> graph : uses
Solution "canFinish()" --> inDegree : uses
Solution "canFinish()" --> q : uses
graph : vector<vector<int>>
inDegree : vector<int>
q : queue<int>
Flow diagram for Kahn's algorithm in Course Schedule solutionflowchart TD
A["Start: Build graph and inDegree array"] --> B["Initialize queue with courses having inDegree 0"]
B --> C["While queue is not empty"]
C --> D["Pop course u from queue"]
D --> E["Increment processed count"]
E --> F["For each neighbor v of u in graph"]
F --> G["Decrement inDegree[v]"]
G --> H["If inDegree[v] == 0, add v to queue"]
H --> C
C --> I["After loop, check if processed == numCourses"]
I --> J["Return true if all courses processed, else false"]
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
@kavishnasta Star the repo as well... |
Intuition
We have numCourses courses numbered from 0 to numCourses-1. The array prerequisites contains pairs [a, b], which means to take course a, you must finish course b first.
We can represent this as a directed graph: courses are nodes, and a prerequisite [a, b] is a directed edge from b to a, since b must come before a.
Instead of thinking about whether all courses can be completed, this transforms to: does this directed graph allow for a valid order to visit all nodes while respecting edges? In other words, is there a topological order for this graph?
If there is a cycle, like A → B → C → A, then you cannot complete the courses because each one depends on another in a loop. There is no starting node with an in-degree of 0.
We shall compute the in-degree, which is the number of incoming edges or prerequisites for every node. Then, you pick all nodes with an in-degree of zero, meaning these are the courses you can take right away. Remove them or mark them as done, and reduce the in-degree of their neighbors, which are the courses depending on them. Continue this until you either process all courses (good) or cannot make further progress while still having courses left (bad, indicating a cycle).
If we manage to process all numCourses courses using this method, we return true. If not, we return false.
The complexity is O(V + E), where V is the number of courses and E is the number of prerequisite pairs.
Key things to monitor include building the graph with the correct direction, ensuring the initial queue contains in-degree zero nodes, running the loop until the queue is empty, and keeping track of the number of processed courses.
Approach
1)Build an adjacency list. Graph[b] contains a if there is a prerequisite [a, b].
2)Create an array inDegree[a]++ for each [a, b].
3)Start a queue with all i such that inDegree[i]==0.
4)Remove one course u from the queue and increase the processed count. For each v in graph[u], reduce inDegree[v]. If inDegree[v] becomes 0 after decrementing, add v to the queue.
5)After the loop, if processed equals numCourses, return true; otherwise, return false.
Code Solution (C++)
Related Issues
Closes #190
By submitting this PR, I confirm that:
Summary by Sourcery
New Features: