Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit f5c5251

Browse files
committed
ApproxMinWeightVC.cpp
1 parent ec4aa23 commit f5c5251

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

ApproxMinWeightVC.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
| 35 | ApproxVertexCover.cpp | Approx Vertex Cover | 1109 |
226226
| 35 | ApproxTSPTour.cpp | Approx TSP Tour | 1112 |
227227
| 35 | GreedySetCover.cpp | Greedy Set Cover | 1119 |
228+
| 35 | ApproxMinWeightVC.cpp | Approx Min Weight VC | 1126 |
228229

229230
# Supplementary Files
230231
* `utils.h`: Utils

Simplex.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void InitializeSimplex(usetst& N, usetst& B, matst& A, vectst& b, vectst& c,
8484
vectst Lc;
8585
T Lv = 0;
8686
for (auto i = B.begin(); i != B.end(); i++)
87-
A[*i][0] = 1;
87+
A[*i][0] = -1;
8888
Lc[0] = -1;
8989
for (auto j = N.begin(); j != N.end(); j++)
9090
Lc[*j] = 0;
@@ -130,9 +130,9 @@ void InitializeSimplex(usetst& N, usetst& B, matst& A, vectst& b, vectst& c,
130130
c[*j] = 0;
131131
for (auto i = B.begin(); i != B.end(); i++) {
132132
if (c.find(*i) != c.end()) {
133-
v += b[*i];
133+
v += b[*i] * c[*i];
134134
for (auto j = N.begin(); j != N.end(); j++)
135-
c[*j] -= A[*i][*j];
135+
c[*j] -= A[*i][*j] * c[*i];
136136
c.erase(*i);
137137
}
138138
}
@@ -194,7 +194,6 @@ void main_T(const size_t n, const size_t m) {
194194
for (size_t i = 1; i <= m; i++)
195195
for (size_t j = 1; j <= n; j++)
196196
A[n + i][j] = int_a[(i - 1) * n + (j - 1)];
197-
vectst x = Simplex(A, b, c);
198197
for (size_t i = 1; i <= m; i++) {
199198
for (size_t j = 1; j <= n; j++)
200199
std::cout << A[n + i][j] << " ";
@@ -207,15 +206,16 @@ void main_T(const size_t n, const size_t m) {
207206
for (size_t i = 1; i <= n; i++)
208207
std::cout << c[i] << " ";
209208
std::cout << std::endl;
209+
vectst x = Simplex(A, b, c);
210210
for (size_t i = 1; i <= n; i++)
211211
std::cout << x[i] << " ";
212212
std::cout << std::endl;
213213
}
214214

215215
int main(int argc, char *argv[]) {
216216
const size_t type = get_argv(argc, argv, 1, 0);
217-
const size_t m = get_argv(argc, argv, 2, 10);
218-
const size_t n = get_argv(argc, argv, 3, 5);
217+
const size_t m = get_argv(argc, argv, 2, 4);
218+
const size_t n = get_argv(argc, argv, 3, 2);
219219
if (!type)
220220
main_T<double>(n, m);
221221
else

0 commit comments

Comments
 (0)