1 / 13
文档名称:

数据结构实验报告图实验.docx

格式:docx   大小:72KB   页数:13页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

数据结构实验报告图实验.docx

上传人:2072510724 2021/12/19 文件大小:72 KB

下载得到文件列表

数据结构实验报告图实验.docx

文档介绍

文档介绍:谢谢你的观赏
谢谢你的观赏
邻接矩阵的实现
.实验目的
(1)掌握图的逻辑结构
(2)掌握图的邻接矩阵的存储结构
(3)验证图的邻接矩阵存储及其遍历操作的实现
.实验内容
(1)建立无向图的邻接矩阵存储
(2)进行深度优先遍历
(3)进行广度优先遍历
.设计与编码

#ifndef MGraph_H
#define MGraph_H
const int MaxSize = 10;
template<class DataType>
class MGraph
{
public:
MGraph(DataType a[], int n, int e);
~MGraph(){
}
谢谢你的观赏
谢谢你的观赏
void DFSTraverse(int v);
void BFSTraverse(int v);
private:
DataType vertex[MaxSize];
int arc[MaxSize][MaxSize];
int vertexNum, arcNum;
};
#endif

#include<iostream>
using namespace std;
#include ""
extern int visited[MaxSize];
template<class DataType>
MGraph<DataType>::MGraph(DataType a[], int n, int e) {
int i, j, k;
vertexNum = n, arcNum = e;
for(i = 0; i < vertexNum; i++)
vertex[i] = a[i];
for(i = 0;i < vertexNum; i++)
for(j = 0; j < vertexNum; j++)
arc[i][j] = 0;
谢谢你的观赏
谢谢你的观赏
for(k = 0; k < arcNum; k++)
{
cout << "Please enter two vertexs number of edge:"
cin >> i >> j;
arc[i][j] = 1;
arc[j][i] = 1;
}
}
template<class DataType>
void MGraph<DataType>::DFSTraverse(int v)
{
cout << vertex[v];
visited[v] = 1;
for(int j = 0; j < vertexNum; j++)
if(arc[v][j] == 1 && visited[j] == 0)
DFSTraverse(j);
}
template<class DataType>
void MGraph<DataType>::BFSTraverse(int v)
{
int Q[MaxSize];
int front = -1, rear = -1;
cout << vertex[v];
visited[v] = 1;
谢谢你的观赏
谢谢你的观赏
Q[++rear] = v;
whilefront != rear)
{
v = Q[++front];
for(int j = 0;j < vertexNum; j++)
if(arc[v][j] == 1 && visited[j] == 0){ cout << vertex[j];
visited[j] = 1;
Q[++rear] = j;
}
}
}

#include<iostream>
using namespace std;
#include ""
extern int visited[MaxSize];
template<class DataType>
MGraph<DataType>::MGraph(DataType a[], int n, int e)
{
int i, j, k;
vertexNum = n, arcNum = e;
谢谢你的观赏
谢谢你的观赏
for(i = 0; i < vertexNum; i++)
vertex[i] = a[i];
for(i = 0;i < vertexNum; i++)
for(j = 0; j < vertexNum; j++)
arc[i][j] = 0;
for(k = 0; k < arcNum;