# HG changeset patch
# User Alpar Juttner <alpar@cs.elte.hu>
# Date 1467812647 -7200
#      Wed Jul 06 15:44:07 2016 +0200
# Node ID da78005589e9653d8b57b000bcae9f0436471ed0
# Parent  f51c01a1b88eead63515a8b22f6f380595245a42
MaxMatching speed improvement for odd graphs (#604)
diff --git a/lemon/matching.h b/lemon/matching.h
        
              
              
                
                  | 
                      a
                   | 
                  
                      b
                   | 
                    | 
                
              
            
                  
                          | 2 | 2 |  * | 
                  
                          | 3 | 3 |  * This file is a part of LEMON, a generic C++ optimization library. | 
                  
                          | 4 | 4 |  * | 
                  
            
                      
                        | 5 |   |  * Copyright (C) 2003-2013 | 
                      
                      
                        |   | 5 |  * Copyright (C) 2003-2016 | 
                      
            
                  
                          | 6 | 6 |  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport | 
                  
                          | 7 | 7 |  * (Egervary Research Group on Combinatorial Optimization, EGRES). | 
                  
                          | 8 | 8 |  * | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 102 | 102 |     const Graph& _graph; | 
                  
                          | 103 | 103 |     MatchingMap* _matching; | 
                  
                          | 104 | 104 |     StatusMap* _status; | 
                  
            
                  
                          |   | 105 |     int _matching_size; | 
                  
            
                  
                          | 105 | 106 |  | 
                  
                          | 106 | 107 |     EarMap* _ear; | 
                  
                          | 107 | 108 |  | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 401 | 402 |         } | 
                  
                          | 402 | 403 |       } | 
                  
                          | 403 | 404 |       _tree_set->eraseClass(tree); | 
                  
            
                      
                        | 404 |   |  | 
                      
                      
                        |   | 405 |       _matching_size++; | 
                      
            
                  
                          | 405 | 406 |     } | 
                  
                          | 406 | 407 |  | 
                  
                          | 407 | 408 |   public: | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 437 | 438 |         (*_matching)[n] = INVALID; | 
                  
                          | 438 | 439 |         (*_status)[n] = UNMATCHED; | 
                  
                          | 439 | 440 |       } | 
                  
            
                  
                          |   | 441 |       _matching_size=0; | 
                  
            
                  
                          | 440 | 442 |     } | 
                  
                          | 441 | 443 |  | 
                  
                          | 442 | 444 |     /// \brief Find an initial matching in a greedy way. | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 444 | 446 |     /// This function finds an initial matching in a greedy way. | 
                  
                          | 445 | 447 |     void greedyInit() { | 
                  
                          | 446 | 448 |       createStructures(); | 
                  
            
                  
                          |   | 449 |       _matching_size=0; | 
                  
            
                  
                          | 447 | 450 |       for (NodeIt n(_graph); n != INVALID; ++n) { | 
                  
                          | 448 | 451 |         (*_matching)[n] = INVALID; | 
                  
                          | 449 | 452 |         (*_status)[n] = UNMATCHED; | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 457 | 460 |               (*_status)[n] = MATCHED; | 
                  
                          | 458 | 461 |               (*_matching)[v] = _graph.oppositeArc(a); | 
                  
                          | 459 | 462 |               (*_status)[v] = MATCHED; | 
                  
            
                  
                          |   | 463 |               _matching_size++; | 
                  
            
                  
                          | 460 | 464 |               break; | 
                  
                          | 461 | 465 |             } | 
                  
                          | 462 | 466 |           } | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 474 | 478 |     template <typename MatchingMap> | 
                  
                          | 475 | 479 |     bool matchingInit(const MatchingMap& matching) { | 
                  
                          | 476 | 480 |       createStructures(); | 
                  
            
                  
                          |   | 481 |       _matching_size=0; | 
                  
            
                  
                          | 477 | 482 |  | 
                  
                          | 478 | 483 |       for (NodeIt n(_graph); n != INVALID; ++n) { | 
                  
                          | 479 | 484 |         (*_matching)[n] = INVALID; | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 491 | 496 |           if ((*_matching)[v] != INVALID) return false; | 
                  
                          | 492 | 497 |           (*_matching)[v] = _graph.direct(e, false); | 
                  
                          | 493 | 498 |           (*_status)[v] = MATCHED; | 
                  
            
                  
                          |   | 499 |  | 
                  
                          |   | 500 |           _matching_size++; | 
                  
            
                  
                          | 494 | 501 |         } | 
                  
                          | 495 | 502 |       } | 
                  
                          | 496 | 503 |       return true; | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 503 | 510 |     /// \pre \ref init(), \ref greedyInit() or \ref matchingInit() must be | 
                  
                          | 504 | 511 |     /// called before using this function. | 
                  
                          | 505 | 512 |     void startSparse() { | 
                  
            
                      
                        | 506 |   |       for(NodeIt n(_graph); n != INVALID; ++n) { | 
                      
                      
                        |   | 513 |       for(NodeIt n(_graph); _matching_size*2<_node_num-1 && n!=INVALID; ++n) { | 
                      
            
                  
                          | 507 | 514 |         if ((*_status)[n] == UNMATCHED) { | 
                  
                          | 508 | 515 |           (*_blossom_rep)[_blossom_set->insert(n)] = n; | 
                  
                          | 509 | 516 |           _tree_set->insert(n); | 
                  
            
              
                
                  | … | 
                  … | 
                    | 
                
              
            
                  
                          | 522 | 529 |     /// \pre \ref init(), \ref greedyInit() or \ref matchingInit() must be | 
                  
                          | 523 | 530 |     /// called before using this function. | 
                  
                          | 524 | 531 |     void startDense() { | 
                  
            
                      
                        | 525 |   |       for(NodeIt n(_graph); n != INVALID; ++n) { | 
                      
                      
                        |   | 532 |       for(NodeIt n(_graph);  _matching_size*2<_node_num-1 && n!=INVALID; ++n) { | 
                      
            
                  
                          | 526 | 533 |         if ((*_status)[n] == UNMATCHED) { | 
                  
                          | 527 | 534 |           (*_blossom_rep)[_blossom_set->insert(n)] = n; | 
                  
                          | 528 | 535 |           _tree_set->insert(n); |