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
//! <style>
//! @import url('https://fonts.googleapis.com/css2?family=Fredoka:wght@500&display=swap');
//!
//! .fredoka-title {
//! font-family: "Fredoka", sans-serif;
//! font-optical-sizing: auto;
//! font-weight: 500;
//! font-size: 6vw;
//! font-style: normal;
//! font-variation-settings: "wdth" 100;
//! margin: 0;
//! text-align: center;
//! }
//!
//! .gradient-title {
//! background: #DF0000;
//! background: linear-gradient(to bottom, #DF0000 0%, #850000 100%);
//! -webkit-background-clip: text;
//! -webkit-text-fill-color: transparent;
//! }
//! </style>
//! <p class="fredoka-title">
//! dendroverse<span class="gradient-title">.rs</span>
//! </p>
//! <br>
//!
//! ```text
//! cargo add dendroverse
//! ```
//!
//! ## π€ What is `dendroverse.rs`?
//! This is a Rust crate that enables you to solve computational problems on graphs using dynamic programming
//! over [tree decompositions][td_wiki].
//!
//! ## β
What this crate does for you
//! * Generates optimal tree decompsitions (using [`arboretum_td`][arboretum] under the hood).
//! * Transforms tree decompositions into nice tree decompositions.
//! * Implements a **multi-threaded**, dynamic-programming-based solution process that includes job orchestration for multiple threads.
//! * Constructs a solution using backtracking.
//!
//! ## β What this crate doesn't do for you
//! * It doesnβt automatically generate a solution algorithm based on the problem description
//! β You'll have to implement payloads for each tree decomposition node yourself.
//! * It doesn't understand how a partial solution should be extended during backtracking in order to construct an answer to the problem
//! β You'll have to implement the iterative partial solution expansion yourself.
//! The good news here is that the `dendroverse.rs` interface makes this step as easy as possible by facilitating backlinking.
//!
//! ## π¨ Four steps to use `dendroverse.rs`
//!
//! #### 1. Design a dynamic programming algorithm
//! First and foremost, you should design an algorithm to solve your specific problem using dynamic programming over tree decompositions.
//! Your algorithm can be based on either arbitrary directed tree decompositions or nice tree decompositions.
//! Either way, the important thing is to understand what should be done at each node of the tree decomposition.
//!
//! #### 2. Define a `MemoType` for your problem
//! In our generic code, we use the name `MemoType` to refer to the specific type of **memo** you use.
//! Each tree decomposition node contains a memo, which is a data structure used to store records associated with the node.
//! Memos are sometimes also called **DP-tables**.
//! When using `dendroverse.rs`, it's your responsibility to implement a custom type (typically a struct) to be used as a memo.
//!
//! #### 3. Implement necessary traits
//! You'll have to implement the necessary payloads for the tree decomposition nodes yourself.
//! The exact traits you need to implement for your `MemoType` depend on the type of tree decomposition you want to use.
//! Specifically:
//! * Implement [`DTDMemo`] for your `MemoType` if your algorithm works with arbitrary tree decompositions.
//! * Implement [`NiceDTDMemo`] for your `MemoType` if your algorithm requires nice tree decompositions.
//! * Implement [`BacktrackableMemo`] for your `MemoType` if you want to retrieve solutions after having your problem instance solved.
//!
//! #### 4. Create and solve a [`DendroverseInstance`]
//! When everything's set up, you can finally create and solve a [`DendroverseInstance`].
//! See its documentation for more details.
//!
//! ## π² Relaxed notion of nice tree decompositions
//!
//! The definition of a nice tree decomposition varies from source to source.
//! Generally, the nodes of a nice tree decomposition are divided into four categories:
//! _leaf_, _introduce_, _forget_ and _join_ nodes.
//! Sometimes the root node or the leaf nodes are required to have empty bags.
//! However, these definitions were originally designed to facilitate the **theoretical description** of an algorithm rather than its practical implementation.
//!
//! For this reason, `dendroverse.rs` uses a more relaxed definition of a nice tree decomposition, which is intended to improve the empirical runtime.
//! Specifically, a **nice tree decomposition** is understood to be a directed tree decompsition whose nodes are divided into three categories:
//! * **Leaf nodes.**
//! Nodes with no children.
//! * **Forget-introduce nodes.**
//! Forget and introduce nodes are merged into one category.
//! Similar to the classical nice tree decompositions, these nodes have exactly one child.
//! However, several vertices of the original graph can be forgotten and introduced at once.
//! Hence, the bag of a forget-introduce node can be represented as
//! (\<the bag of the child node\> \\ \<the set of forgotten vertices\>) βͺ \<the set of introduced vertices\>.
//! * **Join node.**
//! Nodes with exactly two children.
//! The bags of the children must be exactly the same as the bag of the join node, just like in the classical nice tree decompositions.
//!
//! Note that a classical nice tree decomposition is a special case of a relaxed nice tree decomposition and, hence, can still be used.
//!
//! ## π§± Additional feature flags
//!
//! The default functionality of `dendroverse.rs` can be extended with the following feature flags:
//!
//! | Feature flag | Description |
//! |:------------:|-------------|
//! | `integrate-petgraph` | If you work with graphs in Rust, you likely use [`petgraph`][petgraph]. In this case, if your original graph is of type `petgraph::Graph<_, _, petgraph::Undirected, Ix>`, then you don't need to create a derivative type in your project and manually implement [`DendroverseOgGraphInterface`] for it. Instead, you can use your graph directly with `dendroverse.rs` by enabling this feature flag. |
//!
//! ## π€ Examples
//!
//! Examples of using `dendroverse.rs` can be found among our [integration tests][examples].
//! There, we've implemented two algorithms for finding a maximum independent set.
//! One uses arbitrary directed tree decompositions, the other relies on nice tree decompositions.
//!
//! ## π Future plans
//!
//! The following features are planned for `dendroverse.rs`:
//! * **Custom tree decompositions.** Currently, only automatically generated tree decompositions can be used with `dendroverse.rs`.
//! See the documentation for [`DendroverseInstance`] for more detail.
//! * **Solutions without backtracking.** When we solve optimisation problems on graphs, it's sometimes sufficient to retrieve an optimal objective
//! value from the tree decomposition's root node.
//! This doesn't require constructing a complete optimal solution and, hence, traversing the entire tree.
//! Currently, the traversal is unavoidable.
//!
//! [td_wiki]: https://en.wikipedia.org/wiki/Tree_decomposition
//! [arboretum]: https://docs.rs/arboretum-td/latest/arboretum_td/index.html
//! [petgraph]: https://docs.rs/petgraph/latest/petgraph/
//! [examples]: https://github.com/forallthereis/dendroverse/tree/master/tests
use VecDeque;
use EdgeRef;
/// # Instance of a problem to be solved
///
/// This struct is your 'access point' to the most important functionality of `dendroverse.rs`.
/// It defines an instance of a computational problem you'd like to solve using dynamic programming over tree decompositions.
///
/// ## Creating a new instance
///
/// Currently, you have the following options to create a new `DendroverseInstance`:
/// * [`DendroverseInstance::<MemoType, _>::with_auto_generated_dtd(...)`][auto_dtds]
/// Use this when you have an original graph and you want to solve your problem using dynamic programming over **arbitrary** directed tree decompositions.
/// This function will generate an optimal directed tree decomposition for your graph automatically.
/// Note that if you want to use this option, your data must satisfy the following additional requirements:
/// * Your original graph must be of type that implements [`DendroverseOgGraphInterface`].
/// * Your `MemoType` must implement `Default` and [`DTDMemo`].
/// * [`DendroverseInstance::<MemoType, _>::with_auto_generated_nice_dtd(...)`][auto_nice_dtds]
/// Use this when you have an original graph and you want to solve your problem using dynamic programming over **nice** tree decompositions.
/// This function will generate an optimal nice tree decomposition for your graph automatically.
/// Note that if you want to use this option, your data must satisfy the following additional requirements:
/// * Your original graph must be of type that implements [`DendroverseOgGraphInterface`].
/// * Your `MemoType` must implement `Default` and [`NiceDTDMemo`].
///
/// ## Solving an instance
///
/// Once a `DendroverseInstance` is successfully created, it can be solved using one of the following methods:
/// * [`instance.solve_using_dtd(...)`][solve_dtds]
/// This method solves the problem using dynamic programming over **arbitrary** directed tree decompositions.
/// This method is only available when your `MemoType` implements [`DTDMemo`].
/// * [`instance.solve_using_nice_dtd(...)`][solve_nice_dtds]
/// This method solves the problem using dynamic programming over **nice** tree decompositions.
/// This method must only be used when the instance was created using one of the following functions:
/// * [`DendroverseInstance::<MemoType, _>::with_auto_generated_nice_dtd(...)`][auto_nice_dtds]
///
/// ## Retrieving a solution
///
/// Once a `DendroverseInstance` is solved, you may want to see a solution to the solved problem.
/// A solution of a solved instance can be retrieved by calling [`instance.solution()`][soln].
/// Note that this method is only available when your `MemoType` implements [`BacktrackableMemo`].
///
/// [auto_dtds]: DendroverseInstance::with_auto_generated_dtd
/// [auto_nice_dtds]: DendroverseInstance::with_auto_generated_nice_dtd
/// [solve_dtds]: DendroverseInstance::solve_using_dtd
/// [solve_nice_dtds]: DendroverseInstance::solve_using_nice_dtd
/// [soln]: DendroverseInstance::solution
/// # Trait for memos that support solution retrieval using backtracking
///
/// While dynamic programming traverses tree decompositions bottom-up to solve a given [`DendroverseInstance`], backtracking traverses the tree
/// decompositions of the solved instance top-down to retrieve the solution.
/// Each iterative step of this top-down traversal must be implemented by you for your `MemoType`.
///
/// Keep in mind that if you use the automatic generation of tree decompositions to create your [`DendroverseInstance`], then each connected
/// component of the original graph will have its own tree decomposition.
/// During backtracking, all tree decompositions will be processed consecutively, in a single thread.
/// # Trait for original graphs supporting the automatic generation of tree decompositions
///
/// Implement this trait for your original graph type if you intend to generate tree decompositions for it
/// automatically.
/// # Trait for memos that support dynamic programming over arbitrary directed tree decompositions
///
/// Keep in mind that if you use the automatic generation of tree decompositions to create your [`DendroverseInstance`], then each connected
/// component of the original graph will have its own directed tree decomposition.
/// In this case, dynamic programming will be applied independently to each available tree decomposition.
///
/// Note also that you don't have to implement [`NiceDTDMemo`] in order to implement `DTDMemo` for your `MemoType`.
/// # Trait for memos that support dynamic programming over nice tree decompositions
///
/// Keep in mind that if you use the automatic generation of tree decompositions to create your [`DendroverseInstance`], then each connected
/// component of the original graph will have its own nice tree decomposition.
/// In this case, dynamic programming will be applied independently to each available tree decomposition.
///
/// Note also that you don't have to implement [`DTDMemo`] in order to implement `NiceDTDMemo` for your `MemoType`.