非递归算法获得拓扑序列【BFS】

算法思想:将图中入度为0的结点加入队列中;当队列不为空时,输出队头结点,将其加入拓扑序列中,并更新结点入度数组,并将入队为0的结点加入队列中;重复上述步骤,直到队列为空。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_VERTEX_NUM 100
typedef int VertexType;
typedef struct ArcNode {
	int adjvex;
	struct ArcNode* nextArc;
	//int weight;
}ArcNode;
typedef struct VNode {
	VertexType data;
	struct ArcNode* firstArc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
	AdjList vertices;
	int vexNum, arcNum;
	int kind;
}ALGraph;
//链队相关数据结构
typedef VertexType QElemType;
typedef struct LinkNode {
	QElemType data;
	struct LinkNode* next;
}LinkNode, * LinkList;
typedef struct LinkQueue {
	LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q) {
	Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
	Q.front->next = NULL;
}
int IsEmpty(LinkQueue Q) {
	if (Q.front == Q.rear) {
		return 1;
	}
	else {
		return 0;
	}
}
void EnQueue(LinkQueue& Q, QElemType x) {
	LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
	pnode->data = x;
	pnode->next = Q.rear->next;
	Q.rear->next = pnode;
	Q.rear = pnode;
}
void DeQueue(LinkQueue& Q, QElemType& x) {
	LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
	pnode = Q.front->next;
	x = pnode->data;
	Q.front->next = pnode->next;
	if (Q.rear == pnode) {
		Q.rear = Q.front;
	}
	free(pnode);
}
//建图
void createALGraph(ALGraph &G,VertexType* vexList, int vexNum, int arcList[][2], int arcNum, int kind) {
	for (int i = 1; i <= vexNum; i++) {
		G.vertices[i].data = vexList[i];
		G.vertices[i].firstArc = NULL;
	}
	for (int i = 0; i < arcNum; i++) {
		VertexType v = arcList[i][0];
		VertexType w = arcList[i][1];
		ArcNode* pnode = (ArcNode*)malloc(sizeof(ArcNode));
		pnode->adjvex = w;
		pnode->nextArc = G.vertices[v].firstArc;
		G.vertices[v].firstArc = pnode;
		if (kind == 0) {
			ArcNode* pnode1 = (ArcNode*)malloc(sizeof(ArcNode));
			pnode1->adjvex = v;
			pnode1->nextArc = G.vertices[w].firstArc;
			G.vertices[w].firstArc = pnode1;
		}
	}
	G.arcNum = arcNum;
	G.vexNum = vexNum;
	G.kind = kind;
}
int print[MAX_VERTEX_NUM] = { 0 };//拓扑序列数组
int printLength = 0;
int indegree[MAX_VERTEX_NUM] = { 0 };//入度数组
void initialIndegree(ALGraph G,int* indegree) {//初始化入度数组
	for (int index = 1; index <= G.vexNum; index++) {//依次访问图中每条链
		ArcNode* pnode = G.vertices[index].firstArc;
		while (pnode != NULL) {
			VertexType v = pnode->adjvex;
			indegree[v] += 1;//入度加1
			pnode = pnode->nextArc;
		}
	}
}
//拓扑序列
/*算法思想:将图中入度为0的结点加入队列中;当队列不为空时,输出队头结点,将其加入拓扑序列中,并更新结点入度数组,并将入队为0的结点加入队列中;
重复上述步骤,直到队列为空*/
bool TopuSort(ALGraph G) {
	LinkQueue Q;
	InitQueue(Q);
	for (int i = 1; i <= G.vexNum; i++) {
		if (indegree[i] == 0) {
			EnQueue(Q, i);
		}
	}
	while (!IsEmpty(Q)) {
		VertexType v;
		DeQueue(Q, v);
		print[printLength++] = v;
		ArcNode* pnode = G.vertices[v].firstArc;
		while (pnode != NULL) {
			VertexType w = pnode->adjvex;
			indegree[w] -= 1;
			if (indegree[w] == 0) {
				EnQueue(Q, w);
			}
			pnode = pnode->nextArc;
		}
	}
	if (printLength == G.vexNum) {
		return true;
	}
	else {
		return false;
	}
}
int main() {
	ALGraph G;
	int arcList[MAX_VERTEX_NUM][2], vexList[MAX_VERTEX_NUM];
	int arcNum, vexNum, kind;
	scanf("%d", &vexNum);
	for (int i = 1; i <= vexNum; i++) {
		scanf("%d", &vexList[i]);
	}
	scanf("%d", &arcNum);
	for (int i = 0; i < arcNum; i++) {
		for (int j = 0; j < 2; j++) {
			scanf("%d", &arcList[i][j]);
		}
	}
	scanf("%d", &kind);
	createALGraph(G,vexList, vexNum, arcList, arcNum, kind);
	initialIndegree(G,indegree);
	if (TopuSort(G)) {
		for (int i = 0; i < printLength; i++) {
			printf("%d ", print[i]);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值