(程序中graph.h是图存储结构的“算法库”中的头文件,详情请单击链接…)
1、是否有简单路径?
问题:假设图G采用邻接表存储,设计一个算法,判断顶点u到v是否有简单路径。
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];
void ExistPath(ALGraph *G,int u,int v, bool &has)
{
int w;
ArcNode *p;
visited[u]=1;
if(u==v)
{
has=true;
return;
}
p=G->adjlist[u].firstarc;
while (p!=NULL)
{
w=p->adjvex;
if (visited[w]==0)
ExistPath(G,w,v,has);
p=p->nextarc;
}
}
void HasPath(ALGraph *G,int u,int v)
{
int i;
bool flag = false;
for (i=0; i<G->n; i++)
visited[i]=0;
ExistPath(G,u,v,flag);
printf(" 从 %d 到 %d ", u, v);
if(flag)
printf("有简单路径\n");
else
printf("无简单路径\n");
}
int main()
{
ALGraph *G;
int A[5][5]=
{
{0,0,0,0,0},
{0,0,1,0,0},
{0,0,0,1,1},
{0,0,0,0,0},
{1,0,0,1,0},
};
ArrayToList(A[0], 5, G);
HasPath(G, 1, 0);
HasPath(G, 4, 1);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
附:测试图结构及存储
![这里写图片描述](https://img-blog.csdn.net/20151103213225990)
![](https://img-blog.csdn.net/20171114200311012?)
2、输出简单路径
问题:假设图G采用邻接表存储,设计一个算法输出图G中从顶点u到v的一条简单路径(假设图G中从顶点u到v至少有一条简单路径)。
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];
void FindAPath(ALGraph *G,int u,int v,int path[],int d)
{
int w,i;
ArcNode *p;
visited[u]=1;
d++;
path[d]=u;
if (u==v)
{
printf("一条简单路径为:");
for (i=0; i<=d; i++)
printf("%d ",path[i]);
printf("\n");
return;
}
p=G->adjlist[u].firstarc;
while (p!=NULL)
{
w=p->adjvex;
if (visited[w]==0)
FindAPath(G,w,v,path,d);
p=p->nextarc;
}
}
void APath(ALGraph *G,int u,int v)
{
int i;
int path[MAXV];
for (i=0; i<G->n; i++)
visited[i]=0;
FindAPath(G,u,v,path,-1);
}
int main()
{
ALGraph *G;
int A[5][5]=
{
{0,0,0,0,0},
{0,0,1,0,0},
{0,0,0,1,1},
{0,0,0,0,0},
{1,0,0,1,0},
};
ArrayToList(A[0], 5, G);
APath(G, 1, 0);
APath(G, 4, 1);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
![](https://img-blog.csdn.net/20171114202206265?)
3、输出所有路径
问题:输出从顶点u到v的所有简单路径。
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];
void FindPaths(ALGraph *G,int u,int v,int path[],int d)
{
int w,i;
ArcNode *p;
visited[u]=1;
d++;
path[d]=u;
if (u==v && d>1)
{
printf(" ");
for (i=0; i<=d; i++)
printf("%d ",path[i]);
printf("\n");
}
p=G->adjlist[u].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if (visited[w]==0)
FindPaths(G,w,v,path,d);
p=p->nextarc;
}
visited[u]=0;
}
void DispPaths(ALGraph *G,int u,int v)
{
int i;
int path[MAXV];
for (i=0; i<G->n; i++)
visited[i]=0;
printf("从%d到%d的所有路径:\n",u,v);
FindPaths(G,u,v,path,-1);
printf("\n");
}
int main()
{
ALGraph *G;
int A[5][5]=
{
{0,1,0,1,0},
{1,0,1,0,0},
{0,1,0,1,1},
{1,0,1,0,1},
{0,0,1,1,0}
};
ArrayToList(A[0], 5, G);
DispPaths(G, 1, 4);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
附:测试用的图结构、存储结构、运行结果
![这里写图片描述](https://img-blog.csdn.net/20151103214106628)
4、输出一些简单回路
问题:输出图G中从顶点u到v的长度为s的所有简单路径。
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];
void SomePaths(ALGraph *G,int u,int v,int s, int path[],int d)
{
int w,i;
ArcNode *p;
visited[u]=1;
d++;
path[d]=u;
if (u==v && d==s)
{
printf(" ");
for (i=0; i<=d; i++)
printf("%d ",path[i]);
printf("\n");
}
p=G->adjlist[u].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if (visited[w]==0)
SomePaths(G,w,v,s,path,d);
p=p->nextarc;
}
visited[u]=0;
}
void DispSomePaths(ALGraph *G,int u,int v, int s)
{
int i;
int path[MAXV];
for (i=0; i<G->n; i++)
visited[i]=0;
printf("从%d到%d长为%d的路径:\n",u,v,s);
SomePaths(G,u,v,s,path,-1);
printf("\n");
}
int main()
{
ALGraph *G;
int A[5][5]=
{
{0,1,0,1,0},
{1,0,1,0,0},
{0,1,0,1,1},
{1,0,1,0,1},
{0,0,1,1,0}
};
ArrayToList(A[0], 5, G);
DispSomePaths(G, 1, 4, 3);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
附:测试的图结构、运行结果
![这里写图片描述](https://img-blog.csdn.net/20151103214239111)
![](https://img-blog.csdn.net/20171114202957584?)
5、输出通过一个节点的所有简单回路
问题:求图中通过某顶点k的所有简单回路(若存在)
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];
void DFSPath(ALGraph *G,int u,int v,int path[],int d)
{
int w,i;
ArcNode *p;
visited[u]=1;
d++;
path[d]=u;
p=G->adjlist[u].firstarc;
while (p!=NULL)
{
w=p->adjvex;
if (w==v && d>0)
{
printf(" ");
for (i=0; i<=d; i++)
printf("%d ",path[i]);
printf("%d \n",v);
}
if (visited[w]==0)
DFSPath(G,w,v,path,d);
p=p->nextarc;
}
visited[u]=0;
}
void FindCyclePath(ALGraph *G,int k)
{
int path[MAXV],i;
for (i=0; i<G->n; i++)
visited[i]=0;
printf("经过顶点%d的所有回路\n",k);
DFSPath(G,k,k,path,-1);
printf("\n");
}
int main()
{
ALGraph *G;
int A[5][5]=
{
{0,1,1,0,0},
{0,0,1,0,0},
{0,0,0,1,1},
{0,0,0,0,1},
{1,0,0,0,0}
};
ArrayToList(A[0], 5, G);
FindCyclePath(G, 0);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
附:测试用图结构、输出结果
![这里写图片描述](https://img-blog.csdn.net/20151103214408532)
![](https://img-blog.csdn.net/20171114203455551?)