Open
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
|
Owner
|
@kavishnasta Star the repo as well... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: