首页 > 其他 > 详细

course schedule bfs indegree

时间:2018-09-08 11:04:29      阅读:227      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/tobeabetterpig/p/9608272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!