1 //=======================================================================
2 // Copyright (c) 2005 Aaron Windsor
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)
8 //=======================================================================
11 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/graph/max_cardinality_matching.hpp>
17 using namespace boost;
19 typedef adjacency_list<vecS, vecS, undirectedS> my_graph;
24 // Create the following graph: (it'll look better when output
25 // to the terminal in a fixed width font...)
27 const int n_vertices = 18;
29 std::vector<std::string> ascii_graph;
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 ");
39 // It has a perfect matching of size 8. There are two isolated
40 // vertices that we'll use later...
42 my_graph g(n_vertices);
44 // our vertices are stored in a vector, so we can refer to vertices
45 // by integers in the range 0..15
65 std::vector<graph_traits<my_graph>::vertex_descriptor> mate(n_vertices);
67 // find the maximum cardinality matching. we'll use a checked version
68 // of the algorithm, which takes a little longer than the unchecked
69 // version, but has the advantage that it will return "false" if the
70 // matching returned is not actually a maximum cardinality matching
73 bool success = checked_edmonds_maximum_cardinality_matching(g, &mate[0]);
76 std::cout << "In the following graph:" << std::endl << std::endl;
78 for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
79 std::cout << *itr << std::endl;
81 std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;
83 std::cout << "The matching is:" << std::endl;
85 graph_traits<my_graph>::vertex_iterator vi, vi_end;
86 for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
87 if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
88 std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;
90 std::cout << std::endl;
92 //now we'll add two edges, and the perfect matching has size 9
94 ascii_graph.pop_back();
95 ascii_graph.push_back(" 12---13 14---15 16---17 ");
100 success = checked_edmonds_maximum_cardinality_matching(g, &mate[0]);
103 std::cout << "In the following graph:" << std::endl << std::endl;
105 for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
106 std::cout << *itr << std::endl;
108 std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;
110 std::cout << "The matching is:" << std::endl;
112 for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
113 if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
114 std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;