算法思想:将图中入度为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;
}