|
| 1 | +// |
| 2 | +// algorithm - some algorithms in "Introduction to Algorithms", third edition |
| 3 | +// Copyright (C) 2018 lxylxy123456 |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as |
| 7 | +// published by the Free Software Foundation, either version 3 of the |
| 8 | +// License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | + |
| 19 | +#ifndef MAIN |
| 20 | +#define MAIN |
| 21 | +#define MAIN_ApproxMinWeightVC |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef FUNC_ApproxMinWeightVC |
| 25 | +#define FUNC_ApproxMinWeightVC |
| 26 | + |
| 27 | +#include "utils.h" |
| 28 | + |
| 29 | +#include "Graph.cpp" |
| 30 | +#include "Simplex.cpp" |
| 31 | + |
| 32 | +template <typename GT, typename T, typename WT> |
| 33 | +void ApproxMinWeightVC(GT& G, umap<T, WT>& w, uset<T>& C) { |
| 34 | + umap<size_t, umap<size_t, WT>> A; |
| 35 | + umap<size_t, WT> b, c; |
| 36 | + for (auto i = G.V.begin(); i != G.V.end(); i++) |
| 37 | + c[*i + 1] = -w[*i]; |
| 38 | + const size_t n = G.V.size(); |
| 39 | + size_t e = n + 1; |
| 40 | + for (auto i = G.all_edges(); !i.end(); i++, e++) { |
| 41 | + for (auto j = G.V.begin(); j != G.V.end(); j++) |
| 42 | + A[e][*j + 1] = 0; |
| 43 | + A[e][i.s() + 1] = -1; |
| 44 | + A[e][i.d() + 1] = -1; |
| 45 | + b[e] = -1; |
| 46 | + } |
| 47 | + for (auto i = G.V.begin(); i != G.V.end(); i++, e++) { |
| 48 | + for (auto j = G.V.begin(); j != G.V.end(); j++) |
| 49 | + A[e][*j + 1] = 0; |
| 50 | + A[e][*i + 1] = 1; |
| 51 | + b[e] = 1; |
| 52 | + } |
| 53 | + umap<size_t, WT> x = Simplex(A, b, c); |
| 54 | + for (auto i = G.V.begin(); i != G.V.end(); i++) |
| 55 | + if (x[*i + 1] >= (WT) 1 / (WT) 2) |
| 56 | + C.insert(*i); |
| 57 | +} |
| 58 | +#endif |
| 59 | + |
| 60 | +#ifdef MAIN_ApproxMinWeightVC |
| 61 | +int main(int argc, char *argv[]) { |
| 62 | + const size_t v = get_argv(argc, argv, 1, 10); |
| 63 | + const size_t e = get_argv(argc, argv, 2, 20); |
| 64 | + const bool dir = 0; |
| 65 | + GraphAdjList<size_t> G(dir); |
| 66 | + random_graph(G, v, e); |
| 67 | + uset<size_t> ans; |
| 68 | + std::vector<int> weight; |
| 69 | + random_integers(weight, 1, v, v); |
| 70 | + using WT = double; |
| 71 | + umap<size_t, WT> w; |
| 72 | + for (size_t i = 0; i < v; i++) |
| 73 | + w[i] = weight[i]; |
| 74 | + ApproxMinWeightVC(G, w, ans); |
| 75 | + auto f1 = [w, ans](size_t v) mutable { |
| 76 | + std::cout << " [label=" << w[v]; |
| 77 | + if (ans.find(v) != ans.end()) |
| 78 | + std::cout << " color=green"; |
| 79 | + std::cout << "]"; |
| 80 | + return true; |
| 81 | + }; |
| 82 | + auto f2 = [](Edge<size_t>) mutable {}; |
| 83 | + graphviz(G, f1, f2); |
| 84 | + return 0; |
| 85 | +} |
| 86 | +#endif |
| 87 | + |
0 commit comments