题目
一、定义图的相关数据结构
typedef struct {
char vexs[MAXSIZE];
int arcs[MAXSIZE][MAXSIZE];
int arcnum, vexnum;
}MGraph;
二、建图
int Create(MGraph& G)
{
printf("请输入顶点数:");
scanf("%d", &G.vexnum);
getchar();
for (int i = 1; i <= G.vexnum; i++)
for (int j = 1; j <= G.vexnum; j++)
G.arcs[i][j] = 0;
printf("请输入边数:\n");
scanf("%d", &G.arcnum);
getchar();
printf("请输入各个顶点:\n");
for (int i = 1; i <= G.vexnum; i++)
scanf("%c", &G.vexs[i]);
getchar();
char char1, char2;
printf("请输入各条边:\n");
for (int i = 1; i <= G.arcnum; i++)
{
scanf("%c%c", &char1, &char2);
getchar();
int p = Locate(G, char1);
int q = Locate(G, char2);
if (p == -1 || q == -1)return ERROR;
G.arcs[p][q] = 1;
}
return 1;
}
三、拓扑排序判断是否有环
int HUAN(MGraph G)
{
int in[MAXSIZE] = { 0 };
for (int i = 1; i <= G.vexnum; i++)
for (int j = 1; j <= G.vexnum; j++)
if (G.arcs[i][j] == 1)
in[j]++;
Queue q;
int cnt = 0, t;
char c;
InitQueue(q);
for (int i = 1; i <= G.vexnum; i++)
if (!in[i])
EnQueue(q, G.vexs[i]);
while (q.rear != q.front)
{
c = DeQueue(q);
cnt++;
if (cnt > G.vexnum)return 1;
t = Locate(G, c);
for (int i = 1; i <= G.vexnum; i++)
{
if (G.arcs[t][i] == 1)
if (!--in[i])
EnQueue(q, G.vexs[i]);
}
}
return cnt != G.vexnum;
}
四、给出起点,终点及长度,输出路径
void DFS(MGraph G,int t,int len)
{
if(len>m)
{
if(path[m]==end)puts(path);
return;
}
for(int i=1;i<=G.vexnum;i++)
{
if(G.arcs[t][i]==1&&!vis[i])
{
vis[i]=1;
path[len]=G.vexs[t];
DFS(G,i,len+1);
path[len]='\0';
vis[i]=0;
}
}
}
五、主函数中进行测试
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 50
#define ERROR -1
#define INFINITY 0x3f3f3f3f
typedef int Status;
typedef struct {
char vexs[MAXSIZE];
int arcs[MAXSIZE][MAXSIZE];
int arcnum, vexnum;
}MGraph;
typedef struct {
char* base;
int front, rear;
int queuesize;
}Queue;
int m;
char start,end;
char path[MAXSIZE];
int vis[MAXSIZE];
void InitQueue(Queue& q);
void EnQueue(Queue& q, char e);
char DeQueue(Queue& q);
int Locate(MGraph G, char e);
int Create(MGraph& G);
int HUAN(MGraph G);
void DFS(MGraph G,int t,int len);
int main()
{
MGraph G;
Create(G);
if (HUAN(G))printf("经计算,本图存在环\n");
else printf("经计算,本图不存在环\n");
printf("请输入起点,终点,路径长度:");
scanf("%c %c %d",&start,&end, &m);
DFS(G,Locate(G,start),0);
return 0;
}
void DFS(MGraph G,int t,int len)
{
if(len>m)
{
if(path[m]==end)puts(path);
return;
}
for(int i=1;i<=G.vexnum;i++)
{
if(G.arcs[t][i]==1&&!vis[i])
{
vis[i]=1;
path[len]=G.vexs[t];
DFS(G,i,len+1);
path[len]='\0';
vis[i]=0;
}
}
}
void InitQueue(Queue& q)
{
q.base = (char*)malloc(sizeof(char) * MAXSIZE);
q.rear = q.front = 0;
q.queuesize = MAXSIZE;
}
void EnQueue(Queue& q, char e)
{
if((q.rear + 1) % MAXSIZE == q.front)return;
q.base[q.rear] = e;
q.rear = (q.rear + 1) % MAXSIZE;
}
char DeQueue(Queue& q)
{
if (q.rear == q.front)return -1;
char e = q.base[q.front];
q.front = (q.front + 1) % MAXSIZE;
return e;
}
int HUAN(MGraph G)
{
int in[MAXSIZE] = { 0 };
for (int i = 1; i <= G.vexnum; i++)
for (int j = 1; j <= G.vexnum; j++)
if (G.arcs[i][j] == 1)
in[j]++;
Queue q;
int cnt = 0, t;
char c;
InitQueue(q);
for (int i = 1; i <= G.vexnum; i++)
if (!in[i])
EnQueue(q, G.vexs[i]);
while (q.rear != q.front)
{
c = DeQueue(q);
cnt++;
if (cnt > G.vexnum)return 1;
t = Locate(G, c);
for (int i = 1; i <= G.vexnum; i++)
{
if (G.arcs[t][i] == 1)
if (!--in[i])
EnQueue(q, G.vexs[i]);
}
}
return cnt != G.vexnum;
}
int Locate(MGraph G, char e)
{
for (int i = 1; i <= G.vexnum; i++)
if (e == G.vexs[i])
return i;
return -1;
}
int Create(MGraph& G)
{
printf("请输入顶点数:");
scanf("%d", &G.vexnum);
getchar();
for (int i = 1; i <= G.vexnum; i++)
for (int j = 1; j <= G.vexnum; j++)
G.arcs[i][j] = 0;
printf("请输入边数:\n");
scanf("%d", &G.arcnum);
getchar();
printf("请输入各个顶点:\n");
for (int i = 1; i <= G.vexnum; i++)
scanf("%c", &G.vexs[i]);
getchar();
char char1, char2;
printf("请输入各条边:\n");
for (int i = 1; i <= G.arcnum; i++)
{
scanf("%c%c", &char1, &char2);
getchar();
int p = Locate(G, char1);
int q = Locate(G, char2);
if (p == -1 || q == -1)return ERROR;
G.arcs[p][q] = 1;
}
return 1;
}
样例:
8
11
ABCDEFUV
AU
AB
BF
BV
FV
EF
VD
DE
CU
UE
CD
C F 3
运行结果: