matching.c
28.4 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
#include <string.h>
#include <alloca.h>
#include <stdlib.h>
#include <assert.h>
#include "fdc_int.h"
#include "variables.h"
#include "values.h"
#include "matching.h"
#define UNSEEN (-2) // value not seen in any domain
#define WHITE (-1)
#define GREY 0
#define BLACK 1
#define VARIABLE 0
#define VALUE 1
/*
Find out which edges, not belonging to the matching, are in
M-alternating cycles [HCP].
The cycles correspond to the strongly connected components of the
value graph, viewed as a directed graph with the matching edges
going from left (the variables) to right (the values), and the other
edges going in the opposite direction [HCP].
*/
static void _fd_mark_cycles(fd_constraint c, int8_t *vgraph, int r_verts,
int *l_edge, int *r_edge, int *l_scc, int *r_scc,
int min)
{
int nvariables = c->nvariables;
int8_t *l_colour, *r_colour;
int *stack, top;
int8_t *type; // type of node in the stack: VARIABLE or VALUE
#define push(x, t) (top++, type[top] = (t), stack[top] = (x))
#define pop() stack[top--]
#define top() stack[top]
#define last_branch stack[top + 1] // the last branch taken when
// leaving the top() node
int *l_finish, *r_finish, time; // finishing times of the DFS algorithm
int *variables; // variables ordered by finishing time
int scc;
int i, j, v;
/* Strongly connected components found through the [...] algorithm
[Cormen]. */
l_colour = alloca(nvariables * sizeof(*l_colour));
r_colour = alloca(r_verts * sizeof(*r_colour));
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
stack = alloca((nvariables + r_verts) * sizeof(*stack));
type = alloca((nvariables + r_verts) * sizeof(*type));
variables = alloca(nvariables * sizeof(*variables));
// forward DFS
top = -1;
l_finish = alloca(nvariables * sizeof(*l_finish));
r_finish = alloca(r_verts * sizeof(*r_finish));
memset(l_finish, -1, nvariables * sizeof(*l_finish));
memset(r_finish, -1, r_verts * sizeof(*r_finish));
time = -1;
for (i = 0, j = nvariables; i < nvariables; ++i)
{
if (l_colour[i] != WHITE) // already visited this variable
continue;
push(i, VARIABLE);
l_colour[i] = GREY;
while (top >= 0)
{
switch (type[top])
{
case VARIABLE:
v = l_edge[top()]; // matching edges go from left to right
if (r_colour[v] != WHITE)
{
// matched value already visited
//l_colour[top()] = BLACK;
l_finish[top()] = ++time;
variables[--j] = pop();
break;
}
push(v, VALUE);
last_branch = -1;
r_colour[v] = GREY;
break;
case VALUE:
for (v = last_branch + 1; v < nvariables; ++v)
if (vgraph[v * r_verts + top()] == 1 &&
l_colour[v] == WHITE)
break;
if (v == nvariables) // no unvisited successor found
{
//r_colour[top()] = BLACK;
r_finish[top()] = ++time;
pop();
break;
}
push(v, VARIABLE); // sets last_branch to v
l_colour[v] = GREY;
break;
}
}
}
// backward DFS
top = -1;
int *l_parent, *r_parent; // depth-first forest
l_parent = alloca(nvariables * sizeof(*l_parent));
r_parent = alloca(r_verts * sizeof(*r_parent));
memset(l_parent, -1, nvariables * sizeof(*l_parent));
memset(r_parent, -1, r_verts * sizeof(*r_parent));
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
for (j = 0; j < nvariables; ++j)
{
i = variables[j];
if (l_colour[i] != WHITE) // already visited this variable
continue;
push(i, VARIABLE);
last_branch = -1;
l_colour[i] = GREY;
while (top >= 0)
{
switch (type[top])
{
case VARIABLE:
for (v = last_branch + 1; v < r_verts; ++v)
if (vgraph[top() * r_verts + v] == 1 &&
r_colour[v] == WHITE &&
r_finish[v] > -1 && r_finish[v] < l_finish[i])
break;
if (v == r_verts) // no unvisited successor found
{
//l_colour[top()] = BLACK;
pop();
break;
}
r_parent[v] = top();
push(v, VALUE); // sets last_branch to v
r_colour[v] = GREY;
break;
case VALUE:
v = r_edge[top()]; // matching edges go from right to left
if (l_colour[v] != WHITE)
{
// matched variable already visited
//r_colour[top()] = BLACK;
pop();
break;
}
l_parent[v] = top();
push(v, VARIABLE);
last_branch = -1;
l_colour[v] = GREY;
break;
}
}
}
// identify strongly connected components nodes
int8_t *is_root;
int *l_children, *l_nc, *r_child;
//is_root = alloca(nvariables * sizeof(*is_root));
is_root = l_colour; // XXX: reuse memory
l_children = alloca(nvariables * r_verts * sizeof(*l_children));
//l_nc = alloca(nvariables * sizeof(*l_nc));
l_nc = stack; // XXX: reuse memory
r_child = alloca(r_verts * sizeof(*r_child));
memset(is_root, 1, nvariables * sizeof(*is_root));
memset(l_nc, 0, nvariables * sizeof(*l_nc));
memset(r_child, -1, r_verts * sizeof(*r_child));
/* identify strongly connected components nodes */
// reverse edges of the depth-first search forest
for (i = 0; i < nvariables; ++i)
{
if (l_parent[i] == -1)
continue;
is_root[i] = 0;
r_child[l_parent[i]] = i;
}
for (v = 0; v < r_verts; ++v)
{
if (r_parent[v] == -1)
continue;
i = r_parent[v];
l_children[i * r_verts + l_nc[i]] = v;
l_nc[i]++;
}
// mark values as used
int *l_queue, l_head, l_tail, *r_queue, r_head, r_tail;
//l_queue = alloca(nvariables * sizeof(*l_queue));
l_queue = l_finish; // XXX: reuse memory
//r_queue = alloca(r_verts * sizeof(*r_queue));
r_queue = r_finish; // XXX: reuse memory
memset(l_scc, -1, nvariables * sizeof(*l_scc));
memset(r_scc, -1, r_verts * sizeof(*r_scc));
scc = -1;
for (i = 0; i < nvariables; ++i)
{
if (!is_root[i] || l_nc[i] == 0)
continue;
scc++;
l_head = l_tail = 0;
l_queue[l_tail] = i;
r_head = 0; r_tail = -1;
while (l_head <= l_tail)
{
do
{
int n = l_queue[l_head++];
l_scc[n] = scc; // record the scc the variable belongs to
for (v = 0; v < l_nc[n]; ++v)
r_queue[++r_tail] = l_children[n * r_verts + v];
}
while (l_head <= l_tail);
while (r_head <= r_tail)
{
int n = r_queue[r_head++];
r_scc[n] = scc; // record the scc the value belongs to
if (r_child[n] != -1)
l_queue[++l_tail] = r_child[n];
}
}
// all the nodes from one strongly connected component are in
// l_queue (the variables) and r_queue (the values)
for (j = 0; j < l_head; ++j)
{
int var = l_queue[j];
for (v = 0; v < r_head; ++v)
{
int val = r_queue[v];
if (vgraph[var * r_verts + val] == 1)
{
// _fd_debug("marking %d in %d's domain\n", val + min, VAR(c, var)->index);
vgraph[var * r_verts + val] = 3;
}
}
}
assert(l_head == r_head); // bug catching assert
}
#undef push
#undef pop
#undef top
#undef last_branch
}
/*
Recompute the strongly connected components wrt the updated
matching.
*/
static void _fd_remark_cycles(fd_constraint c, int culprit, int8_t *vgraph,
int r_verts, int *l_edge, int *r_edge,
int *l_scc, int *r_scc, int min)
{
int nvariables = c->nvariables;
int8_t *l_colour, *r_colour;
int *stack, top;
int8_t *type; // type of node in the stack: VARIABLE or VALUE
#define push(x, t) (top++, type[top] = (t), stack[top] = (x))
#define pop() stack[top--]
#define top() stack[top]
#define last_branch stack[top + 1] // the last branch taken when
// leaving the top() node
int *l_finish, *r_finish, time; // finishing times of the DFS algorithm
int *variables; // variables ordered by finishing time
int i, j, v;
int scc; // the scc culprit belonged to
int max_scc;
/* Strongly connected components found through the [...] algorithm
[Cormen]. */
max_scc = scc = l_scc[culprit];
for (i = 0; i < nvariables; ++i)
if (l_scc[i] > max_scc)
max_scc = l_scc[i];
for (i = 0; i < r_verts; ++i)
if (r_scc[i] == scc)
r_scc[i] = -1;
l_colour = alloca(nvariables * sizeof(*l_colour));
r_colour = alloca(r_verts * sizeof(*r_colour));
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
stack = alloca((nvariables + r_verts) * sizeof(*stack));
type = alloca((nvariables + r_verts) * sizeof(*type));
variables = alloca(nvariables * sizeof(*variables));
// forward DFS
top = -1;
l_finish = alloca(nvariables * sizeof(*l_finish));
r_finish = alloca(r_verts * sizeof(*r_finish));
memset(l_finish, -1, nvariables * sizeof(*l_finish));
memset(r_finish, -1, r_verts * sizeof(*r_finish));
time = -1;
for (i = 0, j = nvariables; i < nvariables; ++i)
{
if (l_scc[i] != scc) // only need to update the culprit's scc
continue;
l_scc[i] = -1; // a little cleaning up
if (l_colour[i] != WHITE) // already visited this variable
continue;
push(i, VARIABLE);
l_colour[i] = GREY;
while (top >= 0)
{
switch (type[top])
{
case VARIABLE:
v = l_edge[top()]; // matching edges go from left to right
if (r_colour[v] != WHITE)
{
// matched value already visited
//l_colour[top()] = BLACK;
l_finish[top()] = ++time;
variables[--j] = pop();
break;
}
r_scc[v] = -1; // value belongs to a new SCC (XXX: check!)
push(v, VALUE);
last_branch = -1;
r_colour[v] = GREY;
break;
case VALUE:
for (v = last_branch + 1; v < nvariables; ++v)
if (vgraph[v * r_verts + top()] == 1 &&
l_colour[v] == WHITE)
break;
if (v == nvariables) // no unvisited successor found
{
//r_colour[top()] = BLACK;
r_finish[top()] = ++time;
pop();
break;
}
push(v, VARIABLE); // sets last_branch to v
l_colour[v] = GREY;
break;
}
}
}
// backward DFS
top = -1;
int *l_parent, *r_parent; // depth-first forest
l_parent = alloca(nvariables * sizeof(*l_parent));
r_parent = alloca(r_verts * sizeof(*r_parent));
memset(l_parent, -1, nvariables * sizeof(*l_parent));
memset(r_parent, -1, r_verts * sizeof(*r_parent));
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
for (; j < nvariables; ++j) // j's value corresponds to the last variable
// finished above
{
i = variables[j];
if (l_colour[i] != WHITE) // already visited this variable
continue;
push(i, VARIABLE);
last_branch = -1;
l_colour[i] = GREY;
while (top >= 0)
{
switch (type[top])
{
case VARIABLE:
for (v = last_branch + 1; v < r_verts; ++v)
if (vgraph[top() * r_verts + v] == 1 &&
r_colour[v] == WHITE &&
r_finish[v] > -1 && r_finish[v] < l_finish[i])
break;
if (v == r_verts) // no unvisited successor found
{
//l_colour[top()] = BLACK;
pop();
break;
}
r_parent[v] = top();
push(v, VALUE); // sets last_branch to v
r_colour[v] = GREY;
break;
case VALUE:
v = r_edge[top()]; // matching edges go from right to left
if (l_colour[v] != WHITE)
{
// matched variable already visited
//r_colour[top()] = BLACK;
pop();
break;
}
l_parent[v] = top();
push(v, VARIABLE);
last_branch = -1;
l_colour[v] = GREY;
break;
}
}
}
// identify strongly connected components nodes
int8_t *is_root;
int *l_children, *l_nc, *r_child;
//is_root = alloca(nvariables * sizeof(*is_root));
is_root = l_colour; // XXX: reuse memory
l_children = alloca(nvariables * r_verts * sizeof(*l_children));
//l_nc = alloca(nvariables * sizeof(*l_nc));
l_nc = stack; // XXX: reuse memory
r_child = alloca(r_verts * sizeof(*r_child));
memset(is_root, 1, nvariables * sizeof(*is_root));
memset(l_nc, 0, nvariables * sizeof(*l_nc));
memset(r_child, -1, r_verts * sizeof(*r_child));
/* identify strongly connected components nodes */
// reverse edges of the depth-first search forest
for (i = 0; i < nvariables; ++i)
{
if (l_parent[i] == -1)
continue;
is_root[i] = 0;
r_child[l_parent[i]] = i;
}
for (v = 0; v < r_verts; ++v)
{
if (r_parent[v] == -1)
continue;
i = r_parent[v];
l_children[i * r_verts + l_nc[i]] = v;
l_nc[i]++;
}
// mark values as used
int *l_queue, l_head, l_tail, *r_queue, r_head, r_tail;
//l_queue = alloca(nvariables * sizeof(*l_queue));
l_queue = l_finish; // XXX: reuse memory
//r_queue = alloca(r_verts * sizeof(*r_queue));
r_queue = r_finish; // XXX: reuse memory
int mscc = max_scc;
for (i = 0; i < nvariables; ++i)
{
if (!is_root[i] || l_nc[i] == 0)
continue;
max_scc++;
l_head = l_tail = 0;
l_queue[l_tail] = i;
r_head = 0; r_tail = -1;
while (l_head <= l_tail)
{
do
{
int n = l_queue[l_head++];
l_scc[n] = max_scc; // record the scc the variable belongs to
for (v = 0; v < l_nc[n]; ++v)
r_queue[++r_tail] = l_children[n * r_verts + v];
}
while (l_head <= l_tail);
while (r_head <= r_tail)
{
int n = r_queue[r_head++];
r_scc[n] = max_scc; // record the scc the value belongs to
if (r_child[n] != -1)
l_queue[++l_tail] = r_child[n];
}
}
// all the nodes from one strongly connected component are in
// l_queue (the variables) and r_queue (the values)
for (j = 0; j < l_head; ++j)
{
int var = l_queue[j];
for (v = 0; v < r_head; ++v)
{
int val = r_queue[v];
if (vgraph[var * r_verts + val] == 1)
{
// _fd_debug("marking %d in %d's domain\n", val + min, VAR(c, var)->index);
vgraph[var * r_verts + val] = 3;
}
}
}
assert(l_head == r_head); // bug catching assert
}
// re-mark edges from unaffected strongly connected components
for (v = 0; v < r_verts; ++v)
if (r_scc[v] > -1 && r_scc[v] <= mscc)
for (i = 0; i < nvariables; ++i)
if (l_scc[i] == r_scc[v] && vgraph[i * r_verts + v] == 1)
{
// _fd_debug("marking %d in %d's domain\n", v + min, VAR(c, i)->index);
vgraph[i * r_verts + v] = 3;
}
#undef push
#undef pop
#undef top
#undef last_branch
}
static void _fd_mark_paths(fd_constraint c, int8_t *vgraph, int r_verts,
int *l_edge, int *r_edge, int min)
{
#define vgraph(l, c) vgraph[(l) * r_verts + (c)]
int nvariables = c->nvariables;
int *queue, head, tail; // for the breadth-first search
int8_t *l_colour, *r_colour;
int8_t *type; // type of node in queue: VARIABLE or VALUE
int i;
queue = alloca((nvariables + r_verts) * sizeof(*queue));
type = alloca((nvariables + r_verts) * sizeof(*type));
l_colour = alloca(nvariables * sizeof(*l_colour));
r_colour = alloca(r_verts * sizeof(*r_colour));
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
// find all M-alternating paths starting at an unmatched value
for (i = 0; i < r_verts; ++i)
{
if (r_edge[i] != -1) // skip values matched or not in any domain
continue;
r_colour[i] = GREY;
queue[head = tail = 0] = i;
type[head] = VALUE;
while (head <= tail)
{
switch (type[head])
{
case VARIABLE: // node is a variable
{
int v;
v = l_edge[queue[head]];
if (r_colour[v] == WHITE)
{
r_colour[v] = GREY;
queue[++tail] = v;
type[tail] = VALUE;
}
//l_colour[queue[head]] = BLACK;
head++;
}
break;
case VALUE: // node is a value (domain element)
{
int v;
for (v = 0; v < nvariables; ++v)
{
if (!vgraph(v, queue[head]) ||
vgraph(v, queue[head]) == 2)
continue;
#if DEBUG_MATCH > 1
_fd_debug("marking %d in %d's domain\n",
queue[head] + min, VAR(c, v)->index);
#endif
// this edge is in an M-alternating path
vgraph(v, queue[head]) += 4;
if (l_colour[v] == WHITE)
{
l_colour[v] = GREY;
queue[++tail] = v;
type[tail] = VARIABLE;
}
}
//r_colour[queue[head]] = BLACK;
head++;
}
break;
}
}
}
#undef vgraph
}
#ifndef CONSTRAINT_TEMPS
int _fd_find_matching(fd_constraint c)
#else
int _fd_find_matching(fd_constraint c, int **memory)
#endif
{
int nvariables = c->nvariables;
int size; // constraint memory size
int8_t *vgraph; // value graph
int *l_edge, *r_edge; // matches (matching edges)
int *queue, head, tail; // for the breadth-first search
int8_t *l_colour, *r_colour;
int8_t *type; // type of node in queue: VARIABLE or VALUE
int *l_parent, *r_parent; // M-augmenting path
int min, max, r_verts, nvalues = 0;
int *pmin;
int *l_scc, *r_scc; // strongly connected components
bool done;
int i;
// compute an upper bound on the size of the domains
min = _fd_var_min(VAR(c, 0));
max = _fd_var_max(VAR(c, 0));
for (i = 1; i < nvariables; ++i)
{
int v;
if ((v = _fd_var_min(VAR(c, i))) < min)
min = v;
if ((v = _fd_var_max(VAR(c, i))) > max)
max = v;
}
r_verts = max - min + 1;
// if there are less values in the domains than variables, there is
// no maximum cardinality matching
if (r_verts < nvariables)
return 0;
#ifdef CONSTRAINT_TEMPS
// memory requirement
size = 4 * sizeof(int) + // size, min, r_verts, nvalues
nvariables * sizeof(*l_edge) + r_verts * sizeof(*r_edge) +
nvariables * sizeof(*l_scc) + r_verts * sizeof(*r_scc) +
nvariables * r_verts * sizeof(*vgraph);
if (*memory == NULL)
{
#ifdef DEBUG_MATCH
_fd_debug("all-different memory size is %d\n", size);
#endif
*memory = malloc(size); // XXX: NULL
// save memory block size
**memory = size;
}
else if (**memory < size)
{
// the allocated memory is not enough
#ifdef DEBUG_MATCH
_fd_debug("all-different new memory size is %d\n", size);
#endif
*memory = realloc(*memory, size); // XXX: NULL
// save the new size
**memory = size;
}
assert(sizeof(**memory) == sizeof(size));
assert(sizeof(**memory) == sizeof(min));
assert(sizeof(**memory) == sizeof(r_verts));
assert(sizeof(**memory) == sizeof(nvalues));
assert(sizeof(**memory) == sizeof(*l_edge));
assert(sizeof(*l_edge) == sizeof(*r_edge));
assert(sizeof(*r_edge) == sizeof(*l_scc));
assert(sizeof(*l_scc) == sizeof(*r_scc));
pmin = *memory + 1;
l_edge = pmin + 3;
r_edge = l_edge + nvariables;
l_scc = r_edge + r_verts;
r_scc = l_scc + nvariables;
vgraph = (int8_t *) (r_scc + r_verts);
#else
l_edge = alloca(nvariables * sizeof(*l_edge));
r_edge = alloca(r_verts * sizeof(*r_edge));
l_scc = alloca(nvariables * sizeof(*l_scc));
r_scc = alloca(r_verts * sizeof(*r_scc));
vgraph = alloca(nvariables * r_verts * sizeof(*vgraph));
#endif
l_parent = alloca(nvariables * sizeof(*l_parent));
l_colour = alloca(nvariables * sizeof(*l_colour));
r_parent = alloca(r_verts * sizeof(*r_parent));
r_colour = alloca(r_verts * sizeof(*r_colour));
for (i = 0; i < r_verts; ++i)
r_edge[i] = UNSEEN;
// build the value graph
memset(vgraph, 0, nvariables * r_verts * sizeof(*vgraph));
for (i = 0; i < nvariables; ++i)
{
fd_iterator iterator = _fd_val_iterator(DOMAIN(VAR(c, i)));
// XXX: could count values and just return if all domains have
// nvariables values (or ...)
while (_fd_val_has_next(iterator))
{
int v = _fd_val_next_element(iterator) - min;
vgraph[i * r_verts + v] = 1;
if (r_edge[v] == UNSEEN)
{
r_edge[v]++;
nvalues++;
}
}
_fd_val_iterator_dispose(iterator);
}
// now that the exact number of different values in the domains is
// known, check that there are enough
if (nvalues < nvariables)
return 0;
queue = alloca((nvariables + r_verts) * sizeof(*queue));
type = alloca((nvariables + r_verts) * sizeof(*type));
// try to find a maximum cardinality matching
for (i = 0; i < nvariables; ++i)
{
// find an M-augmenting path from variable i to an unmatched value
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
l_parent[i] = -1;
l_colour[i] = GREY;
queue[head = tail = 0] = i;
type[head] = VARIABLE;
done = false;
while (!done && head <= tail)
{
switch (type[head])
{
case VARIABLE: // node is a variable
{
int v;
for (v = 0; v < r_verts; ++v)
{
if (!vgraph[queue[head] * r_verts + v])
continue;
// this is a little faster than enqueueing all
// the values in the variable domain right away
if (r_edge[v] == -1)
{
// found an M-augmenting path
r_parent[v] = queue[head];
while (v != -1)
{
r_edge[v] = r_parent[v];
l_edge[r_parent[v]] = v;
v = l_parent[r_parent[v]];
}
done = true;
break;
}
if (r_colour[v] == WHITE)
{
r_colour[v] = GREY;
queue[++tail] = v;
type[tail] = VALUE;
r_parent[v] = queue[head];
}
}
//l_colour[queue[head]] = BLACK;
head++;
}
break;
case VALUE: // node is a value (domain element)
// the check for whether it is not yet matched has
// already been made above; the next edge belongs to
// the current matching
if (l_colour[r_edge[queue[head]]] == WHITE)
{
l_colour[r_edge[queue[head]]] = GREY;
l_parent[r_edge[queue[head]]] = queue[head];
queue[++tail] = r_edge[queue[head]];
type[tail] = VARIABLE;
}
//r_colour[queue[head]] = BLACK;
head++;
break;
}
}
if (!done)
return 0;
}
// found a maximum cardinality matching; mark its edges in the value
// graph
for (i = 0; i < nvariables; ++i)
vgraph[i * r_verts + l_edge[i]] = 2;
_fd_mark_cycles(c, vgraph, r_verts, l_edge, r_edge, l_scc, r_scc, min);
if (nvalues != nvariables)
_fd_mark_paths(c, vgraph, r_verts, l_edge, r_edge, min);
// remove unusable values from the variables domains
// XXX: there's a slight performance gain when this is moved to
// the end of _fd_mark_cycles()
for (i = 0; i < nvariables; ++i)
{
int changed = 0;
int v;
for (v = 0; v < r_verts; ++v)
if (vgraph[i * r_verts + v] == 1)
{
#ifdef DEBUG_MATCH
_fd_debug("removing %d from %d's domain\n", v + min,
VAR(c, i)->index);
#endif
changed |= _fd_var_del_val(v + min, VAR(c, i));
vgraph[i * r_verts + v] = 0;
}
else if (vgraph[i * r_verts + v] > 2)
vgraph[i * r_verts + v] = 1; // XXX: make sure it doesn't
// come back to bite us
// later
if (changed)
_fd_revise_connected(c, VAR(c, i));
}
#ifdef CONSTRAINT_TEMPS
*pmin = min;
*(pmin + 1) = r_verts;
*(pmin + 2) = nvalues;
#endif
return 1;
}
int _fd_update_matching(fd_constraint c, fd_int culprit, int *memory)
{
int nvariables = c->nvariables;
int8_t *vgraph;
int *l_edge, *r_edge;
int min, r_verts, nvalues;
int *l_scc, *r_scc;
int ci; // culprit index in the constraint
bool affects_scc;
int i;
// retrieve contents of the constraint memory
min = *(memory + 1); // *memory contains the size of the block
r_verts = *(memory + 2);
nvalues = *(memory + 3);
l_edge = memory + 4;
r_edge = l_edge + nvariables;
l_scc = r_edge + r_verts;
r_scc = l_scc + nvariables;
vgraph = (int8_t *) (r_scc + r_verts);
// find out the index of the changed variable
for (ci = 0; VAR(c, ci) != culprit; ++ci)
;
// update the value graph
for (i = 0; i < r_verts; ++i)
vgraph[ci * r_verts + i] = -vgraph[ci * r_verts + i];
{
fd_iterator iterator = _fd_val_iterator(DOMAIN(culprit));
while (_fd_val_has_next(iterator))
{
int v = _fd_val_next_element(iterator) - min;
vgraph[ci * r_verts + v] = 1;
}
_fd_val_iterator_dispose(iterator);
}
for (i = 0; i < r_verts && vgraph[ci * r_verts + i] >= 0; ++i)
;
// check that culprit's domain has really changed wrt the last
// update of the value graph
if (i == r_verts)
{
vgraph[ci * r_verts + l_edge[ci]] = 2;
return 1; // it hasn't
}
vgraph[ci * r_verts + i] = 0;
affects_scc = r_scc[i] != -1;
for (++i; i < r_verts; ++i)
if (vgraph[ci * r_verts + i] < 0)
{
vgraph[ci * r_verts + i] = 0;
if (r_scc[i] != -1)
affects_scc = true;
}
// if the previously matched value is still in the variable domain,
// there's no need to rebuild the matching
if (vgraph[ci * r_verts + l_edge[ci]])
{
vgraph[ci * r_verts + l_edge[ci]] = 2;
if (affects_scc)
_fd_remark_cycles(c, ci, vgraph, r_verts, l_edge, r_edge, l_scc, r_scc, min);
}
else
{
// must try to update the maximum cardinality matching
int *queue, head, tail; // for the breadth-first search
int8_t *l_colour, *r_colour;
int8_t *type; // type of node in queue: VARIABLE or VALUE
int *l_parent, *r_parent; // M-augmenting path
bool done;
for (i = 0; i < nvariables; ++i)
if (i != ci)
assert(vgraph[i * r_verts + l_edge[i]] == 2),
vgraph[i * r_verts + l_edge[i]] = 1;
r_edge[l_edge[ci]] = -1; // the previously matched value is now free
l_parent = alloca(nvariables * sizeof(*l_parent));
l_colour = alloca(nvariables * sizeof(*l_colour));
r_parent = alloca(r_verts * sizeof(*r_parent));
r_colour = alloca(r_verts * sizeof(*r_colour));
queue = alloca((nvariables + r_verts) * sizeof(*queue));
type = alloca((nvariables + r_verts) * sizeof(*type));
// find an M-augmenting path from culprit to an unmatched value
memset(l_colour, WHITE, nvariables * sizeof(*l_colour));
memset(r_colour, WHITE, r_verts * sizeof(*r_colour));
l_parent[ci] = -1;
l_colour[ci] = GREY;
queue[head = tail = 0] = ci;
type[head] = VARIABLE;
done = false;
while (!done && head <= tail)
{
switch (type[head])
{
case VARIABLE: // node is a variable
{
int v;
for (v = 0; v < r_verts; ++v)
{
if (!vgraph[queue[head] * r_verts + v])
continue;
// this is a little faster than enqueueing all
// the values in the variable domain right away
if (r_edge[v] == -1)
{
// found an M-augmenting path
r_parent[v] = queue[head];
while (v != -1)
{
r_edge[v] = r_parent[v];
l_edge[r_parent[v]] = v;
v = l_parent[r_parent[v]];
}
done = true;
break;
}
if (r_colour[v] == WHITE)
{
r_colour[v] = GREY;
queue[++tail] = v;
type[tail] = VALUE;
r_parent[v] = queue[head];
}
}
//l_colour[queue[head]] = BLACK;
head++;
}
break;
case VALUE: // node is a value (domain element)
// the check for whether it is not yet matched has
// already been made above; the next edge belongs to
// the current matching
if (l_colour[r_edge[queue[head]]] == WHITE)
{
l_colour[r_edge[queue[head]]] = GREY;
l_parent[r_edge[queue[head]]] = queue[head];
queue[++tail] = r_edge[queue[head]];
type[tail] = VARIABLE;
}
//r_colour[queue[head]] = BLACK;
head++;
break;
}
}
if (!done)
return 0;
// found a maximum cardinality matching; mark its edges in the value
// graph
for (i = 0; i < nvariables; ++i)
vgraph[i * r_verts + l_edge[i]] = 2;
_fd_remark_cycles(c, ci, vgraph, r_verts, l_edge, r_edge, l_scc, r_scc, min);
}
if (nvalues != nvariables)
_fd_mark_paths(c, vgraph, r_verts, l_edge, r_edge, min);
// remove unusable values from the variables domains
// XXX: there's a slight performance gain when this is moved to
// the end of _fd_mark_cycles()
for (i = 0; i < nvariables; ++i)
{
int changed = 0;
int v;
for (v = 0; v < r_verts; ++v)
if (vgraph[i * r_verts + v] == 1 && /* {XXX */ r_scc[v] == -1 /* XXX} */)
{
#ifdef DEBUG_MATCH
_fd_debug("removing %d from %d's domain\n", v + min,
VAR(c, i)->index);
#endif
changed |= _fd_var_del_val(v + min, VAR(c, i));
vgraph[i * r_verts + v] = 0;
}
else if (vgraph[i * r_verts + v] > 2)
vgraph[i * r_verts + v] = 1; // XXX: make sure it doesn't
// come back to bite us
// later
if (changed)
{
// XXX: needed because of the imperfect view of the state of
// the variables the function has
if (fd_domain_empty(VAR(c, i)))
return 0;
_fd_revise_connected(c, VAR(c, i));
}
}
return 1;
}