广度优先,查找图中顶点间的路径,c/c++描述

  图的广度优先遍历BFS其实还是队列的应用。同迷宫问题里所有路径,用队列解决的思路是一样的。在图里查找两顶点间的路径,也同样要求路径是简单路径,路径中不可出现重复顶点。队列里第一个出现的到达终点的路径就是最短路径。
  在数据结构里,把前几章节的基础打牢了,理解扎实,往后会不那么难了,虽然图里顶点间的关系多了,但分析思路还是和链表和树是一样的。
  函数BFSAllPath:用广度优先,队列的方法查找给定起止顶点间的所有路径。
  感谢bilibili懒猫老师的教导。
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 BFSAllPath(GraphAdjaList& graphAdjList,int indexStart,int indexEnd);

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;

	char goOn;
	int indexStart, indexEnd;
	do {
		cout << "input the indexes of start vertex and end vetex : ";
		cin >> indexStart >> indexEnd;
		BFSAllPath(graphAdjList,indexStart,indexEnd);
		
		cout << endl;
		cout << "input 'y' or 'n' to decide whether to continue : ";
		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;
};

struct Queue {
	int indexVertxInArray;
	int indexParentInQueue;
};

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 BFSAllPath(GraphAdjaList& graphAdjList,
	int indexStart, int indexEnd) {
	Queue queue[MAXVERTEX];
	int head = 0, tail = 0;

	queue[tail].indexVertxInArray = indexStart;
	queue[tail].indexParentInQueue = -1;

	AdjaListNode* pt;
	int indexAdjVertx;
	bool repeated;
	int indexPath[MAXVERTEX], pathLength;
	while (head <= tail) {
		if (queue[head].indexVertxInArray == indexEnd) {
			cout << "path : ";
			pathLength = 0;
			for (int i = head; i > -1; i = queue[i].indexParentInQueue) {
				indexPath[pathLength] = queue[i].indexVertxInArray;
				pathLength++;
			}

			for (int i = pathLength - 1; i >= 0 ; i--)
				cout << graphAdjList.vertexes[indexPath[i]].vertex << " ";
			cout << endl;
		}
		else {
			pt = graphAdjList.vertexes[queue[head].indexVertxInArray].pt;
			while (pt != NULL) {
				indexAdjVertx = pt->indexOfVertex;
				repeated = false;
				for (int i = queue[head].indexParentInQueue; i >= 0;
					i = queue[i].indexParentInQueue)
					if (queue[i].indexVertxInArray == indexAdjVertx) {
						repeated = true;
						break;
					}

				if (!repeated) {
					tail++;
					queue[tail].indexVertxInArray = indexAdjVertx;
					queue[tail].indexParentInQueue = head;
				}

				pt = pt->pt;
			}
		}
		head++;
	}
}

测试结果及对应图如下:
在这里插入图片描述
在这里插入图片描述
经测试结果是正确的。谢谢阅读

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值