Leetcode 207. Course Schedule

207. Course Schedule

题目

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?

Example 1:

Input: 2, [[1,0]] 
Output: true
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0. So it is possible.

Example 2:

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.

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.

解题思路

方法一:

该题是一个典型的判断有向图是否有环的问题,可以运用拓扑排序解决:

(1)先将该图用邻接表表示,计算每个节点的入度;

(2)找到第一个入度为0的节点;

(3)将此节点的入度修改为-1(防止重复找到此节点),将所有此节点的所有后驱节点的入度减1;

(4)重复(2)(3)步骤,直至找不到入度为0的节点。

(5)若找到的入度为0的节点个数小于numCourses,则说明有环,返回false;否则,返回true。

代码如下:

class Solution {
public:
  bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
    vector<vector<int>> graph(numCourses);
    vector<int> indegree(numCourses, 0);
    buildGraph(graph, prerequisites);
    countIndegree(graph, indegree);
    for (int i=0; i<numCourses; i++){
    	int j = 0;
    	for(; j<numCourses; j++){
    		if (indegree[j] == 0){
    			indegree[j] = -1;
    			for(int neighbor : graph[j]){
    				indegree[neighbor]--;
    			}
         		break;
    		}
    	}
    	if (j == numCourses) return false;
    }
    return true;
  }

private:
	void buildGraph(vector<vector<int>> &graph, vector<pair<int, int>>& prerequisites) {
		for(pair<int, int> p : prerequisites) {
			graph[p.first].push_back(p.second);
		}
	}

	void countIndegree(vector<vector<int>> &graph, vector<int> &indegree){
		for(vector<int> node : graph){
			for(int neighbor : node){
				indegree[neighbor]++;
			}
		}
	} 
};

方法二:

可以使用dfs的方法查找该图是否有环:

(1)给所有节点设置一个状态为0,表示该节点未搜索过;

(2)任意从一个状态为0的节点开始搜索,将该节点状态设置为1,表示正在搜索;

(3)如果后驱节点状态为0,深度优先搜索后驱节点。如果后驱节点状态为1,说明当前正在搜索的节点形成了一个环,将result置为false;

(4)注意到:一个节点(记为A)dfs搜索完后,若没有检测出环,则从另一个不属于A的子孙节点的节点(记为B)出发,指向A的子孙节点,从该子孙节点开始dfs,同样检测不出环。因此可以在将一个节点(A)dfs完后,将其状态设置为2(表示搜索结束),这样,另一个指向节点A的节点B将不会搜索指向A的这条路径。避免重复搜索。

(5)当所有节点的状态都为2时,表明所有节点都搜索过。如果找到了环,则result被置为false,否则result为true,将result返回即可。

代码如下:

class Solution {
public:
  bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
  	bool res = true;
    vector<vector<int>> graph(numCourses);
    vector<int> state(numCourses, 0);
    buildGraph(graph, prerequisites);
    for(int i=0; i<numCourses; i++){
    	if(state[i] == 0){
    		dfs(graph, state, i, res);
    	}
    }  
    return res;
  }

private:
	void buildGraph(vector<vector<int>> &graph, vector<pair<int, int>>& prerequisites) {
		for(pair<int, int> p : prerequisites) {
			graph[p.first].push_back(p.second);
		}
	}

	void dfs(vector<vector<int>> &graph, vector<int> &state, int &i, bool &res){
		state[i] = 1;
		for(int neighbor : graph[i]){
			if (state[neighbor] == 0){
				dfs(graph, state, neighbor, res);
			}
			else if (state[neighbor] == 1) {
				res = false;
			}
		}
		state[i] = 2;
	} 
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值