TTK
Loading...
Searching...
No Matches
GabowTarjan.h
Go to the documentation of this file.
1#pragma once
2
3#include <Debug.h>
5
6#include <algorithm>
7#include <iostream>
8#include <map>
9#include <queue>
10#include <vector>
11
12namespace ttk {
13
14 class GabowTarjan : public Debug {
15
16 struct Edge {
17 int v1{};
18 int v2{};
19 double weight{};
20
21 Edge(int p1, int p2, double w) : v1{p1}, v2{p2}, weight{w} {
22 }
23
24 bool operator<(const Edge &other) const {
25 return weight < other.weight;
26 }
27 };
28
29 public:
31 this->setDebugMsgPrefix("Gabow-Tarjan");
32 }
33
34 double Distance();
35
37
38 int run(std::vector<MatchingType> &matchings);
39
40 inline void setInput(int rowSize_,
41 int colSize_,
42 std::vector<std::vector<double>> *C_) {
43 Cptr = C_;
44
45 Size1 = (unsigned int)rowSize_ - 1;
46 Size2 = (unsigned int)colSize_ - 1;
47 if(Size1 <= 0 || Size2 <= 0) {
48 this->printMsg("One or more empty diagram(s).");
49 }
50
51 MaxSize = Size1 + Size2;
52 Edges.clear();
53
54 // Connect diagonal points.
55 for(unsigned int i = Size1; i < MaxSize; ++i)
56 for(unsigned int j = MaxSize + Size2; j < 2 * MaxSize; ++j) {
57 const Edge localEdge(i, j, (double)0);
58 Edges.emplace_back(localEdge);
59 }
60
61 // Connect real points.
62 for(unsigned int i = 0; i < Size1; ++i) {
63 unsigned int k = MaxSize;
64 for(unsigned int j = 0; j < Size2; ++j) {
65 auto val = (*C_)[i][j];
66 const Edge localEdge(i, k++, val);
67 Edges.emplace_back(localEdge);
68 }
69 }
70
71 // Connect real points with their diagonal.
72 for(unsigned int i = 0; i < Size1; ++i) {
73 auto val = (*C_)[i][Size2];
74 const Edge localEdge(i, MaxSize + Size2 + i, val);
75 Edges.emplace_back(localEdge);
76 }
77
78 for(unsigned int j = 0, k = MaxSize; j < Size2; ++j, ++k) {
79 auto val = (*C_)[Size1][j];
80 const Edge localEdge(Size1 + (k - MaxSize), k, val);
81 Edges.emplace_back(localEdge);
82 }
83
84 std::sort(Edges.begin(), Edges.end());
85 }
86
87 inline void clear() {
88 MaxSize = 0;
89 Size1 = 0;
90 Size2 = 0;
91 Edges.clear();
92 Pair.clear();
93 Connections.clear();
94 Layers.clear();
95 }
96
97 private:
98 // Original cost matrix.
99 std::vector<std::vector<double>> *Cptr;
100
101 /*
102 * Total number of persistencePairs
103 */
104 unsigned int MaxSize;
105 unsigned int Size1;
106 unsigned int Size2;
107
108 /*
109 * Edges between all nodes given by
110 * persistencePairs in both persistence diagrams
111 */
112 std::vector<Edge> Edges;
113
114 /*
115 * Pairing between vertices in persistence diagrams,
116 * -1 means the vertex is not paired to any other vertex.
117 * Non negative number, index of the vertex to
118 * which the given vertex is paired.
119 */
120 std::vector<int> Pair;
121
122 /*
123 * Connection matrix, gives edges between
124 * vertices in the first and second diagram
125 */
126 std::vector<std::vector<int>> Connections;
127
128 /*
129 * Layers used in the Hopcroft-Karp algorithm
130 * The layer information is required for a NIL vertex
131 * and all vertices in PersistencePairs1
132 * Hence 0 slot is used for NIL and all the vertices
133 * in PersistencePairs1 are shifted by one.
134 * So to read layer of the vertex 0 we access Layers[1]
135 */
136 std::vector<int> Layers;
137
138 bool DFS(int v);
139 bool BFS();
140
141 // Hopcroft-Karp algorithm: find a maximal matching
142 void HopcroftKarp(unsigned int &matching);
143 };
144
145} // namespace ttk
Minimalist debugging class.
Definition Debug.h:88
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
int run(std::vector< MatchingType > &matchings)
void printCurrentMatching()
void setInput(int rowSize_, int colSize_, std::vector< std::vector< double > > *C_)
Definition GabowTarjan.h:40
The Topology ToolKit.
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)