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(0,4,g); |
---|
48 | add_edge(1,5,g); |
---|
49 | add_edge(2,6,g); |
---|
50 | add_edge(3,7,g); |
---|
51 | add_edge(4,5,g); |
---|
52 | add_edge(6,7,g); |
---|
53 | add_edge(4,8,g); |
---|
54 | add_edge(5,9,g); |
---|
55 | add_edge(6,10,g); |
---|
56 | add_edge(7,11,g); |
---|
57 | add_edge(8,9,g); |
---|
58 | add_edge(10,11,g); |
---|
59 | add_edge(8,13,g); |
---|
60 | add_edge(9,14,g); |
---|
61 | add_edge(10,15,g); |
---|
62 | add_edge(11,16,g); |
---|
63 | add_edge(14,15,g); |
---|
64 | |
---|
65 | std::vector<graph_traits<my_graph>::vertex_descriptor> mate(n_vertices); |
---|
66 | |
---|
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 |
---|
71 | // in the graph. |
---|
72 | |
---|
73 | assert(checked_edmonds_maximum_cardinality_matching(g, &mate[0])); |
---|
74 | |
---|
75 | std::cout << "In the following graph:" << std::endl << std::endl; |
---|
76 | |
---|
77 | for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr) |
---|
78 | std::cout << *itr << std::endl; |
---|
79 | |
---|
80 | std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl; |
---|
81 | |
---|
82 | std::cout << "The matching is:" << std::endl; |
---|
83 | |
---|
84 | graph_traits<my_graph>::vertex_iterator vi, vi_end; |
---|
85 | for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) |
---|
86 | if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi]) |
---|
87 | std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl; |
---|
88 | |
---|
89 | std::cout << std::endl; |
---|
90 | |
---|
91 | //now we'll add two edges, and the perfect matching has size 9 |
---|
92 | |
---|
93 | ascii_graph.pop_back(); |
---|
94 | ascii_graph.push_back(" 12---13 14---15 16---17 "); |
---|
95 | |
---|
96 | add_edge(12,13,g); |
---|
97 | add_edge(16,17,g); |
---|
98 | |
---|
99 | assert(checked_edmonds_maximum_cardinality_matching(g, &mate[0])); |
---|
100 | |
---|
101 | std::cout << "In the following graph:" << std::endl << std::endl; |
---|
102 | |
---|
103 | for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr) |
---|
104 | std::cout << *itr << std::endl; |
---|
105 | |
---|
106 | std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl; |
---|
107 | |
---|
108 | std::cout << "The matching is:" << std::endl; |
---|
109 | |
---|
110 | for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) |
---|
111 | if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi]) |
---|
112 | std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl; |
---|
113 | |
---|
114 | return 0; |
---|
115 | } |
---|