leetcode 207. Course Schedule

这是一个关于LeetCode第207题的解析,讨论如何判断给定课程是否存在能完成所有课程的顺序。题目中提到,如果课程之间的依赖形成环路,则无法完成所有课程。解决方案涉及到了拓扑排序的概念,可以使用DFS或BFS来检查图中是否存在环路。若存在环路,则无法进行课程排序,故完成所有课程是不可能的。
摘要由CSDN通过智能技术生成

请点击:原题目链接以打开leetcode题目

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.

思路: 当课程之间构成的graph存在环路时–》“to finish all courses is impossible.”借鉴拓扑排序中的思想,检测graph是否存在环路。

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<vector<bool> > adj(numCourses,vector<bool>(numCourses));//adjadency matrix
        for(int i=0;i<prerequisites.size();i++)
            adj[prerequisites[i].second][prerequisites[i].first]=1;
        if(kahann(adj)) return 1;
        else return 0;
    }
    bool kahann(vector<vector<bool> >& adj){
    // 给定邻接矩阵,构造拓扑序列
        int N=adj.size();
        vector<int> indeg(N,0);
        vector<int> S;
        vector<int> L;//from S
        int sum;
        for(int i=0;i<N;i++){
            sum=0;
            for(int j=0;j<N;j++)
                sum+=adj[j][i];
            indeg[i]=sum;
            if(sum==0) S.push_back(i);
        }
        int node;
        while(!S.empty()){
            node=S[0];
            L.push_back(node);
            S.erase(S.begin());
            for(int i=0;i<N;i++){
                if(adj[node][i]==0) continue;
                adj[node][i]=0;
                indeg[i]--;
                if(indeg[i]==0 && i!=node) S.push_back(i);
            }
        }
        sum=0;
        for(int i=0;i<N;i++){
            sum+=indeg[i];
        }
        if(sum) return false;//有环路
        return true; //DAG 无环图
    }
};
int main(){
    vector<pair<int, int>> prerequisites;
    pair<int,int> cur;
    cur.first=0;
    cur.second=1;
    prerequisites.push_back(cur);
    Solution s;
    cout<<s.canFinish(2, prerequisites)<<endl;
    return 0;
}

这里写图片描述
【注】可以看到利用Kahn算法进行环路检测的做法时间复杂度较高,可以考虑换成DFS的方法。这点在leetcode的题目中也有说明。

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值