当前位置:文档之家› 拓扑排序、关键路径分析

拓扑排序、关键路径分析

assig n(i nput,'topsort.i n');reset(i nput);
assign(output,'topsort.out');rewrite(output); read(n); //读入顶点数量for i:=1 to n do for j:=1 to n do begi n
read(map[i,j]); //读入邻接矩阵(i,j关系)
if map[i,j]=1 then inc(into[j]); // j 入度为1 则j 点入度数量into[j]累加1 end; begin in it; // 读入数据并初始化for i:=1 to n do begi n j:=1;
while (j<=n)and(into[j]<>0) do inc(j); // 查找第一个入度为0 的点j write(j,' '); // 输出j into[j]:=255; //入度不再为0,而是255,作为已经输出标志for k:=1 to n do
if map[j,k]=1 then dec(into[k]); //把所有以j为前驱的点的入度减去1,即删除这条边end;
close(output);第1 页共4 页
2、关键路径的算法
GJLJ.OUT
Wi沪
14 3 2
program gjlj; const maxn=10; var
map:array[1..maxn,1..maxn] of integer; /〃己录邻接矩阵a:array[1..maxn] of 0..maxn; // 记录拓扑排序后的编号
b:array[1..maxn] of integer; 〃b[i]表示起点至U i 点的最长距离c:array[1..maxn] of 0..maxn; //c[i]表示点i 的前一个编号n:integer;
procedure in it; var i,j:i nteger; beg in
read ln(n);
for i:=1 to n do for j:=1 to n do
read(map[i,j]); //读入邻接矩阵fillchar(a,sizeof(a),0); fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0); end;
function tporder:boolean; //拓扌卜排序,成功则返回true var i,j,k:integer;
in to:array[1..max n] of byte; beg in
tporder:=false;
fillchar(into,sizeof(into),0); for i:=1 to n do for j:=1 to n do
if map[i,j]>0 then inc(into[j]); // 计算各点的入度for i:=1 to n do begin
GJLJJN
5
0 00 23
0 0 0 00
0 6 0 0 0
0 4 7 0 0
005 00。

相关主题