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
|
/* This file is part of the Gudhi Library. The Gudhi library
* (Geometric Understanding in Higher Dimensions) is a generic C++
* library for computational topology.
*
* Author: Francois Godi
*
* Copyright (C) 2015 INRIA (France)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BOTTLENECK_H_
#define BOTTLENECK_H_
#include <gudhi/Graph_matching.h>
#include <cmath>
namespace Gudhi {
namespace persistence_diagram {
double bottleneck_distance_approx(Persistence_graph& g, double e) {
double b_lower_bound = 0.;
double b_upper_bound = g.diameter_bound();
const double alpha = std::pow(g.size(), 1./5.);
if(e < std::numeric_limits<double>::epsilon() * alpha){
e = std::numeric_limits<double>::epsilon() * alpha;
#ifdef DEBUG_TRACES
std::cout << "Epsilon user given value is less than eps_min. Forced to eps_min by the application" << std::endl;
#endif // DEBUG_TRACES
}
Graph_matching m(g);
Graph_matching biggest_unperfect(g);
while (b_upper_bound - b_lower_bound > 2*e) {
double step = b_lower_bound + (b_upper_bound - b_lower_bound)/alpha;
m.set_r(step);
while (m.multi_augment()); //compute a maximum matching (in the graph corresponding to the current r)
if (m.perfect()) {
m = biggest_unperfect;
b_upper_bound = step;
} else {
biggest_unperfect = m;
b_lower_bound = step;
}
}
return (b_lower_bound + b_upper_bound)/2.;
}
double bottleneck_distance_exact(Persistence_graph& g) {
std::vector<double> sd = g.sorted_distances();
long lower_bound_i = 0;
long upper_bound_i = sd.size()-1;
const double alpha = std::pow(g.size(), 1./5.);
Graph_matching m(g);
Graph_matching biggest_unperfect(g);
while (lower_bound_i != upper_bound_i) {
long step = lower_bound_i + static_cast<long>((upper_bound_i - lower_bound_i - 1)/alpha);
m.set_r(sd.at(step));
while (m.multi_augment()); //compute a maximum matching (in the graph corresponding to the current r)
if (m.perfect()) {
m = biggest_unperfect;
upper_bound_i = step;
} else {
biggest_unperfect = m;
lower_bound_i = step + 1;
}
}
return sd.at(lower_bound_i);
}
/** \brief Function to use in order to compute the Bottleneck distance between two persistence diagrams (see concepts).
* If the last parameter e is not 0, you get an additive e-approximation, which is a lot faster to compute whatever is e.
* Thus, by default, e is a very small positive double, actually the smallest double possible such that the floating-point inaccuracies don't lead to a failure of the algorithm.
*
* \ingroup bottleneck_distance
*/
template<typename Persistence_diagram1, typename Persistence_diagram2>
double bottleneck_distance(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2, double e=std::numeric_limits<double>::epsilon()) {
Persistence_graph g(diag1, diag2, e);
if(g.bottleneck_alive() == std::numeric_limits<double>::infinity())
return std::numeric_limits<double>::infinity();
return std::max(g.bottleneck_alive(), e == 0. ? bottleneck_distance_exact(g) : bottleneck_distance_approx(g, e));
}
} // namespace persistence_diagram
} // namespace Gudhi
#endif // BOTTLENECK_H_
|