os/ossrv/stdcpp/tsrc/Boost_test/graph/src/matching_example.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 //=======================================================================
     2 // Copyright (c) 2005 Aaron Windsor
     3 //
     4 // Distributed under the Boost Software License, Version 1.0. 
     5 // (See accompanying file LICENSE_1_0.txt or copy at
     6 // http://www.boost.org/LICENSE_1_0.txt)
     7 //
     8 //=======================================================================
     9 
    10 /*
    11  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
    12 */
    13 
    14 
    15 #include <boost/graph/adjacency_list.hpp>
    16 #include <boost/graph/max_cardinality_matching.hpp>
    17 
    18 #include <cassert>
    19 #include <string>
    20 #include <iostream>
    21 
    22 #ifdef __SYMBIAN32__
    23 #include "std_log_result.h"
    24 #define LOG_FILENAME_LINE __FILE__, __LINE__
    25 #endif
    26 
    27 #include <boost/test/minimal.hpp>
    28 
    29 using namespace boost;
    30 
    31 typedef adjacency_list<vecS, vecS, undirectedS> my_graph; 
    32 
    33 int test_main(int argc, char* argv[])
    34 {
    35 
    36 
    37   // Create the following graph: (it'll look better when output
    38   // to the terminal in a fixed width font...)
    39 
    40   const int n_vertices = 18;
    41 
    42   std::vector<std::string> ascii_graph;
    43 
    44   ascii_graph.push_back("           0       1---2       3       ");
    45   ascii_graph.push_back("            \\     /     \\     /        ");
    46   ascii_graph.push_back("             4---5       6---7         ");
    47   ascii_graph.push_back("             |   |       |   |         ");
    48   ascii_graph.push_back("             8---9      10---11        ");
    49   ascii_graph.push_back("            /     \\     /     \\        ");
    50   ascii_graph.push_back("     12   13      14---15      16   17 ");
    51 
    52   // It has a perfect matching of size 8. There are two isolated
    53   // vertices that we'll use later...
    54 
    55   my_graph g(n_vertices);
    56   
    57   // our vertices are stored in a vector, so we can refer to vertices
    58   // by integers in the range 0..15
    59 
    60   add_edge(0,4,g);
    61   add_edge(1,5,g);
    62   add_edge(2,6,g);
    63   add_edge(3,7,g);
    64   add_edge(4,5,g);
    65   add_edge(6,7,g);
    66   add_edge(4,8,g);
    67   add_edge(5,9,g);
    68   add_edge(6,10,g);
    69   add_edge(7,11,g);
    70   add_edge(8,9,g);
    71   add_edge(10,11,g);
    72   add_edge(8,13,g);
    73   add_edge(9,14,g);
    74   add_edge(10,15,g);
    75   add_edge(11,16,g);
    76   add_edge(14,15,g);
    77 
    78   std::vector<graph_traits<my_graph>::vertex_descriptor> mate(n_vertices);
    79 
    80   // find the maximum cardinality matching. we'll use a checked version
    81   // of the algorithm, which takes a little longer than the unchecked
    82   // version, but has the advantage that it will return "false" if the
    83   // matching returned is not actually a maximum cardinality matching
    84   // in the graph.
    85    
    86    
    87   #ifndef __SYMBIAN32__
    88   bool success = checked_edmonds_maximum_cardinality_matching(g, &mate[0]);
    89   assert(success);
    90   #endif
    91   
    92   std::cout << "In the following graph:" << std::endl << std::endl;
    93 
    94   for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
    95     std::cout << *itr << std::endl;
    96 
    97   std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;
    98 
    99   std::cout << "The matching is:" << std::endl;
   100   
   101   graph_traits<my_graph>::vertex_iterator vi, vi_end;
   102   for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
   103     if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
   104       std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;
   105 
   106   std::cout << std::endl;
   107 
   108   //now we'll add two edges, and the perfect matching has size 9
   109 
   110   ascii_graph.pop_back();
   111   ascii_graph.push_back("     12---13      14---15      16---17 ");
   112 
   113   add_edge(12,13,g);
   114   add_edge(16,17,g);
   115 
   116   #ifndef __SYMBIAN32__
   117   success = checked_edmonds_maximum_cardinality_matching(g, &mate[0]);
   118   assert(success);
   119   #endif
   120 
   121   std::cout << "In the following graph:" << std::endl << std::endl;
   122 
   123   for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
   124     std::cout << *itr << std::endl;
   125 
   126   std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;
   127 
   128   std::cout << "The matching is:" << std::endl;
   129   
   130   for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
   131     if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
   132       std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;
   133     
   134     
   135   #ifdef __SYMBIAN32__
   136   std_log(LOG_FILENAME_LINE,"[End Test Case ]");
   137 	testResultXml("matching_example");
   138 	close_log_file();
   139   #endif  
   140 
   141   return 0;
   142 }
   143 
   144 
   145 
   146