C++数据结构之图——邻接矩阵实现

C++数据结构之图——邻接矩阵实现

一、图的邻接矩阵实现

1.实现了以顶点数组、邻接矩阵为存储结构的图;

2.实现了图的创建(包含有向/无向图、有向/无向网)、顶点/边的增删操作、深度/广度优先遍历的算法;

3.采用顶点对象列表、边(弧)对象列表的方式,对图的创建进行初始化;引用 "ObjArrayList.h"头文件,头文件可参看之前博文“数据结构之顺序列表(支持对象元素)”代码;

4.采用将顶点数组空位的下标索引入队列的方式,解决顶点在数组(静态)中因增删操作造成的不连续存储的问题;引用 “LinkQueue.h”头文件,头文件可参看之前博文“数据结构之队列(循环队列、链队列的类模板实现)”代码;

5.深度优先遍历采用递归算法;广度优先遍历采用队列方式实现;

6.测试代码中以有向网的所有带权边作为边的初始化数据,选择图类型(DG, UDG, DN, UDN)可创建成不同类型的图。二、测试代码中的图结构图

C++数据结构之图——邻接矩阵实现

深度优先遍历序列(从 v1 顶点开始):

1.无向图/网:v1-v2-v3-v5-v4-v6-v7

2.有向图/网:v1-v2-v5-v3-v4-v6-v7

广度优先遍历序列(从 v2 顶点开始):

1.无向图/网:v2-v1-v3-v5-v4-v6-v7

2.有向图/网:v2-v5 后序无法遍历

注:有向图的遍历 是遵循出度方向遍历的,若无出度方向,则遍历终止。

C++数据结构之图——邻接矩阵实现

C++数据结构之图——邻接矩阵实现

