对于有向无环图G=(V,E)进行拓扑排序后,结果为该图所有顶点的线性序列,如果G包含边(u,v),那么在排序后的序列中,u就一定出现在v的前面。
所有,一个图的拓扑排序可以看成是图中所有顶点沿着水平线排列而成的一个序列,使得所有有向边均从左指向右。
DAG的拓扑排序主要依赖dfs来实现:
/*
file:topligic_sort.c
brief:使用dfs对有向无环图进行拓扑排序,先用dfs计算出所有顶点的finish time,
每个顶点结束之后,将它插入一个链表的前面
version;1.0 yejing@2014.09.01
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NO_WEIGHT 0x7fffffff
#define INFINITY 0x7fffffff
int time = 0;//时间戳
typedef enum{
WHITE,
GRAY,
BLACK
}color_t;
typedef struct __graph{
char* vex_vector;//顶点向量
int* color;//颜色
int* parent;//父节点
int* deepth;//节点被发现时的深度,bfs使用
int* disc_tm;//节点被发现的时间
int* fini_tm;//节点被搜索完成的时间
int arcs[255][255];
int vexnum, arcnum;
}graph_t;
typedef struct __list{
int vex_index;
struct __list next;
}list_t;
list_t* list = NULL;
l