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 来显示统计结果 ...
随机推荐
- Linux 下安装 SVN服务器
前段时间换了一个新项目组.过去发现居然SVN都没有.代码都是手动对比存档.当时就蛋疼了.这他妈也太苦逼了.话不多说,要来测试服务器地址.开工了.由于服务器不能连接外网. 1.先下载安装包.本次安装不结 ...
- KMP 代码 暂存
#include <stdio.h> #include <string.h> ],B[]; ]; int n, m; void _next(){ ; ; next[] = ; ...
- Step one : 熟悉Unix/Linux Shell 常见命令行 (三)
3.学会使用一些管理命令 ps/top/lsof/netstat/kill/tcpdump/iptables/dd 端口查看 ps -- process status ps aux 观察程序所有程序 ...
- 5 MySQL索引
目录: 1. 索引概述 1.1 为什么引入索引 1.2 什么是索引 1.3 索引的好处 1.4 索引的不足 1.5 索引分类 2. 索引设计原则 3. 索引建立和删除 3.1 索引创建 3.2 索引删 ...
- mac 下安装oh my zsh
1.直接从github上下载 git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh 2.拷贝到账户目录下 cp ~/. ...
- 通过VS2010命令提示窗口创建强命名文件时报错问题
问题描述详见图 解决方案 00000005意思是Access Denied(即拒绝访问). 原因是:Program Files(x86)目录对一般用户和未提升权限的管理员是只读的. 所以只要赋予Use ...
- 安装mono和jexus,运行asp.net程序
随笔- 62 文章- 1 评论- 7 raspberrypi(树莓派)上安装mono和jexus,运行asp.net程序 参考网址: http://www.linuxdot.net/ htt ...
- 统计知识选讲(一)——主成分分析(PCA)的思想
主成分分析的主要目的是希望用较少的变量去解释原来资料中的大部分变异,将我们手中许多相关性很高的变量转化成彼此相互独立或不相关的变量,从而达到降维的目的.在原始数据“预处理”阶段通常要先对它们采用PCA ...
- 迷你MVVM框架 avalonjs 0.82发布
迷你MVVM框架 avalonjs 0.82发布 本版本最大的改进是启用全新的parser. parser是用于干什么的?在视图中,我们通过绑定属性实现双向绑定,比如ms-text="fir ...
- 对用户控件(ascx)属性(property)赋值
对用户控件(ascx)属性(property)赋值 Insus.NET写此博文,是对用户控件(ASCX)的属性赋值经验与技巧分享.是这样子的,在做新闻站点时,一般都会有分很多类别. 在站点首页会显示最 ...