深度优先遍历,是遍历图的一种思路,用递归的方法,依次遍历完给定顶点的所有子图,因为我们不能叫做该顶点的所有子树。子图就是依该顶点的邻接点为代表的子图。遍历过程中,不允许出现重复顶点,直至路径结束,遍历不到不重复的新的顶点;若是出现了与路径头顶点相同的顶点,说明出现了一条简单回路,即为所求。所以该题目是在DFS深度优先遍历的基础上进行的。
函数DFSFindPath:依次查找包含各个顶点的所有简单回路。本例中,查找完包含A的所有回路后,再查找包含B的所有回路,依次类推。
DFS深度优先遍历,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 DFSFindPath(GraphAdjaList& graphAdjList,int indexStart,int indexEnd,
int indexesInPath[],int pathLength,int & pathNum);
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 indexInPath[MAXVERTEX], pathLength,pathNum;
for (int i = 0; i < graphAdjList.numVertexes; i++) {
pathLength = pathNum = 0;
DFSFindPath(graphAdjList,i,i,indexInPath,pathLength,pathNum);
if (pathNum == 0)
cout << "there is no such path passed certex " <<
graphAdjList.vertexes[i].vertex << endl<<endl;
else
cout << "there are " << pathNum << " path passes vertex " <<
graphAdjList.vertexes[i].vertex<<endl<<endl;
}
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 indexStart, int indexEnd,
int indexesInPath[], int pathLength, int& pathNum) {
if (pathLength == 0 || indexStart != indexEnd) {
indexesInPath[pathLength] = indexStart;
pathLength++;
}
else {
pathNum++;
cout << "the " << pathNum << "th path : ";
for (int i = 0; i < pathLength; i++)
cout << graphAdjList.vertexes[indexesInPath[i]].vertex << " ";
cout << graphAdjList.vertexes[indexEnd].vertex<<endl;
return;
}
AdjaListNode* pt = graphAdjList.vertexes[indexStart].pt;
int indexNew;
bool repeated;
while (pt != NULL) {
indexNew = pt->indexOfVertex;
repeated = false;
for(int i =0 ; i < pathLength; i++)
if (indexesInPath[i] == indexNew && indexNew != indexEnd) {
repeated = true;
break;
}
if (!repeated)
DFSFindPath(graphAdjList,indexNew,indexEnd,
indexesInPath,pathLength,pathNum);
pt = pt->pt;
}
}
测试结果及图如下:
经测试,结果是正确的,谢谢阅读。