64template <
typename Graph,
typename C1,
typename C2>
66 using T =
typename C2::value_type;
68 [[maybe_unused]]
auto total_dual_cost = T(0);
69 auto total_primal_cost = T(0);
71 for (
auto&& edge : gra.edges()) {
72 auto [utx, vtx] = edge.end_points();
73 if (cover[utx] || cover[vtx]) {
76 if (gap[utx] < gap[vtx]) {
80 total_dual_cost += gap[vtx];
81 total_primal_cost += weight[vtx];
86 if (total_dual_cost > total_primal_cost) {
87 throw std::logic_error(
88 "min_vertex_cover_pd: dual cost exceeds primal cost (invalid weights?)");
90 if (total_primal_cost > 2 * total_dual_cost) {
91 throw std::logic_error(
92 "min_vertex_cover_pd: 2-approximation guarantee violated (invalid weights?)");
94 return total_primal_cost;
146template <
typename Graph,
typename C1,
typename C2>
148 using T =
typename C2::value_type;
150 if (&indset == &dep) {
151 throw std::invalid_argument(
"min_maximal_independant_set_pd: indset and dep must differ");
154 auto cover = [&](
const auto& utx) {
156 for (
auto&& vtx : gra[utx]) {
162 auto total_primal_cost = T(0);
163 for (
auto&& utx : gra) {
171 auto min_val = gap[utx];
173 for (
auto&& vtx : gra[utx]) {
177 if (min_val > gap[vtx]) {
183 indset[min_vtx] =
true;
184 total_primal_cost += weight[min_vtx];
185 if (min_vtx == utx) {
188 for (
auto&& vtx : gra[utx]) {
192 return total_primal_cost;
auto min_vertex_cover_pd(const Graph &gra, C1 &cover, const C2 &weight)
Minimum weighted vertex cover using primal-dual algorithm.
Definition primal_dual.hpp:65
auto min_maximal_independant_set_pd(const Graph &gra, C1 &indset, C1 &dep, const C2 &weight)
Minimum maximal independent set using primal-dual algorithm.
Definition primal_dual.hpp:147