打算使用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的更多相关文章

  1. 论文解读(DAEGC)《Improved Deep Embedded Clustering with Local Structure Preservation》

    Paper Information Title:<Attributed Graph Clustering: A Deep Attentional Embedding Approach>Au ...

  2. Prometheus Node_exporter 之 Basic CPU / Mem Graph

    1. CPU Basic cpu 的基本信息 /proc/stat type: GraphUnit: shortBusy System: cpu 处于核心态的占比 metrics: sum by (i ...

  3. some basic graph theoretical measures

    · mean characteristic path length calculated as the average length of the shortest path between two ...

  4. 转债---Pregel: A System for Large-Scale Graph Processing(译)

    转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/   作者:Grzegorz Malewicz, Matthew ...

  5. Pregel: A System for Large-Scale Graph Processing(译)

    [说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...

  6. Graph Databases—The NOSQL Phenomenon阅读笔记

    本章内容着重对了NOSQL和RDBMS(关系型数据库管理系统)的不同,以及其各自背后设计时考虑的因素.然后接下来,着重讲述了NOSQL的4种分类方法.下面我们将对重要知识点进行汇总. 1.We def ...

  7. [TensorFlow] Basic Usage

    Install TensorFlow on mac Install pip # Mac OS X $ sudo easy_install pip $ sudo easy_install --upgra ...

  8. 掀起Azure AD的盖头来——深入理解Microsoft Graph应用程序和服务权限声明

    作者:陈希章 发表于 2017年7月12日 引子 这是一篇计划外的文章.我们都知道要进行Microsoft Graph的开发的话,需要进行应用程序注册.这个在此前我已经有专门的文章写过了.但这里存在一 ...

  9. python生成组织架构图(网络拓扑图、graph.editor拓扑图编辑器)

    Graph.Editor是一款基于HTML5技术的拓补图编辑器,采用jquery插件的形式,是Qunee图形组件的扩展项目,旨在提供可供扩展的拓扑图编辑工具, 拓扑图展示.编辑.导出.保存等功能,此外 ...

  10. 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 ...

随机推荐

  1. struts2 具体学习资料

    [struts2]<package>的配置:https://www.cnblogs.com/ningvsban/p/3734562.html struts2  具体学习资料 :http:/ ...

  2. kali linux安装搜狗输入法

    1. 更新软件源 vi /etc/apt/sources.list 2. 安装fcitx apt-get install fcitx 3. 下载deb包到指定目录, dpkg -i sogou**** ...

  3. Ubuntu 18.04 Python3.6.6导入wx模块报Gtk-Message : 17:06:05.797 :Failed to load module "canberra-gtk-module"

    解决办法: root@sishen:~# apt-get install libcanberra-gtk-module

  4. scrollHelper

    (function ($) { var mouseScroll = function (e) { try { var origEvent = e.originalEvent; origEvent.pr ...

  5. Java transient关键字使用

    1. transient的作用及其使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的 ...

  6. IOSruntime : 运行时机制

    首先必须明白的: 1.是什么 1> runtime是一套比较底层的纯C语言API, 属于1个C语言库, 包含了很多底层的C语言API 2> 平时编写的OC代码, 在程序运行过程中, 其实最 ...

  7. SharePoint 2010 缺少站点保存为模板选项

    如果您尝试在SharePoint Server 2010中保存网站,并且没有看到"将网站另存为模板"选项,则可能是因为该网站已启用发布功能.如果使用站点发布功能,则有几个选项可将网 ...

  8. 家校通Code

    9 http://dlwt.csdn.net/fd.php?i=621573845033702&s=44d46a459acce7fef39aa4dcff51bfba

  9. JavaScript_10_错误

    Try...catch... throw <!DOCTYPE html> <html> <head> <title></title> < ...

  10. 【Python图像特征的音乐序列生成】关于数据库到底在哪里下载

    毕竟原网站一个是14年前的一个是16年前的…… 1,http://ifdo.ca/~seymour/nottingham/nottingham.html 这个网站可以下载zip包. 2,https:/ ...