• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

[help]java programming

BIGphAntom

Junior Member
I need someone to "translate" the following algorithm into JAVA, I`m a newbie but i need this to be translated and working in JAVA

Anyone can help ?




Danzig Algorithm(to determine the road of minimal length in a graph )

Let it be D=(V,A)={v1,v2,...,vn} a graph; I wish to calculate the minimal lengths from the point v1 to the other points: v2 ... vn.

Procedure DANZIG;
var

l: array[dom,dom] of byte;
q,dv,dw,i,j,min,ind,s1,s2,dd,di: byte;
t,vs, ws, dist, drum, index : array (sir);
c: char;


function dosnotbelong(a:byte) :boolean;forward;

function nearest (a:byte) :byte;
var
k,minaux:byte;
begin
minaux:=255; nearest:=0;
for k:=1 to nr_points do
if ma[a,k]=1 then
if(l[a,k]<=minaux) and not (doesnotbelong(k)) then
begin
minaux:=l[a,k]; nearest:=k, min:= minaux;
end;

procedure minim (dist:array; var min:byte; var ind:byte);
var
k:byte;
begin
min:=255; ind:=1;
for k:=1 to dv do

if (dist[k]<=min) then
if (dist[k]>0) then
begin
min:=dist[k]; ind:=k;
end;
end;

procedure distante (var dist:array);
var
k:=byte;
begin
for k:=1 to dv do
if ws[k]>0 then
dist[k]:=t[vs[k]+l[vs[k],ws[k]]
else dist[k]:=0;
end;


function doesnotbelong(a:byte):boolean;
var
bol:boolean;
k;byte;
begin
bol:=false;
for k:=1 to dv do
if a=vs[k] then bol:=true;
doesnotbelong:=bol;
end;

procedure write(fin:byte; dist:array; dim:byte);
var
k:byte;
begin
dw:=1; dist[1]:fin; k:=fin;
repeat
inc(dw); k:=index[k]; dist[dw]:=k;
until index[k]=0;
for k:=dw downto 1 do
write(dist[k], '');
end;
procedure Dant(nr_points:byte; var t:array);
var i,j:byte;
l:array[dom,dom] of longint; {BK}
begin
dv:=1; dw:=1; vs[dv]:=1; t[1]:=0;
for i:=1 to nr_points-1 do
begin
for j:=1 to dv do ws[j]:=nearest(vs[j]);
distante(dist);
minim(dist,min,ind);
inc(dv);vs[dv]:=ws[ind];
t[ws[ind]:=min; index[ws[ind]]:=vs[ind];
end;
end;

procedure read_data;
var
a1,a2,i,j:byte;
begin
writeln('type the distances: ');
for i:=1 to nr_points do
for j:=1 to nr_points do
if ma[i,j]=1 then
begin
repeat
write('Distance from ',i,'to ',j,' is: ');
readln(l[i,j]);
if l[i,j]<=0 then
begin
write ('INCORECT LENGHT! ');
end;
until l[i,j]>0
end;
end;

begin

read_data;
Dant(nr_points,t);
for i:=2 to nr_points do
begin
if t=0 then
begin
writeln('There is no road from 1 to ',i)
end
else
begin
writeln('The distance from 1 to ',i,' is: ', t);
writeln('The road from ',1,' to ',i,' is: ');
write(i,dist,dw);
end;
end;

repeat until keypressed;
end;

 
As has been said before. You are more likely to get help if you ask specific questions rather than asking someone to do your work for you.
 
Hmm...looks like pascal code. It doesn't look too difficult, it even looks quite similar to java code already.

So, here's my advice:

Step 1. Learn Java
Step 2. Translate code

I mean...you expect somebody here to just do it for you?
rolleye.gif
 
Back
Top