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

Commit ec4aa23

Browse files
committed
GreedySetCover.cpp
1 parent 3d2c498 commit ec4aa23

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

GreedySetCover.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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_GreedySetCover
22+
#endif
23+
24+
#ifndef FUNC_GreedySetCover
25+
#define FUNC_GreedySetCover
26+
27+
#include <cassert>
28+
#include <iomanip>
29+
#include <set>
30+
#include "utils.h"
31+
32+
#define set_T typename std::set<T>
33+
34+
template <typename T>
35+
std::set<size_t> GreedySetCover(set_T& X, std::vector<set_T>& F) {
36+
size_t U_size = X.size();
37+
std::set<size_t> e;
38+
std::vector<set_T> FU = F;
39+
while (U_size) {
40+
size_t s, s_size = 0;
41+
for (size_t i = 0; i < FU.size(); i++) {
42+
size_t i_size = FU[i].size();
43+
if (s_size < i_size) {
44+
s = i;
45+
s_size = i_size;
46+
}
47+
}
48+
assert(s_size);
49+
for (auto i = FU[s].begin(); i != FU[s].end(); i++) {
50+
U_size--;
51+
for (size_t j = 0; j < FU.size(); j++)
52+
if (j != s)
53+
FU[j].erase(*i);
54+
}
55+
FU[s].clear();
56+
e.insert(s);
57+
}
58+
return e;
59+
}
60+
#endif
61+
62+
#ifdef MAIN_GreedySetCover
63+
int main(int argc, char *argv[]) {
64+
const size_t n = get_argv(argc, argv, 1, 10);
65+
const size_t m = get_argv(argc, argv, 2, 10);
66+
const size_t l = get_argv(argc, argv, 2, 20);
67+
using T = size_t;
68+
set_T X;
69+
std::vector<set_T> F;
70+
for (size_t i = 0; i < n; i++)
71+
X.insert(i);
72+
std::random_device rd;
73+
std::uniform_int_distribution<T> d1(0, n * m);
74+
for (size_t i = 0; i < m; i++) {
75+
set_T s;
76+
for (size_t j = 0; j < n; j++)
77+
if (d1(rd) < l)
78+
s.insert(j);
79+
F.push_back(s);
80+
}
81+
std::uniform_int_distribution<T> d2(0, m - 1);
82+
for (size_t i = 0; i < n; i++)
83+
F[d2(rd)].insert(i);
84+
std::uniform_int_distribution<T> d3(0, n - 1);
85+
for (size_t i = 0; i < m; i++)
86+
F[i].insert(d3(rd));
87+
std::set<size_t> ans = GreedySetCover(X, F);
88+
for (size_t i = 0; i < m; i++) {
89+
if (ans.find(i) != ans.end())
90+
std::cout << "+ ";
91+
else
92+
std::cout << " ";
93+
for (size_t j = 0; j < n; j++) {
94+
if (F[i].find(j) != F[i].end())
95+
std::cout << std::setw(4) << j;
96+
else
97+
std::cout << std::string(4, ' ');
98+
}
99+
std::cout << std::endl;
100+
}
101+
return 0;
102+
}
103+
#endif
104+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
| 33 | ClosestPairPoints.cpp | Closest Pair Points | 1043 |
225225
| 35 | ApproxVertexCover.cpp | Approx Vertex Cover | 1109 |
226226
| 35 | ApproxTSPTour.cpp | Approx TSP Tour | 1112 |
227+
| 35 | GreedySetCover.cpp | Greedy Set Cover | 1119 |
227228

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

0 commit comments

Comments
 (0)