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 来显示统计结果 ...
随机推荐
- Weapsy 分析网站架构
Weapsy 分析(一)网站架构 这个项目看了好久了,但是老没时间写一些分析心得.下班后想了想,事情也不能老拖着,还是得做. 如图所示:Weapsy由5个项目所组成,有点可惜了,没有测试的项目,说明一 ...
- CGI杂谈
CGI是一个连接外部应用程序到信息服务器(比如HTTP或者网络服务器)的标准.一个简单的HTML文档是无交互后台程序,它是静态的,也就是说它处于一个不可变的状态,即文本文件不可以变化.相反地,CGI程 ...
- -协同IResult
Caliburn.Micro学习笔记(五)----协同IResult 今天说一下协同IResult 看一下IResult接口 /// <summary> /// Allows cust ...
- geek 的博客
hexo 适合前端 geek 的博客 原文出自:http://www.qiangji.tk/hexo%E9%80%82%E5%90%88%E5%89%8D%E7%AB%AFgeek%E7%9A%8 ...
- TOGAF架构内容框架之构建块(Building Blocks)
TOGAF架构内容框架之构建块(Building Blocks) 之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并 ...
- ajaxFileUpload+struts2实现多文件上传(动态添加文件上传框)
上篇文章http://blog.csdn.net/itmyhome1990/article/details/36396291介绍了ajaxfileupload实现多文件上传, 但只是固定的文件个数,如 ...
- 【DOS】这个命令太牛逼了
删除一个程序了后 竟然上不了网了 运行下列命令重启后就可以了 实在是救了我系统一命 命令如下: netsh winsock reset
- jquery animate stop函数解析
今天我们来看看jquery中动画操作的stop函数.其实我至今不是很明白啊,所以此文算是求救以及抛砖引玉. 在jquery 1.7版本以前,stop支持两个参数,分别是clearQueue和jumpT ...
- 一种利用异常机制基于MVC过滤器的防止重复提交的机制分享
防止重复提交验证机制 某些时候因为系统反应稍慢,急性子用户可能不耐烦会进行重复的提交,这个操作不仅可能造成系统负担,也可能产生垃圾数据. 出现这两种状况都是我们不希望的. 为此,在公司项目系统设计了以 ...
- DDNS client on a Linux machine
Using this tool -> inadyn apt-get install inadyn -y Usage: https://github.com/troglobit/inadyn#ex ...