File: matching_example.cpp

package info (click to toggle)
boost 1.34.1-14
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 116,412 kB
  • ctags: 259,566
  • sloc: cpp: 642,395; xml: 56,450; python: 17,612; ansic: 14,520; sh: 2,265; yacc: 858; perl: 481; makefile: 478; lex: 94; sql: 74; csh: 6
file content (115 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//=======================================================================
// Copyright (c) 2005 Aaron Windsor
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
//=======================================================================
#include <string>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <cassert>

#include <boost/graph/max_cardinality_matching.hpp>


using namespace boost;

typedef adjacency_list<vecS, vecS, undirectedS> my_graph; 

int main()
{

  // Create the following graph: (it'll look better when output
  // to the terminal in a fixed width font...)

  const int n_vertices = 18;

  std::vector<std::string> ascii_graph;

  ascii_graph.push_back("           0       1---2       3       ");
  ascii_graph.push_back("            \\     /     \\     /        ");
  ascii_graph.push_back("             4---5       6---7         ");
  ascii_graph.push_back("             |   |       |   |         ");
  ascii_graph.push_back("             8---9      10---11        ");
  ascii_graph.push_back("            /     \\     /     \\        ");
  ascii_graph.push_back("     12   13      14---15      16   17 ");

  // It has a perfect matching of size 8. There are two isolated
  // vertices that we'll use later...

  my_graph g(n_vertices);
  
  // our vertices are stored in a vector, so we can refer to vertices
  // by integers in the range 0..15

  add_edge(0,4,g);
  add_edge(1,5,g);
  add_edge(2,6,g);
  add_edge(3,7,g);
  add_edge(4,5,g);
  add_edge(6,7,g);
  add_edge(4,8,g);
  add_edge(5,9,g);
  add_edge(6,10,g);
  add_edge(7,11,g);
  add_edge(8,9,g);
  add_edge(10,11,g);
  add_edge(8,13,g);
  add_edge(9,14,g);
  add_edge(10,15,g);
  add_edge(11,16,g);
  add_edge(14,15,g);

  std::vector<graph_traits<my_graph>::vertex_descriptor> mate(n_vertices);

  // find the maximum cardinality matching. we'll use a checked version
  // of the algorithm, which takes a little longer than the unchecked
  // version, but has the advantage that it will return "false" if the
  // matching returned is not actually a maximum cardinality matching
  // in the graph.

  assert(checked_edmonds_maximum_cardinality_matching(g, &mate[0]));

  std::cout << "In the following graph:" << std::endl << std::endl;

  for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
    std::cout << *itr << std::endl;

  std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;

  std::cout << "The matching is:" << std::endl;
  
  graph_traits<my_graph>::vertex_iterator vi, vi_end;
  for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
    if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
      std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;

  std::cout << std::endl;

  //now we'll add two edges, and the perfect matching has size 9

  ascii_graph.pop_back();
  ascii_graph.push_back("     12---13      14---15      16---17 ");

  add_edge(12,13,g);
  add_edge(16,17,g);

  assert(checked_edmonds_maximum_cardinality_matching(g, &mate[0]));

  std::cout << "In the following graph:" << std::endl << std::endl;

  for(std::vector<std::string>::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
    std::cout << *itr << std::endl;

  std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl;

  std::cout << "The matching is:" << std::endl;
  
  for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
    if (mate[*vi] != graph_traits<my_graph>::null_vertex() && *vi < mate[*vi])
      std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl;

  return 0;
}