program nn;                        (* Nearest Neighbour, Thomas Klein  1999 *)
uses graph,tsp;
var anfang:stadtnr;

procedure nneighbour(anfang:stadtnr;var w:weg);
 (* Nearest Neighbour mit Startpunkt anfang *)
 var n,i,j:stadtnr;
     min,l:entfernung;
 begin
  n:=anfang; w:=chr(n);
  while length(w)<stadtanzahl do begin
   min:=maxentfernung;
   for i:=1 to stadtanzahl do
    if pos(chr(i),w)=0 then begin
     l:=entftab[n,i]; if l<min then begin j:=i; min:=l; end;
    end;
   n:=j; w:=w+chr(n);
  end;
  w:=w+chr(anfang);
 end;

begin
 if tspinit('TSP: Nearest Neighbour','Berechnen') then
  while tspmenue([0..255]) do begin
   meldung('Startpunkt w„hlen!',green,white);
   anfang:=stadtwahl(255);
   start; nneighbour(anfang,aktuell); stop;
   wegkarte(aktuell,true,brown,yellow);
   laenge_aus(aktuell); zeit_aus(true);
  end;
 closegraph;
end.
