280 Node node = _graph.u(e); |
280 Node node = _graph.u(e); |
281 Arc arc = _graph.direct(e, true); |
281 Arc arc = _graph.direct(e, true); |
282 Node base = (*_blossom_rep)[_blossom_set->find(node)]; |
282 Node base = (*_blossom_rep)[_blossom_set->find(node)]; |
283 |
283 |
284 while (base != nca) { |
284 while (base != nca) { |
285 _ear->set(node, arc); |
285 (*_ear)[node] = arc; |
286 |
286 |
287 Node n = node; |
287 Node n = node; |
288 while (n != base) { |
288 while (n != base) { |
289 n = _graph.target((*_matching)[n]); |
289 n = _graph.target((*_matching)[n]); |
290 Arc a = (*_ear)[n]; |
290 Arc a = (*_ear)[n]; |
291 n = _graph.target(a); |
291 n = _graph.target(a); |
292 _ear->set(n, _graph.oppositeArc(a)); |
292 (*_ear)[n] = _graph.oppositeArc(a); |
293 } |
293 } |
294 node = _graph.target((*_matching)[base]); |
294 node = _graph.target((*_matching)[base]); |
295 _tree_set->erase(base); |
295 _tree_set->erase(base); |
296 _tree_set->erase(node); |
296 _tree_set->erase(node); |
297 _blossom_set->insert(node, _blossom_set->find(base)); |
297 _blossom_set->insert(node, _blossom_set->find(base)); |
298 _status->set(node, EVEN); |
298 (*_status)[node] = EVEN; |
299 _node_queue[_last++] = node; |
299 _node_queue[_last++] = node; |
300 arc = _graph.oppositeArc((*_ear)[node]); |
300 arc = _graph.oppositeArc((*_ear)[node]); |
301 node = _graph.target((*_ear)[node]); |
301 node = _graph.target((*_ear)[node]); |
302 base = (*_blossom_rep)[_blossom_set->find(node)]; |
302 base = (*_blossom_rep)[_blossom_set->find(node)]; |
303 _blossom_set->join(_graph.target(arc), base); |
303 _blossom_set->join(_graph.target(arc), base); |
304 } |
304 } |
305 } |
305 } |
306 |
306 |
307 _blossom_rep->set(_blossom_set->find(nca), nca); |
307 (*_blossom_rep)[_blossom_set->find(nca)] = nca; |
308 |
308 |
309 { |
309 { |
310 |
310 |
311 Node node = _graph.v(e); |
311 Node node = _graph.v(e); |
312 Arc arc = _graph.direct(e, false); |
312 Arc arc = _graph.direct(e, false); |
313 Node base = (*_blossom_rep)[_blossom_set->find(node)]; |
313 Node base = (*_blossom_rep)[_blossom_set->find(node)]; |
314 |
314 |
315 while (base != nca) { |
315 while (base != nca) { |
316 _ear->set(node, arc); |
316 (*_ear)[node] = arc; |
317 |
317 |
318 Node n = node; |
318 Node n = node; |
319 while (n != base) { |
319 while (n != base) { |
320 n = _graph.target((*_matching)[n]); |
320 n = _graph.target((*_matching)[n]); |
321 Arc a = (*_ear)[n]; |
321 Arc a = (*_ear)[n]; |
322 n = _graph.target(a); |
322 n = _graph.target(a); |
323 _ear->set(n, _graph.oppositeArc(a)); |
323 (*_ear)[n] = _graph.oppositeArc(a); |
324 } |
324 } |
325 node = _graph.target((*_matching)[base]); |
325 node = _graph.target((*_matching)[base]); |
326 _tree_set->erase(base); |
326 _tree_set->erase(base); |
327 _tree_set->erase(node); |
327 _tree_set->erase(node); |
328 _blossom_set->insert(node, _blossom_set->find(base)); |
328 _blossom_set->insert(node, _blossom_set->find(base)); |
329 _status->set(node, EVEN); |
329 (*_status)[node] = EVEN; |
330 _node_queue[_last++] = node; |
330 _node_queue[_last++] = node; |
331 arc = _graph.oppositeArc((*_ear)[node]); |
331 arc = _graph.oppositeArc((*_ear)[node]); |
332 node = _graph.target((*_ear)[node]); |
332 node = _graph.target((*_ear)[node]); |
333 base = (*_blossom_rep)[_blossom_set->find(node)]; |
333 base = (*_blossom_rep)[_blossom_set->find(node)]; |
334 _blossom_set->join(_graph.target(arc), base); |
334 _blossom_set->join(_graph.target(arc), base); |
335 } |
335 } |
336 } |
336 } |
337 |
337 |
338 _blossom_rep->set(_blossom_set->find(nca), nca); |
338 (*_blossom_rep)[_blossom_set->find(nca)] = nca; |
339 } |
339 } |
340 |
340 |
341 |
341 |
342 |
342 |
343 void extendOnArc(const Arc& a) { |
343 void extendOnArc(const Arc& a) { |
344 Node base = _graph.source(a); |
344 Node base = _graph.source(a); |
345 Node odd = _graph.target(a); |
345 Node odd = _graph.target(a); |
346 |
346 |
347 _ear->set(odd, _graph.oppositeArc(a)); |
347 (*_ear)[odd] = _graph.oppositeArc(a); |
348 Node even = _graph.target((*_matching)[odd]); |
348 Node even = _graph.target((*_matching)[odd]); |
349 _blossom_rep->set(_blossom_set->insert(even), even); |
349 (*_blossom_rep)[_blossom_set->insert(even)] = even; |
350 _status->set(odd, ODD); |
350 (*_status)[odd] = ODD; |
351 _status->set(even, EVEN); |
351 (*_status)[even] = EVEN; |
352 int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(base)]); |
352 int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(base)]); |
353 _tree_set->insert(odd, tree); |
353 _tree_set->insert(odd, tree); |
354 _tree_set->insert(even, tree); |
354 _tree_set->insert(even, tree); |
355 _node_queue[_last++] = even; |
355 _node_queue[_last++] = even; |
356 |
356 |
360 Node even = _graph.source(a); |
360 Node even = _graph.source(a); |
361 Node odd = _graph.target(a); |
361 Node odd = _graph.target(a); |
362 |
362 |
363 int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(even)]); |
363 int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(even)]); |
364 |
364 |
365 _matching->set(odd, _graph.oppositeArc(a)); |
365 (*_matching)[odd] = _graph.oppositeArc(a); |
366 _status->set(odd, MATCHED); |
366 (*_status)[odd] = MATCHED; |
367 |
367 |
368 Arc arc = (*_matching)[even]; |
368 Arc arc = (*_matching)[even]; |
369 _matching->set(even, a); |
369 (*_matching)[even] = a; |
370 |
370 |
371 while (arc != INVALID) { |
371 while (arc != INVALID) { |
372 odd = _graph.target(arc); |
372 odd = _graph.target(arc); |
373 arc = (*_ear)[odd]; |
373 arc = (*_ear)[odd]; |
374 even = _graph.target(arc); |
374 even = _graph.target(arc); |
375 _matching->set(odd, arc); |
375 (*_matching)[odd] = arc; |
376 arc = (*_matching)[even]; |
376 arc = (*_matching)[even]; |
377 _matching->set(even, _graph.oppositeArc((*_matching)[odd])); |
377 (*_matching)[even] = _graph.oppositeArc((*_matching)[odd]); |
378 } |
378 } |
379 |
379 |
380 for (typename TreeSet::ItemIt it(*_tree_set, tree); |
380 for (typename TreeSet::ItemIt it(*_tree_set, tree); |
381 it != INVALID; ++it) { |
381 it != INVALID; ++it) { |
382 if ((*_status)[it] == ODD) { |
382 if ((*_status)[it] == ODD) { |
383 _status->set(it, MATCHED); |
383 (*_status)[it] = MATCHED; |
384 } else { |
384 } else { |
385 int blossom = _blossom_set->find(it); |
385 int blossom = _blossom_set->find(it); |
386 for (typename BlossomSet::ItemIt jt(*_blossom_set, blossom); |
386 for (typename BlossomSet::ItemIt jt(*_blossom_set, blossom); |
387 jt != INVALID; ++jt) { |
387 jt != INVALID; ++jt) { |
388 _status->set(jt, MATCHED); |
388 (*_status)[jt] = MATCHED; |
389 } |
389 } |
390 _blossom_set->eraseClass(blossom); |
390 _blossom_set->eraseClass(blossom); |
391 } |
391 } |
392 } |
392 } |
393 _tree_set->eraseClass(tree); |
393 _tree_set->eraseClass(tree); |
425 /// Sets the actual matching to the empty matching. |
425 /// Sets the actual matching to the empty matching. |
426 /// |
426 /// |
427 void init() { |
427 void init() { |
428 createStructures(); |
428 createStructures(); |
429 for(NodeIt n(_graph); n != INVALID; ++n) { |
429 for(NodeIt n(_graph); n != INVALID; ++n) { |
430 _matching->set(n, INVALID); |
430 (*_matching)[n] = INVALID; |
431 _status->set(n, UNMATCHED); |
431 (*_status)[n] = UNMATCHED; |
432 } |
432 } |
433 } |
433 } |
434 |
434 |
435 ///\brief Finds an initial matching in a greedy way |
435 ///\brief Finds an initial matching in a greedy way |
436 /// |
436 /// |
437 ///It finds an initial matching in a greedy way. |
437 ///It finds an initial matching in a greedy way. |
438 void greedyInit() { |
438 void greedyInit() { |
439 createStructures(); |
439 createStructures(); |
440 for (NodeIt n(_graph); n != INVALID; ++n) { |
440 for (NodeIt n(_graph); n != INVALID; ++n) { |
441 _matching->set(n, INVALID); |
441 (*_matching)[n] = INVALID; |
442 _status->set(n, UNMATCHED); |
442 (*_status)[n] = UNMATCHED; |
443 } |
443 } |
444 for (NodeIt n(_graph); n != INVALID; ++n) { |
444 for (NodeIt n(_graph); n != INVALID; ++n) { |
445 if ((*_matching)[n] == INVALID) { |
445 if ((*_matching)[n] == INVALID) { |
446 for (OutArcIt a(_graph, n); a != INVALID ; ++a) { |
446 for (OutArcIt a(_graph, n); a != INVALID ; ++a) { |
447 Node v = _graph.target(a); |
447 Node v = _graph.target(a); |
448 if ((*_matching)[v] == INVALID && v != n) { |
448 if ((*_matching)[v] == INVALID && v != n) { |
449 _matching->set(n, a); |
449 (*_matching)[n] = a; |
450 _status->set(n, MATCHED); |
450 (*_status)[n] = MATCHED; |
451 _matching->set(v, _graph.oppositeArc(a)); |
451 (*_matching)[v] = _graph.oppositeArc(a); |
452 _status->set(v, MATCHED); |
452 (*_status)[v] = MATCHED; |
453 break; |
453 break; |
454 } |
454 } |
455 } |
455 } |
456 } |
456 } |
457 } |
457 } |
467 template <typename MatchingMap> |
467 template <typename MatchingMap> |
468 bool matchingInit(const MatchingMap& matching) { |
468 bool matchingInit(const MatchingMap& matching) { |
469 createStructures(); |
469 createStructures(); |
470 |
470 |
471 for (NodeIt n(_graph); n != INVALID; ++n) { |
471 for (NodeIt n(_graph); n != INVALID; ++n) { |
472 _matching->set(n, INVALID); |
472 (*_matching)[n] = INVALID; |
473 _status->set(n, UNMATCHED); |
473 (*_status)[n] = UNMATCHED; |
474 } |
474 } |
475 for(EdgeIt e(_graph); e!=INVALID; ++e) { |
475 for(EdgeIt e(_graph); e!=INVALID; ++e) { |
476 if (matching[e]) { |
476 if (matching[e]) { |
477 |
477 |
478 Node u = _graph.u(e); |
478 Node u = _graph.u(e); |
479 if ((*_matching)[u] != INVALID) return false; |
479 if ((*_matching)[u] != INVALID) return false; |
480 _matching->set(u, _graph.direct(e, true)); |
480 (*_matching)[u] = _graph.direct(e, true); |
481 _status->set(u, MATCHED); |
481 (*_status)[u] = MATCHED; |
482 |
482 |
483 Node v = _graph.v(e); |
483 Node v = _graph.v(e); |
484 if ((*_matching)[v] != INVALID) return false; |
484 if ((*_matching)[v] != INVALID) return false; |
485 _matching->set(v, _graph.direct(e, false)); |
485 (*_matching)[v] = _graph.direct(e, false); |
486 _status->set(v, MATCHED); |
486 (*_status)[v] = MATCHED; |
487 } |
487 } |
488 } |
488 } |
489 return true; |
489 return true; |
490 } |
490 } |
491 |
491 |
1642 /// Initialize the algorithm |
1642 /// Initialize the algorithm |
1643 void init() { |
1643 void init() { |
1644 createStructures(); |
1644 createStructures(); |
1645 |
1645 |
1646 for (ArcIt e(_graph); e != INVALID; ++e) { |
1646 for (ArcIt e(_graph); e != INVALID; ++e) { |
1647 _node_heap_index->set(e, BinHeap<Value, IntArcMap>::PRE_HEAP); |
1647 (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
1648 } |
1648 } |
1649 for (NodeIt n(_graph); n != INVALID; ++n) { |
1649 for (NodeIt n(_graph); n != INVALID; ++n) { |
1650 _delta1_index->set(n, _delta1->PRE_HEAP); |
1650 (*_delta1_index)[n] = _delta1->PRE_HEAP; |
1651 } |
1651 } |
1652 for (EdgeIt e(_graph); e != INVALID; ++e) { |
1652 for (EdgeIt e(_graph); e != INVALID; ++e) { |
1653 _delta3_index->set(e, _delta3->PRE_HEAP); |
1653 (*_delta3_index)[e] = _delta3->PRE_HEAP; |
1654 } |
1654 } |
1655 for (int i = 0; i < _blossom_num; ++i) { |
1655 for (int i = 0; i < _blossom_num; ++i) { |
1656 _delta2_index->set(i, _delta2->PRE_HEAP); |
1656 (*_delta2_index)[i] = _delta2->PRE_HEAP; |
1657 _delta4_index->set(i, _delta4->PRE_HEAP); |
1657 (*_delta4_index)[i] = _delta4->PRE_HEAP; |
1658 } |
1658 } |
1659 |
1659 |
1660 int index = 0; |
1660 int index = 0; |
1661 for (NodeIt n(_graph); n != INVALID; ++n) { |
1661 for (NodeIt n(_graph); n != INVALID; ++n) { |
1662 Value max = 0; |
1662 Value max = 0; |
2829 /// Initialize the algorithm |
2829 /// Initialize the algorithm |
2830 void init() { |
2830 void init() { |
2831 createStructures(); |
2831 createStructures(); |
2832 |
2832 |
2833 for (ArcIt e(_graph); e != INVALID; ++e) { |
2833 for (ArcIt e(_graph); e != INVALID; ++e) { |
2834 _node_heap_index->set(e, BinHeap<Value, IntArcMap>::PRE_HEAP); |
2834 (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
2835 } |
2835 } |
2836 for (EdgeIt e(_graph); e != INVALID; ++e) { |
2836 for (EdgeIt e(_graph); e != INVALID; ++e) { |
2837 _delta3_index->set(e, _delta3->PRE_HEAP); |
2837 (*_delta3_index)[e] = _delta3->PRE_HEAP; |
2838 } |
2838 } |
2839 for (int i = 0; i < _blossom_num; ++i) { |
2839 for (int i = 0; i < _blossom_num; ++i) { |
2840 _delta2_index->set(i, _delta2->PRE_HEAP); |
2840 (*_delta2_index)[i] = _delta2->PRE_HEAP; |
2841 _delta4_index->set(i, _delta4->PRE_HEAP); |
2841 (*_delta4_index)[i] = _delta4->PRE_HEAP; |
2842 } |
2842 } |
2843 |
2843 |
2844 int index = 0; |
2844 int index = 0; |
2845 for (NodeIt n(_graph); n != INVALID; ++n) { |
2845 for (NodeIt n(_graph); n != INVALID; ++n) { |
2846 Value max = - std::numeric_limits<Value>::max(); |
2846 Value max = - std::numeric_limits<Value>::max(); |