关于无向无权图任意两点是否存在长度为len的路径问题?
-
以如下无向无权图为例,求0到2之间是否存在长度为5的路径。
/**
* 连接点结构
*/
typedef struct ArcNode{
/**
* 连接点的定点下标
*/
int adjvex;
/**
* 下一个邻接点
*/
struct ArcNode * nextarc;
/**
* 与此邻接点构成的边的信息
*/
InfoType * info;
}ArcNode;
typedef struct VNode{
/**
* 定点信息
*/
VertexType data;
/**
* 与其相连的第一个邻接点
*/
ArcNode * firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct ALGraph{
/**
* 图的顶点集
*/
AdjList vertices;
int vexnum, arcnum;
/**
* 图的种类
*/
int kind;
}ALGraph;
/**
* 构建初始化的例图
* @return
*/
ALGraph * GetSampleGraph(){
//...
return alGraph;
}
- 递归深度搜索是否存在从0到2是否存在长度为5的路径,核心代码如下
/**
* 无向图从u到v路径节点的访问标志
*
*/
int visited[MAX_VERTEX_NUM];
//出发点为i,终点为j,长度为k
int exist_path_len(ALGraph * G,int i,int j,int k)
{
ArcNode *p;
if(i==j&&k==0)
return 1;//存在长度为k的路径从i到j
else if(k>0)
{
visited[i]=1;//标记为已经访问
for(p=G->vertices[i].firstarc;p;p=p->nextarc)
{
int temp=p->adjvex;
/**
* 对没有被访问过的定点进行深度搜索
*/
if(!visited[temp]&&exist_path_len(G,temp,j,k-1))
return 1;
}
visited[i]=0;//如果经过这个定点没有符合的路径,则取消标记
//这里需要把已经访问的点重新置为0,因为如果当前不存在长度为k
//到达j点,那么这个点还是可以使用的,因为有可能从其他点出发
//可以到达j点并且长度为k
}
return 0;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define VertexType int
#define MAX_VERTEX_NUM 50
#define InfoType int
/**
* 连接点结构
*/
typedef struct ArcNode{
/**
* 连接点的定点下标
*/
int adjvex;
/**
* 下一个邻接点
*/
struct ArcNode * nextarc;
/**
* 与此邻接点构成的边的信息
*/
InfoType * info;
}ArcNode;
typedef struct VNode{
/**
* 定点信息
*/
VertexType data;
/**
* 与其相连的第一个邻接点
*/
ArcNode * firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct ALGraph{
/**
* 图的顶点集
*/
AdjList vertices;
int vexnum, arcnum;
/**
* 图的种类
*/
int kind;
}ALGraph;
/**
* 构建初始化的例图
* @return
*/
ALGraph * GetSampleGraph(){
ALGraph * alGraph = (ALGraph * ) malloc(sizeof(ALGraph));
alGraph->arcnum = 12;
alGraph->vexnum = 7;
alGraph->kind = 0;//无向无权图
ArcNode* arc01 = (ArcNode * )malloc(sizeof(ArcNode));
arc01->adjvex = 1;
ArcNode* arc05 = (ArcNode * )malloc(sizeof(ArcNode));
arc05->adjvex = 5;
ArcNode* arc06 = (ArcNode * )malloc(sizeof(ArcNode));
arc06->adjvex = 6;
alGraph->vertices[0].firstarc = arc01;
alGraph->vertices[0].firstarc->nextarc = arc05;
alGraph->vertices[0].firstarc->nextarc->nextarc = arc06;
alGraph->vertices[0].firstarc->nextarc->nextarc ->nextarc = NULL;
alGraph->vertices[0].data = 0;
ArcNode* arc10 = (ArcNode * )malloc(sizeof(ArcNode));
arc10->adjvex = 0;
ArcNode* arc15 = (ArcNode * )malloc(sizeof(ArcNode));
arc15->adjvex = 5;
ArcNode* arc12 = (ArcNode * )malloc(sizeof(ArcNode));
arc12->adjvex = 2;
alGraph->vertices[1].data = 1;
alGraph->vertices[1].firstarc = arc10;
alGraph->vertices[1].firstarc->nextarc = arc15;
alGraph->vertices[1].firstarc->nextarc->nextarc = arc12;
alGraph->vertices[1].firstarc->nextarc->nextarc ->nextarc = NULL;
ArcNode* arc21 = (ArcNode * )malloc(sizeof(ArcNode));
arc21->adjvex = 1;
ArcNode* arc25 = (ArcNode * )malloc(sizeof(ArcNode));
arc25->adjvex = 5;
ArcNode* arc24 = (ArcNode * )malloc(sizeof(ArcNode));
arc24->adjvex = 4;
ArcNode* arc23 = (ArcNode * )malloc(sizeof(ArcNode));
arc23->adjvex=3;
alGraph->vertices[2].data=2;
alGraph->vertices[2].firstarc = arc21;
alGraph->vertices[2].firstarc->nextarc = arc25;
alGraph->vertices[2].firstarc->nextarc->nextarc = arc24;
alGraph->vertices[2].firstarc->nextarc->nextarc ->nextarc= arc23;
alGraph->vertices[2].firstarc->nextarc->nextarc ->nextarc->nextarc = NULL;
ArcNode* arc32 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc34 = (ArcNode * )malloc(sizeof(ArcNode));
arc32 ->adjvex = 2;
arc34 ->adjvex=4;
alGraph->vertices[3].data=3;
alGraph->vertices[3].firstarc = arc32;
alGraph->vertices[3].firstarc->nextarc = arc34;
alGraph->vertices[3].firstarc->nextarc->nextarc = NULL;
ArcNode* arc42 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc43 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc45 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc46 = (ArcNode * )malloc(sizeof(ArcNode));
arc42->adjvex=2;
arc43->adjvex=3;
arc45->adjvex=5;
arc46->adjvex=6;
alGraph->vertices[4].data=4;
alGraph->vertices[4].firstarc = arc42;
alGraph->vertices[4].firstarc->nextarc = arc43;
alGraph->vertices[4].firstarc->nextarc->nextarc = arc45;
alGraph->vertices[4].firstarc->nextarc->nextarc ->nextarc= arc46;
alGraph->vertices[4].firstarc->nextarc->nextarc ->nextarc->nextarc = NULL;
ArcNode* arc50 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc51 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc52 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc54 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc56 = (ArcNode * )malloc(sizeof(ArcNode));
arc50->adjvex=0;
arc51->adjvex=1;
arc52->adjvex=2;
arc54->adjvex=4;
arc56->adjvex=6;
alGraph->vertices[5].data =5;
alGraph->vertices[5].firstarc = arc50;
alGraph->vertices[5].firstarc->nextarc = arc51;
alGraph->vertices[5].firstarc->nextarc->nextarc = arc52;
alGraph->vertices[5].firstarc->nextarc->nextarc ->nextarc= arc54;
alGraph->vertices[5].firstarc->nextarc->nextarc ->nextarc->nextarc = arc56;
alGraph->vertices[5].firstarc->nextarc->nextarc ->nextarc->nextarc ->nextarc= NULL;
ArcNode* arc60 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc64 = (ArcNode * )malloc(sizeof(ArcNode));
ArcNode* arc65 = (ArcNode * )malloc(sizeof(ArcNode));
arc60->adjvex = 0;
arc64->adjvex = 4;
arc65->adjvex = 5;
alGraph->vertices[6].data=6;
alGraph->vertices[6].firstarc = arc60;
alGraph->vertices[6].firstarc->nextarc = arc64;
alGraph->vertices[6].firstarc->nextarc->nextarc = arc65;
alGraph->vertices[6].firstarc->nextarc->nextarc ->nextarc= NULL;
return alGraph;
}
/**
* 无向图从u到v路径节点的访问标志
*
*/
int visited[MAX_VERTEX_NUM];
//出发点为i,终点为j,长度为k
int exist_path_len(ALGraph * G,int i,int j,int k)
{
ArcNode *p;
if(i==j&&k==0)
return 1;//存在长度为k的路径从i到j
else if(k>0)
{
visited[i]=1;//标记为已经访问
for(p=G->vertices[i].firstarc;p;p=p->nextarc)
{
int temp=p->adjvex;
/**
* 对没有被访问过的定点进行深度搜索
*/
if(!visited[temp]&&exist_path_len(G,temp,j,k-1))
return 1;
}
visited[i]=0;//如果经过这个定点没有符合的路径,则取消标记
//这里需要把已经访问的点重新置为0,因为如果当前不存在长度为k
//到达j点,那么这个点还是可以使用的,因为有可能从其他点出发
//可以到达j点并且长度为k
}
return 0;
}
int main() {
ALGraph * alGraph = GetSampleGraph();
cout<<exist_path_len(alGraph,0,2,6)<<endl;
return 0;
}