finale Version des Vorlesungsskripts - ZIB

finale Version des Vorlesungsskripts - ZIB finale Version des Vorlesungsskripts - ZIB

23.06.2013 Aufrufe

inp, { input file } outp : text; { output file } BEGIN {MAIN PROGRAM} {====== Input of complete graph =====} reset(inp); rewrite(outp); {- number of nodes -} writeln(outp,’Enter number of nodes:’); read(inp,n); IF (nmax_n) THEN BEGIN writeln(outp,’Number of nodes too large or not positive!’); HALT; END; {- initialize dope vector -} nchoose2 := (n * (n-1)) DIV 2; FOR i:=1 TO nchoose2 DO w[i] := inf; dope[1] := -1; FOR i:=2 TO n DO dope[i] := dope[i-1] + n - i; {- input mode -} writeln(outp,’Enter input mode:’); read(inp,mode); {- edge weights -} CASE mode OF 0 : {* full matrix *} BEGIN FOR i:=1 TO n DO FOR j:=1 TO n DO BEGIN read(inp, c); IF i0) DO BEGIN IF (in) OR (jn) THEN BEGIN writeln(outp,’Input error, node out of range!’); HALT; END; IF i

5 Bäume und Wege THEN w[dope[i]+j] := c ELSE w[dope[j]+i] := c; read(inp, i, j, c); END; END; ELSE: {* invalid mode *} BEGIN writeln(outp,’Invalid input mode!’); HALT; END; END;{OF CASE} {====== Initialization ======} connected := true; outnodes := n-1; weight := 0; FOR i:=1 TO outnodes DO BEGIN in_t[i] := 1; out_t[i] := i+1; dist[i] := w[i]; END; {====== Prim’s Algorithm ======} WHILE (outnodes > 1) AND connected DO BEGIN {- determine entering node -} min := inf; ind := 0; FOR i:=1 TO outnodes DO IF dist[i] < min THEN BEGIN min := dist[i]; ind := i; END; IF ind = 0 THEN connected := false ELSE BEGIN {- augment tree -} weight := weight + min; newnode := out_t[ind]; t1 := in_t[ind]; t2 := out_t[ind]; c := dist[ind]; in_t[ind] := in_t[outnodes]; out_t[ind] := out_t[outnodes]; dist[ind] := dist[outnodes]; in_t[outnodes] := t1; out_t[outnodes] := t2; dist[outnodes] := c; outnodes := outnodes - 1; {- update dist[] and in_t[] -} FOR i:=1 TO outnodes DO BEGIN IF newnode < out_t[i] THEN c := w[dope[newnode]+out_t[i]] ELSE c := w[dope[out_t[i]]+newnode]; IF c < dist[i] THEN BEGIN in_t[i] := newnode; dist[i] := c; END; END; END; END; {- insert the last edge -} 88

5 Bäume und Wege<br />

THEN w[dope[i]+j] := c<br />

ELSE w[dope[j]+i] := c;<br />

read(inp, i, j, c);<br />

END;<br />

END;<br />

ELSE: {* invalid mode *}<br />

BEGIN<br />

writeln(outp,’Invalid input mode!’);<br />

HALT;<br />

END;<br />

END;{OF CASE}<br />

{====== Initialization ======}<br />

connected := true;<br />

outno<strong>des</strong> := n-1;<br />

weight := 0;<br />

FOR i:=1 TO outno<strong>des</strong> DO<br />

BEGIN<br />

in_t[i] := 1;<br />

out_t[i] := i+1;<br />

dist[i] := w[i];<br />

END;<br />

{====== Prim’s Algorithm ======}<br />

WHILE (outno<strong>des</strong> > 1) AND connected DO<br />

BEGIN<br />

{- determine entering node -}<br />

min := inf;<br />

ind := 0;<br />

FOR i:=1 TO outno<strong>des</strong> DO<br />

IF dist[i] < min<br />

THEN BEGIN<br />

min := dist[i];<br />

ind := i;<br />

END;<br />

IF ind = 0<br />

THEN connected := false<br />

ELSE BEGIN<br />

{- augment tree -}<br />

weight := weight + min;<br />

newnode := out_t[ind];<br />

t1 := in_t[ind];<br />

t2 := out_t[ind];<br />

c := dist[ind];<br />

in_t[ind] := in_t[outno<strong>des</strong>];<br />

out_t[ind] := out_t[outno<strong>des</strong>];<br />

dist[ind] := dist[outno<strong>des</strong>];<br />

in_t[outno<strong>des</strong>] := t1;<br />

out_t[outno<strong>des</strong>] := t2;<br />

dist[outno<strong>des</strong>] := c;<br />

outno<strong>des</strong> := outno<strong>des</strong> - 1;<br />

{- update dist[] and in_t[] -}<br />

FOR i:=1 TO outno<strong>des</strong> DO<br />

BEGIN<br />

IF newnode < out_t[i]<br />

THEN c := w[dope[newnode]+out_t[i]]<br />

ELSE c := w[dope[out_t[i]]+newnode];<br />

IF c < dist[i]<br />

THEN BEGIN<br />

in_t[i] := newnode;<br />

dist[i] := c;<br />

END;<br />

END;<br />

END;<br />

END;<br />

{- insert the last edge -}<br />

88

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!