-
Notifications
You must be signed in to change notification settings - Fork 5
/
CourseSchedule.java
82 lines (71 loc) · 2.79 KB
/
CourseSchedule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Problem Statement:
There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Problem Link:
Course Schedule:https://leetcode.com/problems/course-schedule/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/CourseSchedule.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
return topologicalSortUsingKahnsAlgortihm(numCourses, prerequisites);
}
/* Kahns Algorithm:
- Find Degrees of Each node in the graph and add the ones with 0 degree to the queue, Visited 0
- Pick the node with degree 0
- Increment Visited and decrease degree of each neighbor by 1
- If any neighbor's degree becomes 0 then add it to the queue
- If at any point the queue is empty but visited!=numberOfNodes then Toplogical Order does not exist
*/
public boolean topologicalSortUsingKahnsAlgortihm(int numCourses, int[][] prerequisites){
boolean[][] adj = new boolean[numCourses][numCourses];
for(int i=0;i<prerequisites.length;i++)
adj[prerequisites[i][0]][prerequisites[i][1]] = true;
int degree[] = new int[numCourses];
Queue<Integer> queue = new ArrayDeque<>();
int visited =0 ;
setupDegrees(adj, degree, queue);
if(queue.isEmpty())
return false;
while(!queue.isEmpty()){
int node = (int) queue.remove();
visited++;
decreaseDegreeOfNeighbors(adj, node, degree, queue);
}
if(visited==numCourses)
return true;
return false;
}
public void setupDegrees(boolean adj[][], int[] degree, Queue queue){
for(int i=0;i<adj.length;i++){
degree[i]=getDegree(adj, i);
if(degree[i]==0)
queue.add(i);
}
}
public int getDegree(boolean adj[][], int node){
int degree =0;
for(int i =0;i<adj.length;i++){
if(adj[i][node] == true)
degree++;
}
return degree;
}
public void decreaseDegreeOfNeighbors(boolean adj[][], int node, int degree[], Queue queue){
for(int i=0;i<adj.length;i++){
if(adj[node][i]==true){
degree[i]--;
if(degree[i]==0){
queue.add(i);
}
}
}
}
}