<html>
<head><title>Dijkstra Algorithms </title>
</head>
<body>
<script language = "javascript">
var NilaiAcuan = 10000;
var TakTerdefinisi = -1;
var NamaVerteks = new Array('A','B','C','D','E','F');
var Matriks = new Array(6);
function Bobot(a,b)
{
return Matriks[a][b];
}
function Dijkstra(JumlahVerteks,Awal,d)
{
var Posisi = new Array(JumlahVerteks);
var i;
var Kunjungan = new Array(JumlahVerteks);
var pred = new Array(JumlahVerteks);
for(i=0; i<JumlahVerteks; i++)
{
Posisi[i] = NilaiAcuan;
pred[i] = TakTerdefinisi;
Kunjungan[i] = false;
}
Posisi[Awal] = 0;
var Verteks;
for (Verteks = 0; Verteks < JumlahVerteks; Verteks++)
{
var JarakTerpendek = NilaiAcuan, closest = -1;
for (i=0; i < JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
if (Posisi[i] <= JarakTerpendek)
{
JarakTerpendek = Posisi[i]; closest = i;
}
}
}
Kunjungan[closest] = true
for (i=0; i < JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
var w = Bobot(closest, i);
if (Posisi[closest]+w < Posisi[i])
{
Posisi[i] = Posisi[closest] + w;
pred[i] = closest;
}
}
}
}
i=d
if (Posisi[i] < NilaiAcuan)
{
var Lintasan = NamaVerteks[i];
var Verteks = i;
while (Verteks>0)
{
Verteks = pred[Verteks];
if (Verteks>=0) Lintasan = NamaVerteks[Verteks] + '->' + Lintasan;
}
alert("Jarak :" + Posisi[i]+' ('+Lintasan+')'+'<br>');
} else
{
alert("Tidak ada Jalur");
}
}
var x = '~';
document.write('<pre>');
document.write(Matriks[0]=new Array(0,2,3,x,x,x));
document.write('<br>');
document.write(Matriks[1]=new Array(2,0,3,6,x,x));
document.write('<br>');
document.write(Matriks[2]=new Array(3,3,0,3,5,x));
document.write('<br>');
document.write(Matriks[3]=new Array(x,6,3,0,1,3));
document.write('<br>');
document.write(Matriks[4]=new Array(x,x,5,1,0,1));
document.write('<br>');
document.write(Matriks[5]=new Array(x,x,x,3,1,0));
document.write('<br>');
document.write('</pre>');
document.write('<pre>A-2-B-6--D--3-F <br>');
document.write('\\ | /| / <br>');
document.write(' 3 3 3 1 1 <br>');
document.write(' \\ | / | / <br>');
document.write(' \\|/ |/ <br>');
document.write(' C--5-E <br> </pre>');
var Dari=0; // A
var Ke =5; // F
Dijkstra(Matriks.length,Dari,Ke);
//-->
</script>
</body>
</htmL>
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar