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
use crate::implementation::{
    debug_assert_graph_has_no_consecutive_dummy_edges, make_graph_eulerian_with_breaking_edges,
    MatchtigEdgeData, PerformanceDataType, RelaxedAtomicBoolSlice, RelaxedAtomicBoolVec,
    TARGET_DIJKSTRA_BLOCK_TIME,
};
use crate::{HeapType, NodeWeightArrayType, TigAlgorithm};
use atomic_counter::{AtomicCounter, RelaxedCounter};
use genome_graph::bigraph::algo::eulerian::{
    compute_eulerian_superfluous_out_biedges,
    compute_minimum_bidirected_eulerian_cycle_decomposition, decomposes_into_eulerian_bicycles,
    find_non_eulerian_binodes_with_differences,
};
use genome_graph::bigraph::interface::dynamic_bigraph::DynamicEdgeCentricBigraph;
use genome_graph::bigraph::interface::static_bigraph::StaticBigraph;
use genome_graph::bigraph::interface::BidirectedData;
use genome_graph::bigraph::traitgraph::index::{GraphIndex, OptionalGraphIndex};
use genome_graph::bigraph::traitgraph::interface::GraphBase;
use genome_graph::bigraph::traitgraph::walks::{EdgeWalk, VecEdgeWalk};
use log::{error, info, warn};
use std::collections::BinaryHeap;
use std::marker::PhantomData;
use std::ops::AddAssign;
use std::sync::Mutex;
use std::time::Instant;
use traitgraph_algo::dijkstra::epoch_array_dijkstra_node_weight_array::EpochNodeWeightArray;
use traitgraph_algo::dijkstra::performance_counters::{
    DijkstraPerformanceCounter, DijkstraPerformanceData, NoopDijkstraPerformanceCounter,
};
use traitgraph_algo::dijkstra::{
    Dijkstra, DijkstraExhaustiveness, DijkstraHeap, DijkstraWeightedEdgeData, NodeWeightArray,
};

/// The greedy matchtigs algorithm.
#[derive(Default)]
pub struct GreedytigAlgorithm<SequenceHandle> {
    _phantom_data: PhantomData<SequenceHandle>,
}

/// The options for the greedy matchtigs algorithm.
pub struct GreedytigAlgorithmConfiguration {
    /// The number of threads to use.
    pub threads: usize,
    /// The k used to build the de Bruijn graph.
    pub k: usize,
    /// If given, enables staged parallelism mode.
    /// In this mode, shortest path queries are limited in the amount of memory they can use, and larger queries are postponed to be executed with less queries in parallel.
    pub staged_parallelism_divisor: Option<f64>,
    /// If staged parallelism mode is on, then the resource limits are calculated based on the existing nodes in the graph times this factor, divided by the number of threads.
    pub resource_limit_factor: usize,
    /// The type of the node weight array used by Dijkstra's algorithm.
    pub node_weight_array_type: NodeWeightArrayType,
    /// The type of the heap used by Dijkstra's algorithm.
    pub heap_type: HeapType,
    /// The type of the performance data collector used by Dijkstra's algorithm.
    pub performance_data_type: PerformanceDataType,
}

impl GreedytigAlgorithmConfiguration {
    /// Construct the configuration for the greedytig algorithm, using default values wherever possible.
    // part of public interface
    #[allow(dead_code)]
    pub fn new(threads: usize, k: usize) -> Self {
        Self {
            threads,
            k,
            staged_parallelism_divisor: None,
            resource_limit_factor: 0,
            node_weight_array_type: NodeWeightArrayType::HashbrownHashMap,
            heap_type: HeapType::StdBinaryHeap,
            performance_data_type: PerformanceDataType::None,
        }
    }
}

impl<Graph: GraphBase, SequenceHandle: Default + Clone> TigAlgorithm<Graph>
    for GreedytigAlgorithm<SequenceHandle>
where
    Graph: DynamicEdgeCentricBigraph + Send + Sync,
    Graph::NodeIndex: Send + Sync,
    Graph::EdgeData: BidirectedData + Eq + Clone + MatchtigEdgeData<SequenceHandle>,
{
    type Configuration = GreedytigAlgorithmConfiguration;

    fn compute_tigs(
        graph: &mut Graph,
        configuration: &Self::Configuration,
    ) -> Vec<VecEdgeWalk<Graph>> {
        compute_greedytigs_choose_heap_type(graph, configuration)
    }
}

fn compute_greedytigs_choose_heap_type<
    NodeIndex: GraphIndex<OptionalNodeIndex> + Send + Sync,
    OptionalNodeIndex: OptionalGraphIndex<NodeIndex>,
    SequenceHandle: Default + Clone,
    EdgeData: BidirectedData + Eq + MatchtigEdgeData<SequenceHandle> + Clone,
    Graph: DynamicEdgeCentricBigraph<
            NodeIndex = NodeIndex,
            OptionalNodeIndex = OptionalNodeIndex,
            EdgeData = EdgeData,
        > + Send
        + Sync,
>(
    graph: &mut Graph,
    configuration: &GreedytigAlgorithmConfiguration,
) -> Vec<VecEdgeWalk<Graph>> {
    match configuration.heap_type {
        HeapType::StdBinaryHeap => {
            compute_greedytigs_choose_node_weight_array_type::<_, _, _, _, _, BinaryHeap<_>>(
                graph,
                configuration,
            )
        }
    }
}

fn compute_greedytigs_choose_node_weight_array_type<
    NodeIndex: GraphIndex<OptionalNodeIndex> + Send + Sync,
    OptionalNodeIndex: OptionalGraphIndex<NodeIndex>,
    SequenceHandle: Default + Clone,
    EdgeData: BidirectedData + Eq + MatchtigEdgeData<SequenceHandle> + Clone,
    Graph: DynamicEdgeCentricBigraph<
            NodeIndex = NodeIndex,
            OptionalNodeIndex = OptionalNodeIndex,
            EdgeData = EdgeData,
        > + Send
        + Sync,
    DijkstraHeapType: DijkstraHeap<usize, Graph::NodeIndex>,
>(
    graph: &mut Graph,
    configuration: &GreedytigAlgorithmConfiguration,
) -> Vec<VecEdgeWalk<Graph>> {
    match configuration.node_weight_array_type {
        NodeWeightArrayType::EpochNodeWeightArray => {
            compute_greedytigs_choose_dijkstra_performance_type::<
                _,
                _,
                _,
                _,
                _,
                DijkstraHeapType,
                EpochNodeWeightArray<_>,
            >(graph, configuration)
        }
        NodeWeightArrayType::HashbrownHashMap => {
            compute_greedytigs_choose_dijkstra_performance_type::<
                _,
                _,
                _,
                _,
                _,
                DijkstraHeapType,
                hashbrown::HashMap<_, _>,
            >(graph, configuration)
        }
    }
}

fn compute_greedytigs_choose_dijkstra_performance_type<
    NodeIndex: GraphIndex<OptionalNodeIndex> + Send + Sync,
    OptionalNodeIndex: OptionalGraphIndex<NodeIndex>,
    SequenceHandle: Default + Clone,
    EdgeData: BidirectedData + Eq + MatchtigEdgeData<SequenceHandle> + Clone,
    Graph: DynamicEdgeCentricBigraph<
            NodeIndex = NodeIndex,
            OptionalNodeIndex = OptionalNodeIndex,
            EdgeData = EdgeData,
        > + Send
        + Sync,
    DijkstraHeapType: DijkstraHeap<usize, Graph::NodeIndex>,
    DijkstraNodeWeightArray: NodeWeightArray<usize>,
>(
    graph: &mut Graph,
    configuration: &GreedytigAlgorithmConfiguration,
) -> Vec<VecEdgeWalk<Graph>> {
    match configuration.performance_data_type {
        PerformanceDataType::None => compute_greedytigs::<
            _,
            _,
            _,
            _,
            _,
            DijkstraHeapType,
            DijkstraNodeWeightArray,
            NoopDijkstraPerformanceCounter,
        >(graph, configuration),
        PerformanceDataType::Complete => compute_greedytigs::<
            _,
            _,
            _,
            _,
            _,
            DijkstraHeapType,
            DijkstraNodeWeightArray,
            DijkstraPerformanceCounter,
        >(graph, configuration),
    }
}

/// Computes greedy matchtigs in the given graph.
fn compute_greedytigs<
    NodeIndex: GraphIndex<OptionalNodeIndex> + Send + Sync,
    OptionalNodeIndex: OptionalGraphIndex<NodeIndex>,
    SequenceHandle: Default + Clone,
    EdgeData: BidirectedData + Eq + MatchtigEdgeData<SequenceHandle> + Clone,
    Graph: DynamicEdgeCentricBigraph<
            NodeIndex = NodeIndex,
            OptionalNodeIndex = OptionalNodeIndex,
            EdgeData = EdgeData,
        > + Send
        + Sync,
    DijkstraHeapType: DijkstraHeap<usize, Graph::NodeIndex>,
    DijkstraNodeWeightArray: NodeWeightArray<usize>,
    DijkstraPerformance: DijkstraPerformanceData + Default + AddAssign + Send,
>(
    graph: &mut Graph,
    configuration: &GreedytigAlgorithmConfiguration,
) -> Vec<VecEdgeWalk<Graph>> {
    let threads = configuration.threads;
    let k = configuration.k;

    info!("Collecting nodes with missing incoming or outgoing edges");
    let mut out_nodes = Vec::new(); // Misses outgoing edges
    let mut in_node_count = 0;
    let in_node_map = RelaxedAtomicBoolVec::new(graph.node_count());
    let mut node_multiplicities = vec![0; graph.node_count()];
    let mut unbalanced_self_mirror_count = 0;

    for node_index in graph.node_indices() {
        let diff = compute_eulerian_superfluous_out_biedges(graph, node_index);
        if graph.is_self_mirror_node(node_index) && diff != 0 {
            in_node_count += 1;
            in_node_map.set(node_index.as_usize(), true);
            node_multiplicities[node_index.as_usize()] = diff;
            out_nodes.push(node_index);
            unbalanced_self_mirror_count += 1;
        } else if diff > 0 {
            in_node_count += 1;
            in_node_map.set(node_index.as_usize(), true);
            node_multiplicities[node_index.as_usize()] = diff;
        } else if diff < 0 {
            out_nodes.push(node_index);
            node_multiplicities[node_index.as_usize()] = diff;
        }
    }

    info!(
        "Found {} nodes with missing outgoing edges",
        out_nodes.len()
    );
    info!("Found {} nodes with missing incoming edges", in_node_count);
    info!(
        "Of those there are {} self-mirrors",
        unbalanced_self_mirror_count
    );

    info!("Computing shortest paths between nodes with missing outgoing and nodes with missing incoming edges");

    let dummy_sequence = SequenceHandle::default();
    let mut dummy_edge_id = {
        info!(
            "Using {} threads to run ~{} dijkstras",
            threads,
            out_nodes.len()
        );
        let start = Instant::now();

        let locked_node_multiplicities: Vec<_> = node_multiplicities
            .iter()
            .copied()
            .map(Mutex::new)
            .collect();
        let executed_dijkstras = RelaxedCounter::default();

        #[allow(clippy::too_many_arguments)]
        fn compute_dijkstras<
            EdgeData: DijkstraWeightedEdgeData<usize>,
            Graph: StaticBigraph<EdgeData = EdgeData>,
            DijkstraNodeWeightArray: NodeWeightArray<usize>,
            DijkstraHeapType: DijkstraHeap<usize, Graph::NodeIndex>,
            DijkstraPerformance: DijkstraPerformanceData + Default,
        >(
            graph: &Graph,
            dijkstra: &mut Dijkstra<Graph, usize, DijkstraNodeWeightArray, DijkstraHeapType>,
            distances: &mut Vec<(Graph::NodeIndex, usize)>,
            shortest_paths: &mut Vec<(Graph::NodeIndex, Graph::NodeIndex, usize)>,
            out_nodes: &[Graph::NodeIndex],
            missed_nodes: RelaxedAtomicBoolSlice,
            dijkstra_performance_limit: usize,
            in_node_map: &RelaxedAtomicBoolVec,
            locked_node_multiplicities: &[Mutex<isize>],
            k: usize,
            executed_dijkstras: &RelaxedCounter,
            output: bool,
            output_step: usize,
            total_dijkstras: usize,
        ) -> DijkstraPerformance {
            let mut last_output = 0;
            let mut dijkstra_performance = DijkstraPerformance::default();

            for (i, &out_node) in out_nodes.iter().enumerate() {
                let out_node_is_self_mirror = graph.is_self_mirror_node(out_node);
                let out_node_mirror = graph.mirror_node(out_node).unwrap();
                // The linter suggestion to use atomic values for locked_node_multiplicities here is spurious,
                // as we explicitly want to update a set of up to four values atomically later on.
                let out_node_multiplicity_lock = locked_node_multiplicities
                    [out_node_mirror.as_usize()]
                .lock()
                .unwrap();
                let mut out_node_multiplicity: isize = *out_node_multiplicity_lock;
                drop(out_node_multiplicity_lock);

                debug_assert!(
                    (0..=4).contains(&out_node_multiplicity),
                    "out_node_multiplicity = {out_node_multiplicity}, out_node_is_self_mirror = {out_node_is_self_mirror}"
                );

                if out_node_multiplicity == 0 {
                    continue;
                }

                while out_node_multiplicity > 0 {
                    let target_amount = (out_node_multiplicity + 1).try_into().unwrap();
                    let dijkstra_status = dijkstra.shortest_path_lens(
                        graph,
                        out_node,
                        in_node_map,
                        target_amount,
                        k - 1,
                        true,
                        distances,
                        dijkstra_performance_limit,
                        dijkstra_performance_limit,
                        dijkstra_performance,
                    );
                    dijkstra_performance = dijkstra_status.performance_data;

                    if distances.is_empty() {
                        missed_nodes.set(
                            i,
                            dijkstra_status.exhaustiveness != DijkstraExhaustiveness::Complete,
                        );

                        // If no in_nodes are reachable, fix this with breaking dummy arcs in post-processing
                        break;
                    }

                    let abort_after_this = distances.len() < target_amount;

                    for &(in_node, distance) in distances.iter() {
                        let mut is_self_mirror_edge = false;
                        if in_node == out_node_mirror {
                            if out_node_multiplicity < 2 {
                                continue;
                            } else {
                                is_self_mirror_edge = true;
                            }
                        };
                        let is_self_mirror_edge = is_self_mirror_edge;
                        debug_assert!(!is_self_mirror_edge || !out_node_is_self_mirror);

                        let in_node_mirror = graph.mirror_node(in_node).unwrap();
                        let in_node_is_self_mirror = graph.is_self_mirror_node(in_node);
                        debug_assert!(!is_self_mirror_edge || !in_node_is_self_mirror);

                        let mut lock_indices = vec![out_node.as_usize()];
                        if !out_node_is_self_mirror {
                            lock_indices.push(out_node_mirror.as_usize());
                        }
                        if !is_self_mirror_edge {
                            lock_indices.push(in_node.as_usize());
                            if !in_node_is_self_mirror {
                                lock_indices.push(in_node_mirror.as_usize());
                            }
                        }
                        let lock_indices = lock_indices;

                        let lock_permutation = permutation::sort(lock_indices.as_slice());
                        let mut locks = vec![None, None, None, None];

                        for lock_rank in 0..lock_indices.len() {
                            let lock_index = lock_permutation.apply_inv_idx(lock_rank);

                            // The linter suggestion to use atomic values for locked_node_multiplicities here is spurious,
                            // as we explicitly want to update a set of up to four values atomically.
                            locks[lock_index] = Some(
                                locked_node_multiplicities[lock_indices[lock_index]]
                                    .lock()
                                    .unwrap(),
                            );
                        }

                        let mut locks: Vec<_> = locks
                            .into_iter()
                            .take(lock_indices.len())
                            .map(|o| o.unwrap())
                            .collect();
                        let in_node_lock_offset = if out_node_is_self_mirror { 1 } else { 2 };
                        let multiplicity_reduction = if is_self_mirror_edge { 2 } else { 1 };

                        if out_node_is_self_mirror {
                            debug_assert!(*locks[0] >= 0);
                            debug_assert!(*locks[0] <= 1);
                            out_node_multiplicity = *locks[0];
                        } else {
                            debug_assert!(*locks[0] <= 0);
                            debug_assert!(*locks[0] >= -4);
                            debug_assert_eq!(*locks[0], -*locks[1]);
                            out_node_multiplicity = -*locks[0];
                        }

                        if out_node_multiplicity == 0 {
                            break;
                        }

                        if !is_self_mirror_edge {
                            if in_node_is_self_mirror {
                                debug_assert!(*locks[in_node_lock_offset] >= 0,
                                              "*locks[in_node_lock_offset] = {}, in_node_lock_offset = {}, out_node_is_self_mirror = {}, in_node_is_self_mirror = {}, is_self_mirror_edge = {}, lock_indices = {:?}, lock_permutation = {:?}, locks = {:?}",
                                              *locks[in_node_lock_offset],
                                              in_node_lock_offset,
                                              out_node_is_self_mirror,
                                              in_node_is_self_mirror,
                                              is_self_mirror_edge,
                                              lock_indices,
                                              lock_permutation,
                                              locks.iter().map(|l| **l).collect::<Vec<_>>());
                                debug_assert!(*locks[in_node_lock_offset] <= 1,
                                              "*locks[in_node_lock_offset] = {}, in_node_lock_offset = {}, out_node_is_self_mirror = {}, in_node_is_self_mirror = {}, is_self_mirror_edge = {}, lock_indices = {:?}, lock_permutation = {:?}, locks = {:?}",
                                              *locks[in_node_lock_offset],
                                              in_node_lock_offset,
                                              out_node_is_self_mirror,
                                              in_node_is_self_mirror,
                                              is_self_mirror_edge,
                                              lock_indices,
                                              lock_permutation,
                                              locks.iter().map(|l| **l).collect::<Vec<_>>());
                            } else {
                                debug_assert!(*locks[in_node_lock_offset] >= 0,
                                              "*locks[in_node_lock_offset] = {}, in_node_lock_offset = {}, in_node_is_self_mirror = {}, is_self_mirror_edge = {}, lock_permutation = {:?}, locks = {:?}",
                                              *locks[in_node_lock_offset],
                                              in_node_lock_offset,
                                              in_node_is_self_mirror,
                                              is_self_mirror_edge,
                                              lock_permutation,
                                              locks.iter().map(|l| **l).collect::<Vec<_>>());
                                debug_assert!(*locks[in_node_lock_offset] <= 4);
                                debug_assert_eq!(
                                    *locks[in_node_lock_offset],
                                    -*locks[in_node_lock_offset + 1]
                                );
                            }

                            let in_node_multiplicity = *locks[in_node_lock_offset];
                            if in_node_multiplicity == 0 {
                                in_node_map.set(in_node.as_usize(), false);
                                continue;
                            }
                        }

                        shortest_paths.push((out_node, in_node, distance));

                        if out_node_is_self_mirror {
                            *locks[0] -= 1;
                            debug_assert!(*locks[0] >= 0);
                            debug_assert!(*locks[0] <= 1);
                        } else {
                            *locks[0] += multiplicity_reduction;
                            *locks[1] -= multiplicity_reduction;
                            debug_assert!(*locks[0] <= 0);
                            debug_assert!(*locks[0] >= -4);
                            debug_assert_eq!(*locks[0], -*locks[1]);
                        }
                        out_node_multiplicity = -(*locks[0]);

                        if !is_self_mirror_edge {
                            *locks[in_node_lock_offset] -= 1;

                            if in_node_is_self_mirror {
                                debug_assert!(*locks[in_node_lock_offset] >= 0);
                                debug_assert!(*locks[in_node_lock_offset] <= 1);
                            } else {
                                *locks[in_node_lock_offset + 1] += 1;
                                debug_assert!(*locks[in_node_lock_offset] >= 0);
                                debug_assert!(*locks[in_node_lock_offset] <= 4);
                                debug_assert_eq!(
                                    *locks[in_node_lock_offset],
                                    -*locks[in_node_lock_offset + 1]
                                );
                            }
                        }

                        if out_node_multiplicity == 0 {
                            in_node_map.set(out_node_mirror.as_usize(), false);
                        }

                        if !is_self_mirror_edge {
                            if *locks[in_node_lock_offset] == 0 {
                                in_node_map.set(in_node.as_usize(), false);
                            }
                        }
                    }

                    if abort_after_this {
                        missed_nodes.set(
                            i,
                            dijkstra_status.exhaustiveness != DijkstraExhaustiveness::Complete,
                        );

                        break;
                    }
                }

                executed_dijkstras.inc();
                if output && executed_dijkstras.get() - last_output > output_step {
                    last_output += output_step;
                    println!(
                        "{}%, ~{} total shortest paths",
                        last_output * 100 / total_dijkstras,
                        (shortest_paths.len() as f64 * total_dijkstras as f64 / i as f64) as u64,
                    );
                }
            }

            dijkstra_performance
        }

        let dijkstra_start = Instant::now();

        let results = Mutex::new(Vec::new());
        let shared_graph = &*graph;
        let mut dijkstra_performance = DijkstraPerformance::default();

        let mut unbalanced_out_nodes = out_nodes;
        let mut missed_nodes = RelaxedAtomicBoolVec::new(unbalanced_out_nodes.len());

        for iteration in 0.. {
            let threads = if iteration == 0 {
                threads
            } else {
                (((threads as f64)
                    / configuration
                        .staged_parallelism_divisor
                        .expect("Iteration > 0 will never be reached if this is None")
                        .powi(iteration)) as usize)
                    .max(1)
            };
            let resource_limit =
                if threads == 1 || configuration.staged_parallelism_divisor.is_none() {
                    usize::MAX
                } else {
                    graph
                        .node_count()
                        .saturating_mul(configuration.resource_limit_factor)
                        / threads
                };
            let offset = Mutex::new(0);

            crossbeam::scope(|scope| {
                info!("Starting {threads} dijkstra threads with a resource limit of {resource_limit} each");
                let mut thread_handles = Vec::new();

                for _ in 0..threads {
                    thread_handles.push(scope.spawn(|_| {
                        let graph = shared_graph;
                        let mut dijkstra =
                            Dijkstra::<_, _, DijkstraNodeWeightArray, DijkstraHeapType>::new(graph);
                        let mut distances = Vec::new();
                        let mut shortest_paths = Vec::new();
                        let mut chunk_size = 1024;
                        let mut dijkstra_performance = DijkstraPerformance::default();

                        loop {
                            let (current_offset, current_limit) = {
                                let mut offset = offset.lock().unwrap();
                                let current_offset = *offset;
                                let remaining_nodes = unbalanced_out_nodes.len() - *offset;

                                if remaining_nodes == 0 {
                                    break;
                                }

                                chunk_size = chunk_size
                                    .min(remaining_nodes / threads)
                                    .max(10)
                                    .max(chunk_size / 10)
                                    .min(remaining_nodes);
                                let current_limit = current_offset + chunk_size;
                                *offset = current_limit;
                                (current_offset, current_limit)
                            };

                            let dijkstra_start = Instant::now();
                            dijkstra_performance += compute_dijkstras(
                                graph,
                                &mut dijkstra,
                                &mut distances,
                                &mut shortest_paths,
                                &unbalanced_out_nodes[current_offset..current_limit],
                                missed_nodes.slice(current_offset..current_limit),
                                resource_limit,
                                &in_node_map,
                                &locked_node_multiplicities,
                                k,
                                &executed_dijkstras,
                                false,
                                unbalanced_out_nodes.len() / 100,
                                unbalanced_out_nodes.len(),
                            );
                            let dijkstra_end = Instant::now();

                            let duration = (dijkstra_end - dijkstra_start).as_secs_f32();
                            chunk_size = ((chunk_size as f32) * (TARGET_DIJKSTRA_BLOCK_TIME / duration))
                                as usize;
                            chunk_size = chunk_size.max(10);
                        }

                        results.lock().unwrap().append(&mut shortest_paths);
                        dijkstra_performance
                    }));
                }

                info!("Waiting for {} dijkstra threads to finish", thread_handles.len());
                for thread_handle in thread_handles {
                    dijkstra_performance += thread_handle.join().unwrap();
                }
            }).unwrap();

            unbalanced_out_nodes = unbalanced_out_nodes
                .iter()
                .zip(missed_nodes.iter())
                .filter_map(|(&out_node, missed)| if missed { Some(out_node) } else { None })
                .collect();
            missed_nodes.reinitialise(unbalanced_out_nodes.len());
            info!(
                "{} remaining unbalanced out nodes",
                unbalanced_out_nodes.len()
            );

            if unbalanced_out_nodes.is_empty() {
                break;
            }
            assert!(configuration.staged_parallelism_divisor.is_some(), "Have nodes where Dijkstra missed part of the search space, but not in staged parallelism mode");
        }

        let results = results.into_inner().unwrap();
        if let (Some(unnecessary_heap_elements), Some(iterations)) = (
            dijkstra_performance.unnecessary_heap_elements(),
            dijkstra_performance.iterations(),
        ) {
            info!(
                "Dijkstras had a factor of {:.3} unnecessary heap elements",
                unnecessary_heap_elements as f64 / iterations as f64
            );
        }
        if let Some(max_max_heap_size) = dijkstra_performance.max_max_heap_size() {
            info!("Dijktras had a maximum maximum heap size of {max_max_heap_size}");
        }
        if let Some(max_max_distance_array_size) =
            dijkstra_performance.max_max_distance_array_size()
        {
            info!("Dijktras had a maximum maximum distance array size of {max_max_distance_array_size}");
        }
        if let Some(average_max_heap_size) = dijkstra_performance.average_max_heap_size() {
            info!("Dijktras had an average maximum heap size of {average_max_heap_size:.0}");
        }
        if let Some(average_max_distance_array_size) =
            dijkstra_performance.average_max_distance_array_size()
        {
            info!(
                "Dijktras had an average maximum heap size of {average_max_distance_array_size:.0}"
            );
        }

        info!("Found {} shortest paths", results.len());
        let dijkstra_time = (Instant::now() - dijkstra_start).as_nanos();

        let mut dummy_edge_id = 0;
        for (out_node, in_node, distance) in results {
            let sequence = dummy_sequence.clone();
            dummy_edge_id += 1;
            let edge_data = EdgeData::new(sequence, true, distance, dummy_edge_id);
            graph.add_edge(out_node, in_node, edge_data.clone());
            graph.add_edge(
                graph.mirror_node(in_node).unwrap(),
                graph.mirror_node(out_node).unwrap(),
                edge_data.mirror(),
            );
        }

        let end = Instant::now();
        info!(
            "Took {:.6}s for computing paths and getting edges, of this {:.6}s are from dijkstra",
            (end - start).as_secs_f64(),
            dijkstra_time as f64 / 1e9
        );
        dummy_edge_id
    };

    debug_assert!(graph.verify_node_pairing());
    debug_assert!(graph.verify_edge_mirror_property());
    debug_assert_graph_has_no_consecutive_dummy_edges(graph, k);

    info!("Making graph Eulerian by adding breaking dummy edges");
    make_graph_eulerian_with_breaking_edges(graph, dummy_sequence, &mut dummy_edge_id, k);

    // Check if the graph now really is Eulerian, and if not, output some debug information
    if !decomposes_into_eulerian_bicycles(graph) {
        let non_eulerian_nodes_and_differences = find_non_eulerian_binodes_with_differences(graph);
        error!(
            "Failed to make the graph Eulerian. Non-Eulerian nodes and differences:\n{:?}",
            non_eulerian_nodes_and_differences
        );
        panic!("Failed to make the graph Eulerian.");
    }
    debug_assert!(graph.verify_node_pairing());
    debug_assert!(graph.verify_edge_mirror_property());
    debug_assert_graph_has_no_consecutive_dummy_edges(graph, k);

    info!("Finding Eulerian bicycle");
    //debug!("{:?}", graph);
    let mut eulerian_cycles = compute_minimum_bidirected_eulerian_cycle_decomposition(graph);
    info!("Found {} Eulerian bicycles", eulerian_cycles.len());

    info!("Breaking Eulerian bicycles at expensive temporary edges");
    let mut greedytigs = Vec::new();

    let mut removed_edges = 0;
    for eulerian_cycle in &mut eulerian_cycles {
        info!(
            "Processing Eulerian bicycle with {} biedges",
            eulerian_cycle.len()
        );
        debug_assert!(eulerian_cycle.is_circular_walk(graph));

        // Rotate cycle such that longest dummy is first edge
        let mut longest_dummy_weight = 0;
        let mut longest_dummy_index = 0;
        for (index, &edge) in eulerian_cycle.iter().enumerate() {
            let edge_data = graph.edge_data(edge);
            if edge_data.is_dummy() && edge_data.weight() > longest_dummy_weight {
                longest_dummy_weight = edge_data.weight();
                longest_dummy_index = index;
            }
        }
        if longest_dummy_weight > 0 {
            eulerian_cycle.rotate_left(longest_dummy_index);
        }

        let mut offset = 0;
        let mut last_edge_is_dummy = false;
        for (current_cycle_index, &current_edge_index) in eulerian_cycle.iter().enumerate() {
            let edge_data = graph.edge_data(current_edge_index);

            if edge_data.is_original() {
                last_edge_is_dummy = false;
            } else {
                if last_edge_is_dummy {
                    warn!(
                        "Found consecutive dummy edges at {}",
                        current_edge_index.as_usize()
                    );
                }
                last_edge_is_dummy = true;
            }

            if (edge_data.weight() >= k && edge_data.is_dummy())
                || (edge_data.is_dummy() && current_cycle_index == 0)
            {
                if offset < current_cycle_index {
                    greedytigs.push(eulerian_cycle[offset..current_cycle_index].to_owned());
                } else if current_cycle_index > 0 {
                    warn!("Found consecutive breaking edges");
                }
                offset = current_cycle_index + 1;
                removed_edges += 1;
            }
        }
        if offset < eulerian_cycle.len() {
            if graph
                .edge_data(*eulerian_cycle.last().unwrap())
                .is_original()
            {
                greedytigs.push(eulerian_cycle[offset..eulerian_cycle.len()].to_owned());
            } else if offset < eulerian_cycle.len() - 1 {
                greedytigs.push(eulerian_cycle[offset..eulerian_cycle.len() - 1].to_owned());
            }
        }
    }

    info!("Found {} expensive temporary edges", removed_edges);
    info!("Found {} greedytigs", greedytigs.len());

    for greedytig in &greedytigs {
        debug_assert!(!greedytig.is_empty());
        debug_assert!(graph.edge_data(*greedytig.first().unwrap()).is_original());
        debug_assert!(graph.edge_data(*greedytig.last().unwrap()).is_original());
    }

    greedytigs
}