|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + This file is part of Set--Based Graph Library. |
| 4 | +
|
| 5 | + SBG Library is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + SBG Library 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 General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with SBG Library. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +
|
| 18 | + ******************************************************************************/ |
| 19 | + |
| 20 | +#include "algorithms/mfvs/smallest_sv_mfvs.hpp" |
| 21 | +#include "algorithms/scc/scc.hpp" |
| 22 | +#include "algorithms/scc/scc_fact.hpp" |
| 23 | +#include "sbg/natural.hpp" |
| 24 | +#include "sbg/pw_map.hpp" |
| 25 | +#include "sbg/set_fact.hpp" |
| 26 | +#include "util/logger.hpp" |
| 27 | + |
| 28 | +#include <numeric> |
| 29 | + |
| 30 | +namespace SBG { |
| 31 | + |
| 32 | +namespace LIB { |
| 33 | + |
| 34 | +namespace detail { |
| 35 | + |
| 36 | +//////////////////////////////////////////////////////////////////////////////// |
| 37 | +// Greedy MFVS Algorithm ------------------------------------------------------- |
| 38 | +//////////////////////////////////////////////////////////////////////////////// |
| 39 | + |
| 40 | +SmallestSVMFVS::SmallestSVMFVS() {} |
| 41 | + |
| 42 | +/** |
| 43 | + * @brief It calculates the minimum vertex from the smallest SV. |
| 44 | + */ |
| 45 | +MD_NAT getVertexFromSmallestSV(const DirectedSBG& dsbg) |
| 46 | +{ |
| 47 | + PWMap Vmap = dsbg.Vmap(); |
| 48 | + PWMap mmap = Vmap.imageMultiplicity(); |
| 49 | + |
| 50 | + MD_NAT min_mult{dsbg.V().arity(), Inf}; |
| 51 | + Set remaining = mmap.image(); |
| 52 | + while (!remaining.isEmpty()) { |
| 53 | + MD_NAT jth_mult = remaining.minElem(); |
| 54 | + NAT current_degree = std::accumulate(min_mult.begin(), min_mult.end() |
| 55 | + , 0); |
| 56 | + NAT jth_degree = std::accumulate(jth_mult.begin(), jth_mult.end(), 0); |
| 57 | + if (jth_degree < current_degree) { |
| 58 | + min_mult = jth_mult; |
| 59 | + } |
| 60 | + |
| 61 | + remaining = remaining.difference(SET_FACT.createSet(jth_mult)); |
| 62 | + } |
| 63 | + |
| 64 | + Set small_sv = mmap.preImage(SET_FACT.createSet(min_mult)); |
| 65 | + Set vertex = Vmap.preImage(small_sv); |
| 66 | + |
| 67 | + return vertex.minElem(); |
| 68 | +} |
| 69 | + |
| 70 | +Set SmallestSVMFVS::calculate(const DirectedSBG& input_dsbg) const |
| 71 | +{ |
| 72 | + DirectedSBG dsbg = input_dsbg; |
| 73 | + |
| 74 | + Util::DEBUG_LOG << "initial smallest set-vertex mfvs dsbg:\n" << dsbg << "\n"; |
| 75 | + |
| 76 | + PWMap rmap = SCC_FACT.createSCCAlgorithm().calculate(dsbg).rmap(); |
| 77 | + Set fvs_result = SET_FACT.createSet(); |
| 78 | + Set visitedSV = SET_FACT.createSet(); |
| 79 | + while (rmap.fixedPoints() != rmap.domain()) { |
| 80 | + // Get vertex from smallest set-vertex |
| 81 | + MD_NAT smallest_sv_vertex = getVertexFromSmallestSV(dsbg); |
| 82 | + Set Vj = SET_FACT.createSet(smallest_sv_vertex); |
| 83 | + fvs_result = std::move(fvs_result).disjointCup(Vj); |
| 84 | + |
| 85 | + // Handle repetition |
| 86 | + PWMap Vmap = dsbg.Vmap(); |
| 87 | + Set repeatedSV = visitedSV.intersection(Vmap.image(Vj)); |
| 88 | + if (!repeatedSV.isEmpty()) { |
| 89 | + Set V_plus = Vmap.preImage(Vmap.image(Vj)); |
| 90 | + fvs_result = std::move(fvs_result).cup(std::move(V_plus)); |
| 91 | + |
| 92 | + visitedSV = repeatedSV.difference(Vmap.image(Vj)); |
| 93 | + } else { |
| 94 | + visitedSV = std::move(repeatedSV).disjointCup(Vmap.image(Vj)); |
| 95 | + } |
| 96 | + |
| 97 | + // Erase selected vertices |
| 98 | + dsbg.eraseVertices(fvs_result); |
| 99 | + |
| 100 | + // Resulting SCC from induced graph |
| 101 | + rmap = SCC_FACT.createSCCAlgorithm().calculate(dsbg).rmap(); |
| 102 | + |
| 103 | + Util::DEBUG_LOG << "Vj: " << Vj << "\n"; |
| 104 | + Util::DEBUG_LOG << "new rmap: " << rmap << "\n\n"; |
| 105 | + } |
| 106 | + |
| 107 | + fvs_result.compact(); |
| 108 | + return fvs_result; |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace detail |
| 112 | + |
| 113 | +} // namespace LIB |
| 114 | + |
| 115 | +} // namespace SBG |
0 commit comments