Skip to content

Commit 4929a06

Browse files
committed
[iss-124]
* Developed up to scalar matching * Small changes * [iss-125] * Added bipartite graph for performance/boost * Use bipartite SBG in builders * Added scalar SCC calculation
1 parent 017c8d1 commit 4929a06

98 files changed

Lines changed: 2010 additions & 2259 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

algorithms/matching/bfs_matching.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ bool BFSMatching::ExitCondition::full_match() { return full_match_; }
3737

3838
bool BFSMatching::ExitCondition::found_paths() { return found_paths_; }
3939

40+
bool BFSMatching::ExitCondition::isSatisfied()
41+
{
42+
return full_match_ || !found_paths_;
43+
}
44+
45+
// Algorithm -------------------------------------------------------------------
46+
4047
BFSMatching::BFSMatching() : M_(SET_FACT.createSet()), dsbg_()
4148
, direction_(Direction::kForward) {}
4249

@@ -157,31 +164,37 @@ BFSMatching::ExitCondition BFSMatching::step(const Set& right_vertices)
157164
// Calculate exit conditions
158165
Set matchedU = dsbg_.mapD().image(M_);
159166
bool full_match = right_vertices.difference(matchedU).isEmpty();
160-
bool found_paths = !M_.isEmpty();
167+
bool found_paths = !augmenting_edges.isEmpty();
161168

162169
return ExitCondition(full_match, found_paths);
163170
}
164171

165-
bool BFSMatching::ExitCondition::isSatisfied()
172+
void BFSMatching::init(const BipartiteSBG& bsbg)
166173
{
167-
return full_match_ || !found_paths_;
168-
}
174+
PWMap map1 = bsbg.map1().compact();
175+
PWMap map2 = bsbg.map2().compact();
169176

170-
void BFSMatching::init(const SBG& sbg)
171-
{
172-
dsbg_ = DSBG(sbg.V().compact(), sbg.Vmap().compact(), sbg.map2()
173-
, sbg.map1().compact(), sbg.Emap().compact(), sbg.subEmap().compact());
177+
Set Y = bsbg.Y();
178+
PWMap map1_toY = map1.restrict(map1.preImage(Y));
179+
PWMap map2_toY = map2.restrict(map2.preImage(Y));
180+
PWMap mapB = map1_toY.concatenation(map2_toY);
174181

175-
return;
182+
Set X = bsbg.X();
183+
PWMap map1_toX = map1.restrict(map1.preImage(X));
184+
PWMap map2_toX = map2.restrict(map2.preImage(X));
185+
PWMap mapD = map1_toX.concatenation(map2_toX);
186+
187+
dsbg_ = DSBG(bsbg.V().compact(), bsbg.Vmap().compact(), mapB, mapD
188+
, bsbg.Emap().compact(), bsbg.subEmap().compact());
176189
}
177190

178-
MatchData BFSMatching::calculate(const SBG& sbg)
191+
MatchData BFSMatching::calculate(const BipartiteSBG& bsbg)
179192
{
180-
Util::DEBUG_LOG << "Matching sbg: \n" << sbg << "\n\n";
193+
Util::DEBUG_LOG << "Matching bsbg: \n" << bsbg << "\n\n";
181194

182195
auto begin = std::chrono::high_resolution_clock::now();
183-
init(sbg);
184-
Set right_vertices = dsbg_.mapB().image();
196+
init(bsbg);
197+
Set right_vertices = bsbg.Y();
185198

186199
ExitCondition exit_cond(false, false);
187200
do {
@@ -192,7 +205,7 @@ MatchData BFSMatching::calculate(const SBG& sbg)
192205
end - begin);
193206
Util::SBG_LOG << "Total matching exec time: " << total.count() << " [μs]\n";
194207

195-
MatchData result(sbg, M_.compact(), exit_cond.full_match());
208+
MatchData result(bsbg, M_.compact(), exit_cond.full_match());
196209
Util::SBG_LOG << result << "\n\n";
197210

198211
return result;

algorithms/matching/bfs_matching.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "algorithms/matching/bfs_paths.hpp"
2828
#include "algorithms/matching/matching.hpp"
29+
#include "sbg/directed_sbg.hpp"
2930

3031
namespace SBG {
3132

@@ -50,7 +51,7 @@ class BFSMatching : public MatchStrategy {
5051
public:
5152
BFSMatching();
5253

53-
MatchData calculate(const SBG& sbg) override;
54+
MatchData calculate(const BipartiteSBG& bsbg) override;
5455

5556
private:
5657
/**
@@ -72,9 +73,9 @@ class BFSMatching : public MatchStrategy {
7273
};
7374

7475
/**
75-
* @brief Initializes data members determined by the input SBG.
76+
* @brief Initializes data members determined by the input bipartite SBG.
7677
*/
77-
void init(const SBG& sbg);
78+
void init(const BipartiteSBG& bsbg);
7879

7980
/**
8081
* @brief Performs an iteration of the algorithm. It looks up alternating
@@ -117,7 +118,7 @@ class BFSMatching : public MatchStrategy {
117118
*/
118119
Set edgesInPaths(const PWMap& smap, const Set& E) const;
119120

120-
DSBG dsbg_; ///< Input DSBG
121+
DSBG dsbg_; ///< Directed graph according to matching
121122
Set M_; ///< Matched edges
122123
Direction direction_;
123124
};

algorithms/matching/matching.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ std::ostream& operator<<(std::ostream& out, const Direction& direction)
4444
return out;
4545
}
4646

47-
MatchData::MatchData(SBG sbg, Set M, bool full_match)
48-
: sbg_(sbg), M_(M), full_match_(full_match) {}
47+
MatchData::MatchData(BipartiteSBG bsbg, Set M, bool full_match)
48+
: bsbg_(bsbg), M_(M), full_match_(full_match) {}
4949

50-
const SBG& MatchData::sbg() const { return sbg_; }
50+
const BipartiteSBG& MatchData::bsbg() const { return bsbg_; }
5151
const Set& MatchData::M() const { return M_; }
5252
const bool& MatchData::full_match() const { return full_match_; }
5353

@@ -74,9 +74,9 @@ MatchStrategy::MatchStrategy() {}
7474

7575
Matching::Matching(MatchStratPtr strat) : strategy_(std::move(strat)) {}
7676

77-
MatchData Matching::calculate(const SBG& sbg)
77+
MatchData Matching::calculate(const BipartiteSBG& bsbg)
7878
{
79-
return strategy_->calculate(sbg);
79+
return strategy_->calculate(bsbg);
8080
}
8181

8282
} // namespace LIB

algorithms/matching/matching.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#ifndef SBG_MATCH_HPP
2525
#define SBG_MATCH_HPP
2626

27-
#include "sbg/sbg.hpp"
27+
#include "sbg/bipartite_sbg.hpp"
2828

2929
namespace SBG {
3030

@@ -42,15 +42,15 @@ std::ostream& operator<<(std::ostream& out, const Direction& direction);
4242
*/
4343
struct MatchData {
4444
public:
45-
MatchData(SBG sbg, Set M, bool full_match);
45+
MatchData(BipartiteSBG bsbg, Set M, bool full_match);
4646

47-
const SBG& sbg() const;
47+
const BipartiteSBG& bsbg() const;
4848
const Set& M() const;
4949
const bool& full_match() const;
5050

5151
private:
52-
SBG sbg_; ///< Original input for the algorithm
53-
Set M_; ///< Matched edges
52+
BipartiteSBG bsbg_; ///< Original input for the algorithm
53+
Set M_; ///< Matched edges
5454
bool full_match_; ///< Returns true if all right vertices are saturated
5555
};
5656
std::ostream& operator<<(std::ostream& out, const MatchData& data);
@@ -69,7 +69,7 @@ class MatchStrategy {
6969

7070
MatchStrategy();
7171

72-
virtual MatchData calculate(const SBG& sbg) = 0;
72+
virtual MatchData calculate(const BipartiteSBG& bsbg) = 0;
7373
};
7474

7575

@@ -81,7 +81,7 @@ class Matching {
8181
public:
8282
Matching(MatchStratPtr strat);
8383

84-
MatchData calculate(const SBG& sbg);
84+
MatchData calculate(const BipartiteSBG& bsbg);
8585

8686
private:
8787
MatchStratPtr strategy_;

algorithms/matching/paths.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#ifndef SBG_PATH_HPP
2727
#define SBG_PATH_HPP
2828

29-
#include "sbg/sbg.hpp"
29+
#include "sbg/directed_sbg.hpp"
3030

3131
namespace SBG {
3232

algorithms/misc/causalization_builders.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,34 @@
2525

2626
namespace MISC {
2727

28-
SBG::LIB::DSBG buildSCCFromMatching(const SBG::LIB::MatchData &data)
28+
SBG::LIB::DSBG buildSCCFromMatching(const SBG::LIB::MatchData& data)
2929
{
3030
auto start = std::chrono::high_resolution_clock::now();
3131

32-
const SBG::LIB::SBG &sbg = data.sbg();
32+
const SBG::LIB::BipartiteSBG& bsbg = data.bsbg();
3333
SBG::LIB::Set M = data.M();
34-
SBG::LIB::Set free_edges = sbg.E().difference(M);
34+
SBG::LIB::Set free_edges = bsbg.E().difference(M);
3535

3636
SBG::LIB::Set V = M.compact();
37-
SBG::LIB::PWMap auxVmap = data.sbg().subEmap().restrict(M);
37+
SBG::LIB::PWMap auxVmap = data.bsbg().subEmap().restrict(M);
3838
SBG::LIB::PWMap Vmap = SBG::LIB::PW_FACT.createPWMap();
39-
for (const SBG::LIB::Map &map : auxVmap)
39+
for (const SBG::LIB::Map& map : auxVmap) {
4040
Vmap.emplaceBack(SBG::LIB::Map(map.dom().compact()
4141
, map.exp()));
42+
}
4243

43-
SBG::LIB::PWMap mapF = sbg.map1();
44-
SBG::LIB::PWMap mapU = sbg.map2();
44+
SBG::LIB::PWMap map1 = bsbg.map1();
45+
SBG::LIB::PWMap map2 = bsbg.map2();
46+
47+
SBG::LIB::Set X = bsbg.X();
48+
SBG::LIB::PWMap map1_toX = map1.restrict(map1.preImage(X));
49+
SBG::LIB::PWMap map2_toX = map2.restrict(map2.preImage(X));
50+
SBG::LIB::PWMap mapF = map1_toX.concatenation(map2_toX);
51+
52+
SBG::LIB::Set Y = bsbg.Y();
53+
SBG::LIB::PWMap map1_toY = map1.restrict(map1.preImage(Y));
54+
SBG::LIB::PWMap map2_toY = map2.restrict(map2.preImage(Y));
55+
SBG::LIB::PWMap mapU = map1_toY.concatenation(map2_toY);
4556

4657
SBG::LIB::PWMap matchedF_inv = mapF.restrict(M).inverse();
4758
SBG::LIB::PWMap unmatchedF = mapF.restrict(free_edges);
@@ -52,8 +63,8 @@ SBG::LIB::DSBG buildSCCFromMatching(const SBG::LIB::MatchData &data)
5263
SBG::LIB::PWMap mapD = matchedU_inv.composition(unmatchedU);
5364
mapD = mapD.compact();
5465

55-
SBG::LIB::PWMap Emap = sbg.Emap().restrict(free_edges);
56-
SBG::LIB::PWMap subEmap = sbg.subEmap().restrict(free_edges);
66+
SBG::LIB::PWMap Emap = bsbg.Emap().restrict(free_edges);
67+
SBG::LIB::PWMap subEmap = bsbg.subEmap().restrict(free_edges);
5768

5869
SBG::LIB::DSBG res(V, Vmap, mapB, mapD, Emap, subEmap);
5970
auto end = std::chrono::high_resolution_clock::now();
@@ -65,11 +76,11 @@ SBG::LIB::DSBG buildSCCFromMatching(const SBG::LIB::MatchData &data)
6576
return res;
6677
}
6778

68-
SBG::LIB::DSBG buildSortFromSCC(const SBG::LIB::SCCData &data)
79+
SBG::LIB::DSBG buildSortFromSCC(const SBG::LIB::SCCData& data)
6980
{
7081
auto start = std::chrono::high_resolution_clock::now();
7182

72-
const SBG::LIB::DSBG &dsbg = data.dsbg();
83+
const SBG::LIB::DSBG& dsbg = data.dsbg();
7384
SBG::LIB::PWMap rmap = data.rmap();
7485
SBG::LIB::Set Ediff = data.Ediff();
7586

algorithms/misc/causalization_builders.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333

3434
namespace MISC {
3535

36-
SBG::LIB::DSBG buildSCCFromMatching(const SBG::LIB::MatchData &data);
36+
SBG::LIB::DSBG buildSCCFromMatching(const SBG::LIB::MatchData& data);
3737

38-
SBG::LIB::DSBG buildSortFromSCC(const SBG::LIB::SCCData &data);
38+
SBG::LIB::DSBG buildSortFromSCC(const SBG::LIB::SCCData& data);
3939

4040
} // namespace MISC
4141

algorithms/scc/mrv.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#ifndef SBG_MRV_HPP
2727
#define SBG_MRV_HPP
2828

29-
#include "sbg/sbg.hpp"
29+
#include "sbg/directed_sbg.hpp"
3030

3131
namespace SBG {
3232

algorithms/scc/scc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#ifndef SBG_SCC_HPP
2525
#define SBG_SCC_HPP
2626

27-
#include "sbg/sbg.hpp"
27+
#include "sbg/directed_sbg.hpp"
2828

2929
namespace SBG {
3030

algorithms/toposort/topo_sort.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#ifndef SBG_TOPOSORT_HPP
2525
#define SBG_TOPOSORT_HPP
2626

27-
#include "sbg/sbg.hpp"
27+
#include "sbg/directed_sbg.hpp"
2828

2929
namespace SBG {
3030

0 commit comments

Comments
 (0)