1 / 4
文档名称:

C 图的遍历算法.doc

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

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

分享

预览

C 图的遍历算法.doc

上传人:mh900965 2018/2/25 文件大小:38 KB

下载得到文件列表

C 图的遍历算法.doc

文档介绍

文档介绍:#include<>
#define UNVISITED 0
#define VISITED 1
class AdjMatrixUndirGraph
{
public:
int vexNum, edgeNum; // 顶点个数和边数
int **Matrix; // 邻接矩阵
char *elems; // 顶点数据
bool *tag;

AdjMatrixUndirGraph(char es[], int vertexNum);
int FirstAdjVex(int v) ; // 返回顶点v的第一个邻接点
int NextAdjVex(int v1, int v2);// 返回顶点v1的相对于v2的下一个邻接点
void Display();
void BFS(int v);
void DFS(int v);
};
AdjMatrixUndirGraph::AdjMatrixUndirGraph(char es[], int vertexNum)
{
vexNum = vertexNum;
edgeNum = 0;
elems = new char[vexNum];
int u, v;
for(v = 0; v < vexNum; v++)
{
elems[v] = es[v];
}
tag = new bool[vexNum]; // 生成标志数组
for(v = 0; v < vexNum; v++)
{ // 初始化标志数组
tag[v] = UNVISITED;
}
Matrix = (int **)new int *[vexNum];// 生成邻接矩阵
for (v = 0; v < vexNum; v++)
{ // 生成邻接矩阵的行
Matrix[v] = new int[vexNum];
}
for (u = 0; u < vexNum; u++)
{
for (v = 0; v < vexNum; v++)
{ // 为邻接矩阵元素赋值
Matrix[u][v] = 0;
}
}
}
int AdjMatrixUndirGraph::FirstAdjVex(int v)
// 操作结果:返回顶点v的第1个邻接点
{
if (v < 0 || v >= vexNum)
{
cout<<"v不合法!";
return -1;
}
for (int cur = 0; cur < vexNum; cur++)
{ // 查找邻接点
if (Matrix[v][cur] != 0) return cur;
}
return -1; // 返回-1表示无邻接点
}
int AdjMatrixUndirGraph::NextAdjVex(int v1, int v2)
// 操作结果:返回顶点v1的相对于v2的下1个邻接点
{
if (v1 < 0 || v1 >= vexNum)
{
cout<<"v1不合法!";
return -1;
}
if (v2 < 0