23.06.2013 Aufrufe

aktuelle Version des Vorlesungsskripts - ZIB

aktuelle Version des Vorlesungsskripts - ZIB

aktuelle Version des Vorlesungsskripts - ZIB

MEHR ANZEIGEN
WENIGER ANZEIGEN

Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.

YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.

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!