图的深度优先遍历,DFS,非递归,c/c++描述

  图的深度优先遍历的课本定义是:
在这里插入图片描述

  受bilibili懒猫老师的讲解启发,我更愿意把图的DFS遍历过程概括为:先遍历图的根节点,再遍历根节点的第一邻接子图,依次遍历完根节点的所有子图。图的根节点就是遍历图时指定的第一个顶点。这样更好记忆其精髓,要点。
  递归DFS时调用系统栈。不采用递归的方法,也可以,就自己建立一个栈,存储图里顶点的下标,从栈里依次弹出顶点的顺序,就是DFS遍历图的顺序。
  具体过程为,每弹出一个顶点,就修改其对应的visited[]数组的值。对图的遍历,无论DFS还是BFS,都用了visited[]数组,来存储和表明哪些顶点已被访问输出,哪些顶点未被访问输出,防止输出重复顶点。弹出一个顶点后,就把其所有未被输出的邻接点的下标存入栈。依次循环,直至栈为空。
  遍历要求不重不漏即可。先遍历根节点的哪个邻接节点也是不确定的。所以DFS遍历结果也是不唯一的。
  完整代码如下,先是main函数所在源文件:

#include<iostream>
#include<stdio.h>
using namespace std;
#define NUMVERTEX 6
#define INFINI 65555

struct GraphAdjaMatrix {
	char vertexes[NUMVERTEX];
	int edges[NUMVERTEX][NUMVERTEX];
	int numVertexes;
	int numEdges;
};

struct AdjaListNode {
	int indexOfVertex;
	int weightOfEdge;
	AdjaListNode* pt;
};

struct AdjListHead {
	char vertex;
	AdjaListNode* pt;
};

struct GraphAdjaList {
	AdjListHead vertexes[NUMVERTEX];
	int numVertexes;
	int numEdges;
};

extern void createGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix,
			int numVertexes,int numEdges,int edges[][NUMVERTEX],char vertexes[]);
extern void createGraphAdjList(GraphAdjaList& graphAdjList,
	int numVertexes, int numEdges, int edges[][NUMVERTEX], char vertexes[]);
extern void dispalyGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix);
extern void displayGrapgAdjList(GraphAdjaList& graphAdjList);
extern void DFSNoTraverse(GraphAdjaList& graphAdjList,int indexStart);

int main() {
	GraphAdjaMatrix graphAdjMatrix ;
	GraphAdjaList graphAdjList;

	int numEdges = 10;
	int edges[][6] = {	{0,5,INFINI,7,INFINI,INFINI},
						{INFINI,0,4,INFINI,INFINI,INFINI},
						{8,INFINI,0,INFINI,INFINI,9},
						{INFINI,INFINI,5,0,INFINI,6},
						{INFINI,INFINI,INFINI,5,0,INFINI},
						{3,INFINI,INFINI,INFINI,1,0} };
	char vertexes[] = {'0','1','2','3','4','5'};

	createGraphAdjMatrix(graphAdjMatrix,NUMVERTEX,numEdges,edges,vertexes);
	createGraphAdjList(graphAdjList,NUMVERTEX,numEdges,edges,vertexes);

	dispalyGraphAdjMatrix(graphAdjMatrix);
	displayGrapgAdjList(graphAdjList);
	cout << endl;

	cout << "DFS no traverse : ";
	DFSNoTraverse(graphAdjList,0);
	
	return 0;
}

接着是各函数所在源文件:

#include<iostream>
#include<stdio.h>
using namespace std;
#define NUMVERTEX 6
#define INFINI 65555

struct GraphAdjaMatrix {
	char vertexes[NUMVERTEX];
	int edges[NUMVERTEX][NUMVERTEX];
	int numVertexes;
	int numEdges;
};

struct AdjaListNode {
	int indexOfVertex;
	int weightOfEdge;
	AdjaListNode* pt;
};

struct AdjListHead {
	char vertex;
	AdjaListNode* pt;
};

struct GraphAdjaList {
	AdjListHead vertexes[NUMVERTEX];
	int numVertexes;
	int numEdges;
};

void createGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix,
	int numVertexes, int numEdges, int edges[][NUMVERTEX], char vertexes[]) {
	graphAdjMatrix.numVertexes = numVertexes;
	graphAdjMatrix.numEdges = numEdges;
	
	for (int i = 0; i < numVertexes; i++)
		graphAdjMatrix.vertexes[i] = vertexes[i];

	for (int row = 0; row < numVertexes; row++)
		for (int column = 0; column < numVertexes; column++)
			graphAdjMatrix.edges[row][column] = edges[row][column];
}

void createGraphAdjList(GraphAdjaList &graphAdjList,
	int numVertexes, int numEdges, int edges[][NUMVERTEX], char vertexes[]){
	graphAdjList.numEdges = numEdges;
	graphAdjList.numVertexes = numVertexes;

	for (int i = 0; i < numVertexes; i++)
		graphAdjList.vertexes[i].pt = NULL;

	for (int i = 0; i < numVertexes; i++)
		graphAdjList.vertexes[i].vertex = vertexes[i];

	AdjaListNode* ptTail = NULL,*ptNew;
	int i, j;
	for ( i = 0; i < numVertexes; i++) 
		for (j = 0; j < numVertexes; j++) 
			if (edges[i][j] != 0 && edges[i][j] != INFINI) {
				ptNew = new AdjaListNode;

				ptNew->indexOfVertex = j;
				ptNew->weightOfEdge = edges[i][j];
			
				if (graphAdjList.vertexes[i].pt == NULL) {
					ptNew->pt = NULL;
					graphAdjList.vertexes[i].pt = ptNew;
					ptTail = ptNew;
				}
				else {
					ptNew->pt = ptTail->pt;
					ptTail->pt = ptNew;
					ptTail = ptNew;
				}
			}
}


void dispalyGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix) {
	cout << "adjacensy matrix :" << endl;
	int row,column;
	printf("%3c",' ');
	for (row = 0; row < graphAdjMatrix.numVertexes; row++)
		printf("%3c",graphAdjMatrix.vertexes[row]);
	printf("\n");
	for (row = 0; row < graphAdjMatrix.numVertexes; row++) {
		printf("%-3c", graphAdjMatrix.vertexes[row]);
		for (column = 0; column < graphAdjMatrix.numVertexes; column++)
			if (graphAdjMatrix.edges[row][column] == INFINI)
				printf("%3s", "∞");
			else
				printf("%3d",graphAdjMatrix.edges[row][column]);
		cout << endl;
	}
}

void displayGrapgAdjList(GraphAdjaList &graphAdjList) {
	cout << "graph adjacency list : " << endl;
	AdjaListNode* pt;
	int index;
	for (int i = 0; i < graphAdjList.numVertexes; i++) {
		printf("%2c:",graphAdjList.vertexes[i].vertex);
		pt = graphAdjList.vertexes[i].pt;
		while (pt != NULL) {
			index = pt->indexOfVertex;
			printf("%5c(%d)",graphAdjList.vertexes[index].vertex,pt->weightOfEdge);
			pt = pt->pt;
		}
		cout << endl;
	}
}

void DFSNoTraverse(GraphAdjaList& graphAdjList, int indexStart) {
	bool visited[NUMVERTEX];
	for (int i = 0; i < NUMVERTEX; i++)
		visited[i] = false;

	AdjaListNode* pt;

	int stack[NUMVERTEX * 2], top = 0;
	stack[top] = indexStart;

	while (top >= 0) {
		cout << graphAdjList.vertexes[stack[top]].vertex << " ";
		visited[stack[top]] = true;
		pt = graphAdjList.vertexes[stack[top]].pt;
		top--;

		while (pt != NULL) {
			if (visited[pt->indexOfVertex] == false) {
				top++;
				stack[top] = pt->indexOfVertex;
			}
				
			pt = pt->pt;
		}
	}
}

  测试结果及对应的图如下:
在这里插入图片描述
在这里插入图片描述

  课本给出的DFS非递归的结果是 0 1 2 5 4 3 。我认为都是正确的。
  谢谢阅读。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值