20190415-Leetcode-207.课程表

Leetcode-207.课程表

现在你总共有 n 门课需要选,记为 0 到 n-1。
在选修某些课程之前需要一些先修课程。 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]
给定课程总量以及它们的先决条件,判断是否可能完成所有课程的学习?
示例 1:
输入: 2, [[1,0]]
输出: true
解释: 总共有 2 门课程。学习课程 1 之前,你需要完成课程 0。所以这是可能的。
示例 2:
输入: 2, [[1,0],[0,1]]
输出: false
解释: 总共有 2 门课程。学习课程 1 之前,你需要先完成​课程 0;并且学习课程 0 之前,你还应先完成课程 1。这是不可能的。
说明:
输入的先决条件是由边缘列表表示的图形,而不是邻接矩阵。详情请参见图的表示法。
你可以假定输入的先决条件中没有重复的边。
提示:
这个问题相当于查找一个循环是否存在于有向图中。如果存在循环,则不存在拓扑排序,因此不可能选取所有课程进行学习。
通过 DFS 进行拓扑排序 - 一个关于Coursera的精彩视频教程(21分钟),介绍拓扑排序的基本概念。
拓扑排序也可以通过 BFS 完成。
思路1:
用邻接链表构建图的结构,然后采用深度优先搜索遍历,依次访问,用state[N]记录每个结点的状态,若访问链表的过程中存在环的结构,即相互依赖的结构则返回false,否则返回true。
代码1:

struct GraphNode{
    int label;
    vector<GraphNode* > neighbors;
    GraphNode(int x) : label(x){}
};
class Solution {
public:
    bool dfs(vector<int>& state, GraphNode* node){
        if(state[node->label] == 1){
            return true;
        }
        state[node->label] = 0;
        for(int i = 0; i < node->neighbors.size(); i++){
            if(state[node->neighbors[i]->label] == 0){
                return false;
            }
            if(!dfs(state, node->neighbors[i])){
                return false;
            }

        }
        state[node->label] = 1;
        return true;
    }
    
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<int> state;
        vector<GraphNode*> Graph;
        for(int i = 0; i < numCourses; i++){
            state.push_back(-1);
            Graph.push_back(new GraphNode(i));
        }
        for(int i = 0; i < prerequisites.size(); i++){
            Graph[prerequisites[i].first]->neighbors.push_back(Graph[prerequisites[i].second]);
        }
        for(int i = 0; i < numCourses; i++){
            if(state[i] == -1 && !dfs(state, Graph[i])){
                return false;
            }
        }
        
        for(int i = 0; i < numCourses; i++){
            delete Graph[i];
        }
        return true;
        
    }     
};

思路2:
构建好图的结构,用宽度优先搜索,声明一个变量degree[N]用来记录每个图结点的入度数量,并且用一个队列记录所有入度为0的结点,弹出所有入度为0的结点并且该节点指向的结点的入度减1,若又出现入度为0的,则继续进队。
在最后的时候,访问所有结点的度,若都为0,则返回true,若有结点的度不为0,则返回false
代码2:

struct GraphNode{
    int label;
    vector<GraphNode* > neighbors;
    GraphNode(int x) : label(x){}
};
class Solution {
public:
    //bfs
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<int> degree;
        vector<GraphNode* > Graph; 
        queue<GraphNode* > node_queue;
        for(int i = 0; i < numCourses; i++){
            degree.push_back(0);
            Graph.push_back(new GraphNode(i));
        }
        for(int i = 0; i < prerequisites.size(); i++){
            Graph[prerequisites[i].second]->neighbors.push_back(Graph[prerequisites[i].first]);
            degree[prerequisites[i].first]++;
        }
        for(int i = 0; i < numCourses; i++){
            if(degree[i] == 0){
                node_queue.push(Graph[i]);
            }
        }
        while(!node_queue.empty()){
            GraphNode* temp = node_queue.front();
            node_queue.pop();
            for(int i = 0; i < temp->neighbors.size(); i++){
                degree[temp->neighbors[i]->label]--;
                if(degree[temp->neighbors[i]->label] == 0){
                    node_queue.push(temp->neighbors[i]);
                }
            }
        }
        for(int i = 0; i < numCourses; i++){
            delete Graph[i];
        } 
        for(int i = 0; i < numCourses; i++){
            if(degree[i] > 0){
                return false;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/study_deep/article/details/89877369
今日推荐