打算使用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. java课后思考问题(八)

    1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. (1)import javax.swing.*; class AboutEx ...

  2. CSS3 - CheakBox 开关效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. JS正则改变字符之间文字

    var reg = /([[^[]*])/g; html = html.replace(reg, "<span class=\"bold\">$1</s ...

  4. 如何更改Android的默认虚拟机地址

    第一种,虚拟机已经建立 1)找到虚拟机.ini这个文件,例如: zhai.ini 寻找方法:你可以在运行SDK Manager时看到最上面显示的虚拟机存放地址 例如显示: List of existi ...

  5. uvm_mem_mam——寄存器模型(十三)

    有了存储器模型,再来看看存储器的管理 //------------------------------------------------------------------------------ ...

  6. Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境

    首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...

  7. 第一篇Active Directory疑难解答概述(2)

    从故障诊断的角度来看,无论用户对象存在于哪个Active Directory域中,Exchange都需要访问此数据.这意味着所有包含启用Exchange的对象的域必须对其运行Setup / Prepa ...

  8. 一步一步教你玩转.NET Framework的配置文件app.config

    转自https://www.cnblogs.com/tonnie/archive/2010/12/17/appconfig.html 在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的h ...

  9. 使用JS的画布制作一个瞄准镜

    <canvas width="600" height="500" id="myCanvas"></canvas> & ...

  10. Netbackup驱动器常用命令vmoprcmd

    1.vmoprcmd vmoprcmd – 对驱动器执行操作员功能 大纲 vmoprcmd -devmon [pr | ds | hs] [-h device_host] default_operat ...