83 Pair.assign(2 * MaxSize, -1);
87 Layers.resize(MaxSize + 1);
90 unsigned int matching = 0;
91 unsigned int matching0 = 0;
95 Connections.resize(MaxSize);
98 double currentWeight = 0;
102 unsigned int firstNotAddedEdge = 0;
103 const unsigned int nbEdges = (
unsigned int)Edges.size();
105 unsigned int lowerBound = 0;
106 unsigned int upperBound = nbEdges;
107 unsigned int guessEdge = (lowerBound + nbEdges) / 2;
110 while(matching < MaxSize) {
112 matching0 = matching;
113 const unsigned int oldGuessEdge = guessEdge;
115 currentWeight = Edges[guessEdge].weight;
116 while(Edges[guessEdge].weight == currentWeight && guessEdge < nbEdges)
119 if(guessEdge >= nbEdges) {
120 this->
printMsg(
"ran out of edges.");
124 while(firstNotAddedEdge >= guessEdge) {
125 const int v1 = Edges[firstNotAddedEdge].v1;
126 const int v2 = Edges[firstNotAddedEdge].v2;
128 std::vector<int> vec1 = Connections[v1];
129 vec1.erase(std::remove(vec1.begin(), vec1.end(), v2), vec1.end());
130 Connections[v1] = vec1;
137 while(firstNotAddedEdge < guessEdge) {
138 const int v1 = Edges[firstNotAddedEdge].v1;
139 const int v2 = Edges[firstNotAddedEdge].v2;
141 Connections[v1].push_back(v2);
147 Pair.assign(2 * MaxSize, -1);
150 Layers.resize(MaxSize + 1);
153 this->
printMsg(
"Guessing for " + std::to_string(guessEdge) +
"...");
156 HopcroftKarp(matching);
159 if(matching >= MaxSize) {
162 matching = matching0;
163 upperBound = guessEdge;
164 guessEdge = (lowerBound + guessEdge) / 2;
167 if(oldGuessEdge == guessEdge) {
168 this->
printMsg(
"Binary search success.");
169 return Edges[(guessEdge > 0 ? guessEdge - 1 : guessEdge)].weight;
175 if(firstNotAddedEdge == nbEdges) {
176 this->
printMsg(
"Not enough edges to find the matching!");
178 return currentWeight;
181 lowerBound = guessEdge;
182 guessEdge = (guessEdge + upperBound) / 2;
187 if(oldGuessEdge == guessEdge) {
188 this->
printMsg(
"Binary search success.");
189 return Edges[(guessEdge > 0 ? guessEdge - 1 : guessEdge)].weight;
198 const int size = 2 * MaxSize;
199 std::vector<int> missedPlaces;
202 std::stringstream msg;
203 msg <<
"Assignment matrix: " << std::endl;
204 for(
int i = 0; i < size; ++i) {
205 const int k = Pair[i];
206 if(k < 0 || k > size)
207 missedPlaces.push_back(i);
208 for(
int j = 0; j < size; ++j) {
209 msg << (j == k ?
"1 " :
"0 ");
213 msg <<
"/Assignment matrix." << std::endl << std::endl;
218 std::stringstream msg;
219 msg <<
"Missed:" << std::endl;
220 for(
unsigned int i = 0; i < missedPlaces.size(); ++i) {
221 msg << missedPlaces.at(i) <<
" ";
223 msg << std::endl << std::endl;
230 const double dist = this->Distance();
231 this->
printMsg(
"Computed distance " + std::to_string(dist));
236 for(
unsigned int i = 0; i < Size1; ++i) {
240 const int j = Pair[i] - MaxSize;
241 if(j <= -1 || (j < (
int)Size2 && Pair[j + MaxSize] != (
int)i)) {
242 this->printErr(
"Hopcroft-Karp built an invalid matching.");
246 if(j >= (
int)Size2) {
247 matchings.emplace_back(i, j, (*Cptr)[i][Size2]);
249 matchings.emplace_back(i, j, (*Cptr)[i][j]);
253 for(
unsigned int j = Size1; j < MaxSize; ++j) {
257 const int i = Pair[j] - MaxSize - Size2;
258 if(i > -1 && (i >= (
int)Size1 || Pair[i + MaxSize + Size2] != (
int)j)) {
259 this->printErr(
"Hopcroft-Karp built an invalid matching.");
264 matchings.emplace_back(i, j - Size1, (*Cptr)[Size1][j - Size1]);
267 matchings.emplace_back(i, j - Size1, (*Cptr)[i][j - Size1]);
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)