打算使用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;
}

  

最新文章

  1. wpf 触发器理解
  2. mybatis输出SQL
  3. dev中控件属性设置
  4. javascript的基本语法、数据结构
  5. jquery源码学习之extend
  6. shell中实现自动登录(bash环境脚本中)
  7. C++ 必知必会:条款16 指向成员函数的指针并非指针
  8. Asp.Net-创建网站的快捷方式到桌面,开始菜单,收藏夹
  9. JDBC 学习笔记(二)—— 大数据+存储过程+批处理+事务管理
  10. 无法在People Picker中选择用户
  11. Atom 编辑器系列视频课程
  12. bzoj2144 【国家集训队2011】跳跳棋
  13. Angular 路由配置
  14. Python内置函数(67)——zip
  15. ibatis 多种传参方式
  16. ado.net调用带参数的存储过程
  17. 求一个数的n次幂
  18. 在mac终端先打开mysql
  19. http和WebSocket
  20. 异常空格,ASCII (194,160)问题

热门文章

  1. Maven--setting详解
  2. Spring Boot Admin 监控中心
  3. .net 中 Json 与List 相互转
  4. Linux netstat命令详解和使用例子(显示各种网络相关信息)
  5. Fleet(集群管理器)
  6. 微信小程序干货
  7. SpringBoot 封装返回类以及session 添加获取
  8. qrcode length overflow (1632&gt;1056)--qrcode.js使用过程中二维码长度溢出解决办法
  9. 【extjs6学习笔记】1.3 初始:根据模板创建项目
  10. 快速搭建高可用 LNMP Web应用基础架构