Skip to content

Commit 804a594

Browse files
committed
Redefined topological sorting SBG algorithm [IN PROGRESS]
1 parent 9105bea commit 804a594

10 files changed

Lines changed: 308 additions & 210 deletions

File tree

algorithms/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ add_subdirectory(mfvs)
55
add_subdirectory(misc)
66
#add_subdirectory(partitioner)
77
add_subdirectory(scc)
8-
#add_subdirectory(toposort)
8+
add_subdirectory(sorting)
99

1010
install_lib_headers("${CMAKE_CURRENT_SOURCE_DIR}")

algorithms/sorting/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Add subdirectories
2+
add_subdirectory(topological)
3+
4+
install_lib_headers("${CMAKE_CURRENT_SOURCE_DIR}")

algorithms/toposort/CMakeLists.txt renamed to algorithms/sorting/topological/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ target_sources(
22
sbgraph
33
PRIVATE
44
min_vertex_ts.cpp
5-
topo_sort.cpp
5+
topological_sorting.cpp
66
ts_fact.cpp
77
)
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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/sorting/topological/min_vertex_ts.hpp"
21+
#include "sbg/natural.hpp"
22+
#include "sbg/pwmap_fact.hpp"
23+
#include "sbg/set_fact.hpp"
24+
#include "util/debug.hpp"
25+
#include "util/logger.hpp"
26+
27+
namespace SBG {
28+
29+
namespace LIB {
30+
31+
namespace detail {
32+
33+
////////////////////////////////////////////////////////////////////////////////
34+
// Minimum Vertex Topological Sort Algorithm -----------------------------------
35+
////////////////////////////////////////////////////////////////////////////////
36+
37+
MinVertexTS::MinVertexTS()
38+
: _smap(PWMAP_FACT.createPWMap()), _dsbg(), _visitedSV(SET_FACT.createSet())
39+
, _n(0) {}
40+
41+
Expression calculateExpr(const MD_NAT& old_value, const MD_NAT& new_value)
42+
{
43+
return Expression{old_value};
44+
}
45+
46+
PWMap MinVertexTS::independentRepetition(const Set& independent_V_plus,
47+
const Expression& successor_expr) const
48+
{
49+
PWMap result = PWMAP_FACT.createPWMap();
50+
51+
result.emplace(independent_V_plus, successor_expr);
52+
bool independent_image = result.image().difference(independent_V_plus).isEmpty();
53+
Util::ERROR_UNLESS(independent_image/*&& result.isInjective()*/
54+
, "MinVertexTS::independentRepetition: incorrect guessing ", result);
55+
56+
return result;
57+
}
58+
59+
PWMap MinVertexTS::dependentRepetition(const Set& init_V) const
60+
{
61+
PWMap result = PWMAP_FACT.createPWMap();
62+
63+
Set Vi = init_V;
64+
Set V = SET_FACT.createSet();
65+
for (unsigned int j = 0; j < _n; ++j) {
66+
Vi = _smap.image(Vi);
67+
V = std::move(V).disjointCup(std::move(Vi));
68+
}
69+
PWMap smap_repetition = _smap.restrict(V);
70+
PWMap mapB = _dsbg.mapB();
71+
PWMap mapD = _dsbg.mapD();
72+
Set E_repetition = smap_repetition.composition(mapB).equalImage(mapD);
73+
74+
PWMap Emap = _dsbg.Emap();
75+
Set E_plus = Emap.preImage(Emap.image(E_repetition));
76+
PWMap mapB_plus = mapB.restrict(E_plus);
77+
PWMap mapD_plus = mapD.restrict(E_plus);
78+
result = mapB_plus.minAdj(mapD_plus);
79+
80+
/*TODO: check well-formed result*/
81+
82+
return result;
83+
}
84+
85+
PWMap MinVertexTS::calculate(const DirectedSBG& dsbg
86+
, const PWMap& pmap)
87+
{
88+
Util::DEBUG_LOG << "Topological sort dsbg:\n" << dsbg << "\n\n";
89+
90+
_dsbg = dsbg;
91+
92+
PWMap _smap = PWMAP_FACT.createPWMap();
93+
94+
Set same_SCC = _dsbg.V();
95+
Set same_SV = _dsbg.V();
96+
Set visited_SV = SET_FACT.createSet();
97+
Expression successor_expr{_dsbg.V().arity(), 1, 0};
98+
MD_NAT old_vi;
99+
do {
100+
Set V = _dsbg.V();
101+
PWMap mapD = _dsbg.mapD();
102+
Set independent = V.difference(mapD.image());
103+
104+
Set Vi = independent;
105+
if (Vi.isEmpty()) {
106+
Util::ERROR("Topological sorting: the SBG is not acyclic");
107+
}
108+
Set independent_same_SCC = Vi.intersection(same_SCC);
109+
if (independent_same_SCC.isEmpty()) {
110+
Vi = independent_same_SCC;
111+
}
112+
Set Vi_same_SV = Vi.intersection(same_SV);
113+
if (Vi_same_SV.isEmpty()) {
114+
Vi = Vi_same_SV;
115+
}
116+
117+
// Handle repetition
118+
PWMap Vmap = _dsbg.Vmap();
119+
MD_NAT vi = Vi.minElem();
120+
Set vi_set = SET_FACT.createSet(vi);
121+
Set repeatedSV = _visitedSV.intersection(Vmap.image(vi_set));
122+
if (!repeatedSV.isEmpty()) {
123+
Set V_plus = Vmap.preImage(Vmap.image(vi_set));
124+
Set independent_V_plus = independent.intersection(V_plus);
125+
PWMap smap_plus = PWMAP_FACT.createPWMap();
126+
if (!independent_V_plus.isEmpty()) {
127+
smap_plus = independentRepetition(independent_V_plus, successor_expr);
128+
} else {
129+
smap_plus = dependentRepetition(vi_set);
130+
}
131+
_smap = std::move(smap_plus).combine(std::move(_smap));
132+
_n = 0;
133+
} else {
134+
_smap.emplace(vi_set, successor_expr);
135+
136+
_visitedSV = std::move(_visitedSV).disjointCup(Vmap.image(vi_set));
137+
++_n;
138+
}
139+
140+
// Update values for new iteration
141+
_dsbg.eraseVertices(_smap.domain());
142+
old_vi = vi;
143+
successor_expr = calculateExpr(old_vi, vi);
144+
same_SCC = pmap.preImage(pmap.image(vi_set));
145+
same_SV = Vmap.preImage(Vmap.image(vi_set));
146+
} while (!_dsbg.V().isEmpty());
147+
148+
_smap.compact();
149+
Util::DEBUG_LOG << "Topological sort result:\n" << _smap << "\n\n";
150+
return _smap;
151+
}
152+
153+
} // namespace detail
154+
155+
} // namespace LIB
156+
157+
} // namespace SBG

