查找图里从某一顶点出发的最长路径,显然,用队列记录从队列头部顶点出发的所有路径,到达队列尾部最后一个顶点的路径就是最长的路径。当然可能有长度相等的多个最长路径。那样会使问题复杂化。这里以找到一条路径即可。
函数BFSLongestPath:根据图里指定的路径起始顶点,查找出其能到达的最长路径。
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 BFSLongestPath(GraphAdjaList& graphAdjList,int indexStart);
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;
for (int i = 0; i < graphAdjList.numVertexes; i++)
BFSLongestPath(graphAdjList,i);
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 BFSLongestPath(GraphAdjaList& graphAdjList, int indexStart) {
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) {
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++;
}
cout << "longest path : ";
pathLength = 0;
for (int i = tail; 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;
}
测试结果及对应图如下:
经测试无误,谢谢阅读。