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
//! Dense k-partite problems and shared quotient graphs.
use std::{fmt, sync::Arc};
use crate::{Acc, Cost, LapError, LapSolver, MatrixView, Sense};
/// Missing original vertex in a quotient node.
pub const NONE: u32 = u32::MAX;
/// Invalid input or failure of a constituent LAP.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum KapError {
/// Matrix count does not describe 2 through 64 partites.
BadCount(usize),
/// A matrix dimension disagrees with a previously seen partite size.
ShapeMismatch {
/// Condensed matrix index.
matrix: usize,
/// Original partite index.
partite: usize,
/// Previously established size.
expected: usize,
/// Actual dimension.
actual: usize,
},
/// Invalid part positions, vertex indices, or repeated matching vertices.
BadMatching,
/// The supplied initial solution does not have max(partite sizes) nodes.
IncompleteInitial {
/// Required number of nodes, counting singleton vertices.
expected: usize,
/// Actual number after adding all unlisted vertices as singletons.
actual: usize,
},
/// The supplied gated initial solution contains a forbidden pair.
ForbiddenInitial,
/// A partite is too large for u32 vertex identifiers.
TooManyVertices(usize),
/// Constituent LAP error.
Lap(LapError),
/// An option has an invalid value.
BadOption(&'static str),
/// The request exceeds a documented limit of the algorithm.
Unsupported(&'static str),
}
impl fmt::Display for KapError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadCount(n) => write!(
f,
"invalid matrix count {n}: expected k(k-1)/2 for 2 <= k <= 64"
),
Self::ShapeMismatch {
matrix,
partite,
expected,
actual,
} => write!(
f,
"matrix {matrix}: partite {partite} has size {actual}, expected {expected}"
),
Self::BadMatching => write!(f, "invalid part positions or matching"),
Self::IncompleteInitial { expected, actual } => write!(
f,
"initial must be a complete solution: {expected} nodes expected, {actual} given"
),
Self::ForbiddenInitial => write!(f, "initial places a forbidden pair in one clique"),
Self::TooManyVertices(n) => write!(f, "partite size {n} exceeds u32 vertex capacity"),
Self::Lap(e) => e.fmt(f),
Self::Unsupported(what) => f.write_str(what),
Self::BadOption("initial") => write!(f, "initial is unsupported for this algorithm"),
Self::BadOption("threshold") => write!(
f,
"threshold must be finite, and an integer for integer costs"
),
Self::BadOption(name) => write!(f, "{name} must be a positive integer"),
}
}
}
impl std::error::Error for KapError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Lap(e) => Some(e),
_ => None,
}
}
}
impl From<LapError> for KapError {
fn from(e: LapError) -> Self {
Self::Lap(e)
}
}
fn index(k: usize, a: usize, b: usize) -> usize {
debug_assert!(a < b && b < k);
k * a + b - a * (a + 1) / 2 - a - 1
}
fn write_row<A: Copy>(dst: &mut [std::mem::MaybeUninit<A>], src: &[A]) {
for (d, &x) in dst.iter_mut().zip(src) {
d.write(x);
}
}
/// `dst` = transpose of the row-major `rows x cols` matrix `src`, cache-blocked.
fn transpose_into<A: Copy>(dst: &mut Vec<A>, src: &[A], rows: usize, cols: usize) {
const B: usize = 32;
dst.clear();
if rows * cols == 0 {
return;
}
dst.resize(rows * cols, src[0]);
for r0 in (0..rows).step_by(B) {
for c0 in (0..cols).step_by(B) {
for r in r0..(r0 + B).min(rows) {
for c in c0..(c0 + B).min(cols) {
dst[c * rows + r] = src[r * cols + c];
}
}
}
}
}
#[derive(Clone, Debug)]
struct Dense<A> {
data: Arc<[A]>,
rows: usize,
cols: usize,
}
impl<A: Copy> Dense<A> {
fn view(&self) -> MatrixView<'_, A> {
MatrixView::from_row_major(&self.data, self.rows, self.cols).expect("dense shape")
}
}
/// Original pair costs in combinations order, widened and copied once.
#[derive(Clone, Debug)]
pub struct KPartite<A> {
sizes: Arc<[usize]>,
matrices: Arc<[Dense<A>]>,
gated: bool,
}
impl<A: Acc + Cost<Acc = A>> KPartite<A> {
/// Validate the condensed matrix count and all shared dimensions.
pub fn new<T: Cost<Acc = A>>(mats: &[MatrixView<'_, T>]) -> Result<Self, KapError> {
let k = (2..=64)
.find(|&k| k * (k - 1) / 2 == mats.len())
.ok_or(KapError::BadCount(mats.len()))?;
let mut sizes = vec![None; k];
let mut matrices = Vec::with_capacity(mats.len());
for a in 0..k {
for b in a + 1..k {
let cid = index(k, a, b);
let m = &mats[cid];
for (p, actual) in [(a, m.rows()), (b, m.cols())] {
if actual > u32::MAX as usize {
return Err(KapError::TooManyVertices(actual));
}
if let Some(expected) = sizes[p] {
if actual != expected {
return Err(KapError::ShapeMismatch {
matrix: cid,
partite: p,
expected,
actual,
});
}
} else {
sizes[p] = Some(actual);
}
}
let data = (0..m.rows())
.flat_map(|r| (0..m.cols()).map(move |c| m.get(r, c).widen()))
.collect::<Vec<_>>();
matrices.push(Dense {
data: data.into(),
rows: m.rows(),
cols: m.cols(),
});
}
}
Ok(Self {
sizes: sizes.into_iter().map(Option::unwrap).collect(),
matrices: matrices.into(),
gated: false,
})
}
/// Copy costs shifted by `tau` for optional matching. The unfavorable
/// infinity is forbidden: `+inf` for minimization, `-inf` for maximization.
pub fn gated(&self, tau: f64, sense: Sense) -> Result<Self, KapError> {
let tau = A::from_f64_exact(tau).ok_or(KapError::BadOption("threshold"))?;
let forbidden = match sense {
Sense::Minimize => A::INF,
Sense::Maximize => -A::INF,
};
let mut matrices = Vec::with_capacity(self.matrices.len());
for m in self.matrices.iter() {
let mut data = Vec::with_capacity(m.data.len());
for (i, &value) in m.data.iter().enumerate() {
if !value.is_finite() && value != forbidden {
return Err(LapError::InvalidCost(i / m.cols, i % m.cols).into());
}
let shifted = value
.checked_sub(tau)
.ok_or(KapError::Unsupported("cost overflow"))?;
// Clipping hides non-improving entries from the LAP, so apply
// its entry rule here: quotient sums then have the same
// headroom as without a threshold, and a float shift that
// overflows to the wrong infinity is rejected.
let oriented = match sense {
Sense::Minimize => Some(shifted),
Sense::Maximize => shifted.checked_neg(),
};
if !oriented.is_some_and(Acc::is_valid) {
return Err(LapError::InvalidCost(i / m.cols, i % m.cols).into());
}
data.push(shifted);
}
matrices.push(Dense {
data: data.into(),
rows: m.rows,
cols: m.cols,
});
}
Ok(Self {
sizes: self.sizes.clone(),
matrices: matrices.into(),
gated: true,
})
}
/// Recompute clique and total costs from this problem's pair matrices.
pub(crate) fn recost(&self, solution: &mut KSolution<A>) {
solution.costs.clear();
solution.cost = A::ZERO;
for clique in &solution.cliques {
let mut cost = A::ZERO;
for (i, &(a, r)) in clique.iter().enumerate() {
for &(b, c) in &clique[i + 1..] {
cost = cost
+ self.matrices[index(self.sizes.len(), a, b)]
.view()
.get(r, c);
}
}
solution.costs.push(cost);
solution.cost = solution.cost + cost;
}
}
/// Original partite sizes.
pub fn sizes(&self) -> &[usize] {
&self.sizes
}
}
#[derive(Clone, Debug)]
struct Part<A> {
members: u64,
nodes: Arc<[u32]>,
costs: Arc<[A]>,
}
/// A merge state; unchanged parts and matrices are shared across clones.
#[derive(Clone, Debug)]
pub struct Quotient<A> {
k_orig: usize,
gated: bool,
parts: Vec<Part<A>>,
matrices: Vec<Dense<A>>,
/// Total cost already fixed inside quotient nodes.
pub score: A,
}
/// Reconstructed cliques and unmatched vertices in quotient node order.
/// With a threshold, the objective sums `d - threshold` over pairs within
/// transversal partial cliques, and singleton vertices are free. Reported
/// costs from `k_assignment` sum original pair costs without the shift.
#[derive(Clone, Debug, PartialEq)]
pub struct KSolution<A> {
/// Cliques, each in increasing original partite order.
pub cliques: Vec<Vec<(usize, usize)>>,
/// Internal edge sum of each clique.
pub costs: Vec<A>,
/// Singleton vertices, in quotient node order.
pub free: Vec<(usize, usize)>,
/// Sum of clique costs.
pub cost: A,
}
impl<A: Acc + Cost<Acc = A>> Quotient<A> {
/// Unmerged state of an original problem.
pub fn root(problem: &KPartite<A>) -> Self {
let k = problem.sizes.len();
let parts = problem
.sizes
.iter()
.enumerate()
.map(|(p, &n)| {
let mut nodes = vec![NONE; n * k];
for i in 0..n {
nodes[i * k + p] = i as u32;
}
Part {
members: 1 << p,
nodes: nodes.into(),
costs: vec![A::ZERO; n].into(),
}
})
.collect();
Self {
k_orig: k,
gated: problem.gated,
parts,
matrices: problem.matrices.to_vec(),
score: A::ZERO,
}
}
/// Build a final quotient from a row-major table of original vertices.
/// `NONE` denotes an absent vertex. Every original vertex must occur once,
/// and every row must contain at least one vertex.
pub fn from_nodes(root: &Self, nodes: Vec<u32>) -> Result<Self, KapError> {
Self::from_nodes_impl::<false>(root, nodes)
}
/// Build a warm start, checking integer clique and total sums for overflow.
pub(crate) fn from_nodes_checked(root: &Self, nodes: Vec<u32>) -> Result<Self, KapError> {
Self::from_nodes_impl::<true>(root, nodes)
}
fn from_nodes_impl<const CHECKED: bool>(
root: &Self,
nodes: Vec<u32>,
) -> Result<Self, KapError> {
let add = |a: A, b: A| {
if CHECKED {
a.checked_add(b)
.ok_or(KapError::Unsupported("cost overflow"))
} else {
Ok(a + b)
}
};
let k = root.k_orig;
if root.n_parts() != k || (0..k).any(|p| root.members(p) != 1 << p) || nodes.len() % k != 0
{
return Err(KapError::BadMatching);
}
let mut used: Vec<Vec<bool>> = (0..k).map(|p| vec![false; root.n_nodes(p)]).collect();
let mut costs = Vec::with_capacity(nodes.len() / k);
let mut score = A::ZERO;
for node in nodes.chunks_exact(k) {
if node.iter().all(|&v| v == NONE) {
return Err(KapError::BadMatching);
}
for (p, &v) in node.iter().enumerate() {
if v != NONE {
let Some(seen) = used[p].get_mut(v as usize) else {
return Err(KapError::BadMatching);
};
if *seen {
return Err(KapError::BadMatching);
}
*seen = true;
}
}
let mut cost = A::ZERO;
for a in 0..k {
for b in a + 1..k {
if node[a] != NONE && node[b] != NONE {
cost = add(
cost,
root.matrix(a, b).get(node[a] as usize, node[b] as usize),
)?;
}
}
}
costs.push(cost);
score = add(score, cost)?;
}
if used.iter().flatten().any(|&seen| !seen) {
return Err(KapError::BadMatching);
}
Ok(Self {
k_orig: k,
gated: root.gated,
parts: vec![Part {
members: u64::MAX >> (64 - k),
nodes: nodes.into(),
costs: costs.into(),
}],
matrices: Vec::new(),
score,
})
}
/// Row-major original vertex table of a final quotient.
pub(crate) fn nodes(&self) -> &[u32] {
assert_eq!(self.n_parts(), 1);
&self.parts[0].nodes
}
pub(crate) fn is_gated(&self) -> bool {
self.gated
}
/// Number of current super-partites.
pub fn n_parts(&self) -> usize {
self.parts.len()
}
/// Number of nodes in a current super-partite.
pub fn n_nodes(&self, part: usize) -> usize {
self.parts[part].costs.len()
}
/// Original partites contained in a current super-partite.
pub fn members(&self, part: usize) -> u64 {
self.parts[part].members
}
/// Current pair matrix, transposed when the requested positions are reversed.
pub fn matrix(&self, a: usize, b: usize) -> MatrixView<'_, A> {
if a < b {
self.matrices[index(self.n_parts(), a, b)].view()
} else {
self.matrices[index(self.n_parts(), b, a)].view().t()
}
}
/// Solve the current pair LAP and merge its matched nodes.
pub fn merge_lap(
&self,
a: usize,
b: usize,
solver: &mut LapSolver<A>,
sense: Sense,
) -> Result<Self, KapError> {
let (matching, cost) = self.solve_pair(a, b, solver, sense)?;
self.merge(a, b, &matching, cost)
}
pub(crate) fn solve_pair(
&self,
a: usize,
b: usize,
solver: &mut LapSolver<A>,
sense: Sense,
) -> Result<(Vec<(u32, u32)>, A), KapError> {
self.check_pair(a, b)?;
if self.gated {
let m = self.matrix(a, b);
let improving = |value| match sense {
Sense::Minimize => value < A::ZERO,
Sense::Maximize => value > A::ZERO,
};
let mut clipped = Vec::with_capacity(m.rows() * m.cols());
let mut any = false;
for r in 0..m.rows() {
for c in 0..m.cols() {
let value = m.get(r, c);
// Validate before comparing: an unordered value must not
// silently become zero through the clipping branch.
if value.partial_cmp(&A::ZERO).is_none() {
return Err(LapError::InvalidCost(r, c).into());
}
let keep = improving(value);
any |= keep;
clipped.push(if keep { value } else { A::ZERO });
}
}
if !any {
return Ok((Vec::new(), A::ZERO));
}
#[cfg(test)]
LAP_CALLS.with(|n| n.set(n.get() + 1));
// Any partial matching extends to a maximum one. Clipping makes
// completion non-worsening; dropping zero edges preserves its cost.
let sol = solver.solve(
MatrixView::from_row_major(&clipped, m.rows(), m.cols())?,
sense,
)?;
let mut matching = Vec::new();
let mut cost = A::ZERO;
for (r, c) in sol.row_ind.into_iter().zip(sol.col_ind) {
let value = m.get(r, c);
if improving(value) {
matching.push((r as u32, c as u32));
cost = cost + value;
}
}
return Ok((matching, cost));
}
#[cfg(test)]
LAP_CALLS.with(|n| n.set(n.get() + 1));
let sol = solver.solve(self.matrix(a, b), sense)?;
Ok((
sol.row_ind
.into_iter()
.zip(sol.col_ind)
.map(|(r, c)| (r as u32, c as u32))
.collect(),
sol.cost,
))
}
fn check_pair(&self, a: usize, b: usize) -> Result<(), KapError> {
if a == b || a >= self.n_parts() || b >= self.n_parts() {
Err(KapError::BadMatching)
} else {
Ok(())
}
}
/// Merge a fixed, possibly partial matching, computing its current pair cost.
/// The matching is not checked against forbidden pairs of a gated problem.
pub fn fix(&self, a: usize, b: usize, matching: &[(u32, u32)]) -> Result<Self, KapError> {
self.check_pair(a, b)?;
let m = self.matrix(a, b);
let mut cost = A::ZERO;
for &(r, c) in matching {
if r as usize >= m.rows() || c as usize >= m.cols() {
return Err(KapError::BadMatching);
}
cost = cost + m.get(r as usize, c as usize);
}
self.merge(a, b, matching, cost)
}
pub(crate) fn merge(
&self,
a: usize,
b: usize,
matching: &[(u32, u32)],
cost: A,
) -> Result<Self, KapError> {
self.check_pair(a, b)?;
if a > b {
let reversed: Vec<_> = matching.iter().map(|&(r, c)| (c, r)).collect();
return self.merge(b, a, &reversed, cost);
}
let mut used_a = vec![false; self.n_nodes(a)];
let mut used_b = vec![false; self.n_nodes(b)];
let mut rows = Vec::with_capacity(used_a.len() + used_b.len());
for &(r, c) in matching {
let (r, c) = (r as usize, c as usize);
if r >= used_a.len() || c >= used_b.len() || used_a[r] || used_b[c] {
return Err(KapError::BadMatching);
}
used_a[r] = true;
used_b[c] = true;
rows.push((Some(r), Some(c)));
}
rows.extend(
used_a
.iter()
.enumerate()
.filter(|&(_, &used)| !used)
.map(|(r, _)| (Some(r), None)),
);
rows.extend(
used_b
.iter()
.enumerate()
.filter(|&(_, &used)| !used)
.map(|(c, _)| (None, Some(c))),
);
let mut nodes = Vec::with_capacity(rows.len() * self.k_orig);
let mut costs = Vec::with_capacity(rows.len());
let ab = self.matrix(a, b);
for &(r, c) in &rows {
for p in 0..self.k_orig {
let left = r.map_or(NONE, |r| self.parts[a].nodes[r * self.k_orig + p]);
nodes.push(if left != NONE {
left
} else {
c.map_or(NONE, |c| self.parts[b].nodes[c * self.k_orig + p])
});
}
costs.push(match (r, c) {
(Some(r), Some(c)) => {
(ab.get(r, c) + self.parts[a].costs[r]) + self.parts[b].costs[c]
}
(Some(r), None) => self.parts[a].costs[r],
(None, Some(c)) => self.parts[b].costs[c],
_ => unreachable!(),
});
}
let merged = Part {
members: self.parts[a].members | self.parts[b].members,
nodes: nodes.into(),
costs: costs.into(),
};
let pre: Vec<_> = (0..self.n_parts()).filter(|&p| p != b).collect();
let parts = pre
.iter()
.map(|&p| {
if p == a {
merged.clone()
} else {
self.parts[p].clone()
}
})
.collect();
// `rows` lists the matched pairs, then a's unmatched nodes, then b's.
let k = self.n_parts();
let mut scratch = Vec::new();
let mut matrices = Vec::new();
for i in 0..pre.len() {
for j in i + 1..pre.len() {
let (pi, pj) = (pre[i], pre[j]);
if pi != a && pj != a {
matrices.push(self.matrices[index(k, pi, pj)].clone());
continue;
}
let other = if pi == a { pj } else { pi };
let no = self.n_nodes(other);
let (nr, nc) = if pi == a {
(rows.len(), no)
} else {
(no, rows.len())
};
let mut data = Arc::<[A]>::new_uninit_slice(nr * nc);
let out = Arc::get_mut(&mut data).expect("fresh allocation");
if nr * nc > 0 && pi == a {
// Merged nodes are the rows: add a's row r to b's row c.
// a < other, so (a, other) is stored with a's nodes as rows.
let ma = &self.matrices[index(k, a, other)].data;
let mb: &[A] = if b < other {
&self.matrices[index(k, b, other)].data
} else {
// (other, b) is stored with other's nodes as rows;
// transpose it once so b's rows are contiguous.
let src = &self.matrices[index(k, other, b)].data;
transpose_into(&mut scratch, src, no, self.n_nodes(b));
&scratch
};
for (dst, &(r, c)) in out.chunks_exact_mut(no).zip(&rows) {
match (r, c) {
(Some(r), Some(c)) => {
let ra = &ma[r * no..(r + 1) * no];
let rb = &mb[c * no..(c + 1) * no];
for ((d, &x), &y) in dst.iter_mut().zip(ra).zip(rb) {
d.write(x + y);
}
}
(Some(r), None) => write_row(dst, &ma[r * no..(r + 1) * no]),
(None, Some(c)) => write_row(dst, &mb[c * no..(c + 1) * no]),
_ => unreachable!(),
}
}
} else if nr * nc > 0 {
// Merged nodes are the columns; other < a < b, so both
// (other, a) and (other, b) are stored with other's rows.
let ma = &self.matrices[index(k, other, a)].data;
let mb = &self.matrices[index(k, other, b)].data;
let (na, nb) = (self.n_nodes(a), self.n_nodes(b));
for (o, dst) in out.chunks_exact_mut(nc).enumerate() {
let ra = &ma[o * na..(o + 1) * na];
let rb = &mb[o * nb..(o + 1) * nb];
for (d, &(r, c)) in dst.iter_mut().zip(&rows) {
d.write(match (r, c) {
(Some(r), Some(c)) => ra[r] + rb[c],
(Some(r), None) => ra[r],
(None, Some(c)) => rb[c],
_ => unreachable!(),
});
}
}
}
// SAFETY: the loops above wrote every one of the nr * nc
// slots: chunks_exact_mut covers `out` exactly, since its
// length is nr * nc and the chunk length is nc (no or
// rows.len()), with nr chunks zipped against nr row entries.
let data = unsafe { data.assume_init() };
matrices.push(Dense {
data,
rows: nr,
cols: nc,
});
}
}
Ok(Self {
k_orig: self.k_orig,
gated: self.gated,
parts,
matrices,
score: self.score + cost,
})
}
/// Split a final solution into two portions per node, using original costs.
/// Side 0 always contains original part 0; empty portions are dropped.
/// Returns the two-part quotient and the matching induced by this solution.
pub fn bipartition(&self, root: &Self, mask: u64) -> Result<(Self, Vec<(u32, u32)>), KapError> {
let k = self.k_orig;
let full = u64::MAX >> (64 - k);
if self.n_parts() != 1
|| root.k_orig != k
|| root.n_parts() != k
|| (0..k).any(|p| root.members(p) != 1 << p)
|| mask == 0
|| mask == full
|| mask & !full != 0
{
return Err(KapError::BadMatching);
}
let masks = if mask & 1 != 0 {
[mask, full ^ mask]
} else {
[full ^ mask, mask]
};
let mut counts = vec![0; k];
for node in self.parts[0].nodes.chunks_exact(k) {
for (p, &v) in node.iter().enumerate() {
if v != NONE {
if v as usize >= root.n_nodes(p) {
return Err(KapError::BadMatching);
}
counts[p] += 1;
}
}
}
if (0..k).any(|p| counts[p] != root.n_nodes(p)) {
return Err(KapError::BadMatching);
}
let mut nodes = [Vec::new(), Vec::new()];
let mut matching = Vec::new();
for node in self.parts[0].nodes.chunks_exact(k) {
let mut ids = [None, None];
for side in 0..2 {
if (0..k).any(|p| masks[side] & (1 << p) != 0 && node[p] != NONE) {
ids[side] = Some((nodes[side].len() / k) as u32);
nodes[side].extend(
node.iter()
.enumerate()
.map(|(p, &v)| if masks[side] & (1 << p) != 0 { v } else { NONE }),
);
}
}
if let [Some(r), Some(c)] = ids {
matching.push((r, c));
}
}
let mut parts = Vec::with_capacity(2);
let mut score = A::ZERO;
for (side, nodes) in nodes.into_iter().enumerate() {
let mut costs = vec![A::ZERO; nodes.len() / k];
for a in 0..k {
for b in a + 1..k {
if masks[side] & (1 << a) == 0 || masks[side] & (1 << b) == 0 {
continue;
}
let m = root.matrix(a, b);
for (node, cost) in nodes.chunks_exact(k).zip(&mut costs) {
if node[a] != NONE && node[b] != NONE {
*cost = *cost + m.get(node[a] as usize, node[b] as usize);
}
}
}
}
for &cost in &costs {
score = score + cost;
}
parts.push(Part {
members: masks[side],
nodes: nodes.into(),
costs: costs.into(),
});
}
let (nr, nc) = (parts[0].costs.len(), parts[1].costs.len());
let mut data = vec![A::ZERO; nr * nc];
let mut scratch = Vec::new();
if nr != 0 && nc != 0 {
for a in 0..k {
if masks[0] & (1 << a) == 0 {
continue;
}
for b in 0..k {
if masks[1] & (1 << b) == 0 {
continue;
}
// Orient each original matrix once; its rows are contiguous.
let m: &[A] = if a < b {
&root.matrices[index(k, a, b)].data
} else {
let src = &root.matrices[index(k, b, a)].data;
transpose_into(&mut scratch, src, root.n_nodes(b), root.n_nodes(a));
&scratch
};
let nb = root.n_nodes(b);
for (dst, row) in data
.chunks_exact_mut(nc)
.zip(parts[0].nodes.chunks_exact(k))
{
if row[a] == NONE {
continue;
}
let r = row[a] as usize;
let src = &m[r * nb..(r + 1) * nb];
for (out, col) in dst.iter_mut().zip(parts[1].nodes.chunks_exact(k)) {
if col[b] != NONE {
*out = *out + src[col[b] as usize];
}
}
}
}
}
}
// Without gating, a largest original part fills every node, so the
// induced matching is maximum-cardinality and LAP preserves node count.
// With gating it is a feasible partial matching; solve_pair may drop
// edges and dissolve nodes while improving the shifted objective.
Ok((
Self {
k_orig: k,
gated: self.gated,
parts,
matrices: vec![Dense {
data: data.into(),
rows: nr,
cols: nc,
}],
score,
},
matching,
))
}
/// Induced edges between two original partites, sorted by the first vertex.
pub fn edges_between(&self, a: usize, b: usize) -> Vec<(u32, u32)> {
assert!(a < self.k_orig && b < self.k_orig && a != b);
let mut edges = Vec::new();
for part in &self.parts {
for node in part.nodes.chunks_exact(self.k_orig) {
if node[a] != NONE && node[b] != NONE {
edges.push((node[a], node[b]));
}
}
}
edges.sort_unstable();
edges
}
/// Canonical fingerprint of the vertex partition: for every original vertex,
/// in (partite, vertex) order, the global index of the first member of its
/// node. Two states have equal labels iff they group the vertices alike.
pub fn labels(&self) -> Vec<u32> {
let k = self.k_orig;
let mut sizes = vec![0usize; k];
for part in &self.parts {
for node in part.nodes.chunks_exact(k) {
for (p, &v) in node.iter().enumerate() {
if v != NONE {
sizes[p] += 1;
}
}
}
}
let mut offsets = vec![0usize; k];
for p in 1..k {
offsets[p] = offsets[p - 1] + sizes[p - 1];
}
let mut labels = vec![0u32; offsets[k - 1] + sizes[k - 1]];
for part in &self.parts {
for node in part.nodes.chunks_exact(k) {
let mut first = None;
for (p, &v) in node.iter().enumerate() {
if v != NONE {
let g = offsets[p] + v as usize;
let label = *first.get_or_insert(g as u32);
labels[g] = label;
}
}
}
}
labels
}
/// Reconstruct cliques and singleton vertices (also valid for intermediate states).
pub fn reconstruct(&self) -> KSolution<A> {
let mut sol = KSolution {
cliques: Vec::new(),
costs: Vec::new(),
free: Vec::new(),
cost: A::ZERO,
};
for part in &self.parts {
for (node, &cost) in part.nodes.chunks_exact(self.k_orig).zip(part.costs.iter()) {
let clique: Vec<_> = node
.iter()
.enumerate()
.filter(|&(_, &v)| v != NONE)
.map(|(p, &v)| (p, v as usize))
.collect();
if clique.len() >= 2 {
sol.cliques.push(clique);
sol.costs.push(cost);
sol.cost = sol.cost + cost;
} else {
sol.free.extend(clique);
}
}
}
sol
}
}
#[cfg(test)]
thread_local! { pub(crate) static LAP_CALLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; }
#[cfg(test)]
#[path = "kpartite_tests.rs"]
mod tests;