Graph - leetcode [图]
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 [图]的更多相关文章
- 使用perf生成Flame Graph(火焰图)
具体的步骤参见这里: <flame graph:图形化perf call stack数据的小工具> 使用SystemTap脚本制作火焰图,内存较少时,分配存储采样的数组可能失败,需 ...
- 当前数据库普遍使用wait-for graph等待图来进行死锁检测
当前数据库普遍使用wait-for graph等待图来进行死锁检测 较超时机制,这是一种更主动的死锁检测方式,innodb引擎也采用wait-for graph SQL Server也使用wait-f ...
- [LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点
Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...
- [leetcode]133. Clone Graph 克隆图
题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node neighbor 1 2, 3 2 1 3 1 ...
- 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. ...
- Clone Graph——LeetCode
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- Rikka with Graph(联通图取边,暴力)
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 从Random Walk谈到Bacterial foraging optimization algorithm(BFOA),再谈到Ramdom Walk Graph Segmentation图分割算法
1. 从细菌的趋化性谈起 0x1:物质化学浓度梯度 类似于概率分布中概率密度的概念.在溶液中存在不同的浓度区域. 如放一颗糖在水盆里,糖慢慢溶于水,糖附近的水含糖量比远离糖的水含糖量要高,也就是糖附近 ...
- perf + Flame Graph火焰图分析程序性能
1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...
随机推荐
- Javascript实例技巧精选(7)—设置和获取文本框与文本域的光标位置(兼容IE和Chrome,Firefox)
>>点击这里下载完整html源码<< 截图如下: 本实例描述了如何用Javascript来控制和获取文本框/文本域的鼠标光标位置,以下代码兼容IE和Chrome,Firefox ...
- C#的Task和Java的Future
C#的Task和Java的Future 自从项目中语言换成Java后就很久没有看C#了,但说实话我是身在曹营心在汉啊.早就知道.NET4.5新增了async和await但一直没有用过,今天看到这篇文章 ...
- 浅谈PHP在各系统平台下的换行符
<?php echo 'aaa\n';//用于linux.unix平台C的换行也是如此 echo 'bbb\r';//用于mac平台 echo 'ccc\r\n';//用于windows平台 / ...
- Back to High School Physics - UVa10071
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10071.html 题目描述 Pr ...
- jquery水印插件:placeholder
jquery水印插件:placeholder 有的浏览器支持html5的水印placeholder(如Crome,firefox,ie10+),有的不支持html5的placeholder(ie9,i ...
- 迟到的 WPF 学习 —— 入门
之所以说"迟到的",是因为我太晚才开始学习 WPF 了,之前 WPF 刚发布的时候有过粗浅了解,那时的 WPF 还非常简陋,VS 提供的内置控件十分匮乏,让我这样的非常依赖 Win ...
- 设置启动MyEclipse8.5的时候选择工作区间
以前我的MyEclipse启动的时候默认都会进入到指定工作区间,比如:D:\MyEclipse,最近不知道怎么回事,每次启动的时候都是空的,必须要重新Switch Workspace选择一次 网上查找 ...
- RCP:如何把Preferences中的项从一个类别移动到另一个类别 2013-08-23 18:59 by Binhua Liu,
RCP:如何把Preferences中的项从一个类别移动到另一个类别 前言 很久没写文章了,准备写一系列关于Eclipse RCP /Plugin的文章. 这些文章都是trouble shooting ...
- Reeder Web版
Reeder Web版 访Reeder界面效果 一直很欣赏触控手势的代码实现,所以最近折腾了个Javascript触控手势库--JTouch,效果还有诸多不完善之处,苦于硬件设备不完善,针对ie10的 ...
- django admin.py settings 操作
dango, 怎么说呢,什么东西都内置了,什么东西都是自己的东西.用过flask, cherrypy, web.py, pyramid 等等python 框架后,再选用dango 觉得,理念有很大的区 ...