Algoritma Program Pascal untuk Lintasan Terpendek pada Matematika Diskrit
program lintasan_terpendek;
uses wincrt;
var
i, j, k, n, y, z, cek, jarak: integer;
v : array [1..20,1..20] of integer;
label 1, 2, 3;
Begin
1:
{Input}
writeln('Program Pencarian Lintasan Terpendek');
write('Masukkan besar matriks adjacency (n x n): ');readln(n);
for i:= 1 to n do
begin
for j:= 1 to n do
begin
if j <> i then
begin
writeln('Masukkan 0 jika tidak hubungan antar vertex.');
write('V',i,'V',j,' = ');readln(v[i,j]);
clrscr;
end;
end;
end;
2:
{Cetak Matriks Adjacency}
writeln(' V1 V2 V3 V4 V5 V6 V7');
for i:= 1 to n do
begin
write('V',i,'|');
for j:= 1 to n do
begin
if v[i,j-1] < 10 then write(' ',v[i,j]);
if v[i,j-1] >= 10 then write(' ',v[i,j]);
end;
writeln(' |');
end;
if cek = 0 then
begin
writeln;
writeln('Benarkan ini matriks adjacency yang anda inginkan?');
writeln('1. Ya 2. Tidak');readln(k);
end;
if cek = 0 then
begin
{Jik dipilih ya, maka proses cetak matriks diulangi}
if k = 1 then
begin
cek:= 1;
clrscr;
goto 2;
end;
{Jika dipilih tidak, maka proses input akan diulangi}
if k = 2 then
begin
for i:= 1 to n do
begin
for j:= 1 to n do
begin
v[i,j]:= 0;
end;
end;
clrscr;
goto 1;
end;
end;
writeln;
write('Masukkan vertex awal : V');readln(z);
write('Masukkan vertex akhir: V');readln(y);
writeln;
writeln('Jadi jarak terpendek dari V',z,' ke V',y,' adalah ');
repeat
write('V',z);
k:= 10000;
for i:= 1 to n do
begin
if z <> i then
begin
if v[z,i] <> 0 then
begin
if v[z,i] < k then
begin
k:= v[z,i];
j:= i;
end;
end;
end;
end;
jarak:= jarak + k;
z:= j;
until y = z;
write('V',z,' = ',jarak);
End.
Preview Program:
Tidak ada komentar:
Posting Komentar