graph-basic
打算使用STL中的vector,通过邻接链表的方式存储图。这里贴基本定义,以及depth-first-search和breadth-first-search的实现代码。
其他图的算法实现,就贴在各自的算法解释之后吧。喵。
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std; // define vertex in graph
typedef struct Vertex {
int id;
vector<int> neighbors;
Vertex() {
id = -1;
}
Vertex(int nid){
id = nid;
}
} Vertex;
// define graph
typedef struct Graph {
// Vertex of Graph
vector<Vertex> vertexes;
// number of vertexes
int nVertexes;
bool isDAG; // construct function
Graph(int n, bool isDAG) : nVertexes(n), isDAG(isDAG) { vertexes.resize(n); } // add edge.
bool addEdge(int id1, int id2) {
if (max(id1, id2) >= vertexes.size())
return false;
if (isDAG) {
vertexes[id1].neighbors.push_back(id2);
}
else {
vertexes[id1].neighbors.push_back(id2);
vertexes[id2].neighbors.push_back(id1);
}
return true;
} // depth first search
vector<int> DFS(int start) {
set<int> visited;
vector<int> g, result;
g.push_back(start);
visited.insert(start);
result.push_back(start);
bool found;
while(g.size() > 0) {
int id = g[g.size()-1];
found = false;
for(int i = 0; i < vertexes[id].neighbors.size(); i++) {
int id1 = vertexes[id].neighbors[i];
if (visited.count(id1) == 0) {
g.push_back(id1);
result.push_back(id1);
visited.insert(id1);
found = true;
break;
}
}
// all neighbors have been visited
if (!found) {
int id2 = g[g.size()-1];
//result.push_back(-1 * id2);
//cout << "pop " << id2 << " ";
g.pop_back();
}
}
return result;
} // breadth first search
vector<int> BFS(int start) {
set<int> visited;
vector<int> g, result;
// temporary store
g.push_back(start);
visited.insert(start);
while(g.size() > 0) {
int id = g[0];
g.erase(g.begin());
result.push_back(id);
for(int i = 0; i < vertexes[id].neighbors.size(); i++) {
int id1 = vertexes[id].neighbors[i];
// if id1 is unvisited
if (visited.count(id1) == 0) {
g.push_back(id1);
visited.insert(id1);
}
}
}
return result;
}
} Graph; int main() {
Graph g(8, true);
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(1, 2);
g.addEdge(3, 4);
g.addEdge(3, 5);
//g.addEdge(4, 5);
//g.addEdge(4, 6);
g.addEdge(5, 6);
g.addEdge(5, 7);
//g.addEdge(6, 7);
vector<int> bv = g.BFS(0);
cout << "宽度优先搜索节点顺序:";
for(int j = 0; j < bv.size(); j++)
cout << bv[j] << " ";
cout << endl; bv = g.DFS(0);
for(int j = 0; j < bv.size(); j++)
cout << bv[j] << " ";
cout << endl; cout << "深度优先搜索节点顺序:";
Graph g1(6, false);
g1.addEdge(0, 1);
g1.addEdge(0, 4);
g1.addEdge(0, 5);
g1.addEdge(1, 5);
g1.addEdge(4, 5);
g1.addEdge(5, 2);
g1.addEdge(5, 3);
g1.addEdge(2, 3);
vector<int> route = g1.DFS(0);
for(int i = 0; i < route.size(); i++)
cout << route[i] << " ";
cout << endl; return 0;
}
graph-basic的更多相关文章
- 论文解读(DAEGC)《Improved Deep Embedded Clustering with Local Structure Preservation》
Paper Information Title:<Attributed Graph Clustering: A Deep Attentional Embedding Approach>Au ...
- Prometheus Node_exporter 之 Basic CPU / Mem Graph
1. CPU Basic cpu 的基本信息 /proc/stat type: GraphUnit: shortBusy System: cpu 处于核心态的占比 metrics: sum by (i ...
- some basic graph theoretical measures
· mean characteristic path length calculated as the average length of the shortest path between two ...
- 转债---Pregel: A System for Large-Scale Graph Processing(译)
转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/ 作者:Grzegorz Malewicz, Matthew ...
- Pregel: A System for Large-Scale Graph Processing(译)
[说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...
- Graph Databases—The NOSQL Phenomenon阅读笔记
本章内容着重对了NOSQL和RDBMS(关系型数据库管理系统)的不同,以及其各自背后设计时考虑的因素.然后接下来,着重讲述了NOSQL的4种分类方法.下面我们将对重要知识点进行汇总. 1.We def ...
- [TensorFlow] Basic Usage
Install TensorFlow on mac Install pip # Mac OS X $ sudo easy_install pip $ sudo easy_install --upgra ...
- 掀起Azure AD的盖头来——深入理解Microsoft Graph应用程序和服务权限声明
作者:陈希章 发表于 2017年7月12日 引子 这是一篇计划外的文章.我们都知道要进行Microsoft Graph的开发的话,需要进行应用程序注册.这个在此前我已经有专门的文章写过了.但这里存在一 ...
- python生成组织架构图(网络拓扑图、graph.editor拓扑图编辑器)
Graph.Editor是一款基于HTML5技术的拓补图编辑器,采用jquery插件的形式,是Qunee图形组件的扩展项目,旨在提供可供扩展的拓扑图编辑工具, 拓扑图展示.编辑.导出.保存等功能,此外 ...
- 207. Course Schedule(Graph; BFS)
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- .Net Core 做请求监控NLog
使用 NLog 给 Asp.Net Core 做请求监控 https://www.cnblogs.com/cheesebar/p/9078207.html 为了减少由于单个请求挂掉而拖垮整站的情况发生 ...
- NET Core 开发环境
NET Core 开发环境 最近,一直在往.Net Core上迁移,随着工作的深入,发现.Net Core比.Net Framework好玩多了.不过目前还在windows下开发,虽然VisualSt ...
- Linux Ubuntu系统之PPP拨号经验分享
近期,工作需要,我负责开发PPP拨号模块. 说起拨号,算算时间,我已经做过2次了, 暴露年龄了,呵呵. 第一次是刚毕业做的PPOE拨号,给电信做拨号软件,在河北石家庄工作过一段时间,基于windows ...
- @Primary 使用
造轮子的一个小小的发现 当一个接口被两个service实现时,controller调用接口实现功能,会报错,提示开发者指定service,官方是建议你使用@Qualifier来区分的,但是,总有另一种 ...
- Spring Aspect 获取请求参数
切片(Aspect)也就是Spring AOP 实现Aspect的主要步骤: 1.在哪里切入 .在哪个方法起作用 .什么时候起作用 2.起作用的时候执行什么处理逻辑 下面是代码实现 /** * 切片A ...
- Python定时任务sched(一)
这里介绍一下python中定时任务:sched python中自带的是sched,也可以通过pip下载schedule进行任务定时处理,这里先简单介绍下sched的使用 import datetime ...
- hystrix 给方法加断路器
添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>s ...
- python中的random
random.randint(a,b) 用于生成一个指定范围内的整数,a为下限,b为上限,生成的随机整数a<=n<=b;若a=b,则n=a:若a>b,报错 import ran ...
- MVC4学习之官方教程中迁移版本库报错
因工作需要,学习MVC4,但是微软官方教程中迁移版本库步骤在本地测试报错 官方教程地址:http://www.asp.net/mvc/overview/older-versions/getting-s ...
- SpringBoot服务监控
SpringBoot服务监控分为客户端和服务端,即服务端是监控方,客户端为被监控方. 例如需要对线上的SpringBoot服务project-A进行监控,则project-A 为客户端.而监控的服务p ...