Leetcode: Course Schedule

来源:互联网 发布:godaddy PHP创建 编辑:程序博客网 时间:2024/05/21 11:09

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?

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:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
click to show more hints.

Hints:
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.
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
Topological sort could also be done via BFS.

问题描述及算法分析

课程清单这题是典型的拓扑排序,本题Note中也提到其实质就是判断在一个有向图中是否有环。拓扑排序有两个基本操作:决定一个顶点是否入度为0和删除一个顶点的所有出边。即在有向图中每次找到一个入度为0的节点,然后把它指向其他节点的边都去掉,重复这个过程,直到所有节点已被找到,或者没有符合条件的节点(有环存在)。
算法采用队列存储节点可降低复杂度,二维数组vector<vector<int>>表示一个图,用一维数组vector<int>记录入度的个数,定义一个queue变量,将所有入度为0的点放入队列中,然后开始遍历队列,从graph里遍历其连接的点,每到达一个新节点,将其入度减一,如果此时该点入度为0,则放入队列末尾。直到遍历完队列中所有的值,若此时还有节点的入度不为0,则说明环存在,返回false,反之则返回true。

代码

class Solution {public:    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {        vector<vector<int>> graph(numCourses);        vector<int> indegree(numCourses,0);        for(auto i : prerequisites) {            graph[i.first].push_back(i.second);            indegree[i.second]++;        }        queue<int> q;        for (auto a = 0; a < numCourses;a++) {            if (indegree[a] == 0) {                q.push(a);            }        }        int count = 0;        while (!q.empty()) {            int temp = q.front();            q.pop();            count++;            for(auto v : graph[temp]) {                indegree[v]--;                if (indegree[v] == 0) {                    q.push(v);                }            }        }        return count == numCourses;    }};
【易错点】vector容器的赋值及遍历操作;队列的声明、入队及出队操作

其他算法分析

可采用DFS算法实现

class Solution {public:    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {        vector<vector<int>> graph(numCourses);        vector<int> visited(numCourses, 0);        for (auto a : prerequisites) {            graph[a.second].push_back(a.first);        }        bool cycle = false;        for (auto b = 0; b < numCourses; b++) {            if (cycle) return false;            if (visited[b] == 0) {                DFS(b,graph,cycle,visited);            }        }        return !cycle;    }    void DFS(int node, vector<vector<int>>& graph, bool & cycle, vector<int> & visited) {        if(visited[node] == 1) {            cycle = true;            return;        }        visited[node] = 1;        for (auto i : graph[node]) {            DFS(i,graph,cycle,visited);            if(cycle) return;        }        visited[node] = 2;    }};
原创粉丝点击