[LeetCode]Course Schedule

来源:互联网 发布:浏览器默认端口号 编辑:程序博客网 时间:2024/06/06 06:59

这次的题目是Course Schedule...题目看起来好像很复杂,我也纠结了好久...但是实际上...并没有想象中那么复杂orz

207Course Schedule31.1%Medium

它是Graph分类里面的一道题,还有后续的Course Schedule II

题目介绍是这样子的:

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?

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

2, [[1,0],[0,1]]

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.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.
是不是觉得很复杂...实际上并不...下面的hint才是重点:
Hints:
  1. This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
  2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  3. Topological sort could also be done via BFS.
这道题的精髓就是:判断一个有向图是否有环...这下子就容易多了,两个办法:用DFS或者用拓扑排序。
相比DFS而言,拓扑排序相对来说容易多了,如果一个有向图能够进行拓扑排序,那么一定是没有环的,一个有环的有向图是没法成功完成拓扑排序的,所以我选择了BFS的拓扑排序。
BFS的拓扑排序的思想是这样的,依次抹去图中入度为0的点,我用一个新的vector把它存起来用以判断最后是否成功完成拓扑排序了,然后把它的出边也抹去,这样就会产生新的入度为0的点,这样重复进行下去,直到没有入度为0的点为止。如果一个无环的有向图,那么新的vector的size应该会等于原先的图的顶点数,如果小于原先的顶点数的话,说明有顶点没有被检索到,也就是有环存在。
代码如下:
#include<iostream>#include<vector>#include<queue>#include<algorithm>using namespace std;struct Node{int num;vector<Node*> next;int indegree;bool ischeck;Node(int n){num=n;ischeck=false;indegree=0;}};bool TopologicalSort(int numCourses,vector<Node*>& graph){queue<Node*> list;vector<Node*> sortedList;int count;for(int i=0;i<numCourses;i++){if(graph[i]->indegree==0&&graph[i]->ischeck==false){list.push(graph[i]);}}while(list.size()>0){Node* temp=list.front();//cout<<"temp is "<<temp->num;sortedList.push_back(list.front());temp->ischeck=true;//cout<<"     "<<temp->num<<"'s ischeck is "<<temp->ischeck<<endl;list.pop();for(int i=0;i<temp->next.size();i++){temp->next[i]->indegree--;if(temp->next[i]->indegree==0){    list.push(temp->next[i]);    //cout<<"list pushback "<<temp->next[i]->num<<endl;}}}if(sortedList.size()==numCourses){return true;}else{return false;}}bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites){    if(prerequisites.size()==0)    {        return true;    }    bool finish=true;//queue<Node*> tocheck;vector<Node*> graph;for(int i=0;i<numCourses;i++){Node* temp=new Node(i);graph.push_back(temp);}int size=prerequisites.size();for(int i=0;i<size;i++){int first=prerequisites[i].first;int second=prerequisites[i].second;graph[second]->next.push_back(graph[first]);graph[first]->indegree++;}finish=TopologicalSort(numCourses,graph);return finish;}
相比起DFS来,拓扑排序真的是太太太太太简单了...
0 0
原创粉丝点击