Bottleneck.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author: Francois Godi
6  *
7  * Copyright (C) 2015 INRIA
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef BOTTLENECK_H_
24 #define BOTTLENECK_H_
25 
26 #include <gudhi/Graph_matching.h>
27 
28 #include <vector>
29 #include <algorithm> // for max
30 #include <limits> // for numeric_limits
31 
32 #include <cmath>
33 
34 namespace Gudhi {
35 
36 namespace persistence_diagram {
37 
38 double bottleneck_distance_approx(Persistence_graph& g, double e) {
39  double b_lower_bound = 0.;
40  double b_upper_bound = g.diameter_bound();
41  const double alpha = std::pow(g.size(), 1. / 5.);
42  Graph_matching m(g);
43  Graph_matching biggest_unperfect(g);
44  while (b_upper_bound - b_lower_bound > 2 * e) {
45  volatile double step = b_lower_bound + (b_upper_bound - b_lower_bound) / alpha;
46  if (step <= b_lower_bound || step >= b_upper_bound) // Avoid precision problem
47  break;
48  m.set_r(step);
49  while (m.multi_augment()) {} // compute a maximum matching (in the graph corresponding to the current r)
50  if (m.perfect()) {
51  m = biggest_unperfect;
52  b_upper_bound = step;
53  } else {
54  biggest_unperfect = m;
55  b_lower_bound = step;
56  }
57  }
58  return (b_lower_bound + b_upper_bound) / 2.;
59 }
60 
61 double bottleneck_distance_exact(Persistence_graph& g) {
62  std::vector<double> sd = g.sorted_distances();
63  long lower_bound_i = 0;
64  long upper_bound_i = sd.size() - 1;
65  const double alpha = std::pow(g.size(), 1. / 5.);
66  Graph_matching m(g);
67  Graph_matching biggest_unperfect(g);
68  while (lower_bound_i != upper_bound_i) {
69  long step = lower_bound_i + static_cast<long> ((upper_bound_i - lower_bound_i - 1) / alpha);
70  m.set_r(sd.at(step));
71  while (m.multi_augment()) {} // compute a maximum matching (in the graph corresponding to the current r)
72  if (m.perfect()) {
73  m = biggest_unperfect;
74  upper_bound_i = step;
75  } else {
76  biggest_unperfect = m;
77  lower_bound_i = step + 1;
78  }
79  }
80  return sd.at(lower_bound_i);
81 }
82 
102 template<typename Persistence_diagram1, typename Persistence_diagram2>
103 double bottleneck_distance(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2,
104  double e = (std::numeric_limits<double>::min)()) {
105  Persistence_graph g(diag1, diag2, e);
106  if (g.bottleneck_alive() == std::numeric_limits<double>::infinity())
107  return std::numeric_limits<double>::infinity();
108  return (std::max)(g.bottleneck_alive(), e == 0. ? bottleneck_distance_exact(g) : bottleneck_distance_approx(g, e));
109 }
110 
111 } // namespace persistence_diagram
112 
113 } // namespace Gudhi
114 
115 #endif // BOTTLENECK_H_
Definition: SimplicialComplexForAlpha.h:26
double bottleneck_distance(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2, double e=(std::numeric_limits< double >::min)())
Function to compute the Bottleneck distance between two persistence diagrams.
Definition: Bottleneck.h:103
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 18:07:51 for GUDHI by Doxygen 1.8.13