multipers 2.2.3__cp310-cp310-win_amd64.whl → 2.3.1__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of multipers might be problematic. Click here for more details.

Files changed (182) hide show
  1. multipers/__init__.py +33 -31
  2. multipers/_signed_measure_meta.py +430 -430
  3. multipers/_slicer_meta.py +211 -212
  4. multipers/data/MOL2.py +458 -458
  5. multipers/data/UCR.py +18 -18
  6. multipers/data/graphs.py +466 -466
  7. multipers/data/immuno_regions.py +27 -27
  8. multipers/data/pytorch2simplextree.py +90 -90
  9. multipers/data/shape3d.py +101 -101
  10. multipers/data/synthetic.py +113 -111
  11. multipers/distances.py +198 -198
  12. multipers/filtration_conversions.pxd.tp +84 -84
  13. multipers/filtrations/__init__.py +18 -0
  14. multipers/{ml/convolutions.py → filtrations/density.py} +563 -520
  15. multipers/filtrations/filtrations.py +289 -0
  16. multipers/filtrations.pxd +224 -224
  17. multipers/function_rips.cp310-win_amd64.pyd +0 -0
  18. multipers/function_rips.pyx +105 -105
  19. multipers/grids.cp310-win_amd64.pyd +0 -0
  20. multipers/grids.pyx +350 -350
  21. multipers/gudhi/Persistence_slices_interface.h +132 -132
  22. multipers/gudhi/Simplex_tree_interface.h +239 -245
  23. multipers/gudhi/Simplex_tree_multi_interface.h +516 -561
  24. multipers/gudhi/cubical_to_boundary.h +59 -59
  25. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -450
  26. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -1070
  27. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -579
  28. multipers/gudhi/gudhi/Debug_utils.h +45 -45
  29. multipers/gudhi/gudhi/Fields/Multi_field.h +484 -484
  30. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +455 -455
  31. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +450 -450
  32. multipers/gudhi/gudhi/Fields/Multi_field_small.h +531 -531
  33. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +507 -507
  34. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +531 -531
  35. multipers/gudhi/gudhi/Fields/Z2_field.h +355 -355
  36. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +376 -376
  37. multipers/gudhi/gudhi/Fields/Zp_field.h +420 -420
  38. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +400 -400
  39. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +418 -418
  40. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -337
  41. multipers/gudhi/gudhi/Matrix.h +2107 -2107
  42. multipers/gudhi/gudhi/Multi_critical_filtration.h +1038 -1038
  43. multipers/gudhi/gudhi/Multi_persistence/Box.h +171 -171
  44. multipers/gudhi/gudhi/Multi_persistence/Line.h +282 -282
  45. multipers/gudhi/gudhi/Off_reader.h +173 -173
  46. multipers/gudhi/gudhi/One_critical_filtration.h +1433 -1431
  47. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +769 -769
  48. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +686 -686
  49. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +842 -842
  50. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1350 -1350
  51. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1105 -1105
  52. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +859 -859
  53. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +910 -910
  54. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +139 -139
  55. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +230 -230
  56. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +211 -211
  57. multipers/gudhi/gudhi/Persistence_matrix/boundary_cell_position_to_id_mapper.h +60 -60
  58. multipers/gudhi/gudhi/Persistence_matrix/boundary_face_position_to_id_mapper.h +60 -60
  59. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +136 -136
  60. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +190 -190
  61. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +616 -616
  62. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +150 -150
  63. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +106 -106
  64. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +219 -219
  65. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +327 -327
  66. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1140 -1140
  67. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +934 -934
  68. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +934 -934
  69. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +980 -980
  70. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1092 -1092
  71. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +192 -192
  72. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +921 -921
  73. multipers/gudhi/gudhi/Persistence_matrix/columns/small_vector_column.h +1093 -1093
  74. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +1012 -1012
  75. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1244 -1244
  76. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +186 -186
  77. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +164 -164
  78. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +156 -156
  79. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +376 -376
  80. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +540 -540
  81. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -118
  82. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -173
  83. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -128
  84. multipers/gudhi/gudhi/Persistent_cohomology.h +745 -745
  85. multipers/gudhi/gudhi/Points_off_io.h +171 -171
  86. multipers/gudhi/gudhi/Simple_object_pool.h +69 -69
  87. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +463 -463
  88. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -83
  89. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +106 -106
  90. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -277
  91. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -62
  92. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -27
  93. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +62 -62
  94. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +157 -157
  95. multipers/gudhi/gudhi/Simplex_tree.h +2794 -2794
  96. multipers/gudhi/gudhi/Simplex_tree_multi.h +152 -163
  97. multipers/gudhi/gudhi/distance_functions.h +62 -62
  98. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -104
  99. multipers/gudhi/gudhi/persistence_interval.h +253 -253
  100. multipers/gudhi/gudhi/persistence_matrix_options.h +170 -170
  101. multipers/gudhi/gudhi/reader_utils.h +367 -367
  102. multipers/gudhi/mma_interface_coh.h +256 -255
  103. multipers/gudhi/mma_interface_h0.h +223 -231
  104. multipers/gudhi/mma_interface_matrix.h +291 -282
  105. multipers/gudhi/naive_merge_tree.h +536 -575
  106. multipers/gudhi/scc_io.h +310 -289
  107. multipers/gudhi/truc.h +957 -888
  108. multipers/io.cp310-win_amd64.pyd +0 -0
  109. multipers/io.pyx +714 -711
  110. multipers/ml/accuracies.py +90 -90
  111. multipers/ml/invariants_with_persistable.py +79 -79
  112. multipers/ml/kernels.py +176 -176
  113. multipers/ml/mma.py +713 -714
  114. multipers/ml/one.py +472 -472
  115. multipers/ml/point_clouds.py +352 -346
  116. multipers/ml/signed_measures.py +1589 -1589
  117. multipers/ml/sliced_wasserstein.py +461 -461
  118. multipers/ml/tools.py +113 -113
  119. multipers/mma_structures.cp310-win_amd64.pyd +0 -0
  120. multipers/mma_structures.pxd +127 -127
  121. multipers/mma_structures.pyx +4 -8
  122. multipers/mma_structures.pyx.tp +1083 -1085
  123. multipers/multi_parameter_rank_invariant/diff_helpers.h +84 -93
  124. multipers/multi_parameter_rank_invariant/euler_characteristic.h +97 -97
  125. multipers/multi_parameter_rank_invariant/function_rips.h +322 -322
  126. multipers/multi_parameter_rank_invariant/hilbert_function.h +769 -769
  127. multipers/multi_parameter_rank_invariant/persistence_slices.h +148 -148
  128. multipers/multi_parameter_rank_invariant/rank_invariant.h +369 -369
  129. multipers/multiparameter_edge_collapse.py +41 -41
  130. multipers/multiparameter_module_approximation/approximation.h +2298 -2295
  131. multipers/multiparameter_module_approximation/combinatory.h +129 -129
  132. multipers/multiparameter_module_approximation/debug.h +107 -107
  133. multipers/multiparameter_module_approximation/format_python-cpp.h +286 -286
  134. multipers/multiparameter_module_approximation/heap_column.h +238 -238
  135. multipers/multiparameter_module_approximation/images.h +79 -79
  136. multipers/multiparameter_module_approximation/list_column.h +174 -174
  137. multipers/multiparameter_module_approximation/list_column_2.h +232 -232
  138. multipers/multiparameter_module_approximation/ru_matrix.h +347 -347
  139. multipers/multiparameter_module_approximation/set_column.h +135 -135
  140. multipers/multiparameter_module_approximation/structure_higher_dim_barcode.h +36 -36
  141. multipers/multiparameter_module_approximation/unordered_set_column.h +166 -166
  142. multipers/multiparameter_module_approximation/utilities.h +403 -419
  143. multipers/multiparameter_module_approximation/vector_column.h +223 -223
  144. multipers/multiparameter_module_approximation/vector_matrix.h +331 -331
  145. multipers/multiparameter_module_approximation/vineyards.h +464 -464
  146. multipers/multiparameter_module_approximation/vineyards_trajectories.h +649 -649
  147. multipers/multiparameter_module_approximation.cp310-win_amd64.pyd +0 -0
  148. multipers/multiparameter_module_approximation.pyx +218 -217
  149. multipers/pickle.py +90 -53
  150. multipers/plots.py +342 -334
  151. multipers/point_measure.cp310-win_amd64.pyd +0 -0
  152. multipers/point_measure.pyx +322 -320
  153. multipers/simplex_tree_multi.cp310-win_amd64.pyd +0 -0
  154. multipers/simplex_tree_multi.pxd +133 -133
  155. multipers/simplex_tree_multi.pyx +115 -48
  156. multipers/simplex_tree_multi.pyx.tp +1947 -1935
  157. multipers/slicer.cp310-win_amd64.pyd +0 -0
  158. multipers/slicer.pxd +301 -120
  159. multipers/slicer.pxd.tp +218 -214
  160. multipers/slicer.pyx +1570 -507
  161. multipers/slicer.pyx.tp +931 -914
  162. multipers/tensor/tensor.h +672 -672
  163. multipers/tensor.pxd +13 -13
  164. multipers/test.pyx +44 -44
  165. multipers/tests/__init__.py +57 -57
  166. multipers/torch/diff_grids.py +217 -217
  167. multipers/torch/rips_density.py +310 -304
  168. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/LICENSE +21 -21
  169. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/METADATA +21 -11
  170. multipers-2.3.1.dist-info/RECORD +182 -0
  171. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/WHEEL +1 -1
  172. multipers/tests/test_diff_helper.py +0 -73
  173. multipers/tests/test_hilbert_function.py +0 -82
  174. multipers/tests/test_mma.py +0 -83
  175. multipers/tests/test_point_clouds.py +0 -49
  176. multipers/tests/test_python-cpp_conversion.py +0 -82
  177. multipers/tests/test_signed_betti.py +0 -181
  178. multipers/tests/test_signed_measure.py +0 -89
  179. multipers/tests/test_simplextreemulti.py +0 -221
  180. multipers/tests/test_slicer.py +0 -221
  181. multipers-2.2.3.dist-info/RECORD +0 -189
  182. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/top_level.txt +0 -0
