course schedule bfs indegree

Posted tobeabetterpig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了course schedule bfs indegree相关的知识,希望对你有一定的参考价值。

class Solution{
  public boolean canFinish(int numCourses, int[][] prerequisites){
    if(numCourses == 0 || prerequisites.length == 0) return true;
    
    int count = 0;
    int[] indegree = new int[numCourses];
    Queue<Integer> queue = new LinkedList<>();
    List<List<Integer>> list = new ArrayList<>();
    
    for(int i = 0; i < prerequisites.length; i++){
      indegree[prerequisites[i][0]]++;
    }
    
        
    for(int i = 0; i < numCourses; i++){
      if(indegree[i] == 0){
        count++;
        queue.offer(i);
      }
    }
    

    for(int i = 0; i < numCourses; i++){
      list.add(new ArrayList<>());
    }

    for(int i = 0; i < prerequisites.length; i++){
      list.get(prerequisites[i][1]).add(prerequisites[i][0]);
    }

    
    
    while(!queue.isEmpty()){
      int cur = queue.poll();
      for(int element : list.get(cur)){
        indegree[element]--;
        if(indegree[element] == 0){
          queue.offer(element);
          count++;
        }
      }
    }
    
    return count == numCourses;
  }
}

Use bfs 

 

The given is [[1,0]] 

To take course 1 you should have finished course 0. So it is possible.

 

So we need to convert the input into something like adj list , so 

It’s a list of list, the index of the list is the course number, the list in the index is  a list of courses 

You can take after taking course index I 

 

For example :

 

{

{1,2,3}

{4,5}

}

 

so this means at index 0, course 0, after course 0, you can take course 1, 2,3 

At index 1, course 1, after taking course 1,  you can take course 4 and course 5 

 

in this list of list of integers. We need to fill the list in with array list first, so when we add classes, we can add it directly 

 

for(int i = 0; i < numCourses; i++){

      list.add(new ArrayList<>());

    }

 

    for(int i = 0; i < prerequisites.length; i++){

      list.get(prerequisites[i][1]).add(prerequisites[i][0]);

    }

 

    

 

 

 

 

 

We also need to use the input to build indegree map

 

in bfs, we use queue to store current visiting classes, first , we offer the classes which has indegree = 0. We can use int[] array or hash map to represent indegree map, but if int[] array can do the work, no need to use hash map.

 

 

 

 

 

 

We are also given the number of courses in total in the input

 

the problem ask us if  is it possible for you to finish all courses?

 

Input: 2, [[1,0],[0,1]]

Output: false

Explanation: There are a total of 2 courses to take. 

             To take course 1 you should have finished course 0, and to take course 0 you should

             also have finished course 1. So it is impossible.

 

 

In this case, we can’t. Because there is no way to start, you don’t know which class to start .

In this case, there is no node which has indegree 0. 

 

So this problem can be translated into count the number of indegree = 0 every time we take one class a, we can another class b , and b’s indegree  - -. If b’s indegree becomes 0, we can use a counter ++. 

 

 

At the end, after the queue is empty, after all the classes are processed, we can decide if 

All the classes can be taken by checking if the number of indegree = 0    ?= number of courses we are given 

以上是关于course schedule bfs indegree的主要内容,如果未能解决你的问题,请参考以下文章

207. Course Schedule

leetcode-Course Schedule-207

210. Course Schedule II

leetcode 207. Course Schedule

LeetCode 207. Course Schedule

Course Schedule