三、代码


  1. //文件名:"GraphAdjMat.h"
  2. #pragma once
  3. #ifndef GRAPHADJMAT_H_
  4. #define GRAPHADJMAT_H_
  5. #include
  6. #include
  7. #include "ObjArrayList.h"
  8. #include "LinkQueue.h"
  9. using namespace std;
  10. /*
  11. .图(邻接矩阵实现) Graph Adjacency Matrix
  12. .相关术语:
  13. .顶点 Vertex ; 边 Arc ;权 Weight ;
  14. .有向图 Digraph ;无向图 Undigraph ;
  15. .有向网 Directed Network ;无向网 Undirected Network ;
  16. */
  17. class GraphAdjMat
  18. {
  19. /*
  20. .边(弧) 单元,注:邻接矩阵单元
  21. */
  22. struct ArcCell
  23. {
  24. int adj;//邻接顶点关系。图:0|不相邻 1|相邻 ;网:无穷(INT_MAX)|不相邻 权值(W)|相邻
  25. char * info;//边(弧)信息
  26. };
  27. public:
  28. /*
  29. .图 种类
  30. */
  31. enum GraphType
  32. {
  33. DG,//有向图,默认 0
  34. UDG,//无向图,默认 1
  35. DN,//有向网,默认 2
  36. UDN//无向网,默认 3
  37. };
  38. /*
  39. .边(弧)数据,注:供外部初始化边数据使用
  40. */
  41. struct ArcData
  42. {
  43. string Tail;//弧尾
  44. string Head;//弧头
  45. int Weight;//权重
  46. };
  47. private:
  48. const int _INFINITY = INT_MAX;//无穷大 注:包含于头文件
  49. static const int _MAX_VERTEX_NUM = 10;//支持最大顶点数
  50. //静态存储结构
  51. string vexs[_MAX_VERTEX_NUM];//顶点表
  52. ArcCell arcs[_MAX_VERTEX_NUM][_MAX_VERTEX_NUM];//边(弧)矩阵
  53. int vexNum;//顶点数
  54. int arcNum;//边数
  55. int type;//图种类
  56. int nonAdjInt;//不相邻 int 值:0|无权 无穷|有权
  57. LinkQueue *vexs_null_index_queue = new LinkQueue();//顶点数组中空顶点位置索引队列 (需要销毁)
  58. bool vexs_visited[_MAX_VERTEX_NUM];//顶点访问标记数组:0|未访问 1|已访问
  59. void _CreateDG(ObjArrayList * arcsList);//创建有向图
  60. void _CreateUDG(ObjArrayList * arcsList);//创建无向图
  61. void _CreateDN(ObjArrayList * arcsList);//创建有向网
  62. void _CreateUDN(ObjArrayList * arcsList);//创建无向网
  63. int _Locate(string vertex);//定位顶点元素位置
  64. void _DFS_R(int index);//深度优先遍历 递归
  65. public:
  66. GraphAdjMat(int type);//构造函数:初始化图类型
  67. ~GraphAdjMat();//析构函数:销毁图存储空间
  68. void Init(ObjArrayList * vexs, ObjArrayList * arcsList);//初始化顶点、边数据为 图|网
  69. void Display();//显示 图|网
  70. void InsertVertex(string *vertex);//插入一个新顶点
  71. void DeleteVertex(string *vertex);//删除一个顶点
  72. void InsertArc(ArcData *arc);//插入一条新边(弧)
  73. void DeleteArc(ArcData *arc);//删除一条边(弧)
  74. void Display_DFS(string *vertex);//从指定顶点开始,深度优先遍历
  75. void Display_BFS(string *vertex);//从指定顶点开始,广度优先遍历
  76. };
  77. GraphAdjMat::GraphAdjMat(int type)
  78. {
  79. /*
  80. .构造函数:初始化图类型
  81. */
  82. this->type = type;
  83. this->vexNum = 0;
  84. this->arcNum = 0;
  85. if (this->type == DG || this->type == UDG)
  86. {
  87. //图的不相邻 int 值 0
  88. this->nonAdjInt = 0;
  89. }
  90. else
  91. {
  92. //网的不相邻 int 值 无穷大
  93. this->nonAdjInt = this->_INFINITY;
  94. }
  95. }
  96. GraphAdjMat::~GraphAdjMat()
  97. {
  98. /*
  99. .析构函数:销毁图
  100. */
  101. //1.释放顶点空位置索引队列
  102. int * e;
  103. while (vexs_null_index_queue->GetHead() != NULL)
  104. {
  105. e = vexs_null_index_queue->DeQueue();
  106. delete e;
  107. }
  108. delete vexs_null_index_queue;
  109. }
  110. void GraphAdjMat::Init(ObjArrayList * vexs, ObjArrayList * arcsList)
  111. {
  112. /*
  113. .初始化顶点、边数据,并构建 图|网
  114. .入参:
  115. .vexs: 顶点 列表
  116. .arcsList: 边数据 列表
  117. */
  118. //1.初始化顶点数据
  119. if (vexs->Length() > this->_MAX_VERTEX_NUM)
  120. {
  121. return;
  122. }
  123. for (int i = 0; i < vexs->Length(); i++)
  124. {
  125. this->vexs[i] = *vexs->Get(i);
  126. }
  127. this->vexNum = vexs->Length();
  128. //1.1.初始化顶点数组空顶点位置索引队列
  129. for (int i = vexs->Length(); i < _MAX_VERTEX_NUM; i++)
  130. {
  131. vexs_null_index_queue->EnQueue(new int(i));
  132. }
  133. //2.根据初始化的图类型,创建指定的图
  134. switch (this->type)
  135. {
  136. case DG:
  137. _CreateDG(arcsList); break;
  138. case UDG:
  139. _CreateUDG(arcsList); break;
  140. case DN:
  141. _CreateDN(arcsList); break;
  142. case UDN:
  143. _CreateUDN(arcsList); break;
  144. default:
  145. break;
  146. }
  147. }
  148. void GraphAdjMat::_CreateDG(ObjArrayList * arcsList)
  149. {
  150. /*
  151. .创建有向图
  152. .顶点相邻关系:0|不相邻 1|相邻
  153. .邻接矩阵为 非对称矩阵
  154. */
  155. //初始化临时 边对象
  156. ArcData * arcData = NULL;
  157. //初始化 矩阵二维坐标
  158. int m = 0, n = 0;
  159. //初始化边的邻接矩阵
  160. for (int i = 0; i < _MAX_VERTEX_NUM; i++)
  161. {
  162. for (int j = 0; j < _MAX_VERTEX_NUM; j++)
  163. {
  164. this->arcs[i][j].adj = 0;
  165. this->arcs[i][j].info = NULL;
  166. }
  167. }
  168. //遍历边数据列表
  169. for (int i = 0; i < arcsList->Length(); i++)
  170. {
  171. //按序获取边(弧)
  172. arcData = arcsList->Get(i);
  173. //定位(或设置)边的两端顶点位置
  174. m = _Locate(arcData->Tail);
  175. n = _Locate(arcData->Head);
  176. //设置顶点相邻 为 1 (无向)
  177. if (this->arcs[m][n].adj == 1)
  178. {
  179. //去除重复边
  180. continue;
  181. }
  182. this->arcs[m][n].adj = 1;
  183. //边 计数
  184. this->arcNum++;
  185. }
  186. }
  187. void GraphAdjMat::_CreateUDG(ObjArrayList * arcsList)
  188. {
  189. /*
  190. .创建无向图
  191. .顶点相邻关系:0|不相邻 1|相邻
  192. .邻接矩阵为 对称矩阵
  193. */
  194. //初始化临时 边对象
  195. ArcData * arcData = NULL;
  196. //初始化 矩阵二维坐标
  197. int m = 0, n = 0;
  198. //初始化边的邻接矩阵
  199. for (int i = 0; i < _MAX_VERTEX_NUM; i++)
  200. {
  201. for (int j = 0; j < _MAX_VERTEX_NUM; j++)
  202. {
  203. this->arcs[i][j].adj = 0;
  204. this->arcs[i][j].info = NULL;
  205. }
  206. }
  207. //遍历边数据列表
  208. for (int i = 0; i < arcsList->Length(); i++)
  209. {
  210. //按序获取边(弧)
  211. arcData = arcsList->Get(i);
  212. //定位(或设置)边的两端顶点位置
  213. m = _Locate(arcData->Tail);
  214. n = _Locate(arcData->Head);
  215. //设置顶点相邻 为 1 (有向)
  216. if (this->arcs[m][n].adj == 1 || this->arcs[n][m].adj == 1)
  217. {
  218. //去除重复边
  219. continue;
  220. }
  221. this->arcs[m][n].adj = 1;
  222. this->arcs[n][m].adj = 1;
  223. //边 计数
  224. this->arcNum++;
  225. }
  226. }
  227. void GraphAdjMat::_CreateDN(ObjArrayList * arcsList)
  228. {
  229. /*
  230. .创建有向网
  231. .顶点相邻关系:infinity|不相邻 w|相邻
  232. .邻接矩阵为 非对称矩阵
  233. */
  234. //初始化临时 边对象
  235. ArcData * arcData = NULL;
  236. //初始化 矩阵二维坐标
  237. int m = 0, n = 0;
  238. //初始化边的邻接矩阵
  239. for (int i = 0; i < _MAX_VERTEX_NUM; i++)
  240. {
  241. for (int j = 0; j < _MAX_VERTEX_NUM; j++)
  242. {
  243. this->arcs[i][j].adj = this->_INFINITY;//无穷大
  244. this->arcs[i][j].info = NULL;
  245. }
  246. }
  247. //遍历边数据列表
  248. for (int i = 0; i < arcsList->Length(); i++)
  249. {
  250. //按序获取边(弧)
  251. arcData = arcsList->Get(i);
  252. //定位(或设置)边的两端顶点位置
  253. m = _Locate(arcData->Tail);
  254. n = _Locate(arcData->Head);
  255. //设置顶点相邻 为 weight 权重
  256. if (this->arcs[m][n].adj != this->_INFINITY)
  257. {
  258. //去除重复边
  259. continue;
  260. }
  261. this->arcs[m][n].adj = arcData->Weight;
  262. //边 计数
  263. this->arcNum++;
  264. }
  265. }
  266. void GraphAdjMat::_CreateUDN(ObjArrayList * arcsList)
  267. {
  268. /*
  269. .创建无向网
  270. .顶点相邻关系:infinity|不相邻 w|相邻
  271. .邻接矩阵为 对称矩阵
  272. */
  273. //初始化临时 边对象
  274. ArcData * arcData = NULL;
  275. //初始化 矩阵二维坐标
  276. int m = 0, n = 0;
  277. //初始化边的邻接矩阵
  278. for (int i = 0; i < _MAX_VERTEX_NUM; i++)
  279. {
  280. for (int j = 0; j < _MAX_VERTEX_NUM; j++)
  281. {
  282. this->arcs[i][j].adj = this->_INFINITY;//无穷大
  283. this->arcs[i][j].info = NULL;
  284. }
  285. }
  286. //遍历边数据列表
  287. for (int i = 0; i < arcsList->Length(); i++)
  288. {
  289. //按序获取边(弧)
  290. arcData = arcsList->Get(i);
  291. //定位(或设置)边的两端顶点位置
  292. m = _Locate(arcData->Tail);
  293. n = _Locate(arcData->Head);
  294. //设置顶点相邻 为 weight 权重
  295. if (this->arcs[m][n].adj != this->_INFINITY || this->arcs[n][m].adj != this->_INFINITY)
  296. {
  297. //去除重复边
  298. continue;
  299. }
  300. if (arcData->Weight == this->_INFINITY)
  301. {
  302. //去除权重为 无穷 的边
  303. continue;
  304. }
  305. this->arcs[m][n].adj = arcData->Weight;
  306. this->arcs[n][m].adj = arcData->Weight;
  307. //边 计数
  308. this->arcNum++;
  309. }
  310. }
  311. int GraphAdjMat::_Locate(string vertex)
  312. {
  313. /*
  314. .定位顶点元素位置
  315. .后期可改成【字典树】,顶点数超过100个后定位顶点位置可更快
  316. */
  317. //遍历定位顶点位置
  318. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  319. {
  320. if (vertex == this->vexs[i])
  321. {
  322. return i;
  323. }
  324. }
  325. cout << endl << "顶点[" << vertex << "]不存在。" << endl;
  326. return -1;
  327. }
  328. void GraphAdjMat::Display()
  329. {
  330. /*
  331. .显示 图|网 (输出顶点数组、邻接矩阵)
  332. */
  333. //显示顶点数组
  334. //注:当删除某个中间序号顶点后,顶点数组就不是连续的
  335. cout << endl << "顶点数组:" << endl;
  336. for (int i = 0, num = 0; i < this->_MAX_VERTEX_NUM && num < this->vexNum; i++)
  337. {
  338. if (this->vexs[i] != "")
  339. {
  340. cout << " (" << i << ")" << this->vexs[i];
  341. num++;
  342. }
  343. }
  344. //显示边(邻接矩阵)
  345. cout << endl << "边(邻接矩阵):" << endl;
  346. cout << " ";
  347. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  348. {
  349. cout << "[" << i << "]";
  350. }
  351. cout << endl;
  352. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  353. {
  354. cout << "[" << i << "] ";
  355. for (int j = 0; j < this->_MAX_VERTEX_NUM; j++)
  356. {
  357. if (this->arcs[i][j].adj == this->_INFINITY)
  358. cout << " + ";
  359. else
  360. cout << " " << this->arcs[i][j].adj << " ";
  361. }
  362. cout << endl;
  363. }
  364. }
  365. void GraphAdjMat::InsertVertex(string *vertex)
  366. {
  367. /*
  368. .插入一个新顶点
  369. */
  370. //1.判断顶点是否已存在
  371. if (_Locate(*vertex) > -1)
  372. {
  373. cout << endl << "该顶点已存在。" << endl;
  374. return;
  375. }
  376. //2.判断顶点数是否达到上限
  377. if (this->vexNum >= this->_MAX_VERTEX_NUM)
  378. {
  379. cout << endl << "顶点数已达上限。" << endl;
  380. return;
  381. }
  382. //3.插入新顶点,并自增顶点总数
  383. int * index = vexs_null_index_queue->DeQueue();//从空位置索引队列中取
  384. this->vexs[*index] = *vertex;
  385. this->vexNum++;
  386. //4.新增的顶点在邻接矩阵中不需要作任何操作(已初始化)
  387. }
  388. void GraphAdjMat::DeleteVertex(string *vertex)
  389. {
  390. /*
  391. .删除一个顶点
  392. */
  393. //1.判断顶点是否已存在
  394. int index = _Locate(*vertex);
  395. if (index == -1)
  396. {
  397. cout << endl << "该顶点不存在。" << endl;
  398. return;
  399. }
  400. //2.删除该顶点,并自减顶点总数
  401. this->vexs[index] = "";
  402. this->vexNum--;
  403. //3.清除邻接矩阵 index 行列的数据,即将此行列 恢复初始化状态
  404. if (this->type == DG || this->type == UDG)
  405. {
  406. //图
  407. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  408. {
  409. this->arcs[i][index].adj = 0;
  410. this->arcs[index][i].adj = 0;
  411. }
  412. }
  413. else
  414. {
  415. //网
  416. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  417. {
  418. this->arcs[i][index].adj = this->_INFINITY;
  419. this->arcs[index][i].adj = this->_INFINITY;
  420. }
  421. }
  422. }
  423. void GraphAdjMat::InsertArc(ArcData *arc)
  424. {
  425. /*
  426. .插入一条新边(弧)
  427. .若已存在,将对其更新
  428. */
  429. //1.定位顶点位置
  430. int i = _Locate(arc->Tail);
  431. int j = _Locate(arc->Head);
  432. //2.判断顶点是否存在
  433. if (i == -1 || j == -1)
  434. {
  435. cout << endl << "该边顶点不存在。" << endl;
  436. return;
  437. }
  438. //3.插入/更新一条边
  439. if (this->type == DG)
  440. {
  441. //有向无权图
  442. this->arcs[i][j].adj = 1;
  443. }
  444. else if (this->type == UDG)
  445. {
  446. //无向无权图
  447. this->arcs[i][j].adj = 1;
  448. this->arcs[j][i].adj = 1;
  449. }
  450. else if (this->type == DN)
  451. {
  452. //有向有权网
  453. this->arcs[i][j].adj = arc->Weight;
  454. }
  455. else
  456. {
  457. //无向有权网
  458. this->arcs[i][j].adj = arc->Weight;
  459. this->arcs[j][i].adj = arc->Weight;
  460. }
  461. }
  462. void GraphAdjMat::DeleteArc(ArcData *arc)
  463. {
  464. /*
  465. .删除一条边(弧)
  466. */
  467. //1.定位顶点位置
  468. int i = _Locate(arc->Tail);
  469. int j = _Locate(arc->Head);
  470. //2.判断顶点是否存在
  471. if (i == -1 || j == -1)
  472. {
  473. cout << endl << "该边顶点不存在。" << endl;
  474. return;
  475. }
  476. //3.删除一条边,即 恢复初始化状态
  477. if (this->type == DG)
  478. {
  479. //有向无权图
  480. this->arcs[i][j].adj = 0;
  481. }
  482. else if (this->type == UDG)
  483. {
  484. //无向无权图
  485. this->arcs[i][j].adj = 0;
  486. this->arcs[j][i].adj = 0;
  487. }
  488. else if (this->type == DN)
  489. {
  490. //有向有权网
  491. this->arcs[i][j].adj = this->_INFINITY;
  492. }
  493. else
  494. {
  495. //无向有权网
  496. this->arcs[i][j].adj = this->_INFINITY;
  497. this->arcs[j][i].adj = this->_INFINITY;
  498. }
  499. }
  500. void GraphAdjMat::Display_DFS(string *vertex)
  501. {
  502. /*
  503. .深度优先遍历显示,从指定顶点开始
  504. */
  505. //1.判断顶点是否存在
  506. int index = _Locate(*vertex);
  507. if (index == -1)
  508. return;
  509. //2.初始化顶点访问数组
  510. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  511. {
  512. this->vexs_visited[i] = 0;
  513. }
  514. //3.深度优先遍历 递归
  515. cout << "深度优先遍历:(从顶点" << *vertex << "开始)" << endl;
  516. _DFS_R(index);
  517. }
  518. void GraphAdjMat::_DFS_R(int index)
  519. {
  520. /*
  521. .深度优先遍历 递归
  522. .有向/无向算法是一样的
  523. .有向图|网,以当前顶点的出度方向遍历
  524. .无向图|网,以当前顶点的相邻结点方向遍历(可理解为“出度”,但不是出度)
  525. */
  526. //1.访问顶点,并标记已访问
  527. cout << this->vexs[index] << " ";
  528. this->vexs_visited[index] = 1;
  529. //2.访问其相邻顶点
  530. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  531. {
  532. //当边值 不是 不相邻int值(0|无权 无穷|有权),且未被访问过时,可访问
  533. if (this->arcs[index][i].adj != this->nonAdjInt && this->vexs_visited[i] != 1)
  534. {
  535. _DFS_R(i);
  536. }
  537. }
  538. }
  539. void GraphAdjMat::Display_BFS(string *vertex)
  540. {
  541. /*
  542. .广度优先遍历显示,从指定顶点开始
  543. .类似于树的层次遍历算法
  544. */
  545. //1.判断顶点是否存在
  546. int index = _Locate(*vertex);
  547. if (index == -1)
  548. return;
  549. //2.初始化顶点访问数组
  550. for (int i = 0; i < this->_MAX_VERTEX_NUM; i++)
  551. {
  552. this->vexs_visited[i] = 0;
  553. }
  554. //3.广度优先遍历
  555. cout << "广度优先遍历:(从顶点" << *vertex << "开始)" << endl;
  556. //3.1.初始化队列
  557. LinkQueue * vexQ = new LinkQueue();
  558. //3.2.访问开始顶点,并标记访问、入队
  559. cout << this->vexs[index] << " ";
  560. this->vexs_visited[index] = 1;
  561. vexQ->EnQueue(new int(index));
  562. //3.3.出队,并遍历邻接顶点(下一层次),访问后入队
  563. while (vexQ->GetHead() != NULL)
  564. {
  565. index = * vexQ->DeQueue();
  566. for (int j = 0; j < _MAX_VERTEX_NUM; j++)
  567. {
  568. //未访问过的邻接顶点
  569. if (this->arcs[index][j].adj != this->nonAdjInt && this->vexs_visited[j] != 1)
  570. {
  571. //访问顶点,并标记访问、入队
  572. cout << this->vexs[j] << " ";
  573. this->vexs_visited[j] = 1;
  574. vexQ->EnQueue(new int(j));
  575. }
  576. }
  577. }
  578. //4.释放队列
  579. int * e;
  580. while (vexQ->GetHead() != NULL)
  581. {
  582. e = vexQ->DeQueue();
  583. delete e;
  584. }
  585. delete vexQ;
  586. }
  587. #endif // !GRAPHADJMAT_H_
  588. //文件名:"GraphAdjMat_Test.cpp"
  589. #include "stdafx.h"
  590. #include
  591. #include "GraphAdjMat.h"
  592. #include "ObjArrayList.h"
  593. using namespace std;
  594. int main()
  595. {
  596. //初始化顶点数据
  597. string * v1 = new string("v1");
  598. string * v2 = new string("v2");
  599. string * v3 = new string("v3");
  600. string * v4 = new string("v4");
  601. string * v5 = new string("v5");
  602. string * v6 = new string("v6");
  603. string * v7 = new string("v7");
  604. ObjArrayList * vexs = new ObjArrayList();
  605. vexs->Add(v1);
  606. vexs->Add(v2);
  607. vexs->Add(v3);
  608. vexs->Add(v4);
  609. vexs->Add(v5);
  610. vexs->Add(v6);
  611. vexs->Add(v7);
  612. //初始化边(弧)数据
  613. GraphAdjMat::ArcData * arc1 = new GraphAdjMat::ArcData{ "v1", "v2", 2 };
  614. GraphAdjMat::ArcData * arc2 = new GraphAdjMat::ArcData{ "v1", "v3", 3 };
  615. GraphAdjMat::ArcData * arc3 = new GraphAdjMat::ArcData{ "v1", "v4", 4 };
  616. GraphAdjMat::ArcData * arc4 = new GraphAdjMat::ArcData{ "v3", "v1", 5 };
  617. GraphAdjMat::ArcData * arc5 = new GraphAdjMat::ArcData{ "v3", "v2", 6 };
  618. GraphAdjMat::ArcData * arc6 = new GraphAdjMat::ArcData{ "v3", "v5", 7 };
  619. GraphAdjMat::ArcData * arc7 = new GraphAdjMat::ArcData{ "v2", "v5", 8 };
  620. GraphAdjMat::ArcData * arc8 = new GraphAdjMat::ArcData{ "v4", "v6", 9 };
  621. GraphAdjMat::ArcData * arc9 = new GraphAdjMat::ArcData{ "v4", "v7", 9 };
  622. GraphAdjMat::ArcData * arc10 = new GraphAdjMat::ArcData{ "v6", "v7", 9 };
  623. ObjArrayList<:arcdata> * arcsList = new ObjArrayList<:arcdata>();
  624. arcsList->Add(arc1);
  625. arcsList->Add(arc2);
  626. arcsList->Add(arc3);
  627. arcsList->Add(arc4);
  628. arcsList->Add(arc5);
  629. arcsList->Add(arc6);
  630. arcsList->Add(arc7);
  631. arcsList->Add(arc8);
  632. arcsList->Add(arc9);
  633. arcsList->Add(arc10);
  634. //测试1:无向图
  635. cout << endl << "无向图初始化:" << endl;
  636. GraphAdjMat * udg = new GraphAdjMat(GraphAdjMat::UDG);
  637. udg->Init(vexs, arcsList);
  638. udg->Display();
  639. //1.1.深度优先遍历
  640. cout << endl << "无向图深度优先遍历序列:" << endl;
  641. udg->Display_DFS(v1);
  642. //1.2.广度优先遍历
  643. cout << endl << "无向图广度优先遍历序列:" << endl;
  644. udg->Display_BFS(v2);
  645. //1.3.插入新顶点、新边
  646. cout << endl << "无向图插入新顶点、新边:" << endl;
  647. udg->InsertVertex(new string("v8"));
  648. udg->InsertArc(new GraphAdjMat::ArcData{ "v8", "v1", 8 });
  649. udg->Display();
  650. //1.4.删除顶点、边
  651. cout << endl << "无向图删除顶点v1、边arc9:" << endl;
  652. udg->DeleteVertex(v1);
  653. udg->DeleteArc(arc9);
  654. udg->Display();
  655. //测试2:有向图
  656. cout << endl << "有向图:" << endl;
  657. GraphAdjMat * dg = new GraphAdjMat(GraphAdjMat::DG);
  658. dg->Init(vexs, arcsList);
  659. dg->Display();
  660. //2.1.深度优先遍历
  661. cout << endl << "有向图深度优先遍历序列:" << endl;
  662. dg->Display_DFS(v1);
  663. //2.2.广度优先遍历
  664. cout << endl << "有向图广度优先遍历序列:" << endl;
  665. dg->Display_BFS(v2);
  666. //测试:无向网
  667. cout << endl << "无向网:" << endl;
  668. GraphAdjMat * udn = new GraphAdjMat(GraphAdjMat::UDN);
  669. udn->Init(vexs, arcsList);
  670. udn->Display();
  671. //测试:有向网
  672. cout << endl << "有向网:" << endl;
  673. GraphAdjMat * dn = new GraphAdjMat(GraphAdjMat::DN);
  674. dn->Init(vexs, arcsList);
  675. dn->Display();
  676. return 0;
  677. }


分享到:


相關文章: