关于namespace的使用
题目:

基于宽度优先搜索的拓扑排序
思路:
这个问题相当查找有向图中是否存在环。逐个删除入度为0的节点,当删除的节点数量等于输入的所有节点数量时,判定不存在环。
拓扑排序最好使用邻接链表存储邻接关系,而非使用邻接矩阵。因为邻接链表在能够非常直接查找到邻接节点,查找操作耗时O(m+n),而邻接矩阵需要进行遍历才能查找到邻接节点,查找操作耗时O(n^2)。其中n为节点数量,m为边的数量。
本算法的时间复杂度和空间复杂度都比较大,原因在于创建了邻接矩阵存储邻接关系。导致在查找邻接关系的时候,需要进行遍历操作,时间复杂度为O(n^2)。
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
if(prerequisites.size() == )
return true;
vector< vector<int> > mat( numCourses, vector<int>(numCourses,) );
vector<int> numR(numCourses, );
vector<int> stack(numCourses, );
vector<int> tIn;
int top = , count = ;
for(int i = ; i < prerequisites.size(); ++i) {
tIn = prerequisites[i];
mat[tIn[]][tIn[]] = ;
numR[tIn[]] += ;
}
for(int j = ; j < numCourses; ++j) {
if(numR[j] == ) {
stack[top++] = j;
}
}
while(top > ) {
int gettop = stack[--top];
count += ;
for(int k = ; k < numCourses; ++k) {
if(mat[gettop][k] == ) {
numR[k] -= ;
if(numR[k] == )
stack[top++] = k;
}
}
}
if(count == numCourses )
return true;
else
return false;
}
};
对上述算法进行改进。将邻接矩阵换成邻接链表。时间复杂度为O(m+n),耗时大大缩小。
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
if(prerequisites.size() == )
return true;
vector< vector<int> > mat(numCourses);
vector<int> numR(numCourses, );
vector<int> stack(numCourses, );
vector<int> tIn;
int top = , count = ;
for(int i = ; i < prerequisites.size(); ++i) {
tIn = prerequisites[i];
mat[tIn[]].push_back(tIn[]);
numR[tIn[]] += ;
}
for(int j = ; j < numCourses; ++j) {
if(numR[j] == ) {
stack[top++] = j;
}
}
while(top > ) {
int gettop = stack[--top];
count += ;
for(int k = ; k < mat[gettop].size(); ++k) {
numR[mat[gettop][k]] -= ;
if(numR[mat[gettop][k]] == )
stack[top++] = mat[gettop][k];
}
}
if(count == numCourses )
return true;
else
return false;
}
};
基于深度优先搜索的拓扑排序
时间复杂度:排序过程为O(n+m),其中n为节点数量,m为边数量。构建邻接链表为O(m)。
空间复杂度:邻接链表占据空间为O(m);标识数组占据空间为O(n);递归过程的栈内存最大为O(n),此时拓扑序列为一条链。
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
if(prerequisites.size() == )
return true;
vector< vector<int> > mat(numCourses);
vector<int> flag(numCourses, );
vector<int> tIn;
for(int i = ; i < prerequisites.size(); ++i) {
tIn = prerequisites[i];
mat[tIn[]].push_back(tIn[]);
}
bool ans = true;
for(int i = ; i < numCourses; ++i) {
ans = ans&&dfsF(i, flag, mat); // 每一条路线不允许存在回路,所以是“&&”逻辑
}
return ans;
}
bool dfsF(int i, vector<int>& flag, const vector<vector<int> >& matC){
if(flag[i] == )
return true;
if(flag[i] == -)
return false;
flag[i] = -;
for(int j = ; j < matC[i].size(); ++j) {
if(dfsF(matC[i][j], flag, matC))
continue;
else
return false;
}
flag[i] = ;
return true;
}
};
关于namespace的使用的更多相关文章
- 理解Docker(3):Docker 使用 Linux namespace 隔离容器的运行环境
本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...
- C++ namespace
namespace, 命名空间, 用于解决命名冲突的问题. Python中的package/module, Javascript中的object, Java中的package都具有这样的功能. 如何使 ...
- C++ 之namespace常见用法
一.背景 需要使用Visual studio的C++,此篇对namespace的常用用法做个记录. 二.正文 namespace通常用来给类或者函数做个区间定义,以使编译器能准确定位到适合的类或者函数 ...
- using namespace std 和 using std::cin
相较using std::cin使用using namespace std不会使得程序的效率变低,或者稳定性降低,只是这样作会将很多的名字引入程序,使得程序员使用的名字集合变小,容易引起命名冲突. 在 ...
- Why Namespace? - 每天5分钟玩转 OpenStack(102)
上一节我们讨论了 Neutron 将虚拟 router 放置到 namespace 中实现了不同 subnet 之间的路由.今天探讨为什么要用 namespace 封装 router? 回顾一下前面的 ...
- struts2中错误There is no Action mapped for namespace [/] and action name [] associated with context path
1 There is no Action mapped for namespace [/] and action name [] associated with context path [/Stru ...
- PHP 命名空间(namespace)
PHP 命名空间(namespace) PHP 命名空间(namespace)是在PHP 5.3中加入的,如果你学过C#和Java,那命名空间就不算什么新事物. 不过在PHP当中还是有着相当重要的意义 ...
- AMD and CMD are dead之Why Namespace?
缘由 当我看到_Franky兄的微博的时候: 我觉得我有必要出来详细说说KMDjs到底有什么本质上的优势了,连教主_Franky.貘吃馍香都不能理解他的好处,那么可想而知,在前端圈.或是全端圈.或是I ...
- 使用mvc时,在视图view中使用强类型视图,在web.config文件中添加命名空间namespace的引用不起作用,解决方法
这是view中的model代码: @model t_user_info 这是web.config配置文件只的代码: <namespaces> <add namespace=" ...
- C、C++: 引用、指针、实例、内存模型、namespace
// HelloWorld.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
随机推荐
- phantomjs 抓取、截图中文网站乱码的问题的解决
用phantomjs抓取html乱码的解决方案: phantomjs --output-encoding=gbk test.js http://webscan.360.cn/index/checkwe ...
- Go unitest
待测试: // add.go package util func Add(a int, b int) int { return a + b} 使用gotests工具,自动生成测试用例框架: https ...
- Unity 灯光系统详解
Unity 灯光系统详解 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心 ...
- box布局中文字溢出问题
如果不设置-webkit-box-flex:1:会溢出,设置width也行,在电脑上模拟可能会有问题,手机上没问题
- asm.js 和 Emscripten 入门教程
http://www.ruanyifeng.com/blog/2017/09/asmjs_emscripten.html
- [Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition]
https://codeforc.es/contest/1082/problem/C 题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n, ...
- Eclipse和Intel idea的常用技巧
使用Eclipse的几个必须掌握的快捷方式 “工若善其事,必先利其器”,感谢Eclipse,她 使我们阅读一个大工程的代码更加容易,在阅读的过程中,我发现掌握几个Eclipse的快捷键会使阅读体验 ...
- nginx常用参数设置
1)隐藏nginx header 版本号 使用curl -I http://www.10.0.3.46 会发现server那里显示版本号 在nginx.conf的http里添加参数server_tok ...
- 汇编入门基础与helloworld
一个存储器拥有128个存储单元,可存储128个byte(字节),一个bite则又是由8个二进制位即bit(比特)组成,bit是计算机的最小信息单位. 总线分为地址总线,控制总线,数据总线 一个cpu有 ...
- deno学习四 docker 运行官方的一个http file server
github 上已经有人搞了一个deno 的docker 镜像,是基于源码编译的,挺好的 所以结合官方的http server demo 使用docker 运行 环境准备 docker-compose ...