-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparpgm.p
1790 lines (1520 loc) · 69.4 KB
/
parpgm.p
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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ **** parpgm.p **** }
program maxpar(input,output);
{ **** userGlob.i **** }
{ This module contains all global type and variable decalrations which
depend upon the specific matroid or upon the implementation of required
support modules, (see heading comments in parpgm.p). }
const
NULLELEMENT = 0;
MAXE = 100; { maximum number of elements in a matroid , }
{ including transforms and singletons. }
LOGMAXELEMENTS = 7; { log to the base 2 of # of elements }
MAXDIGIT = 3; { the maximum numbe of digits used to repesent
an element. Used in output routines }
MAXV = 10000; { maximum number of vertices in a graph }
MAXRANK = 9999; { maximum rank of an input matroid }
DELIM = '$'; { this delimiter appears around each element
number in the output }
type
ELEMENT = 0..MAXE; { ordinal type for matroid elements }
VERTEX = 1..MAXV; { ordinal type for vertices in graph module }
EPTR = ^eltype;
{ "eltype" is a record describing the matroid elements. }
elcases= (normal, transform); { two kinds of elements }
eltype= record
case elcases of
normal : (end1, end2: VERTEX); { endpoints of edge }
transform : (el1, el2: ELEMENT); { tips of transform }
end;
QDATA = ELEMENT; { type definition for items to be enqueued }
INDICATOR = array [ELEMENT] of boolean;
{ used for passing parameters to output routine }
{ the following types are used to implement a graph }
aptr = ^adjNode;
adjNode = record { node on adjacency list }
vnum : VERTEX; { number of vertex on list }
next : aptr; { pointer }
end; (* adj_node *)
GRAPH = record
numVertex : integer;
adjacency : array [VERTEX] of aptr;
end; (* GRAPH *)
{ the type FOREST is used to implement a spanning tree }
FOREST = record
pre: array [VERTEX] of integer;
high: array [VERTEX] of integer;
end; (* FOREST *)
var
InputGraph: GRAPH; { the input graph itself }
BasisTree: FOREST; { spanning forest of current basis }
{ **** end userGlob.i **** }
{ **** parityGlob.i **** }
{ This module contains those declarations which are independant of the
matroid elements and the implementation of required user-supplied
routines.
The user must define the follwing types and constants
ELEMENT - an ordinal type for indexing the matroid elements.
NULLELEMENT - a constant of type ELEMENT used to initialize variables.
NULLELEMENT should not correspond to an oridinal used
to indicate an actual matroid element.
MAXRANK - a constant equal to the maximum rank of an input matroid.
ELTYPE - a record defining the matroid element.
EPTR - a pointer to type ELTYPE
}
{ ****************** Global variables for all matroids ********************}
var
Parity: array [ELEMENT] of EPTR; { parity data structure }
{ eptr points to edges in input graph }
Mstar : array [1..MAXRANK] of ELEMENT; { current basis }
BasicXform: array [1..MAXRANK] of ELEMENT; { transforms in current basis }
NumBasicXform: integer; { number of transforms in the basis }
NumEl: integer; { number of matroid elements in problem }
NumSingle : integer; { number of singletons in Mstar }
NumXform : integer; { number of transforms in current step }
InMS : array [ELEMENT] of boolean; { indicates if element is in Mstar }
BasisSize: 0..MAXRANK; { rank of matroid basis }
{ **** trace-module.t **** }
{ This module contains all routines which provide summary information about the
calculations.
The trace routine for procedure foo is named fooTrace. }
var
InBlossomTrace: boolean; { indicator for output formatting }
procedure augmentTrace;
begin
writeln('augment step-');
end;
procedure blossomTrace(bud,tip1,tip2:ELEMENT; trueTips: boolean);
begin
InBlossomTrace:= true;
writeln('blossom step-');
writeln(' t0=',DELIM,tip1:MAXDIGIT,DELIM,
' t1=',DELIM,tip2:MAXDIGIT,DELIM,
' b=',DELIM,bud:MAXDIGIT,DELIM);
if trueTips then
writeln(' true tips')
else
writeln(' not true tips');
end;
procedure checkAdjTrace(e,f: ELEMENT; equiv,adjacent: boolean);
begin
if adjacent then
if equiv then
writeln(' ',DELIM,f:MAXDIGIT,DELIM,': ', DELIM,f:MAXDIGIT,DELIM,
' equivalent to ',DELIM,e:MAXDIGIT,DELIM,
', so do nothing')
else
write(' ',DELIM,f:MAXDIGIT,DELIM,': ');
end;
procedure compactSinglesTrace;
var
index: integer;
begin
InBlossomTrace:= false; { initialize formatting boolean }
writeln;
writeln;
writeln('************* enter IncreaseMatching ***************');
writeln;
writeln(' there are now ',NumSingle:MAXDIGIT,' singletons');
writeln(' they are:');
for index:= NumEl+1 to NumEl+ NumSingle do
writeln(DELIM,index:MAXDIGIT,DELIM,':',
Parity[index]^.end1,',',Parity[index]^.end2);
end;
procedure CreateTransformTrace(z,x,y,bud: ELEMENT);
begin
InBlossomTrace:= false;
writeln(' create ',DELIM,z:MAXDIGIT,DELIM,'=T(',
DELIM,x:MAXDIGIT,DELIM,',',DELIM,y:MAXDIGIT,DELIM,',',
DELIM,bud:MAXDIGIT,DELIM,'), ');
write(' '); { format for label trace }
end;
procedure degenerateBlossomTrace;
begin
writeln('degenerate blossom');
end;
procedure giveLabelTrace(f,back,reverse: ELEMENT; serial: integer);
begin
if InBlossomTrace then
write(' ');
writeln('label[',DELIM,f:MAXDIGIT,DELIM,']=(',DELIM,back:MAXDIGIT,DELIM,
',' ,DELIM,reverse:MAXDIGIT,DELIM, ') s(',DELIM,f:MAXDIGIT,DELIM,
')=',serial:MAXDIGIT);
end;
procedure newBasisGraphTrace(e: ELEMENT; v,w: VERTEX);
begin
end;
procedure readMatchTrace(initMatch: INDICATOR; num: integer);
var arc: integer;
begin
writeln('Initial Matching List');
for arc:=1 to num do
if initMatch[arc] then
writeln(' edge ', arc,' endpoints: ',
Parity[arc]^.end1, ',', Parity[arc]^.end2);
end;
procedure scanTrace(e: ELEMENT);
begin
writeln;
writeln;
writeln('scan label on ',DELIM,e:MAXDIGIT,DELIM,' ; adjacent to--');
end;
procedure SwapTrace(e: ELEMENT; putIn: boolean);
begin
if putIn then
writeln(' put element ',DELIM,e:MAXDIGIT,DELIM,' in basis')
else
writeln(' removed element ',DELIM,e:MAXDIGIT,DELIM,' from basis');
end;
procedure tryToGrowTrace(e,f,fbar: ELEMENT; equiv,unlabelled,pending,
mateUnlabelled,degen: boolean);
begin
if unlabelled or pending then begin
write(' ',DELIM,f:MAXDIGIT,DELIM,': ');
if equiv then
writeln(DELIM,f:MAXDIGIT,DELIM,' equivalent to ',DELIM,e:MAXDIGIT,DELIM,
', so do nothing')
else if degen then
writeln(DELIM,f:MAXDIGIT,DELIM,
' is a tip of a degenerate blossom, so do nothing.')
else if pending then
writeln('s[',DELIM,f:MAXDIGIT,DELIM,']>s[',DELIM,e:MAXDIGIT,DELIM,
'] ,so do nothing')
else if not mateUnlabelled then
writeln(DELIM,fbar:MAXDIGIT,DELIM,
' already labelled so do nothing');
end;
end;
{ **** end parityGlob.i **** }
{ **** graph.i **** }
{ This module implements the data type GRAPH.
A GRAPH consists of a number of entities called vertices,
related in a pairwise manner by entities called edges.
Two vertices related by an edge are said to be adjacent vertices.
We will speak of the edge relating two vertices as the edge
between the vertices.
The vertices are indexed by the ordinal type VERTEX, defined as 1..MAXV.
The user must set the constant MAXV.
The type VERTEX is provided to the user.
The following operations can be performed on a GRAPH:
1. InitGr (var g: GRAPH; n: integer)
Initializes g to be a graph with no edges on n vertices.
2. EraseGr(var g: GRAPH)
Removes all edges from g
3. AddEdgeGr (var g: GRAPH; v1,v2: VERTEX)
Adds an edge between vertices v1 and v2.
4. CopyGr( g1:GRAPH; var g2: GRAPH);
Places a copy of g1 into g2. The copies are independent; one can be
manipulated without changing the other.
g2 is a returned parameter; it need not be initialized before calling
CopyGr.
5. ForAdjGr (g: GRAPH; v: VERTEX; procedure P(u: VERTEX))
For each vertex u adjacent to v in g, perform P.
6. ForVertex(g:GRAPH, procedure P(v: VERTEX))
For each vertex v in g, do P.
Other operations on a GRAPH may be added in other modules which
use the above basic operations.
}
procedure EraseGr(var g: GRAPH);
{ This procedure empties the adjacency list for each vertex,
leaving the graph in the empty state. }
var cur,nxt: aptr;
i: integer;
begin
for i:= 1 to g.numVertex do begin
{D writeln(' clear adjacency list for vertex', i); }
cur:= g.adjacency[i];
g.adjacency[i]:= nil;
while cur <> nil do begin
nxt:= cur^.next;
dispose(cur);
cur:= nxt;
end; (* while *)
end; (* for *)
end; (* Erase Gr *)
procedure InitGr(var g:GRAPH; n: integer);
begin
{T writeln(' entered INIT GR, number of nodes is', n); }
{ check the input parameter n }
if n > MAXV then begin
writeln(' input number of vertices ',n,
' exceeds maximum allowable of ',MAXV);
halt;
end; (* if *)
if n < 1 then begin
writeln(' input number of vertices ',n,
' is not valid');
halt;
end; (* if *)
{ end check n }
g.numVertex:= n;
EraseGr(g);
{T writeln(' exit INIT GR'); }
end; (* Init Gr *)
procedure AddEdgeGr (var g: GRAPH; u,v: VERTEX);
{ This procedure adds v to u's adjacency list, and vice versa }
var ap: aptr;
begin
{D writeln(' enter ADD EDGE GR with vertices', u,v); }
new(ap);
ap^.vnum:= v;
ap^.next:= g.adjacency[u];
g.adjacency[u]:= ap;
{ and vice versa }
new(ap);
ap^.vnum:= u;
ap^.next:= g.adjacency[v];
g.adjacency[v]:= ap;
end; (* Add Edge Gr *)
procedure ForAdjGr ( g: GRAPH; u:VERTEX; procedure P( v:VERTEX));
var
ap: aptr;
begin
ap:= g.adjacency[u];
while ap <> nil do begin
P( ap^.vnum);
ap:= ap^.next;
end; (* while *)
end; (* For Adjacent in Graph *)
procedure ForVertex( g: GRAPH; procedure P(v: VERTEX));
var
v: VERTEX;
begin
{D writeln(' enter FOR VERTEX'); }
for v:= 1 to g.numVertex do
P(v);
{D writeln(' exit FOR VERTEX');}
end; (* For Vertex *)
procedure CopyGr(g1: GRAPH; var g2: GRAPH);
{ This procedure places a copy of g1 into g2 }
procedure copyAdjList(u: VERTEX);
{ This procedure copies the adjacency list of vertex u from g1 to g2 }
var
g1ptr, g2node: aptr;
begin (* copy Adj List *)
g1ptr:= g1.adjacency[u];
while g1ptr <> nil do begin
new(g2node);
g2node^.vnum:= g1ptr^.vnum;
g2node^.next:= g2.adjacency[u];
g2.adjacency[u]:= g2node;
g1ptr:= g1ptr^.next;
end; (* while *)
end; (* Copy Adj List *)
begin (* Copy Graph *)
InitGr(g2, g1.numVertex);
ForVertex(g1, copyAdjList);
end; (* Copy Graph *)
procedure PrintAdjList(g: GRAPH);
(* this routine is used for debug purposes only *)
var ap:aptr;
i: integer;
begin
writeln(' entered PRINT ADJ LIST');
for i:= 1 to g.numVertex do begin
write('adjacency list for vertex ',i);
ap:= g.adjacency[i];
while ap <> nil do begin
write (ap^.vnum);
ap:= ap^.next;
end; (* while *)
writeln;
end; (* for *)
writeln(' exited PRINT ADJ LIST');
end; (* print_adj_list *)
{ **** end graph.i ****}
{ **** spforest.i **** }
{ This module implements a spanning forest on a graph.
The spanning forest is stored in a variable of type FOREST.
FOREST maintains two arrays, 'pre' and 'high', both indexed by
the ordinal type VERTEX. VERTEX must be defined in the module which
implements the graph.
'pre' stores the pre-order number of a node in the spanning forest.
'high' stores the highest descendant of a vertex in the spanning
forest.
The spanning forest is calculated by recursive calls to the routine
search, which implements a depth first search.
The module which implements a graph must provide the following routines:
ForVertex( g:GRAPH, procedue P(v:VERTEX));
This procedure performs P on every vertex v in g.
ForAdjGr( g:GRAPH; u:VERTEX, procedure P(v:VERTEX))
This procedure performs P for every vertex v adjacent to u in g.
The following operations can be performed on a spanning tree:
1. InitSpForest( g:GRAPH; t:FOREST, procedure P(u,v:VERTEX))
This procedure initializes t to be a spanning forest for g.
t is a "returned" parameter from InitSpForest, in that
t is not defined when InitSpForest is called.
The procedure P is performed for every tree edge u,v in the
spanning forest.
2. IsInCycle(v,w,x,y:VERTEX; t:FOREST): boolean;
true if tree edge v,w is in the fundamental cycle formed when
edge x,y is added to the spanning tree t.
********** NOTE *******
IsInCycle does not check that v,w is an edge in the forest.
}
procedure InitSpForest(g:GRAPH; var t:FOREST; procedure P(u,v:VERTEX));
var
visited: array [VERTEX] of boolean; { indicator array }
dfsn: integer; { global to search for assigning PRE }
function search(node: VERTEX): integer;
{ search is called recusivly to perform a depth first search.
The input parameter is the current node in the search.
search returns the highest pre-order number of a descendent
of the input node. }
procedure continueSearch( v: VERTEX);
begin
{D writeln(' enter CONTINUE SEARCH with node',v);}
if not visited[v] then begin
{ do P to the edge node,v }
P(node,v);
t.high[node]:= search(v);
end; (* if *)
end; (* Continue Search *)
begin (* Search *)
{D writeln (' entered search with node ',node); }
visited[node]:= true;
dfsn:= dfsn + 1;
t.pre[node]:= dfsn;
t.high[node]:= dfsn;
{D writeln(' pre-order number of node is ',dfsn); }
ForAdjGr( g, node, continueSearch); { search through adjacency list }
search:= t.high[node];
{D writeln(' HIGH set to ',t.high[node],' for node ',node); }
end; (* Search *)
procedure rootIfNotVisited( v: VERTEX);
begin
{D writeln(' entered ROOT IF NOT VISITED with vertex', v); }
if not visited[v] then
t.high[v]:= search(v);
end; (* Root etc *)
procedure initialVisited( v: VERTEX );
begin
visited[v]:= false;
end;
begin (* InitSpForest *)
{ initialize }
dfsn:= 0;
ForVertex( g, initialVisited);
{ end initialization }
ForVertex( g,rootIfNotVisited);
end; (* Initialize Spanning Forest *)
function IsInCycle( v,w,x,y: VERTEX; t: FOREST): boolean;
{ this function returns true if edge (v,w) is in the fundamental
cycle formed by adding edge (x,y) to the forest t }
var
lim1, lim2: integer; { used to test cycle condition }
function btwn( a: integer): boolean;
{ returns true if lim1 <= a <= lim2 }
begin
if (lim1 <= a) and (lim2 >= a) then
btwn:= true
else
btwn:= false;
end; (* btwn *)
begin
with t do begin
if pre[v] < pre[w] then begin
lim1:= pre[w];
lim2:= high[w];
end
else begin
lim1:= pre[v];
lim2:= high[v];
end; (* if *)
{ check cycle condition }
IsInCycle:= btwn(pre[x]) <> btwn(pre[y]);
end; (* with t *)
end; (* IsInCycle *)
procedure PrintSpForest(g:GRAPH; t: FOREST);
(* This routine prints the pre and high numbers for the forest t,
of graph g.
It is used for debugging only. *)
var j: integer;
begin
writeln(' spanning tree has pre and high as follows');
writeln(' pre:');
for j:= 1 to g.numVertex do
write(t.pre[j]);
writeln;
writeln(' high:');
for j:= 1 to g.numVertex do
write(t.high[j]);
writeln;
end;
{ ***** end spforest.i ****** }
{ **** queues.i **** }
{ This module implements the data type QUEUE.
The module can implement any number of QUEUE's , but all
QUEUE's must contain the same type of entity.
The entities to be placed on QUEUE's must be defined by the
user in a global type called QDATA.
A copy of the entities passed to AddQ are stored on a QUEUE.
Therefore, the actual entity need not be retained after it is
placed on a QUEUE.
The following operations are defined for a QUEUE:
1. InitialQ ( var q:QUEUE )
Initializes the QUEUE q to an empty state.
2. MTQ (q:QUEUE): boolean
returns 'true' if q is empty, 'false' otherwise.
3. DelQ (var q:QUEUE) :QDATA
removes the first entity on the QUEUE q.
If DelQ is called when q is empty, the results will be
unpredictable.
Always use MTQ before DelQ.
If DelQ is called with an empty queue, a message is written to the
Standard Output, and execution is halted.
4. AddQ ( q:QUEUE, var item:QDATA)
Places the entity 'item' at the end of QUEUE q.
5. FlushQ (q:QUEUE)
removes all entities from QUEUE q, and leaves q in the empty
state.
6. DoToQ( var q: QUEUE; procedure P(item: QDATA));
DoToQ performs the procedure P to every item on the queue q.
The queue is not altered by DoToQ, unless P performs some operations
on it.
}
type
linptr = ^linkedNode;
linkedNode = record
item: QDATA; { QDATA is defined by user in an
external module }
next: linptr;
end;
QUEUE = record
head: linptr;
tail: linptr;
end;
procedure InitialQ (var q: QUEUE);
begin
q.head:= nil;
end; (* Initial Q *)
function MTQ(q:QUEUE): boolean;
begin
MTQ:= q.head = nil;
end;
procedure AddQ (var q: QUEUE; newthing: QDATA);
var
p: linptr;
begin
new(p);
p^.item:= newthing;
p^.next:= nil;
if MTQ(q) then
q.head:= p
else
q.tail^.next:= p;
q.tail:= p;
end;
function DelQ(var q:QUEUE): QDATA;
var
p: linptr;
begin
if MTQ(q) then begin
writeln(' DelQ called with empty queue. Execution halted.');
halt;
end;
DelQ:= q.head^.item;
p:= q.head;
q.head:= q.head^.next;
dispose(p);
end;
procedure FlushQ (var q: QUEUE);
var
p: linptr;
begin
p:= q.head;
while p <> nil do begin
q.head:= p^.next;
dispose(p);
p:= q.head;
end; (* while *)
end; (* FlushQ *)
procedure DoToQ( var q: QUEUE; procedure P(item: QDATA));
var
link: linptr;
begin
link:= q.head;
while link <> nil do begin
P(link^.item);
link:= link^.next;
end; (* while *)
end; (* Do To Q *)
{ **** end queues.i ****** }
{ **** equiv.i **** }
{ equiv.i -- implementation of MFSETs, using path compression and merge
by rank (see AHU, chapter 5); note: this is not in "showcase"
form, and probably won't be even in our final version: it is
difficult to get around the assumption that elements are array
indices. }
var
Parent: array [ELEMENT] of ELEMENT;
{ parent of an element in the tree representation of
an MFSET }
Rank: array [ELEMENT] of 0..LOGMAXELEMENTS;
{ length of the longest path in the tree ending at that
element }
procedure InitEquivalence (m: integer);
{ initialize a set of equivalence classes on m elements by putting
each element into an equivalence class by itself }
var e: ELEMENT;
begin
for e := 1 to m do begin
Parent [e] := e;
Rank [e] := 0 end
end; (* Init Equivalence *)
function find (e: ELEMENT): ELEMENT;
{ return the root of the tree in which e is located, and do path
compression along the path from e to the root;
the path compression method used is called "halving" and is described
in Tarjan and Van Leeuwen, "Worst case analysis of set union algorithms,"
JACM, April, 1984 }
begin
while Parent [e] <> Parent [Parent [e]] do begin
Parent [e] := Parent [Parent [e]];
e := Parent [e] end;
find := Parent [e]
end; (* find *)
function AreEquivalent (e,f: ELEMENT): boolean;
{ true if e and f are in the same equivalence class }
begin
AreEquivalent := find (e) = find (f)
end;
procedure Merge (e,f: ELEMENT);
{ make e and f equivalent (if they are not already), that is, merge the
equivalence class containing e with that containing f;
this is done by linking the roots of the two trees in such a way that
the root with higher rank becomes the root of the new tree }
var roote,rootf: ELEMENT; { roots of the respective trees }
begin
roote := find (e); rootf := find (f);
if roote <> rootf then
if Rank [roote] > Rank [rootf] then
Parent [rootf] := roote
else if Rank [roote] < Rank [rootf] then
Parent [roote] := rootf
else (* Rank [roote] = Rank [rootf] *) begin
Parent [rootf] := roote;
Rank [roote] := Rank [roote] + 1 end
end; (* Merge *)
{ **** end, equiv.i **** }
{ **** depgraph.i **** }
{ This module implments the operations to be performed on the dependence
graph of the current basis.
All global variables used in this module are defined in either
parityGlob.i or userGlob.i, except for BasisGraph.
The following operations are provided for manipulating the
dependence graph:
1. IsInMstar( e: ELEMENT): boolean
Returns true if e is in the current basis, false otherwise.
2. Mate(e: ELEMENT): ELEMENT
Returns the parity mate of e.
3. ForAdjacent( e: ELEMENT, procedure P( f: ELEMENT))
Performs P for every element f adjacent to e in the dependance
graph.
4. GetInitialBasis
Constructs an initial basis of all singletons from the input graph.
5. ForElement( procedure P(e: ELEMENT))
executes P for every element in the matroid.
6. Swap(e: ELEMENT)
Swap modifies the global indicator array InMS to indicate a
change in the basis.
The element e (and its mate if e is not a singleton ), are put into
or removed from the basis, according to its current status.
7. Update
Update calls the routines which update the dependence graph
and the basis Mstar.
Update is called after all calls to Swap have been made.
Routines which deal with transforms and singletons are in the modules
transforms.i and singletons.i, resp.
}
var
BasisGraph: GRAPH; { graph containing the edges of the current basis }
function IsInMstar(e: ELEMENT): boolean;
begin
IsInMstar:= InMS[e];
end; (* IsInMstar *)
{ Include subsidiary modules }
{ **** singletons.i **** }
{ This module contains those routines which handle singletons.
The routines are:
1. IsSingleton(e: ELEMENT): boolean
Returns true if e is a singleton.
2. makeSingleton(v1,v2: VERTEX)
This routine is a support procedure for the routine GetInitialBasis.
makeSingleton creates an entry in the Parity data structure for the
edge (v1,v2) as a singleton.
3. ForSingleton( procedure P(e: ELEMENT))
Performs P to every singleton.
4. compactSingles
This routine is a support procedure for the routine Update.
compactSingles removes from the Parity data structure those singletons
which have been Swap'ed, and renumbers the remaining singletons.
}
function IsSingleton( e: ELEMENT): boolean;
begin
IsSingleton:= (e > NumEl) and (e <= NumEl+NumSingle )
end; (* Is Singleton *)
procedure makeSingleton(v1,v2: VERTEX);
{ this procedure adds the edge v1,v2 to Mstar }
var
e: EPTR; { points to an edge }
pos: ELEMENT; { array index }
begin
new(e);
e^.end1:= v1;
e^.end2:= v2;
NumSingle:= NumSingle + 1;
pos:= NumEl + NumSingle;
Parity[pos]:= e;
Mstar[NumSingle]:= pos;
BasisSize:= BasisSize + 1;
InMS[pos]:= true;
end; (* add_singleton *)
procedure ForSingleton( procedure P(e:ELEMENT));
var
i: ELEMENT;
begin
for i:= NumEl+1 to NumEl + NumSingle do
P(i);
end; (* For Singleton *)
procedure compactSingles;
{ This procedure renumbers the singletons in the new basis so that
they form a contiguous sequence.
NumSingle is reset by this procedure.
The procedure uses two pointers. 'last' is initially set to the index
in Parity of the highest numbered singleton in the old basis.
'gap' is initially set to the lowest numbered singleton.
In the main loop, last is initially moved to point to the
highest numbered singleton still in the basis.
If there is no such singleton, then last will be set to a value less
than that of gap. This is the only case in which last will be less
than gap.
In the event that there are singletons remaining in the basis,
the procedure then increments gap to point to a singleton not in the
basis. If there is no such singleton, gap will end up pointing to
last. If there is such a singleton, the singleton pointed to by last
is moved to position 'gap' to make the sequence more contiguous.
last is then decremented by one, and the loop repeats.
When last<=gap, all singletons in the basis have contiguous numbers
At the end of the repeat loop, last and gap both point to the
last singleton in the new basis, provided there is one. }
var
gap,last: integer;
begin (* compact singles *)
last:= NumEl + NumSingle; { position of last singleton }
gap:= NumEl + 1; { position of first singleton }
{ main loop }
repeat
while not IsInMstar(last) and (last>=gap) do
last:= last-1;
{ last now points to highest numbered singleton still in Mstar }
while IsInMstar(gap) and (gap < last) do
gap:= gap+1;
{ gap now points to position of a singleton removed from Mstar
or to last if compaction is completed }
if (last > gap) then begin { not at end of compaction }
Parity[gap]:= Parity[last];
InMS[gap]:= true;
InMS[last]:= false;
last:= last-1;
end; (* if *)
until last<=gap;
{ reset number of singletons }
if (last < gap) then
NumSingle:= 0
else
NumSingle:= last-NumEl;
compactSinglesTrace;
end; (* compact singles *)
{ ***** end, singletons.i ****** }
{ **** transforms.i **** }
{ This module contains those routines which deal with transforms.
The routines in this module are:
1. IsTransform( e: ELEMENT): ELEMENT
Returns true if e is a transform, false otherwise.
2. Equivalent(x,y: ELEMENT): boolean
Returns true if the elements x and y have been made equivalent.
x and y may be transforms, regular matroid elements or singletons.
3. MakeEquivalent(x,y: ELEMENT)
Merges the equivalence classes containing x and y.
x and y may be transforms or matroid elements, but not singletons.
4. IsAdjacent( e,f: ELEMENT): boolean
Returns true if e is adjacent to f in the dependance graph of
the current basis.
5. CreateTransform( x,y,b: ELEMENT): ELEMENT
Creates the transform for the blossom with bud b and tips x and y.
The transform is stored in the Parity data stucture, and
is included in the basis if both x and y are in the basis.
The returned value is the number of the new transform.
6. FirstTip(e: ELEMENT): ELEMENT
Returns the 'first' tip of a transform, i.e. that tip for
which the transform label gives the path through the blossom.
7. SecondTip(e: ELEMENT): ELEMENT
Returns the 'second' tip of a transform. For this tip the path
through the blossom is given by the label (y,x) where (x,y) is
the label on the transform.
}
function IsTransform( e: ELEMENT): boolean;
begin
IsTransform:= e > NumEl + NumSingle;
end;
function Equivalent(x,y: ELEMENT): boolean;
{ This function returns true if x and y are in the same equivalence class }
var
a,b: ELEMENT;
begin
if IsSingleton(x) or IsSingleton(y) then
Equivalent:= false
else begin
if IsTransform(x) then
a:= Parity[x]^.el2
else
a:= x;
if IsTransform(y) then
b:= Parity[y]^.el2
else
b:= y;
Equivalent:= AreEquivalent(a,b);
end; (* else *)
end; (* Equivalent *)
procedure MakeEquivalent( x,y: ELEMENT);
var
a,b: ELEMENT;
begin
if IsTransform(x) then
a:= Parity[x]^.el2
else
a:= x;
if IsTransform(y) then
b:= Parity[y]^.el2
else
b:= y;
Merge(a,b);
end; (* Make Equivalent *)
function IsAdjacent(e,f: ELEMENT): boolean;
function isAdjElEl(e,f: ELEMENT): boolean;
var
inel,outel: ELEMENT; { element in and not in basis, resp }
begin
if IsInMstar(e) then begin
inel:= e;
outel:= f;
end
else begin
inel:= f;
outel:= e;
end; (* if *)
isAdjElEl:= IsInCycle( Parity[inel]^.end1, Parity[inel]^.end2,
Parity[outel]^.end1, Parity[outel]^.end2,
BasisTree);
end;
function isAdjXEl( x,e: ELEMENT): boolean;
begin
isAdjXEl:= isAdjElEl(Parity[x]^.el1, e) <> isAdjElEl( Parity[x]^.el2, e);
end;
function isAdjXX( x,y: ELEMENT): boolean;
begin
isAdjXX:= isAdjXEl(x, Parity[y]^.el1) <> isAdjXEl(x, Parity[y]^.el2);
end;
begin (* IsAdjacent *)
if IsInMstar(e) = IsInMstar(f) then
IsAdjacent:= false
else if IsTransform(e) then
if IsTransform(f) then
IsAdjacent:= isAdjXX(e,f)
else
IsAdjacent:= isAdjXEl(e,f)
else if IsTransform(f) then
IsAdjacent:= isAdjXEl(f,e)
else
IsAdjacent:= isAdjElEl(e,f);
end; (* IsAdjacent *)
function CreateTransform(x,y,b: ELEMENT): ELEMENT;
var p: EPTR;
xnum: integer;
begin
new(p);
p^.el1:= x;
p^.el2:= y;
NumXform:= NumXform + 1;
xnum:= NumEl+ NumSingle + NumXform;
Parity[xnum]:= p;
if IsInMstar(x) and IsInMstar(y) then begin
NumBasicXform:= NumBasicXform + 1;
BasicXform[NumBasicXform]:= xnum;
InMS[xnum]:= true;
end
else