graph-basic
2024-08-27 07:24:11
打算使用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;
}
最新文章
- wpf 触发器理解
- mybatis输出SQL
- dev中控件属性设置
- javascript的基本语法、数据结构
- jquery源码学习之extend
- shell中实现自动登录(bash环境脚本中)
- C++ 必知必会:条款16 指向成员函数的指针并非指针
- Asp.Net-创建网站的快捷方式到桌面,开始菜单,收藏夹
- JDBC 学习笔记(二)—— 大数据+存储过程+批处理+事务管理
- 无法在People Picker中选择用户
- Atom 编辑器系列视频课程
- bzoj2144 【国家集训队2011】跳跳棋
- Angular 路由配置
- Python内置函数(67)——zip
- ibatis 多种传参方式
- ado.net调用带参数的存储过程
- 求一个数的n次幂
- 在mac终端先打开mysql
- http和WebSocket
- 异常空格,ASCII (194,160)问题
热门文章
- Maven--setting详解
- Spring Boot Admin 监控中心
- .net 中 Json 与List 相互转
- Linux netstat命令详解和使用例子(显示各种网络相关信息)
- Fleet(集群管理器)
- 微信小程序干货
- SpringBoot 封装返回类以及session 添加获取
- qrcode length overflow (1632>;1056)--qrcode.js使用过程中二维码长度溢出解决办法
- 【extjs6学习笔记】1.3 初始:根据模板创建项目
- 快速搭建高可用 LNMP Web应用基础架构