1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
| usingSystem;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespaceРазбиение_графа
{
publicpartialclassForm1 : Form
{
int m = 16;
int l = 4;
int n = 4;
List<List<TextBox>> ListTextBox;
Dictionary<int, Dictionary<int, int>> Matrix;
Dictionary<int, Dictionary<int, int>> CopyMatrix;
List<List<int>> G;
Dictionary<int, int> LocalStep;
List<int> RelativeW;
List<int> Incidents;
List<int> TempG;
List<int> Q;
List<int> IncidentsBlocks = newList<int>();
MatrixForm MatrixShow;
publicbool flag = false;
public Form1()
{
InitializeComponent();
}
publicvoid ReadMatrix()
{
FileStream file = newFileStream("Matrix.txt", FileMode.Open, FileAccess.Read);
StreamReader str = newStreamReader(file);
string[] buf = newstring[1000];
int k = 0;
string s;
while ((s = str.ReadLine()) != null)
{
buf[k] = s;
k++;
}
string[] temp;
int L;
Dictionary<int, int> TempList = newDictionary<int, int>();
Matrix = newDictionary<int, Dictionary<int, int>>();
for (int i = 0; i < k; i++)
{
temp = buf[i].Split(' ');
L = temp.Length;
for (int j = 0; j < L; j++)
{
TempList.Add(j, (Convert.ToInt32(temp[j])));
}
Matrix.Add(i, TempList);
TempList = newDictionary<int, int>();
}
str.Close();
file.Close();
}
publicvoid DrawMatrixForInput()
{
ListTextBox = newList<List<TextBox>>();
List<TextBox> TempList=newList<TextBox>();
TextBox temptxtbox;
int k = 0;
for (int i = 0; i < m; i++)
{
for (int j = k; j < m; j++)
{
temptxtbox = newTextBox();
temptxtbox.Parent = this;
temptxtbox.Text = "0";
temptxtbox.Size = newSize(20, 10);
temptxtbox.Location = newPoint(30 + j * 20, 50 + i * 20);
temptxtbox.Show();
TempList.Add(temptxtbox);
}
ListTextBox.Add(TempList);
TempList = newList<TextBox>();
k++;
}
Label tempLabel;
for (int i = 0; i < m; i++)
{
tempLabel = newLabel();
tempLabel.Parent = this;
tempLabel.Text = Convert.ToString(i + 1);
tempLabel.Size = newSize(20, 20);
tempLabel.Location = newPoint(30 + i * 20, 35);
tempLabel.Show();
}
for (int i = 0; i < m; i++)
{
tempLabel = newLabel();
tempLabel.Parent = this;
tempLabel.Text = Convert.ToString(i + 1);
tempLabel.Size = newSize(20, 20);
tempLabel.Location = newPoint(15, 50 + i* 20);
tempLabel.Show();
}
}
publicvoid GraphPartition()
{
G = newList<List<int>>();
//Q = AddClosedVer();
int RightVer;
for (int i = 0; i < l - 1; i++)
{
TempG = newList<int>();
TempG.Add(Q[i]);
Incidents = newList<int>();
Incidents = AddIncidentsVer(TempG[0]);
LocalStep = newDictionary<int, int>();
LocalStep = AddLocalStep();
while (TempG.Count < n)
{
RelativeW = newList<int>();
for (int j = 0; j < Incidents.Count; j++)
{
RelativeW.Add(LocalStep[Incidents[j] - 1] - SearchA(Incidents[j] - 1, TempG));
}
if (RelativeW.Count == 0)
{
flag = true;
break;
}
RightVer = SearchRightVer(Incidents, RelativeW, LocalStep);
TempG.Add(RightVer);
Incidents.Remove(RightVer);
Incidents.AddRange(AddIncidentsVer(RightVer));
}
if (flag == true)
{
MessageBox.Show("Введеныневерныеданные","ошибка", MessageBoxButtons.OK);
break;
}
G.Add(TempG);
DeleteLine(TempG);
MatrixShow.Dispose();
MatrixShow = newMatrixForm(Matrix, AddLocalStep());
MatrixShow.Show();
MessageBox.Show("Кусок G" + Convert.ToString(i + 1) + " сформирован:\n{"
+ G[i][0] + ", " + G[i][1] + ", " + G[i][2] + ", " + G[i][3] + "}",
"Промежуточныевычисления",
MessageBoxButtons.OK);
}
TempG = newList<int>();
foreach (int i in Matrix.Keys)
TempG.Add(i + 1);
G.Add(TempG);
DeleteLine(TempG);
MessageBox.Show("Кусок G" + Convert.ToString(3 + 1) + " сформирован:\n{"
+ G[3][0] + ", " + G[3][1] + ", " + G[3][2] + ", " + G[3][3] + "}",
"Промежуточные вычисления",
MessageBoxButtons.OK);
}
publicList<int> AddClosedVer()
{
List<int> a = newList<int>();
a.Add(1);
a.Add(5);
a.Add(12);
a.Add(15);
return a;
}
publicList<int> AddIncidentsVer(int ver)
{
List<int> Temp = newList<int>();
foreach (int i in Matrix[ver - 1].Keys)
if (Matrix[ver - 1][i] > 0 && !Q.Contains(i + 1) && !Incidents.Contains(i + 1) && !TempG.Contains(i + 1))
Temp.Add(i + 1);
return Temp;
}
publicDictionary<int, int> AddLocalStep()
{
Dictionary<int, int> Temp = newDictionary<int, int>();
int S;
foreach (int i in Matrix.Keys)
{
S = 0;
foreach (int j in Matrix[i].Keys)
S += Matrix[i][j];
Temp.Add(i, S);
}
return Temp;
}
publicint SearchA(int ver, List<int> G)
{
int S = 0;
for (int i = 0; i < G.Count; i++)
S += Matrix[ver][G[i] - 1];
return S;
}
publicint SearchRightVer(List<int> I, List<int> R, Dictionary<int, int> LS)
{
int Min = R[0];
for (int i = 1; i < R.Count; i++)
if (Min > R[i])
Min = R[i];
List<int> Index = newList<int>();
for (int i = 0; i < R.Count; i++)
if (R[i] == Min)
Index.Add(i);
if (Index.Count == 1)
return I[Index[0]];
else
{
int Max = LS[I[Index[0]]-1];
int IndexMax = 0;
for (int i = 1; i < Index.Count; i++)
if (Max < LS[I[Index[i]]-1])
{
Max = LS[I[Index[0]]-1];
IndexMax = i;
}
return I[Index[IndexMax]];
}
}
publicvoid DeleteLine(List<int> TempG)
{
TempG.Sort();
for (int i = TempG.Count - 1; i >= 0; i--)
foreach (int j in Matrix.Keys)
Matrix[j].Remove(TempG[i] - 1);
for (int i = TempG.Count - 1; i >= 0; i--)
Matrix.Remove(TempG[i] - 1);
m -= n;
}
publicvoid SearchIncidentsblock()
{
IncidentsBlocks.Add(SearchIncidentsTwoBlock(G[0],G[3]));
IncidentsBlocks.Add(SearchIncidentsTwoBlock(G[1], G[2]));
IncidentsBlocks.Add(SearchIncidentsTwoBlock(G[0], G[1]));
IncidentsBlocks.Add(SearchIncidentsTwoBlock(G[1], G[3]));
IncidentsBlocks.Add(SearchIncidentsTwoBlock(G[3], G[2]));
IncidentsBlocks.Add(SearchIncidentsTwoBlock(G[2], G[0]));
}
publicint SearchIncidentsTwoBlock(List<int> A, List<int> B)
{
int s = 0;
for (int i = 0; i < A.Count; i++)
for (int j = 0; j < B.Count; j++)
s += CopyMatrix[A[i]-1][B[j]-1];
return s;
}
privatevoid hfpvToolStripMenuItem_Click(object sender, EventArgs e)
{
Dialog D = newDialog();
D.ShowDialog();
if (D.DialogResult == DialogResult.OK)
{
label1.Visible = false;
label2.Visible = false;
m = D.GetM();
l = D.GetL();
n = D.GetN();
Q = D.GetQ();
DrawMatrixForInput();
}
}
privatevoid button1_Click(object sender, EventArgs e)
{
Matrix = newDictionary<int, Dictionary<int, int>>(m);
for(int i=0;i<m;i++)
Matrix[i]=newDictionary<int,int>(m);
int t=m;
int b=0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < t; j++)
{
Matrix[i][j+b] = Convert.ToInt32(ListTextBox[i][j].Text);
Matrix[j+b][i] = Convert.ToInt32(ListTextBox[i][j].Text);
}
t--;
b++;
}
//ReadMatrix();
CopyMatrix = newDictionary<int, Dictionary<int, int>>(m);
for (int i = 0; i < m; i++)
CopyMatrix[i] = newDictionary<int, int>(m);
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
CopyMatrix[i][j] = Matrix[i][j];
Dictionary<int, int> LocStep = AddLocalStep();
MatrixShow = newMatrixForm(Matrix,LocStep);
MatrixShow.Show();
}
privatevoid button2_Click(object sender, EventArgs e)
{
GraphPartition();
if (flag == false)
{
SearchIncidentsblock();
Child ch = newChild(G,IncidentsBlocks);
ch.Show();
}
else
flag = false;
}
}
}
mo2: TMemo;
btn4: TBitBtn;
btn6: TBitBtn;
btn7: TBitBtn;
pm1: TPopupMenu;
N1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure stage1;
procedure stage2;
procedure stage3;
procedure stage4;
procedure stage5;
procedure btn4Click(Sender: TObject);
procedure btn5Click(Sender: TObject);
procedure btn6Click(Sender: TObject);
procedure strngrd1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
procedure btn7Click(Sender: TObject);
function start(turn:boolean):boolean;
procedure initmatrix();
procedure btn2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
graph:TGraph;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure SGCopyToCLP(SG: TStringGrid; CopySel: Boolean; CL: integer = -1;
RT: integer = -1; CR: integer = -1; RB: integer = -1);
var
i, j: Integer;
s: string;
begin
s := '';
with SG do
begin
if CopySel then
begin
CL := Selection.Left;
CR := Selection.Right;
RT := Selection.Top;
RB := Selection.Bottom;
end;
//при необходимости FixedRows и FixedCols можно заменить на 0
if (CL < FixedCols) or (CL > CR) or (CL >= ColCount) then
CL := FixedCols;
if (CR < FixedCols) or (CL > CR) or (CR >= ColCount) then
CR := ColCount - 1;
if (RT < FixedRows) or (RT > RB) or (RT >= RowCount) then
RT := FixedRows;
if (RB < FixedCols) or (RT > RB) or (RB >= RowCount) then
RB := RowCount - 1;
for i := RT to RB do
begin
for j := CL to CR do
begin
s := s + Cells[j, i];
if j < CR then
s := s + #9;
end;
s := s + #13#10;
end;
end;
Clipboard.AsText := s;
end;
function isUInt(str:string):Boolean;
var i:Integer;
begin
result:=True;
if Length(str)=0 then result:= False;
for i:=1 to Length(str) do
if Pos(str[i],'1234567890')=0 then begin
result:=False;
Break;
end;
end;
procedure TGraph.init;
begin
log := TStringList.Create;
end;
procedure TGraph.razbit(turn:boolean);
var
i0,i1,maxls,sum,minid:Integer;
fnd:Boolean;
tmpstr:string;
begin
repeat
teta:=False;
case state of
0: begin
i1:=0;
maxls:=-1;
//fnd max
for i0:= 0 to versh-1 do begin
if not indexs[i0] then Continue;
if ls[i0] > maxls then begin
maxls := ls[i0];
i1:=i0;
end;
end;
state := 1;
Inc(step);
itog[currk][0] := 1;
itog[currk][1] := i1;
log.Append('Шаг '+Inttostr(step)+ ': Выбран элемент с наибольшей локальной степенью х'
+inttostr(i1+1));
end;
1: begin
tmpstr:='x'+IntToStr(itog[currk][1]+1)+' ';
for i0:= 0 to versh-1 do begin
if not indexs[i0] then Continue;
if (i0<>itog[currk][1])and(msmezh[itog[currk][1]][i0] <> 0) then begin
Inc(itog[currk][0]);
itog[currk][itog[currk][0]] := i0;
tmpstr:=tmpstr+'x'+IntToStr(i0+1)+' ';
end;
end;
state := 2;
Inc(step);
log.Append('Шаг '+Inttostr(step)+ ': Сформирован массив смежных вершин состоящий из: '
+tmpstr);
end;
2: begin
if itog[currk][0] <> vershVKuske[currk] then begin
for i0:= 0 to versh-1 do begin
if not indexs[i0] then Continue;
sum:=0;
for i1 := 1 to itog[currk][0] do
if msmezh[i0][itog[currk][i1]] <> 0 then
//Inc(sum);
sum := sum + msmezh[i0][itog[currk][i1]];
alpha[i0] := sum;
end;
if itog[currk][0] < vershVKuske[currk] then begin//polu4ili men'she
minid:=-1;
for i0:= 0 to versh-1 do begin
if not indexs[i0] then Continue;
fnd := False;
for i1 := 1 to itog[currk][0] do
if (itog[currk][i1] = i0) then begin
fnd := True;
Break;
end;
if not fnd then begin
if (minid = -1) or ((ls[i0] - alpha[i0]) < (ls[minid] - alpha[minid]))
or (((ls[i0] - alpha[i0]) = (ls[minid] - alpha[minid]))
and (ls[i0] > ls[minid])) then
minid := i0;
end;
end;
Inc(itog[currk][0]);
itog[currk][itog[currk][0]] := minid;
log.Append('Шаг '+Inttostr(step+1)+ ': В массив смежных вершин добавлена вершина х'+
Inttostr(minid+1));
teta:=True;
end
else begin
minid:= itog[currk][2];
for i1 := 3 to itog[currk][0] do
if alpha[minid] > alpha[itog[currk][i1]] then
minid:=itog[currk][i1];
for i1 := 1 to itog[currk][0] do
if itog[currk][i1] = minid then begin
itog[currk][i1]:=itog[currk][itog[currk][0]];
end;
Dec(itog[currk][0]);
log.Append('Шаг '+Inttostr(step+1)+ ': Из массива смежных вершин удалена вершина х'+
Inttostr(minid+1));
end;
end
else begin
log.Append('Шаг '+Inttostr(step+1)+ ': Очередной кусок сформирован');
state:=3;
end;
Inc(step);
end;
3: begin
i1 := 1;
tmpstr:='';
While i1 <= itog[currk][0] do begin
i0 := 0;
while i0 < versh do begin
if (itog[currk][i1] = i0) then begin
indexs[i0] := false;
tmpstr:=tmpstr+'x'+IntToStr(itog[currk][i1]+1)+' ';
Break;
end;
Inc(i0);
end;
Inc(i1);
end;
state:=0;
Inc(currk);
inc(step);
log.Append('Шаг '+Inttostr(step)+ ': Изматрицысмежностиудаленывершины: '+tmpstr);
for i0 := 1 to versh do begin
if not indexs[i0-1] then Continue;
sum := 0;
for i1 := 1 to versh do begin
if not indexs[i1-1] then Continue;
if msmezh[i0-1,i1-1] <> 0 then
sum := sum + msmezh[i0-1,i1-1];
//Inc(sum);
end;
ls[i0-1]:=sum;
end;
if (currk = kuskov-1) then begin
state :=4;
end;
end;
4: begin
for i0:= 0 to versh-1 do begin
if not indexs[i0] then Continue;
Inc(itog[currk][0]);
itog[currk][itog[currk][0]] := i0;
end;
Inc(currk);
Inc(step);
log.Append('Шаг '+Inttostr(step)+ ': Оставшиеся вершины составляют последний кусок разбиения, конец работы алгоритма');
end;
end;
until ((turn) or (currk = (kuskov)));
if (currk = kuskov) then begin
step:=0;
currk:=0;
end;
end;
procedure TGraph.release;
var
i0:Integer;
begin
versh :=0;
kuskov := 0;
step := 0;
currk := 0;
SetLength(vershVKuske, 0);
SetLength(ls, 0);
SetLength(alpha, 0);
SetLength(indexs, 0);
for i0 := 0 to Length(msmezh)-1 do
SetLength(msmezh[i0], 0);
SetLength(msmezh, 0);
for i0 := 0 to Length(itog)-1 do
SetLength(itog[i0], 0);
SetLength(itog, 0);
end;
procedure TForm1.stage1;
begin
grp2.Enabled:=False;
grp2.Font.Color:=clGrayText;
grp3.Enabled:=False;
grp3.Font.Color:=clGrayText;
grp4.Enabled:=False;
grp4.Font.Color:=clGrayText;
pgc1.Enabled:=False;
pgc1.Font.Color:=clGrayText;
end;
procedure TForm1.stage2;
begin
stage1();
grp2.Enabled:=True;
grp2.Font.Color:=clWindowText;
pgc1.Enabled:=True;
pgc1.Font.Color:=clWindowText;
strngrd1.Options:=strngrd1.Options+[goEditing];
end;
procedure TForm1.stage3;
begin
stage2();
grp3.Enabled:=True;
grp3.Font.Color:=clWindowText;
end;
procedure TForm1.stage4;
begin
stage3();
grp4.Enabled:=True;
grp4.Font.Color:=clWindowText;
end;
procedure TForm1.stage5;
begin
stage4();
strngrd1.Options:=strngrd1.Options-[goEditing];
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Graph:=Tgraph.Create;
graph.init;
stage1();
strngrd2.Rows[0].Append('№ Куска');
strngrd2.Rows[0].Append('Количество');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
graph.release;
graph.log.Free;
graph.Free;
end;
procedure TForm1.btn3Click(Sender: TObject);
var
i:Integer;
begin
graph.release;
if (isUInt(edt1.Text)) and (StrToInt(edt1.Text)>0) then begin
graph.versh := StrToInt(edt1.Text);
SetLength(graph.msmezh, graph.versh);
SetLength(graph.ls, graph.versh);
SetLength(graph.alpha, graph.versh);
SetLength(graph.indexs, graph.versh);
SetLength(graph.itog, graph.versh);
For i:=0 to graph.versh-1 do begin
SetLength(graph.msmezh[i], graph.versh);
SetLength(graph.itog[i], graph.versh+1);
end;
stage2();
end
else
ShowMessage('Некорректноекол-вовершин');
initmatrix();
end;
procedure TForm1.initmatrix;
var
i,i1:Integer;
begin
strngrd1.ColCount:= graph.versh+1;
strngrd1.RowCount:= graph.versh+1;
strngrd1.Rows[0].Clear;
strngrd1.Rows[0].Append('-');
For i:=1 to graph.versh do begin
strngrd1.Rows[i].Clear;
strngrd1.Rows[i].Append('x'+inttostr(i));
for i1 := 1 to graph.versh do
strngrd1.Rows[i].Append(IntToStr(graph.msmezh[i-1][i1-1]));
strngrd1.Cols[i].Append('x'+inttostr(i));
end;
end;
procedure TForm1.btn4Click(Sender: TObject);
var
i:integer;
begin
if (isUInt(edt2.Text)) and (StrToInt(edt2.Text)>1)
and (StrToInt(edt2.Text) <= graph.versh) then begin
graph.kuskov := StrToInt(edt2.Text);
SetLength(graph.vershVKuske, graph.kuskov);
stage3();
end
else
ShowMessage('Некорректноекол-вокусков');
strngrd2.Cols[0].Clear;
strngrd2.Cols[1].Clear;
strngrd2.Rows[0].Append('№ Куска');
strngrd2.Rows[0].Append('Количество');
strngrd2.RowCount := graph.kuskov+1;
for i := 1 to graph.kuskov do
strngrd2.Cols[0].Append(IntToStr(i));
end;
procedure TForm1.btn5Click(Sender: TObject);
var
i,sum:integer;
begin
sum := 0;
for i:= 1 to graph.kuskov do begin
if (isUInt(strngrd2.Cells[1,i]))
and (StrToInt(strngrd2.Cells[1,i])>0) then begin
graph.vershVKuske[i-1] := StrToInt(strngrd2.Cells[1,i]);
sum:=sum+graph.vershVKuske[i-1];
end
else begin
ShowMessage('Некорректное кол-во вершин для '+InttoStr(i)+'го куска');
Exit;
end;
end;
if (sum <> graph.versh) then begin
if (sum<graph.versh) then
ShowMessage('Количество вершин указанное при распределении'+
' меньше указаного при создании на '+Inttostr(graph.versh - sum))
else
ShowMessage('Количество вершин указанное при распределении'+
' больше указаного при создании на '+Inttostr(sum-graph.versh));
Exit;
end;
stage4();
end;
function TForm1.start(turn:boolean):boolean;
var
i0,i1,sum,smesh:integer;
tmpstr:string;
begin
result:=false;
if graph.step=0 then begin
graph.currk:=0;
graph.state:=0;
graph.log.Clear;
strngrd1.ColCount:=graph.versh+2;
strngrd1.Rows[0].Append('p');
for i0 := 1 to graph.versh do begin
graph.indexs[i0-1]:=true;
graph.alpha[i0-1]:=0;
graph.itog[i0-1][0]:=0;
sum := 0;
for i1 := 1 to graph.versh do
if isUInt(strngrd1.Cells[i0,i1]) then
if (StrToInt(strngrd1.Cells[i0,i1])=0){ or (i1=i0) bez petel} then
graph.msmezh[i0-1,i1-1] := 0
else begin
graph.msmezh[i0-1,i1-1] := StrToInt(strngrd1.Cells[i0,i1]);
sum := sum + graph.msmezh[i0-1,i1-1];
//Inc(sum);
end
elsebegin
ShowMessage('Некорректное значение в матрице смежности на позиции '+
'x'+Inttostr(i0)+', x'+Inttostr(i1));
Exit;
end;
graph.ls[i0-1]:=sum;
strngrd1.Rows[i0].Append(IntToStr(sum));
end;
end;
graph.razbit(turn);
mmo1.Lines.Clear;
for i0 := 1 to graph.kuskov do begin
tmpstr:= 'Вершины в куске '+IntToStr(i0)+': ';
for i1 := 1 to graph.itog[i0-1][0] do begin
tmpstr := tmpstr+'x'+Inttostr(graph.itog[i0-1][i1]+1)+' '
end;
mmo1.Lines.Append(tmpstr);
end;
if (graph.kuskov = graph.currk)or(graph.step=0) then begin
initmatrix;
stage4;
mmo2.Text:=graph.log.Text;
Exit;
end
else begin
smesh:=0;
for i0 := 0 to graph.versh-1 do
if not graph.indexs[i0] then Inc(smesh);
strngrd1.RowCount:= graph.versh+1-smesh;
if graph.teta then
strngrd1.ColCount:=graph.versh+3-smesh
else
strngrd1.ColCount:=graph.versh+2-smesh;
strngrd1.Rows[0].Clear;
strngrd1.Rows[0].Append('-');
smesh:=0;
For i0:=1 to graph.versh do begin
if not graph.indexs[i0-1] then begin
Inc(smesh);
Continue;
end;
strngrd1.Rows[i0-smesh].Clear;
strngrd1.Rows[i0-smesh].Append('x'+inttostr(i0));
strngrd1.Cols[i0-smesh].Append('x'+inttostr(i0));
end;
strngrd1.Rows[0].Append('p');
if graph.teta then
strngrd1.Rows[0].Append('s');
smesh:=0;
for i0 := 1 to graph.versh do begin
if not graph.indexs[i0-1] then begin
Inc(smesh);
Continue;
end;
for i1 := 1 to graph.versh do begin
if not graph.indexs[i1-1] then Continue;
strngrd1.Rows[i0-smesh].Append(IntToStr(graph.msmezh[i0-1][i1-1]));
end;
strngrd1.Rows[i0-smesh].Append(IntToStr(graph.ls[i0-1]));
if graph.teta then
strngrd1.Rows[i0-smesh].Append(IntToStr(graph.ls[i0-1]-graph.alpha[i0-1]));
end;
end;
mmo2.Text:=graph.log.Text;
result:=true;
end;
procedure TForm1.btn6Click(Sender: TObject);
begin
if start(false) then
stage5;
end;
procedure TForm1.strngrd1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
begin
strngrd1.cells[ARow,ACol]:=Value;
end;
procedure TForm1.btn7Click(Sender: TObject);
begin
if start(true) then
stage5;
end;
procedure TForm1.btn2Click(Sender: TObject);
var
f1:Textfile;
i,i1:Integer;
begin
if dlgSave1.Execute then begin
if dlgSave1.FileName='' then exit;
assignfile(f1,dlgSave1.FileName);
{$I-}
Rewrite(f1);
{$I+}
if (IOResult <> 0) then begin
ShowMessage('Не удалось создать файл');
exit;
end;
Writeln(f1, Graph.versh);
for i:= 0 to Graph.versh-1 do
for i1:= 0 to Graph.versh-1 do
Writeln(f1, Strngrd1.cells[i+1,i1+1]);
CloseFile(f1);
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
f1:Textfile;
i,i1:Integer;
tmpstr:string;
begin
if dlgOpen1.Execute then begin
if dlgOpen1.FileName='' then exit;
assignfile(f1,dlgOpen1.FileName);
{$I-}
Reset(f1);
{$I+}
if (IOResult <> 0) then begin
ShowMessage('Не удалось открыть файл');
exit;
end;
Readln(f1, Graph.versh);
edt1.Text:=Inttostr(Graph.versh);
btn3Click(self);
for i:= 0 to Graph.versh-1 do
for i1:= 0 to Graph.versh-1 do begin
Readln(f1, tmpstr);
strngrd1.Cells[i+1,i1+1]:=tmpstr;
end;
CloseFile(f1);
end;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
SGCopyToCLP(strngrd1, False);
end;
end. |