对于有向无环图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;
list_t* creat_list(int value){
list_t* list_head = (list_t*)malloc(sizeof(list_t));
if(!list_head){
printf("malloc error errno:%d \n", errno

拓扑排序是对有向无环图(DAG)的顶点进行排序,使得所有边(u, v)满足u在v之前。这个过程可以通过深度优先搜索(DFS)来实现,最终得到的序列中,所有节点沿着水平线从左到右排列,符合有向边的方向。"
90440897,7516738,使用JavaScript判断经纬度点位在陆地或海洋,"['JavaScript', 'node.js']
最低0.47元/天 解锁文章
989

被折叠的 条评论
为什么被折叠?



