207. Course Schedule

有向图的环检测
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
///先来看BFS的解法,我们定义二维数组graph来表示这个有向图,一位数组in来表示每个顶点的入度。我们开始先根据输入来建立这个有向图,并将入度数组也初始化好。然后我们定义一个queue变量,将所有入度为0的点放入队列中,然后开始遍历队列,从graph里遍历其连接的点,每到达一个新节点,将其入度减一,如果此时该点入度为0,则放入队列末尾。直到遍历完队列中所有的值,若此时还有节点的入度不为0,则说明环存在,返回false,反之则返回true。
vector<vector<int>> graph(numCourses, vector<int>(0));
vector<int> indegree(numCourses, 0);
for(auto pre : prerequisites){
graph[pre[1]].push_back(pre[0]);////不能通过
indegree[pre[0]]++;
}
queue<int> q;
for(int i = 0; i < numCourses; i++){
if(indegree[i] == 0) q.push(i);
}
while(!q.empty()){
int top = q.front();
q.pop();
for(auto a : graph[top]){
indegree[a]--;
if(indegree[a] == 0) q.push(a);
}
}
for(int i = 0; i < numCourses; i++){
if(indegree[i] != 0) return false;
}
return true;
}
};

再来看DFS的解法,也需要建立有向图,还是用二维数组来建立,和BFS不同的是,我们像现在需要一个一维数组visit来记录访问状态,大体思路是,先建立好有向图,然后从第一个门课开始,找其可构成哪门课,暂时将当前课程标记为已访问,然后对新得到的课程调用DFS递归,直到出现新的课程已经访问过了,则返回false,没有冲突的话返回true,然后把标记为已访问的课程改为未访问。

////对称

vector<vector<int>> graph(numCourses, vector<int>(0));
vector<bool> isVisit(numCourses, false);
for(auto pre : prerequisites){
graph[pre[1]].push_back(pre[0]);
}
for(int i = 0; i < numCourses; i++){
if(!dfs(graph, isVisit, i)) return false;
}
return true;
}

bool dfs(vector<vector<int>>& graph, vector<bool>& isVisit, int i){
if(isVisit[i] == false){
isVisit[i] = true;
//if(!dfs(graph, isVisit, i++)) return false;
for(auto a : graph[i]){
if(!dfs(graph, isVisit, a)) return false;
}
}else{
return false;
}
isVisit[i] = false;
return true;
}

可以这样!!!

vector<unordered_set<int>> make_graph(int numCourses, vector<pair<int, int>>& prerequisites) {

vector<unordered_set<int>> graph(numCourses);

for (auto pre : prerequisites) graph[pre.second].insert(pre.first);

return graph; }

210. Course Schedule II

而此题正是基于之前解法的基础上稍加修改,我们从queue中每取出一个数组就将其存在结果中,最终若有向图中有环,则结果中元素的个数不等于总课程数,那我们将结果清空即可。

有向图的拓扑排序

vector<int> res;
vector<vector<int> > graph(numCourses, vector<int>(0));
vector<int> in(numCourses, 0);
for (auto &a : prerequisites) {
graph[a.second].push_back(a.first);
++in[a.first];
}
queue<int> q;
for (int i = 0; i < numCourses; ++i) {
if (in[i] == 0) q.push(i);
}
while (!q.empty()) {
int t = q.front();
res.push_back(t);
q.pop();
for (auto &a : graph[t]) {
--in[a];
if (in[a] == 0) q.push(a);
}
}
if (res.size() != numCourses) res.clear();
return res;

Graph - leetcode [图]的更多相关文章

  1. 使用perf生成Flame Graph(火焰图)

      具体的步骤参见这里: <flame graph:图形化perf call stack数据的小工具>   使用SystemTap脚本制作火焰图,内存较少时,分配存储采样的数组可能失败,需 ...

  2. 当前数据库普遍使用wait-for graph等待图来进行死锁检测

    当前数据库普遍使用wait-for graph等待图来进行死锁检测 较超时机制,这是一种更主动的死锁检测方式,innodb引擎也采用wait-for graph SQL Server也使用wait-f ...

  3. [LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点

    Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...

  4. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  5. Clone Graph leetcode java(DFS and BFS 基础)

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  6. Clone Graph——LeetCode

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. Rikka with Graph(联通图取边,暴力)

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. 从Random Walk谈到Bacterial foraging optimization algorithm(BFOA),再谈到Ramdom Walk Graph Segmentation图分割算法

    1. 从细菌的趋化性谈起 0x1:物质化学浓度梯度 类似于概率分布中概率密度的概念.在溶液中存在不同的浓度区域. 如放一颗糖在水盆里,糖慢慢溶于水,糖附近的水含糖量比远离糖的水含糖量要高,也就是糖附近 ...

  9. perf + Flame Graph火焰图分析程序性能

    1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...

随机推荐

  1. 打开Openstack dashboard出现Internal Server Error

    最近研究openstack,想把自己遇到的问题记录下来,同时如果有别的朋友也碰到同样问题的时候可以有个参考. 这次的问题是在openstack搭建好之后,dashboard本身是能用的,但是在某一天后 ...

  2. Akka入门实例

    Akka入门实例 Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用. Actor模型并非什么新鲜事物,它由Carl Hew ...

  3. 利用自定义的AuthenticationFilter实现Basic认证

    [ASP.NET MVC] 利用自定义的AuthenticationFilter实现Basic认证   很多情况下目标Action方法都要求在一个安全上下文中被执行,这里所谓的安全上下文主要指的是当前 ...

  4. NUnit详细使用方法

    http://www.ltesting.net/ceshi/open/kydycsgj/nunit/ http://nunit.org/index.php?p=download NUnit详细使用方法 ...

  5. 2013集训.DAY21.A

    随便点了一套刷,这套质量挺棒的,学了不少的东西,并且碰到了很久都没有打的题目 T1 card [指针技巧] 题1 集卡片 [问题描述] lzh小时候很喜欢收集卡片,他经常要去商店购买新到的卡片. 商店 ...

  6. WCF从零学习之设计和实现服务协定2

    WCF从零学习之设计和实现服务协定(二)   在创建服务协定之前,有很多WCF术语,比如: 消息.服务.终结点 创建协定 类或接口都可以定义服务协定,建议使用接口,因为接口可以直接对服务协定建模 服务 ...

  7. lnmp1.0 升级php.5.4.28 后出错 Nginx 502 Bad Gateway

    碰到一个很奇怪的问题,用lnmp自带的./upgrade_php.sh升级 php5.4.27正常.但升级到php5.4.28就出错,访问p.php 提示:Nginx 502 Bad Gateway. ...

  8. 通过Func 委托理解委托和匿名方法及Lambda 表达式

    Func<T, TResult> 委托 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法. 命名空间: System 程序集: mscorlib(在 mscorlib.d ...

  9. eclipse在Ubuntu 13.04下的安装过程

    eclipse在Ubuntu 13.04下的安装过程及问题小记 一.eclipse安装过程 首先确保在安装eclipse之前已经安装好Java虚拟机 1. eclipse官网下载压缩包 下载地址:ht ...

  10. 【C#】Smtp发送邮件

    class SmtpEmail { SmtpClient smtpclient; MailMessage msg; Attachment attachment; public void sendMai ...