@@ -1,575 +1,536 @@
1
- #ifndef H0V_MERGE_TREE_H
2
- #define H0V_MERGE_TREE_H
3
-
4
- #include <algorithm>
5
- #include <cassert>
6
- #include <iostream>
7
- #include <set>
8
- #include <unordered_map>
9
- #include <utility>
10
- #include <vector>
11
-
12
- #include <boost/functional/hash.hpp>
13
-
14
- struct Bar {
15
- Bar() : dim(-1), birth(-1), death(-1) {}
16
-
17
- Bar(int dim, int birth, int death) : dim(dim), birth(birth), death(death) {}
18
-
19
- int dim;
20
- int birth;
21
- int death;
22
- };
23
-
24
- class Naive_bottleneck_forest {
25
- public:
26
- using index = int;
27
-
28
- Naive_bottleneck_forest() {}
29
- // assumes that ids of vertices are continuous and start with 0
30
- Naive_bottleneck_forest(unsigned int numberOfVertices)
31
- : forest_(numberOfVertices), weights_() {}
32
-
33
- friend void swap(Naive_bottleneck_forest &mf1, Naive_bottleneck_forest &mf2) {
34
- mf1.forest_.swap(mf2.forest_);
35
- mf1.weights_.swap(mf2.weights_);
36
- }
37
-
38
- void add_edge(index firstVertexIndex, index secondVertexIndex,
39
- index edgeWeight) {
40
- forest_[firstVertexIndex].children.insert(secondVertexIndex);
41
- forest_[secondVertexIndex].children.insert(firstVertexIndex);
42
- weights_.emplace(_get_edge(firstVertexIndex, secondVertexIndex),
43
- edgeWeight);
44
- }
45
-
46
- void swap_out_edges(index inVertex1, index inVertex2, index outVertex1,
47
- index outVertex2, index outWeight) {
48
- if (forest_[inVertex1].parent == inVertex2) {
49
- forest_[inVertex1].parent = -1;
50
- forest_[inVertex2].children.erase(inVertex1);
51
- } else {
52
- forest_[inVertex2].parent = -1;
53
- forest_[inVertex1].children.erase(inVertex2);
54
- }
55
- weights_.erase(_get_edge(inVertex1, inVertex2));
56
-
57
- if (forest_[outVertex1].parent == -1) {
58
- forest_[outVertex1].parent = outVertex2;
59
- forest_[outVertex2].children.insert(outVertex1);
60
- } else {
61
- if (forest_[outVertex2].parent != -1)
62
- _reroot(outVertex2);
63
- forest_[outVertex2].parent = outVertex1;
64
- forest_[outVertex1].children.insert(outVertex2);
65
- }
66
- weights_.emplace(_get_edge(outVertex1, outVertex2), outWeight);
67
- }
68
-
69
- // to call once the tree finished
70
- void root() {
71
- for (unsigned int i = 0; i < forest_.size(); ++i) {
72
- if (forest_[i].parent == -1) {
73
- _root(i);
74
- }
75
- }
76
- }
77
-
78
- // assumes root() was already called and that both vertices are in the same
79
- // connected component
80
- index get_bootleneck_weight(index id1, index id2) const {
81
- auto get_weight = [&](index id) {
82
- return forest_[id].parent == -1
83
- ? -1
84
- : weights_.at(_get_edge(id, forest_[id].parent));
85
- };
86
-
87
- std::unordered_map<index, index> partialBottleneck;
88
- index r = id1;
89
- index w = 0;
90
- partialBottleneck.emplace(r, w);
91
- while (forest_[r].parent != -1) {
92
- w = std::max(get_weight(r), w);
93
- partialBottleneck.emplace(forest_[r].parent, w);
94
- r = forest_[r].parent;
95
- }
96
- r = id2;
97
- auto bIt = partialBottleneck.find(r);
98
- w = 0;
99
- while (forest_[r].parent != -1 && bIt == partialBottleneck.end()) {
100
- w = std::max(get_weight(r), w);
101
- r = forest_[r].parent;
102
- bIt = partialBottleneck.find(r);
103
- }
104
- return std::max(w, bIt->second);
105
- }
106
-
107
- void update_weight(index id1, index id2, index newWeight) {
108
- weights_.at(_get_edge(id1, id2)) = newWeight;
109
- }
110
-
111
- void print(std::ostream &stream) const{
112
- stream << "MSF:\n";
113
- unsigned int c = 0;
114
- for (const Node& n : forest_){
115
- stream << "[" << c << "] parent: ";
116
- stream << n.parent << "\n";
117
- stream << "[" << c << "] children: ";
118
- for (index id : n.children) stream << id << " ";
119
- stream << "\n";
120
- if (n.parent != -1){
121
- stream << "[" << c << "] weight of parent edge: ";
122
- stream << weights_.at(_get_edge(c, n.parent)) << "\n";
123
- }
124
- ++c;
125
- }
126
- }
127
-
128
- private:
129
- struct Node {
130
- index parent = -1;
131
- std::set<index> children = {};
132
- };
133
-
134
- std::vector<Node> forest_;
135
- std::unordered_map<std::pair<index, index>, index,
136
- boost::hash<std::pair<index, index>>>
137
- weights_; // TODO: test other map types and having a pair as key is ugly
138
-
139
- void _root(index id) {
140
- const Node &p = forest_[id];
141
- for (index chID : p.children) {
142
- Node &ch = forest_[chID];
143
- ch.children.erase(id);
144
- ch.parent = id;
145
- _root(chID);
146
- }
147
- }
148
-
149
- void _reroot(index id) {
150
- index newParent = -1;
151
- index current = id;
152
- index oldParent = forest_[id].parent;
153
- forest_[current].parent = newParent;
154
- forest_[current].children.insert(oldParent);
155
- newParent = current;
156
- current = oldParent;
157
- oldParent = forest_[current].parent;
158
- while (oldParent != -1) {
159
- forest_[current].parent = newParent;
160
- forest_[current].children.erase(newParent);
161
- forest_[current].children.insert(oldParent);
162
- newParent = current;
163
- current = oldParent;
164
- oldParent = forest_[current].parent;
165
- }
166
- forest_[current].parent = newParent;
167
- forest_[current].children.erase(newParent);
168
- }
169
-
170
- std::pair<index, index> _get_edge(index id1, index id2) const {
171
- return std::make_pair(std::min(id1, id2), std::max(id1, id2));
172
- }
173
- };
174
-
175
- class Naive_merge_forest {
176
- public:
177
- using index = int;
178
-
179
- Naive_merge_forest() {}
180
- Naive_merge_forest(unsigned int numberOfSimplices,
181
- unsigned int numberOfVertices)
182
- : forest_(numberOfVertices * 2 - 1),
183
- barcode_(numberOfSimplices, numberOfVertices),
184
- positionToID_(numberOfSimplices, -1), nextVertexIndex_(0),
185
- nextEdgeIndex_(numberOfVertices),
186
- minimumSpanningForest_(numberOfVertices) {}
187
-
188
- friend void swap(Naive_merge_forest &mf1, Naive_merge_forest &mf2) {
189
- mf1.forest_.swap(mf2.forest_);
190
- swap(mf1.barcode_, mf2.barcode_);
191
- mf1.positionToID_.swap(mf2.positionToID_);
192
- std::swap(mf1.nextVertexIndex_, mf2.nextVertexIndex_);
193
- std::swap(mf1.nextEdgeIndex_, mf2.nextEdgeIndex_);
194
- swap(mf1.minimumSpanningForest_, mf2.minimumSpanningForest_);
195
- }
196
-
197
- // assumes that merge tree is constructed fully with add_edge before any swap
198
- // is done with vine
199
- bool add_edge(index edgeIndex, index firstVertexIndex,
200
- index secondVertexIndex) {
201
- index r1 = _get_root(positionToID_[firstVertexIndex]);
202
- index r2 = _get_root(positionToID_[secondVertexIndex]);
203
-
204
- if (r1 == r2) {
205
- barcode_.add_positive_edge(edgeIndex);
206
- return false; // positive edge
207
- }
208
-
209
- assert(positionToID_[edgeIndex] == -1 && "Edge was already added");
210
- positionToID_[edgeIndex] = nextEdgeIndex_;
211
-
212
- forest_[r1].parent = nextEdgeIndex_;
213
- forest_[r2].parent = nextEdgeIndex_;
214
- forest_[nextEdgeIndex_].leftChild = r1;
215
- forest_[nextEdgeIndex_].rightChild = r2;
216
- index rep1 = _is_vertex(r1) ? r1 : forest_[r1].representative;
217
- index rep2 = _is_vertex(r2) ? r2 : forest_[r2].representative;
218
- forest_[nextEdgeIndex_].representative = rep1 < rep2 ? rep1 : rep2;
219
- index pair = (forest_[nextEdgeIndex_].representative == rep1)
220
- ? forest_[rep2].representative
221
- : forest_[rep1].representative;
222
- barcode_.add_negative_edge(pair, edgeIndex);
223
-
224
- ++nextEdgeIndex_;
225
-
226
- minimumSpanningForest_.add_edge(positionToID_[firstVertexIndex],
227
- positionToID_[secondVertexIndex],
228
- edgeIndex);
229
-
230
- return true; // negative edge
231
- }
232
-
233
- void add_vertex(index vertexIndex) {
234
- positionToID_[vertexIndex] = nextVertexIndex_;
235
- forest_[nextVertexIndex_++].representative = vertexIndex;
236
- barcode_.add_vertex(vertexIndex);
237
- }
238
-
239
- void initialize() {
240
- barcode_.barcode_.resize(barcode_.nextBarIndex_);
241
- minimumSpanningForest_.root();
242
- }
243
-
244
- void vertex_swap(index position) {
245
- index nca = _get_nearest_common_ancestor(positionToID_[position],
246
- positionToID_[position + 1]);
247
-
248
- if (nca == -1) { // not in the same connected component
249
- barcode_.birth_birth_swap(position);
250
- } else {
251
- index rightRep = _is_vertex(forest_[nca].rightChild)
252
- ? forest_[nca].rightChild
253
- : forest_[forest_[nca].rightChild].representative;
254
- index leftRep = _is_vertex(forest_[nca].leftChild)
255
- ? forest_[nca].leftChild
256
- : forest_[forest_[nca].leftChild].representative;
257
- index ncaPairID =
258
- (forest_[nca].representative == rightRep) ? leftRep : rightRep;
259
- index ncaPos = barcode_.death(forest_[ncaPairID].representative);
260
-
261
- index e1 = barcode_.death(position);
262
- index e2 = barcode_.death(position + 1);
263
-
264
- if ((e1 != -1 && e1 < ncaPos) || (e2 != -1 && e2 < ncaPos)) {
265
- barcode_.birth_birth_swap(position);
266
- }
267
- }
268
-
269
- _update_representative(nca, positionToID_[position],
270
- positionToID_[position + 1]);
271
- std::swap(forest_[positionToID_[position]].representative,
272
- forest_[positionToID_[position + 1]].representative);
273
- std::swap(positionToID_[position], positionToID_[position + 1]);
274
- }
275
-
276
- void vertex_edge_swap(index position, index eV1Position, index eV2Position) {
277
- if (_is_positive_edge(position + 1)) {
278
- barcode_.birth_birth_swap(position);
279
- } else {
280
- barcode_.birth_death_swap(position);
281
- minimumSpanningForest_.update_weight(
282
- positionToID_[eV1Position], positionToID_[eV2Position], position);
283
- }
284
- forest_[positionToID_[position]].representative = position + 1;
285
- std::swap(positionToID_[position], positionToID_[position + 1]);
286
- }
287
-
288
- void edge_vertex_swap(index position, index eV1Position, index eV2Position) {
289
- if (_is_positive_edge(position)) {
290
- barcode_.birth_birth_swap(position);
291
- } else {
292
- barcode_.death_birth_swap(position);
293
- minimumSpanningForest_.update_weight(
294
- positionToID_[eV1Position], positionToID_[eV2Position], position + 1);
295
- }
296
- forest_[positionToID_[position + 1]].representative = position;
297
- std::swap(positionToID_[position], positionToID_[position + 1]);
298
- }
299
-
300
- void edge_edge_swap(index position, index e1V1Position, index e1V2Position,
301
- index e2V1Position, index e2V2Position) {
302
- if (_is_positive_edge(position)) {
303
- _pos_edge_edge_switch(position, e2V1Position, e2V2Position);
304
- return;
305
- }
306
-
307
- if (_is_positive_edge(position + 1)) {
308
- _neg_edge_pos_edge_switch(position, e1V1Position, e1V2Position,
309
- e2V1Position, e2V2Position);
310
- return;
311
- }
312
-
313
- _neg_edge_neg_edge_switch(position, e1V1Position, e1V2Position,
314
- e2V1Position, e2V2Position);
315
- }
316
-
317
- const std::vector<Bar> &get_barcode() const { return barcode_.barcode_; }
318
-
319
- int get_dimension(index position) {
320
- return !_is_vertex(positionToID_[position]);
321
- }
322
-
323
- inline friend std::ostream &operator<<(std::ostream &stream, Naive_merge_forest &structure) {
324
- for (unsigned int i = 0; i < structure.forest_.size(); ++i){
325
- if (structure.forest_[i].parent == -1) structure._print_subtree(stream, "", i, false);
326
- }
327
- structure.minimumSpanningForest_.print(stream);
328
- return stream;
329
- }
330
-
331
- private:
332
- struct Node {
333
- index parent = -1;
334
- index leftChild = -1;
335
- index rightChild = -1;
336
- index representative = -1; // cc rep for edges, position for vertices
337
- };
338
-
339
- struct Barcode {
340
- Barcode() {}
341
- Barcode(int numberOfSimplices, int numberOfVertices)
342
- : barcode_(numberOfSimplices), positionToBar_(numberOfSimplices),
343
- nextBarIndex_(0) {}
344
-
345
- friend void swap(Barcode &mf1, Barcode &mf2) {
346
- mf1.barcode_.swap(mf2.barcode_);
347
- mf1.positionToBar_.swap(mf2.positionToBar_);
348
- std::swap(mf1.nextBarIndex_, mf2.nextBarIndex_);
349
- }
350
-
351
- void add_birth(index birth) {
352
- positionToBar_[birth] = nextBarIndex_;
353
- barcode_[nextBarIndex_].birth = birth;
354
- ++nextBarIndex_;
355
- }
356
-
357
- void add_vertex(index birth) {
358
- barcode_[nextBarIndex_].dim = 0;
359
- add_birth(birth);
360
- }
361
-
362
- void add_positive_edge(index birth) {
363
- barcode_[nextBarIndex_].dim = 1;
364
- add_birth(birth);
365
- }
366
-
367
- void add_negative_edge(index birth, index death) {
368
- index b = positionToBar_[birth];
369
- positionToBar_[death] = b;
370
- barcode_[b].death = death;
371
- }
372
-
373
- void birth_birth_swap(index position) {
374
- std::swap(barcode_[positionToBar_[position]].birth,
375
- barcode_[positionToBar_[position + 1]].birth);
376
- std::swap(positionToBar_[position], positionToBar_[position + 1]);
377
- }
378
-
379
- void birth_death_swap(index position) {
380
- std::swap(barcode_[positionToBar_[position]].birth,
381
- barcode_[positionToBar_[position + 1]].death);
382
- std::swap(positionToBar_[position], positionToBar_[position + 1]);
383
- }
384
-
385
- void death_death_swap(index position) {
386
- std::swap(barcode_[positionToBar_[position]].death,
387
- barcode_[positionToBar_[position + 1]].death);
388
- std::swap(positionToBar_[position], positionToBar_[position + 1]);
389
- }
390
-
391
- void death_birth_swap(index position) {
392
- std::swap(barcode_[positionToBar_[position]].death,
393
- barcode_[positionToBar_[position + 1]].birth);
394
- std::swap(positionToBar_[position], positionToBar_[position + 1]);
395
- }
396
-
397
- index death(index birth) { return barcode_[positionToBar_[birth]].death; }
398
-
399
- std::vector<Bar> barcode_;
400
- std::vector<index> positionToBar_;
401
- index nextBarIndex_;
402
- };
403
-
404
- std::vector<Node> forest_;
405
- Barcode barcode_;
406
- std::vector<index> positionToID_;
407
- index nextVertexIndex_;
408
- index nextEdgeIndex_;
409
- Naive_bottleneck_forest minimumSpanningForest_;
410
-
411
- bool _is_vertex(index nodeIndex) const {
412
- const Node &n = forest_[nodeIndex];
413
- return n.leftChild == -1 && n.rightChild == -1;
414
- }
415
-
416
- bool _is_positive_edge(index position) const {
417
- return positionToID_[position] == -1;
418
- }
419
-
420
- index _get_root(index node) const {
421
- while (forest_[node].parent != -1) {
422
- node = forest_[node].parent;
423
- }
424
- return node;
425
- }
426
-
427
- index _get_nearest_common_ancestor(index firstIndex,
428
- index secondIndex) const {
429
- std::set<index> path;
430
- index r = firstIndex;
431
- path.insert(r);
432
- while (forest_[r].parent != -1) {
433
- r = forest_[r].parent;
434
- path.insert(r);
435
- }
436
- r = secondIndex;
437
- while (r != -1 && path.find(r) == path.end()) {
438
- r = forest_[r].parent;
439
- }
440
- return r;
441
- }
442
-
443
- index _is_ancestor_of(index ancID, index descID, index endID = -1) const {
444
- index r = descID;
445
- while (r != endID && forest_[r].parent != ancID) {
446
- r = forest_[r].parent;
447
- }
448
- return r; // either returns endID or the child of the ancestor it came
449
- // through.
450
- }
451
-
452
- void _update_representative(index pos, index oldRep, index newRep) {
453
- index r = pos;
454
- while (r != -1 && forest_[r].representative == oldRep) {
455
- forest_[r].representative = newRep;
456
- r = forest_[r].parent;
457
- }
458
- }
459
-
460
- void _neg_edge_neg_edge_switch(index position, index e1V1Position,
461
- index e1V2Position, index e2V1Position,
462
- index e2V2Position) {
463
- minimumSpanningForest_.update_weight(
464
- positionToID_[e1V1Position], positionToID_[e1V2Position], position + 1);
465
- minimumSpanningForest_.update_weight(positionToID_[e2V1Position],
466
- positionToID_[e2V2Position], position);
467
-
468
- index e1 = positionToID_[position];
469
- index e2 = positionToID_[position + 1];
470
-
471
- if (forest_[e2].leftChild != e1 && forest_[e2].rightChild != e1) {
472
- barcode_.death_death_swap(position);
473
- std::swap(positionToID_[position], positionToID_[position + 1]);
474
- return;
475
- }
476
-
477
- index t1 = forest_[e1].leftChild;
478
- index t2 = forest_[e1].rightChild;
479
- index t3;
480
- if (forest_[e2].leftChild == e1)
481
- t3 = forest_[e2].rightChild;
482
- else
483
- t3 = forest_[e2].leftChild;
484
-
485
- index u = _is_vertex(t1) ? t1 : forest_[t1].representative;
486
- index v = _is_vertex(t2) ? t2 : forest_[t2].representative;
487
- index w = _is_vertex(t3) ? t3 : forest_[t3].representative;
488
- if (forest_[u].representative < forest_[v].representative) {
489
- std::swap(u, v);
490
- std::swap(t1, t2);
491
- }
492
-
493
- index t = _is_ancestor_of(e1, positionToID_[e2V1Position], e2);
494
- if (t != t1 && t != t2)
495
- t = _is_ancestor_of(e1, positionToID_[e2V2Position], e2);
496
-
497
- if (t == t1) {
498
- forest_[e1].leftChild = t1;
499
- forest_[e2].leftChild = t2;
500
- forest_[e1].representative =
501
- (forest_[u].representative < forest_[w].representative) ? u : w;
502
- forest_[e2].representative =
503
- (forest_[forest_[e1].representative].representative <
504
- forest_[v].representative)
505
- ? forest_[e1].representative
506
- : v;
507
- forest_[t2].parent = e2;
508
- } else {
509
- forest_[e1].leftChild = t2;
510
- forest_[e2].leftChild = t1;
511
- forest_[e1].representative =
512
- (forest_[v].representative < forest_[w].representative) ? v : w;
513
- forest_[e2].representative =
514
- (forest_[forest_[e1].representative].representative <
515
- forest_[u].representative)
516
- ? forest_[e1].representative
517
- : u;
518
- forest_[t1].parent = e2;
519
- }
520
- forest_[e1].rightChild = t3;
521
- forest_[e2].rightChild = e1;
522
- forest_[t3].parent = e1;
523
-
524
- if (t == t2 || forest_[u].representative < forest_[w].representative) {
525
- barcode_.death_death_swap(position);
526
- }
527
- }
528
-
529
- void _neg_edge_pos_edge_switch(index position, index e1V1Position,
530
- index e1V2Position, index e2V1Position,
531
- index e2V2Position) {
532
- index bottleneck = minimumSpanningForest_.get_bootleneck_weight(
533
- positionToID_[e2V1Position], positionToID_[e2V2Position]);
534
-
535
- if (bottleneck == position) {
536
- minimumSpanningForest_.swap_out_edges(
537
- positionToID_[e1V1Position], positionToID_[e1V2Position],
538
- positionToID_[e2V1Position], positionToID_[e2V2Position], position);
539
- } else {
540
- minimumSpanningForest_.update_weight(positionToID_[e1V1Position],
541
- positionToID_[e1V2Position],
542
- position + 1);
543
- barcode_.death_birth_swap(position);
544
- std::swap(positionToID_[position], positionToID_[position + 1]);
545
- }
546
- }
547
-
548
- void _pos_edge_edge_switch(index position, index e2V1Position,
549
- index e2V2Position) {
550
- if (_is_positive_edge(position + 1)) {
551
- barcode_.birth_birth_swap(position);
552
- } else {
553
- barcode_.birth_death_swap(position);
554
- minimumSpanningForest_.update_weight(
555
- positionToID_[e2V1Position], positionToID_[e2V2Position], position);
556
- std::swap(positionToID_[position], positionToID_[position + 1]);
557
- }
558
- }
559
-
560
- void _print_subtree(std::ostream &stream, const std::string& prefix, index id, bool isLeft){
561
- if (id != -1){
562
- stream << prefix;
563
- stream << (isLeft ? "├──" : "└──" );
564
-
565
- // print the value of the node
566
- stream << id << std::endl;
567
-
568
- // enter the next tree level - left and right branch
569
- _print_subtree(stream, prefix + (isLeft ? "│ " : " "), forest_[id].leftChild, true);
570
- _print_subtree(stream, prefix + (isLeft ? "│ " : " "), forest_[id].rightChild, false);
571
- }
572
- }
573
- };
574
-
575
- #endif // H0V_MERGE_TREE_H
1
+ #ifndef H0V_MERGE_TREE_H
2
+ #define H0V_MERGE_TREE_H
3
+
4
+ #include <algorithm>
5
+ #include <cassert>
6
+ #include <iostream>
7
+ #include <set>
8
+ #include <unordered_map>
9
+ #include <utility>
10
+ #include <vector>
11
+
12
+ #include <boost/functional/hash.hpp>
13
+
14
+ struct Bar {
15
+ Bar() : dim(-1), birth(-1), death(-1) {}
16
+
17
+ Bar(int dim, int birth, int death) : dim(dim), birth(birth), death(death) {}
18
+
19
+ int dim;
20
+ int birth;
21
+ int death;
22
+ };
23
+
24
+ class Naive_bottleneck_forest {
25
+ public:
26
+ using index = int;
27
+
28
+ Naive_bottleneck_forest() {}
29
+
30
+ // assumes that ids of vertices are continuous and start with 0
31
+ Naive_bottleneck_forest(unsigned int numberOfVertices) : forest_(numberOfVertices), weights_() {}
32
+
33
+ friend void swap(Naive_bottleneck_forest &mf1, Naive_bottleneck_forest &mf2) {
34
+ mf1.forest_.swap(mf2.forest_);
35
+ mf1.weights_.swap(mf2.weights_);
36
+ }
37
+
38
+ void add_edge(index firstVertexIndex, index secondVertexIndex, index edgeWeight) {
39
+ forest_[firstVertexIndex].children.insert(secondVertexIndex);
40
+ forest_[secondVertexIndex].children.insert(firstVertexIndex);
41
+ weights_.emplace(_get_edge(firstVertexIndex, secondVertexIndex), edgeWeight);
42
+ }
43
+
44
+ void swap_out_edges(index inVertex1, index inVertex2, index outVertex1, index outVertex2, index outWeight) {
45
+ if (forest_[inVertex1].parent == inVertex2) {
46
+ forest_[inVertex1].parent = -1;
47
+ forest_[inVertex2].children.erase(inVertex1);
48
+ } else {
49
+ forest_[inVertex2].parent = -1;
50
+ forest_[inVertex1].children.erase(inVertex2);
51
+ }
52
+ weights_.erase(_get_edge(inVertex1, inVertex2));
53
+
54
+ if (forest_[outVertex1].parent == -1) {
55
+ forest_[outVertex1].parent = outVertex2;
56
+ forest_[outVertex2].children.insert(outVertex1);
57
+ } else {
58
+ if (forest_[outVertex2].parent != -1) _reroot(outVertex2);
59
+ forest_[outVertex2].parent = outVertex1;
60
+ forest_[outVertex1].children.insert(outVertex2);
61
+ }
62
+ weights_.emplace(_get_edge(outVertex1, outVertex2), outWeight);
63
+ }
64
+
65
+ // to call once the tree finished
66
+ void root() {
67
+ for (unsigned int i = 0; i < forest_.size(); ++i) {
68
+ if (forest_[i].parent == -1) {
69
+ _root(i);
70
+ }
71
+ }
72
+ }
73
+
74
+ // assumes root() was already called and that both vertices are in the same
75
+ // connected component
76
+ index get_bootleneck_weight(index id1, index id2) const {
77
+ auto get_weight = [&](index id) {
78
+ return forest_[id].parent == -1 ? -1 : weights_.at(_get_edge(id, forest_[id].parent));
79
+ };
80
+
81
+ std::unordered_map<index, index> partialBottleneck;
82
+ index r = id1;
83
+ index w = 0;
84
+ partialBottleneck.emplace(r, w);
85
+ while (forest_[r].parent != -1) {
86
+ w = std::max(get_weight(r), w);
87
+ partialBottleneck.emplace(forest_[r].parent, w);
88
+ r = forest_[r].parent;
89
+ }
90
+ r = id2;
91
+ auto bIt = partialBottleneck.find(r);
92
+ w = 0;
93
+ while (forest_[r].parent != -1 && bIt == partialBottleneck.end()) {
94
+ w = std::max(get_weight(r), w);
95
+ r = forest_[r].parent;
96
+ bIt = partialBottleneck.find(r);
97
+ }
98
+ return std::max(w, bIt->second);
99
+ }
100
+
101
+ void update_weight(index id1, index id2, index newWeight) { weights_.at(_get_edge(id1, id2)) = newWeight; }
102
+
103
+ void print(std::ostream &stream) const {
104
+ stream << "MSF:\n";
105
+ unsigned int c = 0;
106
+ for (const Node &n : forest_) {
107
+ stream << "[" << c << "] parent: ";
108
+ stream << n.parent << "\n";
109
+ stream << "[" << c << "] children: ";
110
+ for (index id : n.children) stream << id << " ";
111
+ stream << "\n";
112
+ if (n.parent != -1) {
113
+ stream << "[" << c << "] weight of parent edge: ";
114
+ stream << weights_.at(_get_edge(c, n.parent)) << "\n";
115
+ }
116
+ ++c;
117
+ }
118
+ }
119
+
120
+ private:
121
+ struct Node {
122
+ index parent = -1;
123
+ std::set<index> children = {};
124
+ };
125
+
126
+ std::vector<Node> forest_;
127
+ std::unordered_map<std::pair<index, index>, index, boost::hash<std::pair<index, index>>>
128
+ weights_; // TODO: test other map types and having a pair as key is ugly
129
+
130
+ void _root(index id) {
131
+ const Node &p = forest_[id];
132
+ for (index chID : p.children) {
133
+ Node &ch = forest_[chID];
134
+ ch.children.erase(id);
135
+ ch.parent = id;
136
+ _root(chID);
137
+ }
138
+ }
139
+
140
+ void _reroot(index id) {
141
+ index newParent = -1;
142
+ index current = id;
143
+ index oldParent = forest_[id].parent;
144
+ forest_[current].parent = newParent;
145
+ forest_[current].children.insert(oldParent);
146
+ newParent = current;
147
+ current = oldParent;
148
+ oldParent = forest_[current].parent;
149
+ while (oldParent != -1) {
150
+ forest_[current].parent = newParent;
151
+ forest_[current].children.erase(newParent);
152
+ forest_[current].children.insert(oldParent);
153
+ newParent = current;
154
+ current = oldParent;
155
+ oldParent = forest_[current].parent;
156
+ }
157
+ forest_[current].parent = newParent;
158
+ forest_[current].children.erase(newParent);
159
+ }
160
+
161
+ std::pair<index, index> _get_edge(index id1, index id2) const {
162
+ return std::make_pair(std::min(id1, id2), std::max(id1, id2));
163
+ }
164
+ };
165
+
166
+ class Naive_merge_forest {
167
+ public:
168
+ using index = int;
169
+
170
+ Naive_merge_forest() {}
171
+
172
+ Naive_merge_forest(unsigned int numberOfSimplices, unsigned int numberOfVertices)
173
+ : forest_(numberOfVertices * 2 - 1),
174
+ barcode_(numberOfSimplices, numberOfVertices),
175
+ positionToID_(numberOfSimplices, -1),
176
+ nextVertexIndex_(0),
177
+ nextEdgeIndex_(numberOfVertices),
178
+ minimumSpanningForest_(numberOfVertices) {}
179
+
180
+ friend void swap(Naive_merge_forest &mf1, Naive_merge_forest &mf2) {
181
+ mf1.forest_.swap(mf2.forest_);
182
+ swap(mf1.barcode_, mf2.barcode_);
183
+ mf1.positionToID_.swap(mf2.positionToID_);
184
+ std::swap(mf1.nextVertexIndex_, mf2.nextVertexIndex_);
185
+ std::swap(mf1.nextEdgeIndex_, mf2.nextEdgeIndex_);
186
+ swap(mf1.minimumSpanningForest_, mf2.minimumSpanningForest_);
187
+ }
188
+
189
+ // assumes that merge tree is constructed fully with add_edge before any swap
190
+ // is done with vine
191
+ bool add_edge(index edgeIndex, index firstVertexIndex, index secondVertexIndex) {
192
+ index r1 = _get_root(positionToID_[firstVertexIndex]);
193
+ index r2 = _get_root(positionToID_[secondVertexIndex]);
194
+
195
+ if (r1 == r2) {
196
+ barcode_.add_positive_edge(edgeIndex);
197
+ return false; // positive edge
198
+ }
199
+
200
+ assert(positionToID_[edgeIndex] == -1 && "Edge was already added");
201
+ positionToID_[edgeIndex] = nextEdgeIndex_;
202
+
203
+ forest_[r1].parent = nextEdgeIndex_;
204
+ forest_[r2].parent = nextEdgeIndex_;
205
+ forest_[nextEdgeIndex_].leftChild = r1;
206
+ forest_[nextEdgeIndex_].rightChild = r2;
207
+ index rep1 = _is_vertex(r1) ? r1 : forest_[r1].representative;
208
+ index rep2 = _is_vertex(r2) ? r2 : forest_[r2].representative;
209
+ forest_[nextEdgeIndex_].representative = rep1 < rep2 ? rep1 : rep2;
210
+ index pair =
211
+ (forest_[nextEdgeIndex_].representative == rep1) ? forest_[rep2].representative : forest_[rep1].representative;
212
+ barcode_.add_negative_edge(pair, edgeIndex);
213
+
214
+ ++nextEdgeIndex_;
215
+
216
+ minimumSpanningForest_.add_edge(positionToID_[firstVertexIndex], positionToID_[secondVertexIndex], edgeIndex);
217
+
218
+ return true; // negative edge
219
+ }
220
+
221
+ void add_vertex(index vertexIndex) {
222
+ positionToID_[vertexIndex] = nextVertexIndex_;
223
+ forest_[nextVertexIndex_++].representative = vertexIndex;
224
+ barcode_.add_vertex(vertexIndex);
225
+ }
226
+
227
+ void initialize() {
228
+ barcode_.barcode_.resize(barcode_.nextBarIndex_);
229
+ minimumSpanningForest_.root();
230
+ }
231
+
232
+ void vertex_swap(index position) {
233
+ index nca = _get_nearest_common_ancestor(positionToID_[position], positionToID_[position + 1]);
234
+
235
+ if (nca == -1) { // not in the same connected component
236
+ barcode_.birth_birth_swap(position);
237
+ } else {
238
+ index rightRep = _is_vertex(forest_[nca].rightChild) ? forest_[nca].rightChild
239
+ : forest_[forest_[nca].rightChild].representative;
240
+ index leftRep =
241
+ _is_vertex(forest_[nca].leftChild) ? forest_[nca].leftChild : forest_[forest_[nca].leftChild].representative;
242
+ index ncaPairID = (forest_[nca].representative == rightRep) ? leftRep : rightRep;
243
+ index ncaPos = barcode_.death(forest_[ncaPairID].representative);
244
+
245
+ index e1 = barcode_.death(position);
246
+ index e2 = barcode_.death(position + 1);
247
+
248
+ if ((e1 != -1 && e1 < ncaPos) || (e2 != -1 && e2 < ncaPos)) {
249
+ barcode_.birth_birth_swap(position);
250
+ }
251
+ }
252
+
253
+ _update_representative(nca, positionToID_[position], positionToID_[position + 1]);
254
+ std::swap(forest_[positionToID_[position]].representative, forest_[positionToID_[position + 1]].representative);
255
+ std::swap(positionToID_[position], positionToID_[position + 1]);
256
+ }
257
+
258
+ void vertex_edge_swap(index position, index eV1Position, index eV2Position) {
259
+ if (_is_positive_edge(position + 1)) {
260
+ barcode_.birth_birth_swap(position);
261
+ } else {
262
+ barcode_.birth_death_swap(position);
263
+ minimumSpanningForest_.update_weight(positionToID_[eV1Position], positionToID_[eV2Position], position);
264
+ }
265
+ forest_[positionToID_[position]].representative = position + 1;
266
+ std::swap(positionToID_[position], positionToID_[position + 1]);
267
+ }
268
+
269
+ void edge_vertex_swap(index position, index eV1Position, index eV2Position) {
270
+ if (_is_positive_edge(position)) {
271
+ barcode_.birth_birth_swap(position);
272
+ } else {
273
+ barcode_.death_birth_swap(position);
274
+ minimumSpanningForest_.update_weight(positionToID_[eV1Position], positionToID_[eV2Position], position + 1);
275
+ }
276
+ forest_[positionToID_[position + 1]].representative = position;
277
+ std::swap(positionToID_[position], positionToID_[position + 1]);
278
+ }
279
+
280
+ void edge_edge_swap(index position, index e1V1Position, index e1V2Position, index e2V1Position, index e2V2Position) {
281
+ if (_is_positive_edge(position)) {
282
+ _pos_edge_edge_switch(position, e2V1Position, e2V2Position);
283
+ return;
284
+ }
285
+
286
+ if (_is_positive_edge(position + 1)) {
287
+ _neg_edge_pos_edge_switch(position, e1V1Position, e1V2Position, e2V1Position, e2V2Position);
288
+ return;
289
+ }
290
+
291
+ _neg_edge_neg_edge_switch(position, e1V1Position, e1V2Position, e2V1Position, e2V2Position);
292
+ }
293
+
294
+ const std::vector<Bar> &get_barcode() const { return barcode_.barcode_; }
295
+
296
+ int get_dimension(index position) { return !_is_vertex(positionToID_[position]); }
297
+
298
+ inline friend std::ostream &operator<<(std::ostream &stream, Naive_merge_forest &structure) {
299
+ for (unsigned int i = 0; i < structure.forest_.size(); ++i) {
300
+ if (structure.forest_[i].parent == -1) structure._print_subtree(stream, "", i, false);
301
+ }
302
+ structure.minimumSpanningForest_.print(stream);
303
+ return stream;
304
+ }
305
+
306
+ private:
307
+ struct Node {
308
+ index parent = -1;
309
+ index leftChild = -1;
310
+ index rightChild = -1;
311
+ index representative = -1; // cc rep for edges, position for vertices
312
+ };
313
+
314
+ struct Barcode {
315
+ Barcode() {}
316
+
317
+ Barcode(int numberOfSimplices, [[maybe_unused]] int numberOfVertices)
318
+ : barcode_(numberOfSimplices), positionToBar_(numberOfSimplices), nextBarIndex_(0) {}
319
+
320
+ friend void swap(Barcode &mf1, Barcode &mf2) {
321
+ mf1.barcode_.swap(mf2.barcode_);
322
+ mf1.positionToBar_.swap(mf2.positionToBar_);
323
+ std::swap(mf1.nextBarIndex_, mf2.nextBarIndex_);
324
+ }
325
+
326
+ void add_birth(index birth) {
327
+ positionToBar_[birth] = nextBarIndex_;
328
+ barcode_[nextBarIndex_].birth = birth;
329
+ ++nextBarIndex_;
330
+ }
331
+
332
+ void add_vertex(index birth) {
333
+ barcode_[nextBarIndex_].dim = 0;
334
+ add_birth(birth);
335
+ }
336
+
337
+ void add_positive_edge(index birth) {
338
+ barcode_[nextBarIndex_].dim = 1;
339
+ add_birth(birth);
340
+ }
341
+
342
+ void add_negative_edge(index birth, index death) {
343
+ index b = positionToBar_[birth];
344
+ positionToBar_[death] = b;
345
+ barcode_[b].death = death;
346
+ }
347
+
348
+ void birth_birth_swap(index position) {
349
+ std::swap(barcode_[positionToBar_[position]].birth, barcode_[positionToBar_[position + 1]].birth);
350
+ std::swap(positionToBar_[position], positionToBar_[position + 1]);
351
+ }
352
+
353
+ void birth_death_swap(index position) {
354
+ std::swap(barcode_[positionToBar_[position]].birth, barcode_[positionToBar_[position + 1]].death);
355
+ std::swap(positionToBar_[position], positionToBar_[position + 1]);
356
+ }
357
+
358
+ void death_death_swap(index position) {
359
+ std::swap(barcode_[positionToBar_[position]].death, barcode_[positionToBar_[position + 1]].death);
360
+ std::swap(positionToBar_[position], positionToBar_[position + 1]);
361
+ }
362
+
363
+ void death_birth_swap(index position) {
364
+ std::swap(barcode_[positionToBar_[position]].death, barcode_[positionToBar_[position + 1]].birth);
365
+ std::swap(positionToBar_[position], positionToBar_[position + 1]);
366
+ }
367
+
368
+ index death(index birth) { return barcode_[positionToBar_[birth]].death; }
369
+
370
+ std::vector<Bar> barcode_;
371
+ std::vector<index> positionToBar_;
372
+ index nextBarIndex_;
373
+ };
374
+
375
+ std::vector<Node> forest_;
376
+ Barcode barcode_;
377
+ std::vector<index> positionToID_;
378
+ index nextVertexIndex_;
379
+ index nextEdgeIndex_;
380
+ Naive_bottleneck_forest minimumSpanningForest_;
381
+
382
+ bool _is_vertex(index nodeIndex) const {
383
+ const Node &n = forest_[nodeIndex];
384
+ return n.leftChild == -1 && n.rightChild == -1;
385
+ }
386
+
387
+ bool _is_positive_edge(index position) const { return positionToID_[position] == -1; }
388
+
389
+ index _get_root(index node) const {
390
+ while (forest_[node].parent != -1) {
391
+ node = forest_[node].parent;
392
+ }
393
+ return node;
394
+ }
395
+
396
+ index _get_nearest_common_ancestor(index firstIndex, index secondIndex) const {
397
+ std::set<index> path;
398
+ index r = firstIndex;
399
+ path.insert(r);
400
+ while (forest_[r].parent != -1) {
401
+ r = forest_[r].parent;
402
+ path.insert(r);
403
+ }
404
+ r = secondIndex;
405
+ while (r != -1 && path.find(r) == path.end()) {
406
+ r = forest_[r].parent;
407
+ }
408
+ return r;
409
+ }
410
+
411
+ index _is_ancestor_of(index ancID, index descID, index endID = -1) const {
412
+ index r = descID;
413
+ while (r != endID && forest_[r].parent != ancID) {
414
+ r = forest_[r].parent;
415
+ }
416
+ return r; // either returns endID or the child of the ancestor it came
417
+ // through.
418
+ }
419
+
420
+ void _update_representative(index pos, index oldRep, index newRep) {
421
+ index r = pos;
422
+ while (r != -1 && forest_[r].representative == oldRep) {
423
+ forest_[r].representative = newRep;
424
+ r = forest_[r].parent;
425
+ }
426
+ }
427
+
428
+ void _neg_edge_neg_edge_switch(index position,
429
+ index e1V1Position,
430
+ index e1V2Position,
431
+ index e2V1Position,
432
+ index e2V2Position) {
433
+ minimumSpanningForest_.update_weight(positionToID_[e1V1Position], positionToID_[e1V2Position], position + 1);
434
+ minimumSpanningForest_.update_weight(positionToID_[e2V1Position], positionToID_[e2V2Position], position);
435
+
436
+ index e1 = positionToID_[position];
437
+ index e2 = positionToID_[position + 1];
438
+
439
+ if (forest_[e2].leftChild != e1 && forest_[e2].rightChild != e1) {
440
+ barcode_.death_death_swap(position);
441
+ std::swap(positionToID_[position], positionToID_[position + 1]);
442
+ return;
443
+ }
444
+
445
+ index t1 = forest_[e1].leftChild;
446
+ index t2 = forest_[e1].rightChild;
447
+ index t3;
448
+ if (forest_[e2].leftChild == e1)
449
+ t3 = forest_[e2].rightChild;
450
+ else
451
+ t3 = forest_[e2].leftChild;
452
+
453
+ index u = _is_vertex(t1) ? t1 : forest_[t1].representative;
454
+ index v = _is_vertex(t2) ? t2 : forest_[t2].representative;
455
+ index w = _is_vertex(t3) ? t3 : forest_[t3].representative;
456
+ if (forest_[u].representative < forest_[v].representative) {
457
+ std::swap(u, v);
458
+ std::swap(t1, t2);
459
+ }
460
+
461
+ index t = _is_ancestor_of(e1, positionToID_[e2V1Position], e2);
462
+ if (t != t1 && t != t2) t = _is_ancestor_of(e1, positionToID_[e2V2Position], e2);
463
+
464
+ if (t == t1) {
465
+ forest_[e1].leftChild = t1;
466
+ forest_[e2].leftChild = t2;
467
+ forest_[e1].representative = (forest_[u].representative < forest_[w].representative) ? u : w;
468
+ forest_[e2].representative = (forest_[forest_[e1].representative].representative < forest_[v].representative)
469
+ ? forest_[e1].representative
470
+ : v;
471
+ forest_[t2].parent = e2;
472
+ } else {
473
+ forest_[e1].leftChild = t2;
474
+ forest_[e2].leftChild = t1;
475
+ forest_[e1].representative = (forest_[v].representative < forest_[w].representative) ? v : w;
476
+ forest_[e2].representative = (forest_[forest_[e1].representative].representative < forest_[u].representative)
477
+ ? forest_[e1].representative
478
+ : u;
479
+ forest_[t1].parent = e2;
480
+ }
481
+ forest_[e1].rightChild = t3;
482
+ forest_[e2].rightChild = e1;
483
+ forest_[t3].parent = e1;
484
+
485
+ if (t == t2 || forest_[u].representative < forest_[w].representative) {
486
+ barcode_.death_death_swap(position);
487
+ }
488
+ }
489
+
490
+ void _neg_edge_pos_edge_switch(index position,
491
+ index e1V1Position,
492
+ index e1V2Position,
493
+ index e2V1Position,
494
+ index e2V2Position) {
495
+ index bottleneck =
496
+ minimumSpanningForest_.get_bootleneck_weight(positionToID_[e2V1Position], positionToID_[e2V2Position]);
497
+
498
+ if (bottleneck == position) {
499
+ minimumSpanningForest_.swap_out_edges(positionToID_[e1V1Position],
500
+ positionToID_[e1V2Position],
501
+ positionToID_[e2V1Position],
502
+ positionToID_[e2V2Position],
503
+ position);
504
+ } else {
505
+ minimumSpanningForest_.update_weight(positionToID_[e1V1Position], positionToID_[e1V2Position], position + 1);
506
+ barcode_.death_birth_swap(position);
507
+ std::swap(positionToID_[position], positionToID_[position + 1]);
508
+ }
509
+ }
510
+
511
+ void _pos_edge_edge_switch(index position, index e2V1Position, index e2V2Position) {
512
+ if (_is_positive_edge(position + 1)) {
513
+ barcode_.birth_birth_swap(position);
514
+ } else {
515
+ barcode_.birth_death_swap(position);
516
+ minimumSpanningForest_.update_weight(positionToID_[e2V1Position], positionToID_[e2V2Position], position);
517
+ std::swap(positionToID_[position], positionToID_[position + 1]);
518
+ }
519
+ }
520
+
521
+ void _print_subtree(std::ostream &stream, const std::string &prefix, index id, bool isLeft) {
522
+ if (id != -1) {
523
+ stream << prefix;
524
+ stream << (isLeft ? "├──" : "└──");
525
+
526
+ // print the value of the node
527
+ stream << id << std::endl;
528
+
529
+ // enter the next tree level - left and right branch
530
+ _print_subtree(stream, prefix + (isLeft ? "│ " : " "), forest_[id].leftChild, true);
531
+ _print_subtree(stream, prefix + (isLeft ? "│ " : " "), forest_[id].rightChild, false);
532
+ }
533
+ }
534
+ };
535
+
536
+ #endif // H0V_MERGE_TREE_H