algorithms/toposort/min_vertex_ts.hpp renamed to algorithms/sorting/topological/min_vertex_ts.hpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @file min_vertex_ts.hpp
22
3-
@brief <b>Concrete SBG Minimum Vertex Topological Sort Algorithm
3+
@brief <b>Concrete SBG Minimum Vertex Topological Sorting Algorithm
44
implementation</b>
55
66
<hr>
@@ -22,31 +22,49 @@
2222
2323
******************************************************************************/
2424

25-
#ifndef SBG_MIN_VERTEX_TS_HPP
26-
#define SBG_MIN_VERTEX_TS_HPP
25+
#ifndef SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_MIN_VERTEX_TS_HPP_
26+
#define SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_MIN_VERTEX_TS_HPP_
2727

28-
#include "algorithms/toposort/topo_sort.hpp"
28+
#include "sbg/directed_sbg.hpp"
29+
#include "sbg/expression.hpp"
30+
#include "sbg/pw_map.hpp"
31+
#include "sbg/set.hpp"
2932

3033
namespace SBG {
3134

3235
namespace LIB {
3336

37+
namespace detail {
38+
3439
////////////////////////////////////////////////////////////////////////////////
35-
// Minimum Vertex Topological Sort Algorithm Implementation (concrete strategy)
40+
// Minimum Vertex Topological Sorting Algorithm Implementation -----------------
3641
////////////////////////////////////////////////////////////////////////////////
3742

3843
/**
3944
* @brief In each step takes out the minimum vertex without dependencies.
4045
*/
41-
class MinVertexTopoSort : public TSStrategy {
42-
public:
43-
MinVertexTopoSort();
46+
class MinVertexTS {
47+
public:
48+
MinVertexTS();
49+
50+
PWMap calculate(const DirectedSBG& dsbg, const PWMap& pmap);
51+
52+
private:
53+
PWMap independentRepetition(const Set& independent_V_plus
54+
, const Expression& successor_expr) const;
4455

45-
PWMap calculate(const DSBG& dsbg) const override;
56+
PWMap dependentRepetition(const Set& init_V) const;
57+
58+
PWMap _smap;
59+
DirectedSBG _dsbg;
60+
Set _visitedSV;
61+
unsigned int _n;
4662
};
4763

64+
} // namespace detail
65+
4866
} // namespace LIB
4967

5068
} // namespace SBG
5169

52-
#endif
70+
#endif // SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_MIN_VERTEX_TS_HPP_

algorithms/toposort/topo_sort.cpp renamed to algorithms/sorting/topological/topological_sorting.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,61 @@
1717
1818
******************************************************************************/
1919

20-
#include <chrono>
21-
22-
#include "algorithms/toposort/topo_sort.hpp"
23-
#include "util/logger.hpp"
20+
#include "algorithms/sorting/topological/topological_sorting.hpp"
21+
#include "util/debug.hpp"
22+
#include "util/time_profiler.hpp"
2423

2524
namespace SBG {
2625

2726
namespace LIB {
2827

2928
////////////////////////////////////////////////////////////////////////////////
30-
// Topological Sort Algorithm Abstract Strategy Constructors -------------------
29+
// Topological Sorting Algorithm implementations -------------------------------
3130
////////////////////////////////////////////////////////////////////////////////
3231

33-
TSStrategy::TSStrategy() {}
32+
std::ostream& operator<<(std::ostream& out, const TSKind kind)
33+
{
34+
switch (kind) {
35+
case TSKind::kMinVertex: {
36+
out << "minimum vertex";
37+
break;
38+
}
39+
40+
default: {
41+
Util::ERROR("TSKind::operator<<: unsupported topological sorting "
42+
, "algorithm implementation");
43+
break;
44+
}
45+
}
46+
47+
return out;
48+
}
3449

3550
////////////////////////////////////////////////////////////////////////////////
36-
// Topological Sort Algorithm Interface ----------------------------------------
51+
// Topological Sorting Algorithm Interface -------------------------------------
3752
////////////////////////////////////////////////////////////////////////////////
3853

39-
TopoSort::TopoSort(TSStratPtr strat) : strategy_(std::move(strat)) {}
54+
TopologicalSorting::TopologicalSorting(TSKind kind) : _kind(kind), _impl()
55+
{
56+
switch (kind) {
57+
case TSKind::kMinVertex: {
58+
_impl = detail::MinVertexTS{};
59+
break;
60+
}
61+
62+
default: {
63+
Util::ERROR("TopologicalSorting::operator<<: unsupported topological "
64+
, "sorting algorithm implementation");
65+
}
66+
}
67+
}
4068

41-
PWMap TopoSort::calculate(const DSBG& dsbg) const
69+
PWMap TopologicalSorting::calculate(const DirectedSBG& dsbg, const PWMap& pmap)
4270
{
43-
return strategy_->calculate(dsbg);
71+
auto text = "Total topological sorting execution time: ";
72+
Util::Internal::TimeProfiler profiler{text};
73+
74+
return std::visit([&](auto& a) { return a.calculate(dsbg, pmap); }, _impl);
4475
}
4576

4677
} // namespace LIB

algorithms/toposort/topo_sort.hpp renamed to algorithms/sorting/topological/topological_sorting.hpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,47 @@
2121
2222
******************************************************************************/
2323

24-
#ifndef SBG_TOPOSORT_HPP
25-
#define SBG_TOPOSORT_HPP
24+
#ifndef SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_TOPOLOGICAL_SORTING_HPP_
25+
#define SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_TOPOLOGICAL_SORTING_HPP_
2626

27+
#include "algorithms/sorting/topological/min_vertex_ts.hpp"
2728
#include "sbg/directed_sbg.hpp"
2829

2930
namespace SBG {
3031

3132
namespace LIB {
3233

34+
namespace detail {
35+
3336
////////////////////////////////////////////////////////////////////////////////
34-
// Topological Sort Algorithm Abstract Strategy --------------------------------
37+
// Topological Sorting Algorithm Implementations -------------------------------
3538
////////////////////////////////////////////////////////////////////////////////
3639

37-
class TSStrategy;
38-
39-
typedef std::unique_ptr<TSStrategy> TSStratPtr;
40+
using TSImpl = std::variant<MinVertexTS>;
4041

41-
class TSStrategy {
42-
public:
43-
virtual ~TSStrategy() = default;
42+
} // namespace detail
4443

45-
TSStrategy();
44+
enum class TSKind { kMinVertex };
4645

47-
virtual PWMap calculate(const DSBG& dsbg) const = 0;
48-
};
46+
std::ostream& operator<<(std::ostream& out, const TSKind kind);
4947

5048
////////////////////////////////////////////////////////////////////////////////
51-
// Topological Sort Algorithm Interface (context) ------------------------------
49+
// Topological Sorting Algorithm -----------------------------------------------
5250
////////////////////////////////////////////////////////////////////////////////
5351

54-
class TopoSort {
55-
private:
56-
TSStratPtr strategy_;
52+
class TopologicalSorting {
53+
public:
54+
TopologicalSorting(TSKind kind);
5755

58-
public:
59-
TopoSort(TSStratPtr strat);
56+
PWMap calculate(const DirectedSBG& dsbg, const PWMap& pmap);
6057

61-
PWMap calculate(const DSBG& dsbg) const;
58+
private:
59+
TSKind _kind;
60+
detail::TSImpl _impl;
6261
};
6362

6463
} // namespace LIB
6564

6665
} // namespace SBG
6766

68-
#endif
67+
#endif // SBGRAPH_ALGORITHMS_SORTING_TOPOLOGICAL_TOPOLOGICAL_SORTING_HPP_

0 commit comments

Comments
 (0)