拓扑排序

使用链式前向星储存边,代码如下:

 

//先将图中没有前驱(即入度为0)的顶点加入队列

For i:=1 to n do if d[i]=0 then

Begin

  Inc(tot); q[tot]:=i;

End;

//使用队列中的点更新d数组并生成拓扑序列

Iq:=0;

While iq<tot do

Begin

  Inc(iq);

  K:=head[iq];

While k<>-1 do

Begin

  Dec(d[e[k].t]);

  If d[e[k].t]=0 then  //入度为0,加入序列

  Begin

    Inc(tot); q[tot]:=e[k].t;

  End;

  K:=e[k].next;

  End;

End;

//输出拓扑序列

For i:=1 to tot do write(q[i],’ ‘);

 

posted @ 2015-05-05 12:48  no regrets  阅读(79)  评论(0编辑  收藏  举报