Vous êtes sur la page 1sur 3

unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, StdCtrls;

var i,j,k,l:integer;//Global variables 1 input:String; //Input degerlerim iin num_edges:integer; num_nodes:integer; size_of_T:integer; type edge_t=record //structured type 2 r,s:Integer; c:real; end; Var G:Array of edge_t; //global Arrays 3 T:array of edge_t; D: array of integer; //Quicksort 10 type { TForm1 } TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; procedure Button1Click(Sender: TObject); procedure MenuItem1Click(Sender: TObject); private { private declarations } public { public declarations } end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.MenuItem1Click(Sender: TObject); begin end; procedure quickSort(var a : array of edge_t; left, right : integer); //quick sort 8, 9, 10

var i,j:Integer; var t,v:edge_t; begin //Quicksort 9 if left < right then begin v := a[right]; i := left - 1; j := right; repeat repeat i := i + 1; until a[i].c >= v.c; repeat j := j - 1; until a[j].c <= v.c; t := a[i]; a[i] := a[j]; a[j] := t; until j <= i; a[j] := a[i]; a[i] := a[right]; a[right] := t; quickSort(a, left, i-1); quickSort(a, i+1, right); end; end; procedure TForm1.Button1Click(Sender: TObject); var gec:integer; Str:string; begin num_edges:=0; //initilization 4 num_nodes:=0; size_of_T:=1; SetLength(T,size_of_T); SetLength(G,num_edges); Edit1.Caption:=''; repeat Label1.Caption:='Enter # of edges (0>):'+FloatToStr(i); //global input 5 input:=InputBox('Get some text input','Please type in some information', 'Some s ample text'); Val(input,num_edges); Label1.Caption:=''; until num_edges>0; setlength (G, num_edges + 1); for i := 1 to num_edges do begin Edit1.Caption:=''; repeat Label1.Caption:='index (>0) of r node for edge '+FloatToStr(i)+ ' ? '; input:=InputBox('Get some text input','Please type in some information', 'Some s ample text'); val (input, G[i].r); until G[i].r > 0; if G[i].r > num_nodes then num_nodes := G[i].r; Edit1.Caption:=''; repeat Label1.Caption:='index (>0) of s node for edge '+FloatToStr(i) +' ? '; input:=InputBox('Get some text input','Please type in some information', 'Some s

ample text'); val (input, G[i].s); until G[i].s > 0; if G[i].s > num_nodes then num_nodes := G[i].s; Edit1.Caption:=''; repeat Label1.Caption:='weight (>0) of edge '+FloatToStr(i)+ '? '; input:=InputBox('Get some text input','Please type in some information', 'Some s ample text'); val (input, G[i].c); until G[i].c > 0; end; quickSort (G, 1, num_edges); //edge sorting by weight 11 setlength (D, num_nodes + 1);//initialize 12 for i := 1 to num_nodes do D[i] := i; //kruskal Alg 13 for i := 1 to num_edges do begin if D[G[i].r] <> D[G[i].s] then begin size_of_T := size_of_T + 1; setlength (T, size_of_T); T[size_of_T - 1] := G[i]; k := D[G[i].r]; l := D[G[i].s]; for j := 1 to num_nodes do if D[j] = l then D[j] := k; end; end; //global output 6 Label2.Caption:= 'num_edges = '+FloatToStr( num_edges)+ ', num_nodes = '+FloatTo Str(num_nodes); for i := 1 to num_edges do Label2.Caption:=Label2.Caption+ 'weight of edge ('+FloatToStr( G[i].r)+ ', '+Flo atToStr( G[i].s)+ ') = '+FloatToStr(i); if size_of_T > 1 then //mst output 7 begin Label3.Caption:='The minimum spanning tree is found:'; for i := 1 to size_of_T - 1 do Label4.Caption:=('weight of edge ('+FloatToStr(T[i].r)+ ', '+FloatToStr(T[i].s)+ ') = '+ FloatToStr(i)); end else begin Label2.Caption:='The minimum spanning tree does not exist.'; end; end; end.