数据结构实验上机考试——判断图是否有环判断及路径输出

题目 

fdb4a914ad494b7eb47552589673920b.png

一、定义图的相关数据结构

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

运行结果:

3697f09cadb340dd9cd700b839ae3b52.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AAAAAZBX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值