@@ -132,7 +132,7 @@ void Graph::printGraph() {
132132#endif /* DEBUG_GRAPH */
133133
134134/* *
135- * Continues an augmentation, on vertex now, which is on the right.
135+ * Continues an augmentation, on vertex now
136136 */
137137bool Graph::internal_augment (int_id now, std::vector<char > & visited,
138138 std::list<int_id> & path) {
@@ -155,41 +155,59 @@ bool Graph::internal_augment(int_id now, std::vector<char> & visited,
155155 if ((bool )visited[next_edge.id ]) {
156156 continue ;
157157 }
158- // Can't add any more flow to this edge.
159- if (next_edge.flow >= next_edge.cap ) {
160- continue ;
161- }
162- // If we're going "backwards" we need to use up (subtract) flow, so check we
163- // have some. Note that since we are checking backwards, the flow will also
164- // be negative
165- if (next_edge.reverse ) {
166- if (next_edge.flow >= 0 ) {
158+ if (_on_right[now]) {
159+ // We're on right, going left, need capacity available.
160+ // Note that next_edge will be in reverse, so it will have negative flow
161+ if ((-next_edge.flow ) == next_edge.cap ) {
167162 continue ;
168163 }
169- }
170- if (_cap_remaining[next_edge.id ] > 0 ) {
171- // Found an augmenting flow. Modify flows used.
172- _cap_remaining[next_edge.id ] -= 1 ;
173- path.push_back (next_edge.id );
164+ // next_edge.id will be on left. If it has capacity, remaining, done!
165+ if (_cap_remaining[next_edge.id ] > 0 ) {
166+ // Found an augmenting flow. Modify flows used.
167+ _cap_remaining[next_edge.id ] -= 1 ;
168+ path.push_back (next_edge.id );
174169
175170#ifdef DEBUG_GRAPH
176- std::cout << " Found augmenting path:" ;
177- for (auto & p: path) {
178- std::cout << " " << _indices[p];
179- }
180- std::cout << std::endl;
171+ std::cout << " Found augmenting path:" ;
172+ for (auto & p: path) {
173+ std::cout << " " << _indices[p];
174+ }
175+ std::cout << std::endl;
181176#endif /* DEBUG_GRAPH */
182- int start = path.front ();
183- path.pop_front ();
184- while (!path.empty ()) {
177+ int start = path.front ();
178+ path.pop_front ();
185179 int next = path.front ();
186180 path.pop_front ();
187- _adjmat[start][next]->flow += 1 ;
188- _adjmat[next][start]->flow -= 1 ;
181+ // Start is on the right, and next is on the left
182+ // Thus we need to increase the flow from next to start
183+ _adjmat[start][next]->flow -= 1 ;
184+ _adjmat[next][start]->flow += 1 ;
189185 start = next;
186+ while (!path.empty ()) {
187+ next = path.front ();
188+ path.pop_front ();
189+ // Now start is on the left, and next is on the right
190+ // So increase from start to next
191+ // And reduce from next to start
192+ _adjmat[start][next]->flow += 1 ;
193+ _adjmat[next][start]->flow -= 1 ;
194+ start = next;
195+ next = path.front ();
196+ path.pop_front ();
197+ // At this point, start is on the right, and next is on the left
198+ // Thus we need to increase the flow from next to start
199+ _adjmat[start][next]->flow -= 1 ;
200+ _adjmat[next][start]->flow += 1 ;
201+ start = next;
202+ }
203+ max_flow += 1 ;
204+ return true ;
205+ }
206+ } else {
207+ // We're on the left, so we want this edge to be used
208+ if (next_edge.flow <= 0 ) {
209+ continue ;
190210 }
191- max_flow += 1 ;
192- return true ;
193211 }
194212 path.push_back (next_edge.id );
195213 visited[next_edge.id ] = (char )true ;
0 commit comments