1、图实验一, 邻接矩阵的实现1. 实验目的(1 ) 掌握图的逻辑结构(2 ) 掌握图的邻接矩阵的存储结构(3 ) 验证图的邻接矩阵存储及其遍历操作的实现2. 实验内容(1 ) 建立无向图的邻接矩阵存储(2 ) 进行深度优先遍历(3 ) 进行广度优先遍历3设计与编码MGraph.h#ifndef MGraph_H#define MGraph_Hconst int MaxSize = 10;templateclass MGraphpublic:MGraph(DataType a, int n, int e);MGraph()void DFSTraverse(int v);void BFSTraver
2、se(int v);private:DataType vertexMaxSize;int arcMaxSizeMaxSize;int vertexNum, arcNum;#endifMGraph.cpp#includeusing namespace std;#include “MGraph.h“extern int visitedMaxSize;templateMGraph:MGraph(DataType a, int n, int e)int i, j, k;vertexNum = n, arcNum = e;for(i = 0; i i j;arcij = 1;arcji = 1;temp
3、latevoid MGraph:DFSTraverse(int v)cout void MGraph:BFSTraverse(int v)int QMaxSize;int front = -1, rear = -1;cout using namespace std;#include “MGraph.h“extern int visitedMaxSize;templateMGraph:MGraph(DataType a, int n, int e)int i, j, k;vertexNum = n, arcNum = e;for(i = 0; i i j;arcij = 1;arcji = 1;
4、templatevoid MGraph:DFSTraverse(int v)cout void MGraph:BFSTraverse(int v)int QMaxSize;int front = -1, rear = -1;cout struct VertexNodeDataType vertex;ArcNode * firstedge;templateclass ALGraphpublic:ALGraph(DataType a, int n, int e);ALGraph();void DFSTraverse(int v);void BFSTraverse(int v);private:Ve
5、rtexNode adjlistMaxSize;int vertexNum, arcNum;#endifALGraph.cpp#includeusing namespace std;#include“ALGraph.h“extern int visitedMaxSize;templateALGraph:ALGraph(DataType a, int n, int e)ArcNode * s;int i, j, k;vertexNum = n; arcNum = e;for(i = 0; i i j;s = new ArcNode; s-adjvex = j;s-next = adjlisti.
6、firstedge;adjlisti.firstedge = s;templateALGraph:ALGraph()ArcNode * p = NULL;for(int i = 0; i next;delete p;p = adjlisti.firstedge;templatevoid ALGraph:DFSTraverse(int v)ArcNode * p = NULL; int j;cout adjvex;if(visitedj = 0) DFSTraverse(j);p = p-next;templatevoid ALGraph:BFSTraverse(int v)int QMaxSi
7、ze;int front = -1, rear = -1;ArcNode * p = NULL;cout adjvex;if(visitedj = 0)cout next;ALGraph_main.cpp#includeusing namespace std;#include“ALGraph.cpp“int visitedMaxSize = 0;int main()char ch = A,B,C,D,E;int i;ALGraph ALG(ch, 5, 6);for(i = 0; i MaxSize; i+)visitedi = 0;cout “Depth-first traverse sequence is: “;ALG.DFSTraverse(0);cout endl;for(i = 0; i MaxSize; i+)visitedi = 0;cout “Breadth-first traverse sequence is: “;ALG.BFSTraverse(0);cout endl;return 0;4. 运行与调试5. 总结与心得通过该实验,掌握了图的邻接表存储结构