summaryrefslogtreecommitdiff
path: root/src/Bottleneck_distance/include/gudhi/Bottleneck.h
blob: 52de30be57254d20170ff879da9df795ffec9cd8 (plain)
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
/*    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>

namespace Gudhi {

namespace bottleneck_distance {

template<typename Persistence_diagram1, typename Persistence_diagram2>
double compute_exactly(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2) {
    Persistence_graph g(diag1, diag2, 0.);
    std::vector<double> sd(g.sorted_distances());
    int idmin = 0;
    int idmax = sd.size() - 1;
    // alpha can change the complexity
    double alpha = pow(sd.size(), 0.25);
    Graph_matching m(g);
    Graph_matching biggest_unperfect(g);
    while (idmin != idmax) {
        int step = static_cast<int>((idmax - idmin) / alpha);
        m.set_r(sd.at(idmin + step));
        while (m.multi_augment());
        //The above while compute a maximum matching (according to the r setted before)
        if (m.perfect()) {
            idmax = idmin + step;
            m = biggest_unperfect;
        } else {
            biggest_unperfect = m;
            idmin = idmin + step + 1;
        }
    }
    return sd.at(idmin);
}

/** \brief Function to use in order to compute the Bottleneck distance between two persistence diagrams.
 * If the last parameter e is not 0 (default value if not explicited), you get an additive e-approximation.
 *
 * \ingroup bottleneck_distance
 */
template<typename Persistence_diagram1, typename Persistence_diagram2>
double compute(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2, double e=0.) {
    if(e == 0.)
        return compute_exactly(diag1, diag2);
    Persistence_graph g(diag1, diag2, e);
    int in = g.diameter_bound()/e + 1;
    int idmin = 0;
    int idmax = in;
    // alpha can change the complexity
    double alpha = pow(in, 0.10);
    Graph_matching m(g);
    Graph_matching biggest_unperfect(g);
    while (idmin != idmax) {
        int step = static_cast<int>((idmax - idmin) / alpha);
        m.set_r(e*(idmin + step));
        while (m.multi_augment());
        //The above while compute a maximum matching (according to the r setted before)
        if (m.perfect()) {
            idmax = idmin + step;
            m = biggest_unperfect;
        } else {
            biggest_unperfect = m;
            idmin = idmin + step + 1;
        }
    }
    return e*(idmin);
}



}  // namespace bottleneck_distance

}  // namespace Gudhi

#endif  // BOTTLENECK_H_

/* Dichotomic version
template<typename Persistence_diagram1, typename Persistence_diagram2>
double compute(const Persistence_diagram1 &diag1, const Persistence_diagram2 &diag2, double e) {
    if(e< std::numeric_limits<double>::min())
        return compute_exactly(diag1, diag2);
    G::initialize(diag1, diag2, e);
    double d = 0.;
    double f = G::diameter();
    while (f-d > e){
        Graph_matching m;
        m.set_r((d+f)/2.);
        while (m.multi_augment());
        //The above while compute a maximum matching (according to the r setted before)
        if (m.perfect())
            f = (d+f)/2.;
         else
            d= (d+f)/2.;
    }
    return d;
} */