//---拓扑排序算法---
//拓扑序列是指顶点活动网(AOV-网)中将活动(即顶点)按发生的先后次序(即从弧尾顶点弧头顶点)进行的一种排列
//1.在有向图中选一个没有前驱的顶点且输出之;
//2.从图中删除顶点和所有以它为尾的弧;
//3.重复上述两步,如果全部顶点已输出,则完成,如果最后图中不存在无前驱的顶点,则图中存在环。
#include <stdio.h>
#include <stdlib.h>
#include "AdjacencyList.h"
typedef enum{FALSE,TRUE}Boolean;
typedef int Status;
//栈结构和方法
typedef struct
{
int st[MAX_VERTEX_NUM];
int top;
}Stack;
void StackPush(Stack *S,int u);
int StackPop(Stack *S);
Status TopologicalSort(ALGraph G);
void FindInDegree(ALGraph G,int *indegree);
int main()
{
ALGraph *G;
G=(ALGraph *)malloc(sizeof(ALGraph));
CreateGraph(G);
TopologicalSort(*G);
return 0;
}
Status TopologicalSort(ALGraph G)
{
int i,count,*indegree; //indegree数组表示各个顶点的入度,索引表示顶点,值表示入度
indegree=(int *)malloc(sizeof(int)*G.vexnum);
for(i=0;i<G.vexnum;i++)
indegree[i]=0;
FindIn
有向无环图的拓扑排序
最新推荐文章于 2023-09-04 15:59:19 发布