Thuattoan Prim
Thuattoan Prim
Prim_Algorithm;
uses dos,crt;
const max=150;
type
filename=string[12];
var
TrongSo: array[1..max,1..max] of integer;
DinhKe: array[1..max] of integer;
CanhKe: array[1..max] of integer;{}
n, DodaiCayKhung: integer;
i,j: integer;
fname: filename;
ch: char;
h,m,s, hund:word;
procedure PrintMatrix;
Begin
(*In ma tran ra *)
Writeln('-------------------------------------------------------');
Writeln('Ma tran trong so, Khong co canh noi thi trong so = MaxInt');
for i:=1 to n do
Begin
For j:=1 to n do
if TrongSo[i,j]=Maxint then write(' 0',' ')
else write(TrongSo[i,j]:5,' ');
Writeln;
End;
Writeln('-------------------------------------------------------');
End;
procedure
ReadInputFile(fname: filename);
var
i,j: integer;
f: text;
begin
assign(f,fname);
reset(f);
readln(f,n);
for i:=1 to n do
begin
for j:=1 to n do
begin
read(f,TrongSo[i,j]);
if TrongSo[i,j]=0 then TrongSo[i,j]:=MaxInt;
end;
readln(f);
end;
close(f);
PrintMatrix;
end;
procedure Prim;
var
v,i,k: integer;
min: integer;
begin
for v:=2 to n do
begin
DinhKe[v]:=1;
CanhKe[v]:=TrongSo[1,v];
end;
for i:=2 to n do
begin
min:=MaxInt;
for k:=2 to n do
if (CanhKe[k]>=0) and (CanhKe[k]<min) then
begin
min:=CanhKe[k]; (
v:=k;
end;
CanhKe[v]:=-1;
for k:=2 to n do
if (TrongSo[v,k]<CanhKe[k]) and (TrongSo[v,k]>0) then
begin
CanhKe[k]:=TrongSo[v,k];
DinhKe[k]:=v;
end;
end;
end;
procedure
WriteOutputFile;
var
i: integer;
f: text;
begin
assign(f,'prim.out');
rewrite(f);
Writeln(f,'-------------------------------------------------------');
Writeln(f,'Ma tran trong so, Khong co canh noi thi trong so = MaxInt');
for i:=1 to n do
Begin
For j:=1 to n do
if TrongSo[i,j]=Maxint then write(f,' 0',' ')
else write(f,TrongSo[i,j]:5,' ');
Writeln(f,);
End;
Writeln(f,'-------------------------------------------------------');
DodaiCayKhung:=0;
writeln(f,'Cay khung nho nhat gom cac canh:');
for i:=2 to n do
begin
write(f,'(',i,',',DinhKe[i],') ');
DodaiCayKhung:=DodaiCayKhung+TrongSo[i,DinhKe[i]];
end;
writeln(f);
writeln(f,'Length: ',DodaiCayKhung);
close(f);
end;
begin
repeat
clrscr;
writeln('THUAT TOAN PRIM TIM CAY KHUNG NHO NHAT');
writeln(' DUNG MA TRAN KE');
writeln('---------------------------------------');
writeln(' Hay bam phim chuc nang:');
Writeln;
writeln(' K(eyboard). Chay chuong trinh - Du lieu nhap tu ban phim.');
Writeln;
writeln(' F(ile). Chay chuong trinh - Du lieu lay tu File.');
Writeln;
writeln(' R(andom). Chay chuong trinh - Du lieu Random.');
Writeln;
writeln(' Q(uit). Thoat khoi chuong trinh.');
Writeln;
ch:=ReadKey;
case UpCase(ch) of
'F':
begin
write(' Hay nhap ten tep du lieu:'); readln(fname);
ReadInputFile(fname);
gettime(h,m,s,hund);
Writeln('Bat dau chay: ',h,':',m,':',s,':',hund);
Prim;
gettime(h,m,s,hund);
Writeln('Ket thuc chay: ',h,':',m,':',s,':',hund);
WriteOutputFile;
write('Cac canh cua cay khung be nhat:');
for i:=2 to n do write('(',i,',',DinhKe[i],')');
writeln;
writeln('Gia cua cay khung : ',DodaiCayKhung);
writeln('***************************************');
writeln('Nhan phim Enter de tiep tuc.');
readln;
end;
'R':
begin
write('Hay nhap so dinh cua do thi:'); readln(n);
for i:=1 to n do TrongSo[i,i]:=0;
for i:=1 to n-1 do
For j:=i+1 to n do TrongSo[i,j]:=random(100);
for i:=2 to n do
For j:=1 to i-1 do TrongSo[i,j]:=TrongSo[j,i];
PrintMatrix;
for i:=1 to n do
for j:=1 to n do
if TrongSo[i,j]=0 then TrongSo[i,j]:=MaxInt;
gettime(h,m,s,hund);
Writeln('Bat dau chay: ',h,':',m,':',s,':',hund);
Prim;
gettime(h,m,s,hund);
Writeln('Ket thuc chay: ',h,':',m,':',s,':',hund);
write('Cac canh cua cay khung be nhat:');
for i:=2 to n do write('(',i,',',DinhKe[i],')');
WriteOutputFile;
Writeln;
writeln('Gia cua cay khung : ',DodaiCayKhung);
writeln;
writeln('***************************************');
writeln('Nhan phim Enter de tiep tuc.');
readln;
end;
'K':
begin
write('Hay nhap so dinh cua do thi:'); readln(n);
for i:=1 to n do TrongSo[i,i]:=0;
for i:=1 to n-1 do
For j:=i+1 to n do
Begin
Write('a[',i,j,']=');
readln(TrongSo[i,j]); (* Nua tren *)
End;
for i:=2 to n do
For j:=1 to i-1 do TrongSo[i,j]:=TrongSo[j,i];
PrintMatrix;
for i:=1 to n do
for j:=1 to n do
if TrongSo[i,j]=0 then TrongSo[i,j]:=MaxInt;
gettime(h,m,s,hund);
Writeln('Bat dau chay: ',h,':',m,':',s,':',hund);
Prim;
gettime(h,m,s,hund);
Writeln('Ket thuc chay: ',h,':',m,':',s,':',hund);
write('Cac canh cua cay khung be nhat:');
for i:=2 to n do write('(',i,',',DinhKe[i],')');
WriteOutputFile;
writeln;
writeln('Gia cua cay khung : ',DodaiCayKhung);
writeln('***************************************');
writeln('Nhan phim Enter de tiep tuc.');
readln;
end;
end;
until
UpCase(ch)='Q';
end.