LeetCode-207. Course Schedule

Description

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.

Node

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.

Solution 1(C++)

class Solution{
public:
	map<int, int> visited;
	bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
		map<int, vector<int>> graph;
		for(auto i : prerequisites){
			graph[i.first].push_back(i.second);
		}

		map<int, vector<int>>::iterator iter = graph.begin();
		while(iter != graph.end()){
			if(visited[cur] == 0){
				if(!dfs(graph, iter->first)) return false;
			}
			iter++;
		}
		return true;
	}

	bool dfs(map<int, vector<int>>& graph, int cur){
		if(visited[cur] == 1) return false;
		visited[cur] = 1;
		for(auto i : graph[cur]){
			if(!dfs(graph, i)) return false;
		}
		visited[cur] = 2;
		return true;
	}
};

Solution 2(C++)

class Solution{
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {//BFS
        vector<vector<int>> graph(numCourses, vector<int>(0));
        vector<int> inDegree(numCourses, 0);
        
        for(auto i : prerequisites){
            graph[i.second].push_back(i.first);
            inDegree[i.first]++;
        }
        queue<int> q;

        for(int i = 0; i < inDegree.size(); i++){
            if(inDegree[i] == 0){
                q.push(i);
            }
        }
        while(!q.empty()){
            int temp = q.front();
            q.pop();
            for(auto j : graph[temp]){
                inDegree[j]--;
                if(inDegree[j] == 0){
                    q.push(j);
                }
            }
        }
        for(int i = 0; i < inDegree.size(); i++){
            if(inDegree[i] != 0){
                return false;
            }
        }
        return true;
    }
};

算法分析

解决问题之前要先认识问题,这个问题其实就是一个拓扑排序的问题。拓扑排序本质上还是排序问题,只是数字排序是按照大小,而拓扑排序是按照先后关系来排序的。这个问题实质就是判断一个有向图存不存在环。如果存在环,就返回false,如果不存在环,就返回true。

图的表示形式有两种:1.领接矩阵;2.邻接表。下面两种解法都用到了邻接表。

解法一
解法一使用的是DFS,其思路是:

  1. 构建邻接表,graph。第一项储存出发点,第二项是该出发点能够到达的其他点。
  2. 构建访问表,visited。0表示还没有访问,1表示正在访问该点与其能够到达的其他点,或者说,正在判断该点是否在一个环中。2表示访问完毕,该点不在一个环中。
  3. 对graph所有出发点进行DFS遍历(步骤4)。如果visited表中,对应的点对应的值为0,说明还没有访问过,就深度优先遍历。
  4. DFS遍历的规则是,如果visited[i]=1,直接返回false,说明存在环;不等于1,那么就递归对该点后续的点进行DFS遍历。如果遍历完成,visted[i]=2,说明该点不存在环。

解法二

解法二要借用入度的概念,入度就是说,图中的一个结点,被其他结点指向的个数。那么对应的出度,就是这个结点指向其他结点的个数。

那么要判断一个图是否存在环,基本做法是:

  1. 找到一个图中所有入度为0的结点。
  2. 删除这些入度为0的结点。更新删除这些结点之后的图。那么原来被入度为0的点所指向的那些点,入度也会相应减少(因为原先指向该点的那些结点(原先入度为0的结点)被删除了)。
  3. 重复1、2步骤,如果图中所有结点的入度都为0,那么说明图中不存在环,返回true;如果存在入度不为0的结点,说明存在环。返回false;

对比解法一与解法二,可以注意到两个解法中对于邻接表的构造方法是不一样的:

  • DFS中的graph第一项储存的是出发点(起点),第二项是出发点能到达的所有结点(终点)的集合;
  • BFS中的graph第一项储存的是到达点(终点),第二项是所有能到达,到达点的所有结点(起点)的集合。

那么这样也很好计算图中各个结点对应的出度与入度了。分别计算对应的graph的第二项,数组的大小即可。

程序分析

略。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值