|
| 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_ApproxVertexCover |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef FUNC_ApproxVertexCover |
| 25 | +#define FUNC_ApproxVertexCover |
| 26 | + |
| 27 | +#include "utils.h" |
| 28 | + |
| 29 | +#include "Graph.cpp" |
| 30 | + |
| 31 | +template <typename GT, typename T> |
| 32 | +void ApproxVertexCover(GT& G, uset<T>& C) { |
| 33 | + uset<Edge<T>, EdgeHash<T>> E; |
| 34 | + for (auto i = G.all_edges(); !i.end(); i++) |
| 35 | + E.insert(*i); |
| 36 | + while (E.begin() != E.end()) { |
| 37 | + Edge<T> e = *E.begin(); |
| 38 | + const T &u = e.s, &v = e.d; |
| 39 | + C.insert(u); |
| 40 | + C.insert(v); |
| 41 | + for (auto i = G.edges_from(u); !i.end(); i++) |
| 42 | + E.erase(*i); |
| 43 | + for (auto i = G.edges_from(v); !i.end(); i++) |
| 44 | + E.erase(*i); |
| 45 | + } |
| 46 | +} |
| 47 | +#endif |
| 48 | + |
| 49 | +#ifdef MAIN_ApproxVertexCover |
| 50 | +int main(int argc, char *argv[]) { |
| 51 | + const size_t v = get_argv(argc, argv, 1, 10); |
| 52 | + const size_t e = get_argv(argc, argv, 2, 20); |
| 53 | + const bool dir = 0; |
| 54 | + GraphAdjList<size_t> G(dir); |
| 55 | + random_graph(G, v, e); |
| 56 | + uset<size_t> ans; |
| 57 | + ApproxVertexCover(G, ans); |
| 58 | + auto f1 = [ans](size_t v) { |
| 59 | + if (ans.find(v) != ans.end()) |
| 60 | + std::cout << " [color=green]"; |
| 61 | + return false; |
| 62 | + }; |
| 63 | + auto f2 = [](Edge<size_t>) mutable {}; |
| 64 | + graphviz(G, f1, f2); |
| 65 | + return 0; |
| 66 | +} |
| 67 | +#endif |
| 68 | + |
0 commit comments