簡體   English   中英

Boost - 在圖中查找最大匹配邊

[英]Boost - Find maximum matching edges in a graph

我是 Boost(和 C++)新手,正在閱讀圖形庫教程 我可以創建一個圖形並為其指定頂點和邊。 我想在 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;
}

您需要做的就是創建讀寫屬性映射並將其作為第二個參數傳遞給

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM