ebi_optimisation 0.3.9

Optimisation techniques for Ebi - a stochastic process mining utility
Documentation
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
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
use super::network_simplex_value_type::{MulWithFloat, ToBigInt};
use core::convert::From;
use ebi_arithmetic::exact::MaybeExact;
use ebi_arithmetic::rand::rng;
use ebi_arithmetic::rand::seq::SliceRandom;
use ebi_arithmetic::{One, Signed, Zero, malachite::Integer};
use rayon::ThreadPool;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{
    cmp::{PartialEq, PartialOrd},
    fmt::{Debug, Display},
    iter::Sum,
    ops::{AddAssign, MulAssign, Neg, SubAssign},
};

// Enums for representing various problem types, supply types, and arc states

/// Enum for representing the type of problem solved by the Network Simplex algorithm
/// - `Optimal`: The problem is feasible and bounded, and an optimal solution has been found
/// - `Infeasible`: The problem is infeasible, i.e., no feasible solution exists
/// - `Unbounded`: The problem is unbounded, i.e., the objective function can be made arbitrarily small
#[derive(Debug, PartialEq)]
pub enum ProblemType {
    Optimal,
    Infeasible,
    Unbounded,
}

/// Enum for representing the type of supply constraints in the network
/// - `GEQ`: The supply constraints are treated as "greater than or equal to" constraints
/// - `LEQ`: The supply constraints are treated as strict equality constraints
/// If the sum of the supplies is zero, both types are equivalent.
#[derive(Debug, PartialEq)]
pub enum SupplyType {
    GEQ,
    LEQ,
}

/// Enum for representing the state of an arc in the spanning tree representation
/// - `Upper`: The flow on the arc is currently equal to its capacity
/// - `Tree`: The arc is currently part of the spanning tree
/// - `Lower`: The arc is currently not part of the spanning tree
///
/// A i32 representation is used to enable conversion to generic type for calculations.
///
/// # Note
/// LEMON also uses an 'Upper' state, which handles arcs that max out their capacity.
/// This implementation does not use capacities, so the 'Upper' state is not needed.
#[derive(Debug, PartialEq, Clone)]
pub enum ArcState<T> {
    Upper(T),
    Tree(T),
    Lower(T),
}

impl<T> ArcState<T>
where
    T: From<i32>, // Need this to convert from our constants
{
    pub fn upper() -> Self {
        ArcState::Upper(T::from(-1))
    }

    pub fn tree() -> Self {
        ArcState::Tree(T::from(0))
    }

    pub fn lower() -> Self {
        ArcState::Lower(T::from(1))
    }

    pub fn value(&self) -> &T {
        match self {
            ArcState::Upper(v) => v,
            ArcState::Tree(v) => v,
            ArcState::Lower(v) => v,
        }
    }
}

/// Enum for representing the direction of an arc in the spanning tree
/// - `Down`: The arc is oriented away from the root node of the spanning tree
/// - `Up`: The arc is oriented towards the root node of the spanning tree
///
/// A i32 representation is used to enable conversion to generic type for calculations.
#[derive(Debug, PartialEq, Clone)]
pub enum ArcDirection<T> {
    Down(T),
    Up(T),
}

impl<T> ArcDirection<T>
where
    T: From<i32>,
{
    pub fn down() -> Self {
        ArcDirection::Down(T::from(-1))
    }

    pub fn up() -> Self {
        ArcDirection::Up(T::from(1))
    }

    pub fn value(&self) -> &T {
        match self {
            ArcDirection::Down(v) => v,
            ArcDirection::Up(v) => v,
        }
    }
}

/// Epislon value for floating point calculations - may require adjustment depending on problem
const EPSILON: f64 = 1e-15;

/// Authored by Leonhard Mühlmeyer (2024)
/// # Network Simplex Implementation
///
/// This Rust implementation of the Network Simplex algorithm is based on the
/// original implementation from the LEMON (Library for Efficient Modeling and Optimization in Networks) library.
///
/// # Acknowledgment
/// The original C++ code is part of the LEMON library:
///
/// ```text
/// Copyright (C) 2003-2013
/// Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
/// (Egervary Research Group on Combinatorial Optimization, EGRES).
///
/// Permission to use, modify, and distribute the software was granted under the
/// Boost Software License, Version 1.0.
/// ```
///
/// For more details, refer to the original LEMON documentation and license:
/// - [LEMON Library](http://lemon.cs.elte.hu)
/// - Boost Software License: <https://www.boost.org/LICENSE_1_0.txt>
///
/// # Changes to LEMON implementation
/// - This implementation always uses the block search pivot rule, while LEMON allows for different pivot rules. However, the block search pivot rule is the most efficient in practice and also LEMON defaults to it.
/// - This implementation works with floating point types, which is not the case for LEMON. However, this might also be instable in this implemenation (see Notes).
/// - This implementation simplifies the process of executing the algorithm over LEMON's implementation, which includes `reset, resetParams` methods and also uses individual methods for setting the digraph, its supplies and its costs. Here, the constructor method `new` is used to streamline this process.
/// - This implementation does not allow for setting arc capacities, as the original LEMON implementation does.
///
/// # Description
/// The Network Simplex algorithm is a network optimization algorithm that solves the minimum cost flow problem, where every node has a supply or demand and every arc has a cost.<br>
/// This implementation uses the generic type `T` to represent the costs, supplies, and flow values. The NetworkSimplexValueType enum provides three options that are ready to use with this implementation (`i64, BigInt, f64`).<br>
/// First, create a new instance of NetworkSimplex with the graph and costs, supply, and other parameters. Then call the `run` method to solve the problem. Finally, the `get_result` method provides the optimal cost if the problem is feasible and bounded.<br>
///
/// # Example
/// ```ignore
/// use NetworkSimplexValueType::Exact64;
/// NetworkSimplexValueType::set_mode_globally(1);
/// let supply: Vec<NetworkSimplexValueType> =
///     vec![20, 0, 0, -5, -14].into_iter().map(Exact64).collect();
/// let graph_and_costs: Vec<Vec<Option<NetworkSimplexValueType>>> = vec![
///     vec![None, Some(4), Some(4), None, None],
///     vec![None, None, Some(2), Some(2), Some(6)],
///     vec![None, None, None, Some(1), Some(3)],
///     vec![None, None, None, None, Some(2)],
///     vec![None, None, Some(3), None, None],
/// ]
/// .into_iter()
/// .map(|row| row.into_iter().map(|x| x.map(Exact64)).collect())
/// .collect();
/// let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, false, false);
/// if ns.run(false) == ProblemType::Optimal {
///     result = ns.get_result().unwrap();
/// }
/// ```
///
/// # Notes
/// Using the Network Simplex algorithm on floating point types may result in issues caused by rounding errors. To deal with these, an EPSILON constant is used.
/// This may impair the result accuracy. Furthermore, correctness and termination are not guaranteed for all network inputs due to lack of extensive testing.
/// If any issues related to the use of floats come up, <https://pythonot.github.io> might be a helpful reference.
/// Pythonot internally uses an adjusted version of LEMON's Network Simplex algorithm, that is explicitely designed to work with floating point types.
pub struct NetworkSimplex<T> {
    // Data related to the underlying digraph
    node_num: usize,
    all_node_num: usize,
    arc_num: usize,
    all_arc_num: usize,
    search_arc_num: usize,
    node_id: Vec<usize>,
    source: Vec<usize>, // stores node_id of source nodes of arcs
    target: Vec<usize>, // stores node_id of target nodes of arcs
    cost: Vec<T>,       // Cost of each arc
    supply: Vec<T>,     // Supply of each node
    sum_supply: T,
    supply_type: SupplyType,
    flow: Vec<T>, // Flow values for arcs
    pi: Vec<T>,   // Potential (dual variable) for nodes

    // Data for storing the spanning tree structure
    parent: Vec<Option<usize>>, // Parent node in the spanning tree - parent(root) = None
    predecessor: Vec<Option<usize>>, // Predecessor arc in the tree - predecessor(root) = None
    thread: Vec<usize>,         // Threading order in the spanning tree
    reverse_thread: Vec<usize>, // Reverse threading order
    successor_num: Vec<usize>,  // Number of successors in the tree
    last_successor: Vec<usize>, // Last successor in the tree
    predecessor_direction: Vec<ArcDirection<T>>, // Direction of predecessor arc
    state: Vec<ArcState<T>>,    // State of arcs (upper, lower, or tree)
    dirty_revs: Vec<usize>,     // Dirty reverse edges (for edge revision tracking)
    root: usize,                // Root node of the spanning tree

    // Temporary data used in the current pivot iteration
    in_arc: usize,
    join: usize,
    u_in: usize,
    v_in: usize,
    u_out: usize,
    v_out: usize,
    delta: T, // Delta value for flow change
    max: T,   // Maximum value for delta

    // Block search pivot rule parameters
    block_size: usize,
    next_arc: usize,

    // Probem Type to reject get_result requests for infeasible or unbounded problems
    problem_type: Option<ProblemType>,
}

impl<T> NetworkSimplex<T>
where
    T: Zero
        + One
        + MaybeExact
        + MulWithFloat
        + Clone
        + for<'a> AddAssign<&'a T>
        + for<'a> SubAssign<&'a T>
        + for<'a> MulAssign<&'a T>
        + Neg<Output = T>
        + Signed
        + PartialEq
        + PartialOrd
        + ?Sized
        + Display
        + Debug
        + From<i32>
        + Sum
        + Send
        + Sync
        + ToBigInt
        + 'static,
{
    /// Creates a new instance of `NetworkSimplex`.
    ///
    /// # Parameters
    /// - `graph_and_costs`: A reference to a 2D vector where each element represents the cost
    ///   of an arc in the graph. Each inner vector corresponds to a row in the adjacency matrix,
    ///   and `None` indicates the absence of an arc between nodes.
    /// - `supply`: A reference to a vector containing the supply or demand for each node.
    ///   Positive values indicate supply, negative values indicate demand, and zero indicates
    ///   a balanced node.
    /// - `arc_mixing`: A boolean flag that determines whether to apply arc mixing.
    ///   For certain network structures it might increase the stability of the algorithm.
    /// - `greater_eq_supply`: A boolean flag that specifies whether the supply constraints should
    ///   be treated as "greater than or equal to" (`true`) or strict equality (`false`). If `0 <= sum_supply` choose false.
    ///
    /// # Returns
    /// A new instance of `NetworkSimplex` initialized with the given graph structure and parameters.
    pub fn new(
        graph_and_costs: &Vec<Vec<Option<T>>>,
        supply: &Vec<T>,
        arc_mixing: bool,
        greater_eq_supply: bool,
    ) -> Self {
        let node_num = supply.len();

        // Ensure that the graph dimensions match the number of nodes
        assert!(
            graph_and_costs.len() == node_num,
            "Graph size and supply size mismatch"
        );

        // Ensure that the graph is square (all rows must be the same size)
        for row in graph_and_costs.iter() {
            assert!(row.len() == node_num, "Graph matrix not square");
        }

        let node_id: Vec<usize> = (0..node_num).collect();
        let supply = (*supply).clone(); // No need to change the supplies

        // Create arcs from the graph and costs matrix
        let mut source = vec![];
        let mut target = vec![];
        let mut cost = vec![];
        for i in 0..node_num {
            for j in 0..node_num {
                if let Some(c) = &graph_and_costs[i][j] {
                    // Could as well allow for self loops: cost>0 -> ignore, cost<0 -> Unbounded if connected to some supply
                    assert!(i != j, "Tried to add arc from node to itself");
                    source.push(i);
                    target.push(j);
                    cost.push((*c).clone());
                }
            }
        }
        let arc_num = cost.len();

        // Shuffle the arcs if arc_mixing is enabled -> might be beneficial for stability in some cases
        if arc_mixing {
            // Combine the data into a single collection
            let mut arcs: Vec<_> = source
                .iter()
                .zip(target.iter())
                .zip(cost.iter())
                .map(|((src, tgt), cst)| (*src, *tgt, cst.clone()))
                .collect();

            // Shuffle the combined data
            let mut rng = rng();
            arcs.shuffle(&mut rng);

            // Unpack the data back into separate vectors
            source.clear();
            target.clear();
            cost.clear();

            for (src, tgt, cst) in arcs {
                source.push(src);
                target.push(tgt);
                cost.push(cst);
            }
        }

        let block_size_factor = 1.0;
        let min_block_size = 10;
        let block_size =
            ((block_size_factor * (arc_num as f64).sqrt()) as usize).max(min_block_size);

        let supply_type = if greater_eq_supply {
            SupplyType::GEQ
        } else {
            SupplyType::LEQ
        };

        let ns = NetworkSimplex {
            // Data related to the underlying digraph
            node_num,
            all_node_num: node_num,
            arc_num,
            all_arc_num: arc_num,
            search_arc_num: 0,

            // Parameters of the problem
            sum_supply: T::zero(),

            // Data structures for storing the digraph
            node_id,
            source,
            target,

            // Node and arc data
            cost,
            supply,
            flow: vec![],
            pi: vec![],

            // Data for storing the spanning tree structure
            parent: vec![],
            predecessor: vec![],
            thread: vec![],
            reverse_thread: vec![],
            successor_num: vec![],
            last_successor: vec![],
            predecessor_direction: vec![],
            state: vec![],
            dirty_revs: vec![],
            root: 0,

            // Temporary data used in the current pivot iteration
            in_arc: 0,
            join: 0,
            u_in: 0,
            v_in: 0,
            u_out: 0,
            v_out: 0,
            delta: T::zero(),

            // Maximum value for delta
            max: T::one(),

            // Block search pivot rule parameters
            block_size,
            next_arc: 0,

            problem_type: None,
            supply_type,
        };

        ns
    }

    /// DEBUG function
    /// Might be useful for debugging if unclear whether the network is set up correctly
    pub fn visualize_network(&self) {
        let mut nodes_output = String::new();
        for i in 0..self.node_id.len() {
            let node = self.node_id[i];
            let supply = &self.supply[i];
            nodes_output.push_str(&format!("{}({})", node, supply));
            if i < self.node_id.len() - 1 {
                nodes_output.push_str(", ");
            }
        }
        // log::debug!("nodes: [{}]", nodes_output);
        let mut arcs_output = String::new();
        for i in 0..self.all_arc_num {
            let source = self.source[i];
            let target = self.target[i];
            let cost = &self.cost[i];
            arcs_output.push_str(&format!("{}--({})-->{}", source, cost, target));
            if i < self.all_arc_num - 1 {
                arcs_output.push_str(", ");
            }
        }
        // log::debug!("arcs: {}", arcs_output);
    }

    /// DEBUG function
    /// Might be useful for debugging if suspected that tree update is not working correctly
    pub fn visualize_tree_graphviz(&self) -> String {
        let mut graphviz_code = String::new();
        graphviz_code.push_str("digraph Tree {\n");

        // Label the root node
        graphviz_code.push_str(&format!(
            "    {} [label=\"{} (Root)\", shape=box];\n",
            self.root, self.root
        ));

        for i in 0..self.all_node_num {
            if self.parent[i] != None {
                let parent = self.parent[i].unwrap();
                let direction = &self.predecessor_direction[i];
                let flow = &self.flow[self.predecessor[i].unwrap()];
                if direction.value() == &T::from(1) {
                    graphviz_code
                        .push_str(&format!("    {} -> {} [label=\"{}\"];\n", i, parent, *flow));
                } else {
                    graphviz_code
                        .push_str(&format!("    {} -> {} [label=\"{}\"];\n", parent, i, *flow));
                }
            }
        }
        graphviz_code.push_str("}\n");
        graphviz_code
    }

    /// Central function performing the primal network simplex algorithm
    ///
    /// # Parameters
    /// - `guarantee_network_feasibility`: if true the algorithm will ignore the final sanity feasibility check whether any flow is left on artificial arcs
    ///
    /// # Returns
    /// The problem type of the network: Optimal, Infeasible, or Unbounded
    ///
    /// # Algorithm
    /// 1. Create initial basic solution (see `initialize_feasible_solution`). If this fails, return Infeasible
    /// 2. WHILE Find entering arc using block search pivot rule is successful (closes a circle within the spanning tree; see `find_entering_arc`)
    ///    - Find join node (node on closed cycle that is closest to the root node; see `find_join_node`)
    ///    - Identify arc that should leave the basis (see `find_leaving_arc`)
    ///    - Update the flow along the cycle (see `change_flow`)
    ///    - Adjust the spanning tree representation (see `update_tree_structure`)
    ///    - Update the potentials where necessary (see `update_potential`)
    /// 3. Check feasibility: any remaining flow on artificial arcs? (only if `guarantee_network_feasibility` is false)
    ///    - If so, return Infeasible
    ///    - Otherwise, return Optimal
    pub fn run(&mut self, guarantee_network_feasibility: bool) -> ProblemType {
        if !self.initialize_feasible_solution() {
            self.problem_type = Some(ProblemType::Infeasible);
            log::info!("Could not initialize feasible solution");
            return ProblemType::Infeasible;
        }
        // log::debug!("{}", self.visualize_tree_graphviz());
        // log::debug!("Potential: {:?}", self.pi);
        let mut iter = 1;

        let num_threads = rayon::current_num_threads();
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(num_threads)
            .build()
            .unwrap();

        //while self.find_entering_arc() {
        while self.find_entering_arc_par(&pool) {
            // log::debug!("_____________________________\nIteration: {}", iter);
            iter += 1;

            // log::debug!(
            //     "Entering arc: {}-->{}",
            //     self.source[self.in_arc],
            //     self.target[self.in_arc]
            // );

            self.find_join_node();
            let change = self.find_leaving_arc();
            // if a cycle with negative cost is found, the network is unbounded
            if self.delta >= self.max {
                self.problem_type = Some(ProblemType::Unbounded);
                log::info!("The current Network is unbounded");
                return ProblemType::Unbounded;
            }

            self.change_flow(change);
            if change {
                // log::debug!(
                //     "Leaving arc: {}-->{} with delta {}",
                //     self.source[self.predecessor[self.u_out].unwrap()],
                //     self.target[self.predecessor[self.u_out].unwrap()],
                //     self.delta
                // );

                self.update_tree_structure();
                self.update_potential(); // update the dual solution for the next iteration
                // log::debug!("Potential updated");
                // log::debug!("Potential: {:?}", self.pi);
                // log::debug!("{}", self.visualize_tree_graphviz());
            }
        }
        log::info!("Network Simplex finished in {} iterations", iter);

        // check feasibility: any remaining flow on artificial arcs?
        if !guarantee_network_feasibility {
            // for floating point types T, check if flow is close to zero; for integer types, check if flow is zero
            if !T::is_exact(&self.sum_supply) {
                for e in self.search_arc_num..self.all_arc_num {
                    // there might be some rounding errors. Increase/scale the epsilon if necessary
                    if self.flow[e] > T::one().mul_with_float(&EPSILON) {
                        self.problem_type = Some(ProblemType::Infeasible);
                        log::info!(
                            "The current Network is infeasible, flow remains on artificial arcs"
                        );
                        return ProblemType::Infeasible;
                    }
                }
            } else {
                for e in self.search_arc_num..self.all_arc_num {
                    if self.flow[e] != T::zero() {
                        self.problem_type = Some(ProblemType::Infeasible);
                        log::info!(
                            "The current Network is infeasible, flow remains on artificial arcs"
                        );
                        return ProblemType::Infeasible;
                    }
                }
            }
        }

        self.problem_type = Some(ProblemType::Optimal);
        log::info!("Optimal solution found");
        return ProblemType::Optimal;
    }

    /// Internal function:
    /// Uses Block Search Pivot Rule to find the entering arc
    /// For each arc in the current block (block_size), the potential deterioration is calculated.
    /// The arc with the most negative deterioration (biggest improvement) is selected as the entering arc.
    /// If the block is exhausted and no improving arc has been found, the next block is started.
    fn find_entering_arc(&mut self) -> bool {
        let mut cost: T;
        let mut min_cost = T::zero();
        let mut count = self.block_size;

        // First loop from next_arc to _search_arc_num
        for e in self.next_arc..self.search_arc_num {
            cost = self.cost[e].clone();
            cost += &self.pi[self.source[e]];
            cost -= &self.pi[self.target[e]];
            cost *= self.state[e].value();

            log::trace!(
                "{}-->{}, cost: {} = {} * ({} + {} - {})",
                self.source[e],
                self.target[e],
                cost,
                self.state[e].value(),
                self.cost[e],
                self.pi[self.source[e]],
                self.pi[self.target[e]]
            );
            if cost < min_cost {
                min_cost = cost;
                self.in_arc = e;
            }
            count -= 1;
            // block exhausted, check if a valid arc was found
            if count == 0 {
                if !T::is_exact(&min_cost) {
                    // Floating-point specific logic
                    let source_value = self.pi[self.source[self.in_arc]].clone().abs();
                    let target_value = self.pi[self.target[self.in_arc]].clone().abs();
                    let cost_value = self.cost[self.in_arc].clone().abs();

                    let mut a = if source_value > target_value {
                        source_value
                    } else {
                        target_value
                    };
                    a = if a > cost_value { a } else { cost_value };

                    if min_cost < -a.mul_with_float(&EPSILON) {
                        self.next_arc = e;
                        return true;
                    }
                } else {
                    // Integer logic
                    if min_cost < T::zero() {
                        self.next_arc = e;
                        return true;
                    }
                }
                // reset count for next block
                count = self.block_size;
            }
        }

        // Second loop from 0 to next_arc. Only used if the end of the arc vector is reached before the block is exhausted.
        // -> continue search from start
        for e in 0..self.next_arc {
            cost = self.cost[e].clone();
            cost += &self.pi[self.source[e]];
            cost -= &self.pi[self.target[e]];
            cost *= self.state[e].value();
            log::trace!(
                "{}-->{}, cost: {} = {} * ({} + {} - {})",
                self.source[e],
                self.target[e],
                cost,
                self.state[e].value(),
                self.cost[e],
                self.pi[self.source[e]],
                self.pi[self.target[e]]
            );
            if cost < min_cost {
                min_cost = cost;
                self.in_arc = e;
            }
            count -= 1;
            if count == 0 {
                if min_cost < T::zero() {
                    self.next_arc = e;
                    return true;
                }
                count = self.block_size;
            }
            // block exhausted, check if a valid arc was found
            if count == 0 {
                if !T::is_exact(&min_cost) {
                    // Floating-point specific logic
                    let source_value = self.pi[self.source[self.in_arc]].clone().abs();
                    let target_value = self.pi[self.target[self.in_arc]].clone().abs();
                    let cost_value = self.cost[self.in_arc].clone().abs();

                    let mut a = if source_value > target_value {
                        source_value
                    } else {
                        target_value
                    };
                    a = if a > cost_value { a } else { cost_value };

                    if min_cost < -a.mul_with_float(&EPSILON) {
                        self.next_arc = e;
                        return true;
                    }
                } else {
                    // Integer logic
                    if min_cost < T::zero() {
                        self.next_arc = e;
                        return true;
                    }
                }
                // reset count for next block
                count = self.block_size;
            }
        }

        // Check if a valid arc was found

        if !T::is_exact(&min_cost) {
            // Floating-point specific logic
            let source_value = self.pi[self.source[self.in_arc]].clone().abs();
            let target_value = self.pi[self.target[self.in_arc]].clone().abs();
            let cost_value = self.cost[self.in_arc].clone().abs();

            let mut a = if source_value > target_value {
                source_value
            } else {
                target_value
            };
            a = if a > cost_value { a } else { cost_value };

            if min_cost >= -a.mul_with_float(&EPSILON) {
                return false;
            }
        } else {
            // Integer logic
            if min_cost >= T::zero() {
                return false;
            }
        }

        true
    }

    fn find_entering_arc_par(&mut self, pool: &ThreadPool) -> bool {
        self.find_entering_arc_par_recursive(pool, 0)
    }

    fn find_entering_arc_par_recursive(&mut self, pool: &ThreadPool, arcs_visited: usize) -> bool {
        let num_threads = pool.current_num_threads();
        let block_size_per_thread = self.block_size / num_threads;
        let arcs_per_thread = self.search_arc_num / num_threads;
        if block_size_per_thread == 0 || arcs_per_thread < 500 {
            return self.find_entering_arc();
        }

        // Shared state between threads
        let min_cost = Arc::new(parking_lot::Mutex::new(T::zero()));
        let min_arc = Arc::new(AtomicUsize::new(0));

        let cost = &self.cost;
        let pi = &self.pi;
        let source = &self.source;
        let target = &self.target;
        let state = &self.state;
        let next_arc = self.next_arc;
        let search_arc_num = self.search_arc_num;

        pool.install(|| {
            pool.scope(|scope| {
                for thread_idx in 0..num_threads {
                    let min_cost = Arc::clone(&min_cost);
                    let min_arc = Arc::clone(&min_arc);

                    scope.spawn(move |_| {
                        let start = next_arc + thread_idx * arcs_per_thread;
                        let end = start + arcs_per_thread;

                        let mut thread_min_cost = T::zero();
                        let mut thread_min_arc = start;
                        let mut first_iteration = true;

                        let mut current = start;
                        while current < end {
                            let e = if current >= search_arc_num {
                                current - search_arc_num
                            } else {
                                current
                            };

                            let mut cost = cost[e].clone();
                            cost += &pi[source[e]];
                            cost -= &pi[target[e]];
                            cost *= state[e].value();

                            if first_iteration || cost < thread_min_cost {
                                thread_min_cost = cost;
                                thread_min_arc = e;
                                first_iteration = false;
                            }

                            current += 1;
                        }

                        if thread_min_cost < T::zero() {
                            let mut global_min = min_cost.lock();
                            if first_iteration || thread_min_cost < *global_min {
                                *global_min = thread_min_cost;
                                min_arc.store(thread_min_arc, Ordering::Relaxed);
                            }
                        }
                    });
                }
            });
        });

        let final_min_cost = min_cost.lock().clone();
        let final_min_arc = min_arc.load(Ordering::Relaxed);

        // Calculate how many arcs we processed in this block
        let arcs_in_block = num_threads * arcs_per_thread;
        let new_arcs_visited = arcs_visited + arcs_in_block;

        // Update struct fields
        self.next_arc = (next_arc + arcs_in_block) % search_arc_num;
        self.in_arc = final_min_arc;

        // Check if a valid arc was found
        let valid_arc_found = if !T::is_exact(&final_min_cost) {
            let source_value = self.pi[self.source[self.in_arc]].clone().abs();
            let target_value = self.pi[self.target[self.in_arc]].clone().abs();
            let cost_value = self.cost[self.in_arc].clone().abs();

            let mut a = if source_value > target_value {
                source_value
            } else {
                target_value
            };
            a = if a > cost_value { a } else { cost_value };

            final_min_cost < -a.mul_with_float(&EPSILON)
        } else {
            final_min_cost < T::zero()
        };

        if valid_arc_found {
            true
        } else if new_arcs_visited >= search_arc_num {
            // We've checked at least as many arcs as exist in total
            false
        } else {
            // Continue searching with the next block
            self.find_entering_arc_par_recursive(pool, new_arcs_visited)
        }
    }

    /// Internal function:
    /// find arc that should leave the basis
    /// i.e. the arc with the minimum flow (primal solution) that is oriented against the closed cycle
    /// returns true iff a leaving arc could be identified
    fn find_leaving_arc(&mut self) -> bool {
        let first;
        let second;
        if self.state[self.in_arc].value() == &T::from(1) {
            first = self.source[self.in_arc];
            second = self.target[self.in_arc];
        } else {
            first = self.target[self.in_arc];
            second = self.source[self.in_arc];
        }

        self.delta = self.max.clone();
        let mut result = 0;
        let mut d;
        let mut e;

        // search tree from first node to join node
        let mut u = Some(first);
        while let Some(u_node) = u {
            if u_node == self.join {
                break;
            }
            e = self.predecessor[u_node].unwrap();
            d = &self.flow[e];
            if self.predecessor_direction[u_node].value() == &T::from(-1) {
                d = &self.max;
            }
            if *d < self.delta {
                self.delta = d.clone();
                self.u_out = u_node;
                result = 1;
            }
            u = self.parent[u_node];
        }

        // search tree from second node to join node
        let mut u = Some(second);
        while let Some(u_node) = u {
            if u_node == self.join {
                break;
            }
            e = self.predecessor[u_node].unwrap();
            d = &self.flow[e];
            if self.predecessor_direction[u_node].value() == &T::from(1) {
                d = &self.max;
            }
            if *d < self.delta {
                self.delta = d.clone();
                self.u_out = u_node;
                result = 2;
            }
            u = self.parent[u_node];
        }
        if result == 1 {
            self.u_in = first;
            self.v_in = second;
        } else {
            self.u_in = second;
            self.v_in = first;
        }
        return result != 0;
    }

    /// Internal function:
    /// Function to update potentials after flow changes
    /// All potentials of the successors of u_in are updated
    fn update_potential(&mut self) {
        let mut sigma = -self.cost[self.in_arc].clone();
        sigma *= &(self.predecessor_direction[self.u_in].value());
        sigma += &self.pi[self.v_in];
        sigma -= &self.pi[self.u_in];

        let end = self.thread[self.last_successor[self.u_in]];
        // log::debug!("u_in: {}, end: {}", self.u_in, end);
        let mut u = self.u_in;
        while u != end {
            // log::trace!("Potential updated, u: {}, end: {}", u, end);
            self.pi[u] += &sigma;
            u = self.thread[u];
        }
    }

    /// Internal function:
    /// Initializes flows and potentials
    /// adds artificial root node, connects all nodes to it (orienation based on supply)
    /// this is the initial basis (feasible solution)
    fn initialize_feasible_solution(&mut self) -> bool {
        // no nodes in the graph
        if self.node_num == 0 {
            log::info!("No nodes in the graph");
            return false;
        }
        // check if sum of supply is valid
        self.sum_supply = T::zero();
        for i in 0..self.node_num {
            self.sum_supply += &self.supply[i];

            if self.supply[i].is_positive() {
                self.max += &self.supply[i]
            }
        }
        if !((self.supply_type == SupplyType::GEQ && self.sum_supply <= T::zero())
            || (self.supply_type == SupplyType::LEQ && self.sum_supply >= T::zero()))
        {
            log::info!("Sum of supply is invalid, try changing supply type");
            return false;
        }

        let mut max_cost = self.find_max_cost();
        max_cost += &T::one();
        max_cost *= &T::from(self.node_num as i32);
        let art_cost: T = max_cost;

        // log::debug!("art_cost identified as: {}", art_cost);

        // resize all vectors
        self.all_node_num = self.node_num + 1;
        let max_arc_num = self.arc_num + 2 * self.node_num;

        self.all_arc_num = self.arc_num + self.node_num;
        self.source.resize(max_arc_num, 0);
        self.target.resize(max_arc_num, 0);
        self.flow.resize(max_arc_num, T::zero());
        self.state.resize(max_arc_num, ArcState::lower());
        self.cost.resize(max_arc_num, T::zero());
        self.supply.resize(self.all_node_num, T::zero());
        self.pi.resize(self.all_node_num, T::zero());
        self.parent.resize(self.all_node_num, Some(0));
        self.predecessor.resize(self.all_node_num, Some(0));
        self.predecessor_direction
            .resize(self.all_node_num, ArcDirection::up());
        self.thread.resize(self.all_node_num, 0);
        self.reverse_thread.resize(self.all_node_num, 0);
        self.successor_num.resize(self.all_node_num, 0);
        self.last_successor.resize(self.all_node_num, 0);

        // initialize arc network arcs
        for i in 0..self.node_num {
            self.flow[i] = T::zero();
            self.state[i] = ArcState::lower();
        }

        // set up artificial root node
        self.root = self.node_num;
        self.node_id.push(self.root);
        self.parent[self.root] = None;
        self.predecessor[self.root] = None;
        self.thread[self.root] = 0;
        self.reverse_thread[0] = self.root;
        self.successor_num[self.root] = self.node_num + 1; // including root
        self.last_successor[self.root] = self.node_num - 1;
        self.supply[self.root] = -self.sum_supply.clone();
        self.pi[self.root] = T::zero();

        // set up aticficial arcs (i, root node) for b_i >= 0, (root node, i) for b_i < 0
        if self.sum_supply == T::zero() {
            self.search_arc_num = self.arc_num;
            let mut e = self.arc_num;
            for u in 0..self.node_num {
                self.parent[u] = Some(self.root);
                self.predecessor[u] = Some(e);
                self.thread[u] = u + 1;
                self.reverse_thread[u + 1] = u;
                self.successor_num[u] = 1;
                self.last_successor[u] = u;
                self.state[e] = ArcState::tree();
                if !self.supply[u].is_negative() {
                    self.predecessor_direction[u] = ArcDirection::up();
                    self.pi[u] = T::zero();
                    self.source[e] = u;
                    self.target[e] = self.root;
                    self.flow[e] = self.supply[u].clone();
                    self.cost[e] = T::zero();
                } else {
                    self.predecessor_direction[u] = ArcDirection::down();
                    self.pi[u] = art_cost.clone();
                    self.source[e] = self.root;
                    self.target[e] = u;
                    self.flow[e] = -self.supply[u].clone();
                    self.cost[e] = art_cost.clone();
                }
                e += 1;
            }
        } else if self.sum_supply > T::zero() {
            // LEQ supply constraints
            self.search_arc_num = self.arc_num + self.node_num;
            let mut f = self.arc_num + self.node_num;
            // log::debug!("node num: {}", self.node_num);
            for u in 0..self.node_num {
                self.parent[u] = Some(self.root);
                self.thread[u] = u + 1;
                self.reverse_thread[u + 1] = u;
                self.successor_num[u] = 1;
                self.last_successor[u] = u;
                if !self.supply[u].is_negative() {
                    self.predecessor_direction[u] = ArcDirection::up();
                    self.pi[u] = T::zero();
                    self.predecessor[u] = Some(self.arc_num + u);
                    self.source[self.arc_num + u] = u;
                    self.target[self.arc_num + u] = self.root;
                    self.state[self.arc_num + u] = ArcState::tree();
                    self.flow[self.arc_num + u] = self.supply[u].clone();
                    self.cost[self.arc_num + u] = T::zero();
                } else {
                    self.predecessor_direction[u] = ArcDirection::down();
                    self.pi[u] = art_cost.clone();
                    self.predecessor[u] = Some(f);
                    self.source[f] = self.root;
                    self.target[f] = u;
                    self.state[f] = ArcState::tree();
                    self.flow[f] = -self.supply[u].clone();
                    self.cost[f] = art_cost.clone();
                    self.source[self.arc_num + u] = u;
                    self.target[self.arc_num + u] = self.root;
                    self.state[self.arc_num + u] = ArcState::lower();
                    self.flow[self.arc_num + u] = T::zero();
                    self.cost[self.arc_num + u] = T::zero();
                    f += 1;
                    // log::debug!("f increased by 1");
                }
            }
            self.all_arc_num = f;
        } else {
            // GEQ supply constraints
            self.search_arc_num = self.arc_num + self.node_num;
            let mut f = self.arc_num + self.node_num;
            for u in 0..self.node_num {
                self.parent[u] = Some(self.root);
                self.thread[u] = u + 1;
                self.reverse_thread[u + 1] = u;
                self.successor_num[u] = 1;
                self.last_successor[u] = u;
                if !self.supply[u].is_positive() {
                    self.predecessor_direction[u] = ArcDirection::down();
                    self.pi[u] = T::zero();
                    self.predecessor[u] = Some(self.arc_num + u);
                    self.source[self.arc_num + u] = self.root;
                    self.target[self.arc_num + u] = u;
                    self.state[self.arc_num + u] = ArcState::tree();
                    self.flow[self.arc_num + u] = -self.supply[u].clone();
                    self.cost[self.arc_num + u] = T::zero();
                } else {
                    self.predecessor_direction[u] = ArcDirection::up();
                    self.pi[u] = -art_cost.clone();
                    self.predecessor[u] = Some(f);
                    self.source[f] = u;
                    self.target[f] = self.root;
                    self.state[f] = ArcState::tree();
                    self.flow[f] = self.supply[u].clone();
                    self.cost[f] = art_cost.clone();
                    self.source[self.arc_num + u] = self.root;
                    self.target[self.arc_num + u] = u;
                    self.state[self.arc_num + u] = ArcState::lower();
                    self.flow[self.arc_num + u] = T::zero();
                    self.cost[self.arc_num + u] = T::zero();
                    f += 1;
                }
            }
            self.all_arc_num = f;
        }
        return true;
    }

    /// Internal function:
    /// Function that identifies the node in the tree where the cycle is closed, i.e. the deepest node that is both a path to v_in and u_in.
    /// In the basis tree, trace down from nodes adjacent to the entering arc to first node closing the circle
    fn find_join_node(&mut self) {
        let mut u = self.source[self.in_arc];
        let mut v = self.target[self.in_arc];
        while u != v {
            // successor number is used to measure the depth of the node in the tree
            // for the u = v = join node the successor number will be the same
            if self.successor_num[u] < self.successor_num[v] {
                u = self.parent[u].unwrap();
            } else {
                v = self.parent[v].unwrap();
            }
        }
        self.join = u;
    }

    /// Internal function:
    /// Function that updates the flow along the cycle with the identified delta value.
    /// For arcs that are oriented against the cycle, the flow is decreased by delta.
    /// For arcs that are oriented with the cycle, the flow is increased by delta.
    /// This leads to a resulting flow of zero on the leaving arc (for floating point types, the flow is close to zero -> could lead to errors).
    fn change_flow(&mut self, change: bool) {
        if self.delta > T::zero() {
            let mut value = self.state[self.in_arc].value().clone();
            value *= &self.delta;
            self.flow[self.in_arc] += &value;
            let mut u = self.source[self.in_arc];
            while u != self.join {
                let mut reduce_by = self.predecessor_direction[u].value().clone();
                reduce_by *= &value;
                self.flow[self.predecessor[u].unwrap()] -= &reduce_by;
                u = self.parent[u].unwrap();
            }
            u = self.target[self.in_arc];
            while u != self.join {
                let mut increase_by = self.predecessor_direction[u].value().clone();
                increase_by *= &value;
                self.flow[self.predecessor[u].unwrap()] += &increase_by;
                u = self.parent[u].unwrap();
            }
        }
        if change {
            self.state[self.in_arc] = ArcState::tree();
            if self.flow[self.predecessor[self.u_out].unwrap()] == T::zero() {
                self.state[self.predecessor[self.u_out].unwrap()] = ArcState::lower();
            } else {
                self.state[self.predecessor[self.u_out].unwrap()] = ArcState::upper();
            }
        } else {
            if self.state[self.in_arc] == ArcState::lower() {
                self.state[self.in_arc] = ArcState::upper();
            } else {
                self.state[self.in_arc] = ArcState::lower();
            }
        }
    }

    /// Internal function:
    /// Function to update the tree structure when arcs are swapped in/out of the basis
    /// Depending on whether the leaving arc is on the branch from u_in to to the root or on the branch from v_in to the root,
    /// the respective branch is shifted and restructured s.t. u_in or v_in becomes the root of the subtree.
    /// This subtree is then inserted into the spanning tree.
    fn update_tree_structure(&mut self) {
        let old_reverse_thread = self.reverse_thread[self.u_out];
        let old_successor_num = self.successor_num[self.u_out];
        let old_last_successor = self.last_successor[self.u_out];
        self.v_out = self.parent[self.u_out].unwrap();

        // check if u_in and u_out coincide
        if self.u_in == self.u_out {
            // update parent, predecessor, predecessor_direction
            self.parent[self.u_in] = Some(self.v_in);
            self.predecessor[self.u_in] = Some(self.in_arc);
            self.predecessor_direction[self.u_in] = if self.u_in == self.source[self.in_arc] {
                ArcDirection::up()
            } else {
                ArcDirection::down()
            };

            // update thread and reverse_thread
            if self.thread[self.v_in] != self.u_out {
                let mut after = self.thread[old_last_successor];
                self.thread[old_reverse_thread] = after;
                self.reverse_thread[after] = old_reverse_thread;
                after = self.thread[self.v_in];
                self.thread[self.v_in] = self.u_out;
                self.reverse_thread[self.u_out] = self.v_in;
                self.thread[old_last_successor] = after;
                self.reverse_thread[after] = old_last_successor;
            }
        } else {
            // Handle the case when old_rev_thread equals to v_in
            // (it also means that join and v_out coincide)
            let thread_continue = if old_reverse_thread == self.v_in {
                self.thread[old_last_successor]
            } else {
                self.thread[self.v_in]
            };
            // update thread and parent along the stem nodes (i.e. the nodes between u_in and u_out, whose parents need adjustment)
            let mut stem = self.u_in;
            let mut stem_parent = self.v_in;
            let mut next_stem;
            let mut last = self.last_successor[self.u_in];
            let mut before;
            let mut after = self.thread[last];
            self.thread[self.v_in] = self.u_in;
            self.dirty_revs.clear();
            self.dirty_revs.push(self.v_in);
            while stem != self.u_out {
                // insert the next stem node into the thread list
                next_stem = self.parent[stem].unwrap();
                self.thread[last] = next_stem;
                self.dirty_revs.push(last);
                // remove the subtree of stem from the thread list
                before = self.reverse_thread[stem];
                self.thread[before] = after;
                self.reverse_thread[after] = before;
                // change the parent node and shift the stem nodes
                self.parent[stem] = Some(stem_parent);
                stem_parent = stem;
                stem = next_stem;
                // update last and after
                last = if self.last_successor[stem] == self.last_successor[stem_parent] {
                    self.reverse_thread[stem_parent]
                } else {
                    self.last_successor[stem]
                };
                after = self.thread[last];
            }
            self.parent[self.u_out] = Some(stem_parent);
            self.thread[last] = thread_continue;
            self.reverse_thread[thread_continue] = last;
            self.last_successor[self.u_out] = last;
            // remove the subtree of u_out from the thread list
            // except for the case when old_rev_thread equals to v_in
            if old_reverse_thread != self.v_in {
                self.thread[old_reverse_thread] = after;
                self.reverse_thread[after] = old_reverse_thread;
            }

            // update reverse_thread using the new thread values
            for i in 0..self.dirty_revs.len() {
                let u = self.dirty_revs[i];
                self.reverse_thread[self.thread[u]] = u;
            }

            // update predecessor, predecessor_direction, last_successor, and successor_num along the stem nodes
            let mut temp_successor_num = 0;
            let temp_last_successor = self.last_successor[self.u_out];
            let mut u = self.u_out;
            let mut p = self.parent[u];
            while u != self.u_in {
                self.predecessor[u] = self.predecessor[p.unwrap()];
                self.predecessor_direction[u] =
                    if self.predecessor_direction[p.unwrap()] == ArcDirection::up() {
                        ArcDirection::down()
                    } else {
                        ArcDirection::up()
                    };
                temp_successor_num += self.successor_num[u] - self.successor_num[p.unwrap()];
                self.successor_num[u] = temp_successor_num;
                self.last_successor[p.unwrap()] = temp_last_successor;

                u = p.unwrap();
                p = self.parent[u];
            }
            self.predecessor[self.u_in] = Some(self.in_arc);
            self.predecessor_direction[self.u_in] = if self.u_in == self.source[self.in_arc] {
                ArcDirection::up()
            } else {
                ArcDirection::down()
            };
            self.successor_num[self.u_in] = old_successor_num;
        }

        // update last_successor from v_in towards the root
        let up_limit_out = if self.last_successor[self.join] == self.v_in {
            Some(self.join)
        } else {
            None
        };
        let last_successor_out = self.last_successor[self.u_out];
        let mut u = Some(self.v_in);
        while u != None && self.last_successor[u.unwrap()] == self.v_in {
            self.last_successor[u.unwrap()] = last_successor_out;
            u = self.parent[u.unwrap()];
        }

        // update last_successor from v_out towards the root
        if self.join != old_reverse_thread && self.v_in != old_reverse_thread {
            u = Some(self.v_out);
            while u != None
                && u != up_limit_out
                && self.last_successor[u.unwrap()] == old_last_successor
            {
                self.last_successor[u.unwrap()] = old_reverse_thread;
                u = self.parent[u.unwrap()];
            }
        } else if last_successor_out != old_last_successor {
            u = Some(self.v_out);
            while u != None
                && u != up_limit_out
                && self.last_successor[u.unwrap()] == old_last_successor
            {
                self.last_successor[u.unwrap()] = last_successor_out;
                u = self.parent[u.unwrap()];
            }
        }
        // update successor_num from v_in to join
        let mut u = self.v_in;
        while u != self.join {
            self.successor_num[u] += old_successor_num;
            u = self.parent[u].unwrap();
        }
        // update successor_num from v_out to join
        u = self.v_out;
        while u != self.join {
            self.successor_num[u] -= old_successor_num;
            u = self.parent[u].unwrap();
        }
    }

    /// Retrieves the total cost of the flow if the problem is optimal.
    ///
    /// # Returns
    /// - `Some(T)`: The total cost of the flow if the problem type is `Optimal`.
    /// - `None`: If the problem type is not optimal or undefined.
    ///
    /// **Calculation**  
    /// The total cost is calculated as:
    /// Total Cost = Σ (flow_i × cost_i) for i = 1 to n <br>
    /// Where:
    /// - `flow_i` is the flow value on arc `i`.
    /// - `cost_i` is the cost associated with arc `i`.
    ///
    /// The result is accumulated over all arcs in the network.
    ///
    /// # Examples
    /// ```ignore
    /// // Assuming `simplex` is an instance of `NetworkSimplex` with optimal flow.
    /// if let Some(total_cost) = simplex.get_result() {
    ///     log::debug!("The total flow cost is: {}", total_cost);
    /// } else {
    ///     log::debug!("The problem is not in an optimal state.");
    /// }
    /// ```
    pub fn get_result(&self) -> Option<T> {
        if let Some(problem_type) = &self.problem_type {
            if problem_type == &ProblemType::Optimal {
                let flow_cost = self.flow.iter().zip(self.cost.iter());
                let mut result = T::zero();
                for (flow, cost) in flow_cost {
                    let mut arc_result = flow.clone();
                    arc_result *= cost;
                    result += &arc_result;
                }
                return Some(result);
            }
        }

        return None;
    }

    pub fn get_bigint_result(&self) -> Option<Integer> {
        if let Some(problem_type) = &self.problem_type {
            if problem_type == &ProblemType::Optimal {
                let flow_cost = self.flow.iter().zip(self.cost.iter());
                let mut result = Integer::zero();
                for (flow, cost) in flow_cost {
                    let mut arc_result = flow.to_big_int();
                    arc_result *= cost.to_big_int();
                    result += arc_result;
                }
                return Some(result);
            }
        }

        None
    }

    /// Retrieves the flow values of the network.
    ///
    /// # Returns
    /// A vector of flow values for each arc in the network.
    pub fn get_flow(&self) -> Vec<T> {
        self.flow.clone()
    }

    /// Retrieves the cost values of the network.
    ///
    /// # Returns
    /// A vector of cost values for each arc in the network.
    pub fn get_cost(&self) -> Vec<T> {
        self.cost.clone()
    }

    /// Internal function: Retrieves the supply values of the network.
    fn find_max_cost(&self) -> T {
        select_max(&self.cost).expect("Cost vector cannot be empty")
    }
}

/// Selects the maximum value from a slice of values.
/// Compared to standard max() function, this only requires the PartialOrd trait.
pub fn select_max<T>(values: &[T]) -> Option<T>
where
    T: PartialOrd + Clone,
{
    values
        .iter()
        .filter(|x| x.partial_cmp(x).is_some()) // Handles NaN if T is f64
        .max_by(|a, b| a.partial_cmp(b).unwrap())
        .cloned()
}

#[cfg(test)]
mod tests {
    use crate::network_simplex::NetworkSimplex;
    use ebi_arithmetic::malachite::Integer;

    #[test]
    fn network_simplex_int() {
        let supply: Vec<i64> = vec![20, 0, 0, -5, -14];

        let graph_and_costs: Vec<Vec<Option<i64>>> = vec![
            vec![None, Some(4), Some(4), None, None],
            vec![None, None, Some(2), Some(2), Some(6)],
            vec![None, None, None, Some(1), Some(3)],
            vec![None, None, None, None, Some(2)],
            vec![None, None, Some(3), None, None],
        ];
        let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, true, false);
        _ = ns.run(false);
        assert_eq!(ns.get_result().unwrap(), 123);
    }

    #[test]
    fn network_simplex_bigint() {
        let supply: Vec<Integer> = vec![20.into(), 0.into(), 0.into(), (-5).into(), (-14).into()];

        let graph_and_costs: Vec<Vec<Option<Integer>>> = vec![
            vec![None, Some(4), Some(4), None, None],
            vec![None, None, Some(2), Some(2), Some(6)],
            vec![None, None, None, Some(1), Some(3)],
            vec![None, None, None, None, Some(2)],
            vec![None, None, Some(3), None, None],
        ]
        .into_iter()
        .map(|row| {
            row.into_iter()
                .map(|x| x.map(|cost| Integer::from(cost)))
                .collect()
        })
        .collect();

        let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, true, false);
        _ = ns.run(false);
        assert_eq!(ns.get_result().unwrap(), Integer::from(123));
    }

    #[test]
    fn network_simplex_float() {
        let supply: Vec<f64> = vec![20, 0, 0, -5, -14]
            .into_iter()
            .map(|s| s.into())
            .collect();

        let graph_and_costs: Vec<Vec<Option<f64>>> = vec![
            vec![None, Some(4), Some(4), None, None],
            vec![None, None, Some(2), Some(2), Some(6)],
            vec![None, None, None, Some(1), Some(3)],
            vec![None, None, None, None, Some(2)],
            vec![None, None, Some(3), None, None],
        ]
        .into_iter()
        .map(|row| row.into_iter().map(|x| x.map(|cost| cost.into())).collect())
        .collect();

        let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, true, false);
        _ = ns.run(false);
        let result = ns.get_result().unwrap();
        assert_eq!(result, 123.0);
    }
}