Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / graph / example / matching_example.cpp
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 #include <string>
10 #include <iostream>
11 #include <boost/graph/adjacency_list.hpp>
12 #include <cassert>
13
14 #include <boost/graph/max_cardinality_matching.hpp>
15
16
17 using namespace boost;
18
19 typedef adjacency_list<vecS, vecS, undirectedS> my_graph; 
20
21 int main()
22 {
23
24   // Create the following graph: (it'll look better when output
25   // to the terminal in a fixed width font...)
26
27   const int n_vertices = 18;
28
29   std::vector<std::string> ascii_graph;
30
31   ascii_graph.push_back("           0       1---2       3       ");
32   ascii_graph.push_back("            \\     /     \\     /        ");
33   ascii_graph.push_back("             4---5       6---7         ");
34   ascii_graph.push_back("             |   |       |   |         ");
35   ascii_graph.push_back("             8---9      10---11        ");
36   ascii_graph.push_back("            /     \\     /     \\        ");
37   ascii_graph.push_back("     12   13      14---15      16   17 ");
38
39   // It has a perfect matching of size 8. There are two isolated
40   // vertices that we'll use later...
41
42   my_graph g(n_vertices);
43   
44   // our vertices are stored in a vector, so we can refer to vertices
45   // by integers in the range 0..15
46
47   add_edge(1,2,g);
48   add_edge(0,4,g);
49   add_edge(1,5,g);
50   add_edge(2,6,g);
51   add_edge(3,7,g);
52   add_edge(4,5,g);
53   add_edge(6,7,g);
54   add_edge(4,8,g);
55   add_edge(5,9,g);
56   add_edge(6,10,g);
57   add_edge(7,11,g);
58   add_edge(8,9,g);
59   add_edge(10,11,g);
60   add_edge(8,13,g);
61   add_edge(9,14,g);
62   add_edge(10,15,g);
63   add_edge(11,16,g);
64   add_edge(14,15,g);
65
66   std::vector<graph_traits<my_graph>::vertex_descriptor> mate(n_vertices);
67
68   // find the maximum cardinality matching. we'll use a checked version
69   // of the algorithm, which takes a little longer than the unchecked
70   // version, but has the advantage that it will return "false" if the
71   // matching returned is not actually a maximum cardinality matching
72   // in the graph.
73
74   bool success = checked_edmonds_maximum_cardinality_matching(g, &mate[0]);
75   assert(success);
76
77   std::cout << "In the following graph:" << std::endl << std::endl;
78
79   for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
80     std::cout << *itr << std::endl;
81
82   std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;
83
84   std::cout << "The matching is:" << std::endl;
85   
86   graph_traits<my_graph>::vertex_iterator vi, vi_end;
87   for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
88     if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
89       std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;
90
91   std::cout << std::endl;
92
93   //now we'll add two edges, and the perfect matching has size 9
94
95   ascii_graph.pop_back();
96   ascii_graph.push_back("     12---13      14---15      16---17 ");
97
98   add_edge(12,13,g);
99   add_edge(16,17,g);
100
101   success = checked_edmonds_maximum_cardinality_matching(g, &mate[0]);
102   assert(success);
103
104   std::cout << "In the following graph:" << std::endl << std::endl;
105
106   for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
107     std::cout << *itr << std::endl;
108
109   std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;
110
111   std::cout << "The matching is:" << std::endl;
112   
113   for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
114     if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
115       std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;
116
117   return 0;
118 }