Boost-查找图形中的最大匹配边

时间:2018-12-05 23:10:06

标签: c++ boost graph-theory

我是Boost(和C ++)的新手,经历过graph library tutorial。我可以创建一个图,并为其赋予顶点和边。我想在Boost中使用最大基数匹配,以返回一组形成图中最大匹配的边。

我已经浏览了max_cardinality_matching.hpp,但不确定如何使用它或使用哪个函数来返回最大匹配边集。

到目前为止,这是我的代码:

#include <iostream>
#include <boost/graph/max_cardinality_matching.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>

using namespace boost;


int main(int argc, const char * argv[]) {
    // create a typedef for the Graph type
    typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;

    // Make convenient labels for the vertices
    enum { A, B, C, D, E, F, N };
    const int num_vertices = N;
//    const char* name = "ABCDE";

    // writing out the edges in the graph
    typedef std::pair<int, int> Edge;
    Edge edge_array[] =
    { Edge(A,B), Edge(B,C), Edge(C,D), Edge(D,E), Edge(E,F) };
//    const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);

    // declare a graph object and add the edges
    Graph g(edge_array, edge_array + sizeof(edge_array) / sizeof(Edge), num_vertices);

    // get the property map for vertex indices
    // property_map<Graph, property type>
    typedef property_map<Graph, vertex_index_t>::type IndexMap;
    IndexMap index = get(vertex_index, g);

    // Create an iterator for vertices
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    std::cout << "vertices(g) = ";

    // Vertices returns a pair of vertex iterators
    // The first iter points to the beginning of the vertices
    // The second points past the end
    std::pair<vertex_iter, vertex_iter> vp;

    // vertices() returns the vertices in graph g
    for (vp = vertices(g); vp.first != vp.second; ++vp.first)
        std::cout << index[*vp.first] <<  " ";
    std::cout << std::endl;

    graph_traits<Graph>::edge_iterator ei, ei_end;
    std::cout << "edges(g) = ";
    // For each tuple of vertices (an edge), till the end of the edge list ...
    for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
        // ... print out the source and target vertices in the edge
        std::cout << "(" << index[source(*ei, g)] << "," << index[target(*ei, g)] << ") ";
    std::cout << std::endl;

    // Return the set of edges that form a maximum matching in graph g

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要做的就是创建读写属性映射并将其作为第二个参数传递给

template <typename Graph, typename MateMap>
bool checked_edmonds_maximum_cardinality_matching(const Graph& g, MateMap mate);

您可以创建普通的 std :: map ,其中键和值是顶点描述符,并通过associative_property_map对其进行改编,以便与 checked_edmonds_maximum_cardinality_matching 一起使用。从此地图中,您可以读取所有可创建图形最大基数的边。

// Return the set of edges that form a maximum matching in graph g
   typedef graph_traits<Graph>::vertex_descriptor VD;

   std::map<VD,  VD> match;
   boost::associative_property_map< std::map<VD,VD> > mapAdapter(match);

   bool rc = checked_edmonds_maximum_cardinality_matching(g,mapAdapter);
   if (rc)
   {
      std::set<graph_traits<Graph>::edge_descriptor> edges;
      for (auto& i : match)
      {
        std::pair<Graph::edge_descriptor,bool> e = boost::edge(i.first,i.second,g);
        if (e.second)
            edges.insert(e.first);
        std::cout << i.first << " is matched to " << i.second << std::endl;
      }

      // print edges
      for (auto& e : edges)
          std::cout << "edge: " << e << std::endl;
   }

作为输出,您可以看到:

0 is matched to 1
1 is matched to 0
2 is matched to 3
3 is matched to 2
4 is matched to 5
5 is matched to 4
edge: (0,1)
edge: (2,3)
edge: (4,5)