邻接表的深度优先遍历算法(非递归)

z_songlin z_songlin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
求大神给个邻接表的深度优先遍历算法(非递归)。。。写了很久还是写不出。。

在下面代码的基础上写个
void DFS(ListGraph* LGraph, int InitVertex)
//LGraph :邻接表
//InitVertex:遍历起始点

#include<iostream>
using namespace std;

#define MAX_VERTEX 6
#define INF INT_MAX //INF表示正无穷
typedef int VertexInfo; //顶点其它信息
typedef int EdgeInfo; //边其它信息

struct Vertex //顶点类型
{
int No; //顶点编号
VertexInfo VerInfo; //顶点其它信息
};

struct MatrixGraph //邻接矩阵图类型
{
int Edges[MAX_VERTEX][MAX_VERTEX]; //邻接矩阵的边数组
Vertex Vertex[MAX_VERTEX]; //顶点数组
int VertexCount; //顶点数
int EdgeCount; //边数
};

struct ArcNode //边节点类型
{
int AdjoinVertex; //该边的邻接点编号
EdgeInfo EdgeInfo; //边其它信息
ArcNode* pNextArc; //指向下一条边
};

struct VertexNode //邻接表表头数组节点
{
Vertex data; //顶点信息
ArcNode* FirstArc; //指向第一条边
};

typedef VertexNode AdjoinList[MAX_VERTEX]; //邻接表类型

struct ListGraph //邻接表图类型
{
AdjoinList AdjList; //邻接表
int VertexCount; //顶点数
int EdgeCount; //边数
};

//创建邻接矩阵
//mg: 指向邻接矩阵的指针
//arr: 用于创建邻接的二维数组
//VertexCount: 顶点数
//EdgeCount: 边数
void CreateMatirxGraph(MatrixGraph* &mg, int arr[MAX_VERTEX][MAX_VERTEX], int VertexCount, int EdgeCount)
{
for (int i = 0; i < MAX_VERTEX; i++)
{
mg->Vertex[i].No = i; //顶点编号为0~MAX_VERTEX-1
mg->Vertex[i].VerInfo = NULL; //顶点无其它信息

for (int j = 0; j < MAX_VERTEX; j++)
{
mg->Edges[i][j] = arr[i][j];
}
}
mg->VertexCount = VertexCount; //顶点数
mg->EdgeCount = VertexCount; //边数
}

//输出邻接矩阵
void DisplayMatirxGraph(const MatrixGraph* mg)
{
if (NULL == mg)
return;
for (int i = 0; i < mg->VertexCount; i++)
{
for (int j = 0; j < mg->VertexCount; j++)
{
if (mg->Edges[i][j] == INF)
cout << "∞" << " ";
else
cout << mg->Edges[i][j] << " ";
}
cout << endl;
}
}

//邻接矩阵转邻接表
void CreateListGraph(const MatrixGraph* MGraph, ListGraph* &LGraph)
{
ArcNode* pNew = NULL;
for (int i = 0; i < MGraph->EdgeCount; i++)
{
LGraph->AdjList[i].FirstArc = NULL;
}

for (int i = 0; i < MGraph->VertexCount; i++)
{
for (int j = MGraph->VertexCount - 1; j >= 0; j--)
{
if (MGraph->Edges[i][j] != 0 && MGraph->Edges[i][j] != INF) //如果顶点i与顶点j关联
{
pNew = new ArcNode;
pNew->AdjoinVertex = j;
pNew->EdgeInfo = MGraph->Edges[i][j]; //EdgeInfo存储该边的权重
LGraph->AdjList[i].data.No = i; //顶点编号
LGraph->AdjList[i].data.VerInfo = NULL; //该顶点无其它信息

//采用头插法创建每一条链表
pNew->pNextArc = LGraph->AdjList[i].FirstArc;
LGraph->AdjList[i].FirstArc = pNew;
}
}
}
LGraph->EdgeCount = MGraph->EdgeCount; //边数
LGraph->VertexCount = MGraph->VertexCount; //顶点数
}

//输出邻接表
void DisplayListGraph(const ListGraph* LGraph)
{
ArcNode* pCurr = NULL;
for (int i = 0; i < LGraph->EdgeCount; i++)
{
pCurr = LGraph->AdjList[i].FirstArc;
cout << LGraph->AdjList[i].data.No << " -> ";
while (NULL != pCurr->pNextArc)
{
cout << pCurr->AdjoinVertex << " -> ";
pCurr = pCurr->pNextArc;
}
cout << pCurr->AdjoinVertex << endl;
}
}


int main()
{
int WeightArr[MAX_VERTEX][MAX_VERTEX] =
{
{ 0, 5, INF, 7, INF, INF },
{ INF, 0, 4, INF, INF, INF },
{ 8, INF, 0, INF, INF, 9 },
{ INF, INF, 5, 0, INF, 6 },
{ INF, INF, INF, 5, 0, INF },
{ 3, INF, INF, INF, 1, 0 },
};
MatrixGraph* MGraph = new MatrixGraph;
CreateMatirxGraph(MGraph, WeightArr, 6, 10);
ListGraph* LGraph = new ListGraph;
CreateListGraph(MGraph, LGraph); //创建邻接表

DisplayListGraph(LGraph);
cout << endl;

system("pause");
return 0;
}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
z_songlin
C++ 语言 65,214 成员 250,514 内容