Debian Codegraph @

src/Skeleton_blocker/example/Skeleton_blocker_iteration.cpp

raw
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): David Salinas
4 *
5 * Copyright (C) 2014 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
11 #include <gudhi/Skeleton_blocker.h>
12 #include <gudhi/Clock.h>
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string>
17 #include <fstream>
18 #include <sstream>
19
20 typedef Gudhi::skeleton_blocker::Skeleton_blocker_simple_traits Traits;
21 typedef Gudhi::skeleton_blocker::Skeleton_blocker_complex<Traits> Complex;
22 typedef Complex::Vertex_handle Vertex_handle;
23 typedef Complex::Simplex Simplex;
24
25 Complex build_complete_complex(int n) {
26 // build a full complex with n vertices and 2^n-1 simplices
27 Complex complex;
28 for (int i = 0; i < n; i++)
29 complex.add_vertex();
30 for (int i = 0; i < n; i++)
31 for (int j = 0; j < i; j++)
32 complex.add_edge_without_blockers(Vertex_handle(i), Vertex_handle(j));
33 return complex;
34 }
35
36 int main(int argc, char *argv[]) {
37 Gudhi::Clock skbl_chrono("Time to build the complete complex, enumerate simplices and Euler Characteristic");
38 const int n = 15;
39
40 // build a full complex with n vertices and 2^n-1 simplices
41 Complex complex(build_complete_complex(n));
42
43 // this is just to illustrate iterators, to count number of vertices
44 // or edges, complex.num_vertices() and complex.num_edges() are
45 // more appropriated!
46 unsigned num_vertices = 0;
47 for (auto v : complex.vertex_range()) {
48 std::clog << "Vertex " << v << std::endl;
49 ++num_vertices;
50 }
51
52 // such loop can also be done directly with distance as iterators are STL compliant
53 auto edges = complex.edge_range();
54 unsigned num_edges = std::distance(edges.begin(), edges.end());
55
56 unsigned euler = 0;
57 unsigned num_simplices = 0;
58 // we use a reference to a simplex instead of a copy
59 // value here because a simplex is a set of integers
60 // and copying it cost time
61 for (const Simplex & s : complex.complex_simplex_range()) {
62 ++num_simplices;
63 if (s.dimension() % 2 == 0)
64 euler += 1;
65 else
66 euler -= 1;
67 }
68 std::clog << "Saw " << num_vertices << " vertices, " << num_edges << " edges and " << num_simplices << " simplices"
69 << std::endl;
70 std::clog << "The Euler Characteristic is " << euler << std::endl;
71 std::clog << skbl_chrono;
72 return EXIT_SUCCESS;
73 }