There is a total of n courses you have to take, labeled from 0
to n - 1
.
Some courses May has 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, are it possible for your to finish all courses?
For example:
2, [[1,0]]
There is a total of 2 courses to take. To take course 1 should has finished course 0. So it is possible.
2, [[1,0],[0,1]]
There is a total of 2 courses to take. To take course 1 should has finished course 0, and to take course 0 you should also has finished course 1. So it is impossible.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more on how a graph is represented.
- Assume that there is no duplicate edges in the input prerequisites.
The main investigation of the graph of the traversal.
I wrote the time-out, stating that there are still a lot of problems.
1, the Brute force algorithm, the result timeout.
Public classSolution { Public BooleanCanfinish (intNumcourses,int[] Prerequisites) { int[] num =New int[numcourses][numcourses]; for(inti = 0; i < prerequisites.length; i++) {num[prerequisites[i][0]][PREREQUISITES[I][1]] = 1; } int[] finish =New int[numcourses]; BooleanFlag =false; while(true) {flag=false; for(inti = 0; i < numcourses; i++){ for(intj = 0; J < Numcourses; J + +){ if(Num[i][j] = = 1){ Break; } Else if(j = = NumCourses-1 && finish[i] = = 0) {Finish[i]= 1; Isfinish (num, i); Flag=true; } } } if(Flag = =false){ for(inti = 0; i < numcourses; i++){ if(Finish[i] = = 0) Break; Else if(i = = NumCourses-1) return true; } return false; } } } Public voidIsfinish (int[] num,intPOS) { for(inti = 0; i < num.length; i++) {Num[i][pos]= 0; } }}
2, optimization, but still time-out.
Public classSolution { Public BooleanCanfinish (intNumcourses,int[] Prerequisites) { int[] num =New int[numcourses][numcourses]; for(inti = 0; i < prerequisites.length; i++){ if(Num[prerequisites[i][1]][prerequisites[i][0]] = = 1){ return false; } num[prerequisites[i][0]][PREREQUISITES[I][1]] = 1; for(intj = 0; J < Numcourses; J + +){ /*k = prerequisites[i][1] The work that needs to be done before (i.e. num[k][j] = = 1) needs to be completed num[num[prerequisites[i][0]][ j] = 1; */ if(Num[prerequisites[i][1]][j] = = 1) {num[prerequisites[i][0]][J] = 1; } /*need to complete k = prerequisites[i][0] to complete the work (num[j][k] = = 1), also to complete num[j][prerequisites[i][1]] = 1; */ if(Num[j][prerequisites[i][0]] = = 1) {num[j][prerequisites[i][1]] = 1; } } } return true; } }
3. DFS (refer to discuss)
Public classSolution { Public BooleanCanfinish (intNumcourses,int[] Prerequisites) {arraylist[] list=Newarraylist[numcourses]; for(inti = 0; i < numcourses; i++) {List[i]=NewArraylist<integer>(); } for(inti = 0; i < prerequisites.length; i++) {list[prerequisites[i][1]].add (prerequisites[i][0]); } Boolean[] Visit =New Boolean[numcourses]; for(inti = 0; i < numcourses; i++){ if(!DFS (list, visit, i)) { return false; } } return true; } Public BooleanDFS (arraylist[) list,Boolean[] Visit,intPOS) { if(Visit[pos]) {return false; } Else{Visit[pos]=true; } for(inti = 0; I < list[pos].size (); i++){ if(!dfs (list, visit, (int) (List[pos].get (i) )) {return false; }
List[pos].remove (i); } Visit[pos]=false; return true; } }
4. BFS
Public classSolution { Public BooleanCanfinish (intNumcourses,int[] Prerequisites) {List<integer>[] Adj =Newlist[numcourses]; for(inti = 0; i < numcourses; i++) Adj[i]=NewArraylist<integer>(); int[] Indegree =New int[numcourses]; Queue<Integer> readycourses =NewLinkedList (); intFinishcount = 0; for(inti = 0; i < prerequisites.length; i++) { intCurcourse = prerequisites[i][0]; intPrecourse = prerequisites[i][1]; Adj[precourse].add (Curcourse); Indegree[curcourse]++; } for(inti = 0; i < numcourses; i++) { if(Indegree[i] = = 0) Readycourses.offer (i); } while(!Readycourses.isempty ()) { intCourse = Readycourses.poll ();//Finishfinishcount++; for(intNextcourse:adj[course]) {Indegree[nextcourse]--; if(Indegree[nextcourse] = = 0) Readycourses.offer (nextcourse); // Ready } } returnFinishcount = =numcourses; }}
Leetcode 207. Course Schedule Lesson Plan----------Java