编程语言
首页 > 编程语言> > 图里查找顶点间是否存在路径,c/c++描述

图里查找顶点间是否存在路径,c/c++描述

作者:互联网

  路径的查找问题,以往我们遇到过的,有栈的迷宫问题,树里根节点到叶节点的路径问题,哈夫曼编码。这些路径的查找,是相似的。找到路径则输出到屏幕。没有则不输出。同时,在图里面要防止路径上顶点重复,基于深度优先遍历DFS。
  函数DFSFindPath:基于深度优先遍历,查找给定俩顶点间是否存在路径。
  main函数所在源文件代码:

#include<iostream>
#include<stdio.h>
using namespace std;
#define MAXVERTEX 15
#define INFINI 65555

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

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

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

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

extern void createGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix,
			int numVertexes,int numEdges,int edges[][6],char vertexes[]);
extern void createGraphAdjList(GraphAdjaList &graphAdjList,
			int numVertexes, int numEdges, int edges[][6], char vertexes[]);
extern void dispalyGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix);
extern void displayGrapgAdjList(GraphAdjaList &graphAdjList);
extern void DFSFindPath(GraphAdjaList& graphAdjList,
	int start,int end,int indexesInPath[],int pathLength);

int main() {
	GraphAdjaMatrix graphAdjMatrix ;
	GraphAdjaList graphAdjList;
	int numVertexes = 6, 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[] = {'a','b','c','d','e','f'};

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

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

	int start, end, indexInPath[MAXVERTEX], pathLength;
	char goOn;
	do {
		cout << "input the indexes of the start and end vertex : ";
		cin >> start >> end;
		pathLength = 0;
		DFSFindPath(graphAdjList,start,end,indexInPath,pathLength);
		cout << endl;
		cout << "input 'y' or 'n' to decide whether to continue search path : ";
		cin >> goOn;
	} while (goOn == 'Y' || goOn == 'y');

	return 0;
}

各函数所在源文件代码:

#include<iostream>
#include<stdio.h>
using namespace std;
#define MAXVERTEX 15
#define INFINI 65555

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

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

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

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

void createGraphAdjMatrix(GraphAdjaMatrix &graphAdjMatrix,
	int numVertexes, int numEdges, int edges[][6], 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[][6], char vertexes[]){
	graphAdjList.numEdges = numEdges;
	graphAdjList.numVertexes = numVertexes;

	for (int i = 0; i < MAXVERTEX; 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 DFSFindPath(GraphAdjaList& graphAdjList,
	int start, int end, int indexesInPath[], int pathLength) {

	AdjaListNode* pt;
	int newStart;
	bool repeat;
	if (start != end) {
		indexesInPath[pathLength] = start;
		pathLength++;

		if (pathLength >= graphAdjList.numVertexes) {
			cout << "have no such path connect the two vertexes;" << endl;
			return;
		}
		else {
			pt = graphAdjList.vertexes[start].pt;
			while (pt != NULL) {
				newStart = pt->indexOfVertex;
				repeat = false;
				for(int i = 0 ; i < pathLength ; i++)
					if (indexesInPath[i] == newStart) {
						repeat = true;
						break;
					}

				if (!repeat)
					DFSFindPath(graphAdjList,newStart,end,indexesInPath,pathLength);
				
				pt = pt->pt;
			}
		}
	}
	else {
		cout << "find path : ";
		for (int i = 0; i < pathLength; i++)
			cout << graphAdjList.vertexes[indexesInPath[i]].vertex<<" ";
		cout << graphAdjList.vertexes[end].vertex << endl;
	}
}

测试结果及对应的图如下:
在这里插入图片描述
在这里插入图片描述
经测试,所有顶点间的路径都正确,全面。
谢谢阅读。程序里DFS函数有些瑕疵,还没想好怎么改,谢谢阅读。

标签:pt,int,vertexes,c++,graphAdjList,查找,图里,INFINI,numVertexes
来源: https://blog.csdn.net/zhangzhangkeji/article/details/119245730