From 857192d08f4fb4d4e19cdbc135e165244f93d32b Mon Sep 17 00:00:00 2001 From: Nikhil_sai <118588515+nikhilsaimarri@users.noreply.github.com> Date: Sat, 25 Oct 2025 13:01:27 -0500 Subject: [PATCH] Add course scheduling solution using BFS Implement BFS-based topological sort to determine if all courses can be finished based on prerequisites. --- Scheduling Courses | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Scheduling Courses diff --git a/Scheduling Courses b/Scheduling Courses new file mode 100644 index 00000000..a6c590d7 --- /dev/null +++ b/Scheduling Courses @@ -0,0 +1,51 @@ +// We use BFS to perform topological sort by starting with nodes that have zero incoming edges (indegree 0). +// As we visit each course, we reduce the indegree of its dependents and add them to the queue if their indegree becomes zero. +// If we can visit all courses this way, there's no cycle, and the courses can be completed. + +// time o(v+e) +// space o(v+e) + +class Solution { + public boolean canFinish(int numCourses, int[][] prerequisites) { + + int[] indegrees = new int[numCourses]; + HashMap> map = new HashMap<>(); + + for(int[] pr : prerequisites){ + // pr[0] - dependent, pr[1] - independent + indegrees[pr[0]]++; + map.putIfAbsent(pr[1], new ArrayList<>()); + map.get(pr[1]).add(pr[0]); + } + + int count = 0; + Queue q = new LinkedList<>(); + + for(int i = 0; i < numCourses; i++){ + if(indegrees[i] == 0){ + q.add(i); + count++; + } + } + + if(q.isEmpty()) return false; + if(count == numCourses) return true; + + while(!q.isEmpty()){ + int curr = q.poll(); + List dependencies = map.get(curr); + if(dependencies != null){ + for(int dependent : dependencies){ + indegrees[dependent]--; + if(indegrees[dependent] == 0){ + q.add(dependent); + count++; + if(count == numCourses) return true; + } + } + } + } + + return false; + } +}