@@ -48,7 +48,8 @@ bool BFSMatching::ExitCondition::isSatisfied()
4848// Algorithm -------------------------------------------------------------------
4949
5050BFSMatching::BFSMatching () : _M(SET_FACT.createSet()), _dsbg()
51- , _direction(Direction::kForward ) {}
51+ , _direction(Direction::kForward ), _X(SET_FACT.createSet())
52+ , _Y(SET_FACT.createSet()) {}
5253
5354void BFSMatching::swapEdgesDirection (const Set& E)
5455{
@@ -62,6 +63,14 @@ void BFSMatching::swapEdgesDirection(const Set& E)
6263 _dsbg = DirectedSBG{_dsbg.V (), _dsbg.Vmap (), mapB, mapD, _dsbg.Emap ()};
6364}
6465
66+ void BFSMatching::swapDirection (const Set& E)
67+ {
68+ swapEdgesDirection (E);
69+ Set temp_X = _X;
70+ _X = _Y;
71+ _Y = temp_X;
72+ }
73+
6574PWMap BFSMatching::partitionSubsetEdges () const
6675{
6776 PWMap result = PWMAP_FACT.createPWMap ();
@@ -90,84 +99,51 @@ PWMap BFSMatching::partitionSubsetEdges() const
9099 return result;
91100}
92101
93- Set BFSMatching::edgesInPaths (const PWMap& smap, const Set& E) const
94- {
95- PWMap mapB = _dsbg.mapB ().restrict (E);
96- PWMap mapD = _dsbg.mapD ().restrict (E);
97-
98- // Vertices that are successors of other vertices in a path
99- Set not_fixed = smap.domain ().difference (smap.fixedPoints ());
100- Set succs = smap.restrict (not_fixed).image ();
101- // Edges whose endings are successors
102- Set ending_edges = mapD.preImage (succs);
103- // Map from a 'successor' edge to its start
104- PWMap auxB = mapB.restrict (ending_edges);
105- // Map from edge to the successor of its start
106- PWMap map_succs = smap.composition (auxB);
107-
108- return map_succs.equalImage (mapD);
109- }
110-
111- Set BFSMatching::directedStep (const Set& E, const Set& right_vertices)
102+ Set BFSMatching::directedStep (const Set& E)
112103{
113104 PWMap mapB = _dsbg.mapB ().restrict (E);
114105 PWMap mapD = _dsbg.mapD ().restrict (E);
115106 PWMap Emap = partitionSubsetEdges ().restrict (E);
116107
117108 // Calculate unmatched vertices in the side determined by the current
118109 // direction of edges
119- Set forward_vertices = _dsbg.V ().difference (right_vertices);
120110 Set matched_forward_vertices = mapB.image (_M);
121- if (_direction == Direction::kBackward ) {
122- forward_vertices = right_vertices;
123- }
124111 Set unmatched_forward_vertices
125- = forward_vertices .difference (matched_forward_vertices);
112+ = _X .difference (matched_forward_vertices);
126113
127114 // Detect paths leading to unmatched_forward_vertices
128115 DirectedSBG restricted_dsbg{_dsbg.V (), _dsbg.Vmap (), mapB, mapD, Emap};
129116 BFSPaths paths;
130- PWMap smap = paths.calculate (restricted_dsbg, unmatched_forward_vertices);
131117
132- // Keep edges
133- Set paths_edges = edgesInPaths (smap, E);
134- PWMap rmap = smap.mapInf ();
135- Set reach_unmatched = rmap.preImage (unmatched_forward_vertices);
136- paths_edges = paths_edges.intersection (mapD.preImage (reach_unmatched));
137-
138- Util::DEBUG_LOG << " paths_edges in " << _direction << " direction: "
139- << paths_edges << " \n " ;
140-
141- return paths_edges;
118+ return paths.calculate (restricted_dsbg, unmatched_forward_vertices);
142119}
143120
144- BFSMatching::ExitCondition BFSMatching::step (const Set& right_vertices )
121+ BFSMatching::ExitCondition BFSMatching::step ()
145122{
146123 Set E = _dsbg.E ();
147124
148125 // Forward direction
149- Set paths_edgesD = directedStep (E, right_vertices );
126+ Set P = directedStep (E);
150127
151128 // Backward direction
152- swapEdgesDirection (E);
129+ swapDirection (E);
153130 _direction = Direction::kBackward ;
154- Set paths_edgesB = directedStep (paths_edgesD, right_vertices );
131+ Set augmenting_edges = directedStep (P );
155132
156- // Calculate augmenting paths and swap edges in these paths
157- Set augmenting_edges = paths_edgesB.intersection (paths_edgesD);
133+ // Swap direction for edges in augmenting paths
158134 Util::DEBUG_LOG << " augmenting paths: " << augmenting_edges << " \n " ;
159- swapEdgesDirection (augmenting_edges);
135+ swapDirection (augmenting_edges);
160136
161137 // Swap directions in all graph
162138 swapEdgesDirection (E);
163139 _direction = Direction::kForward ;
164140
165141 // Calculate new matched edges
166- _M = _dsbg.mapD ().preImage (right_vertices );
142+ _M = _dsbg.mapD ().preImage (_Y );
167143
168144 // Calculate exit conditions
169145 Set matchedU = _dsbg.mapD ().image (_M);
170- bool full_match = right_vertices .difference (matchedU).isEmpty ();
146+ bool full_match = _Y .difference (matchedU).isEmpty ();
171147 bool found_paths = !augmenting_edges.isEmpty ();
172148
173149 return ExitCondition{full_match, found_paths};
@@ -204,11 +180,12 @@ MatchData BFSMatching::calculate(const BipartiteSBG& bsbg)
204180 Util::Internal::TimeProfiler profiler{" Total matching exec time: " };
205181
206182 init (bsbg);
207- Set right_vertices = bsbg.Y ();
183+ _X = bsbg.X ();
184+ _Y = bsbg.Y ();
208185
209186 ExitCondition exit_cond{false , false };
210187 do {
211- exit_cond = step (right_vertices );
188+ exit_cond = step ();
212189 } while (!exit_cond.isSatisfied ());
213190
214191 _M.compact ();
0 commit comments