定义:根据某个集合上定义的一个偏序求出该集合上的全序
偏序:
若集合X上的关系R是自反的,反对称的和传递的,称R是集合X上的偏序
全序:
设R是集合X上的偏序关系,如果对于每个 x,y∈X 必有 xRy 或 yRx,则称R是集合X上的全序关系
通俗来讲,拓扑排序就是将集合上的元素按某种关系进行排序
AOV网的拓扑排序算法流程
- 在AOV网中选取一个没有前驱的顶点v开始遍历
- 输出顶点v
- 删除顶点v和所有以v为弧尾的弧
- 重复1~3步,直到
1)AOV网中全部顶点都已输出(得到拓扑有序序列)
2)或者图中再无没有前驱的顶点(AOV网中有环)
例子:
代码:
///* 边节点类型 */
//typedef struct ANode{
// int adjvex; //该边的邻接点编号
// struct ANode * nextarc; //指向下一条边的指针
// int weight; //该边的权值
//}ArcNode;
//
///* 头节点类型 */
//typedef struct{
// Vertex data; //顶点信息
// int count; //增加数据域:存放顶点入度
// ArcNode* firstarc; //指向第一个邻接点
//}VNode;
//
///* 完整的图邻接表类型 */
//typedef struct {
// VNode adjlist[MAXV]; //邻接表的头节点数组
// int n,e; //图中的顶点数n和边数e
//}AdjGraph;
//
//
//void TopSort(AdjGraph *G){
// int i,j;
// int St[MAXV],top = -1;
// ArcNode *p;
// for (i=0;i<G->n;i++) G->adjlist[i].count=0;
// for (i=0;i<G->n;i++){
// p = G -> adjlist[i].firstarc;
// while (p!=NULL){
// G->adjlist[p->adjvex].count++;
// p = p->nextarc;
// }
// }
// for(i=0;i<G->n;i++)
// if(G->adjlist[i].count==0){
// top++;
// St[top] = i;
// }
// while (top>-1){
// i = St[top];top--;
// p
// }
//以后再更吧}