[英]Boost Graph edge comparison not working
我正在嘗試對圖形進行一些統計(比較2個圖形)。 但是當我比較邊緣時會有問題。
因此,我創建了兩個帶有一些邊的圖形,然后創建了一些用於頂點和邊操作的模板。 對於頂點,它工作得很好,但是對於邊緣,則工作不正常。
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/graph_utility.hpp>
typedef boost::adjacency_list < boost::vecS, boost::vecS,
boost::undirectedS > Graph;
template <typename T> std::set<T> operator-(const std::set<T>& a,
const std::set<T>& b)
{
std::set<T> r;
std::set_difference(
a.begin(), a.end(),
b.begin(), b.end(),
std::inserter(r, r.end()));
return r;
}
template <typename T> std::set<T> operator/(const std::set<T>& a,
const std::set<T>& b)
{
std::set<T> r;
std::set_intersection(
a.begin(), a.end(),
b.begin(), b.end(),
std::inserter(r, r.end()));
return r;
}
void compare(const Graph& a, const Graph& b)
{
std::set<Graph::vertex_descriptor > av, bv, samev, extrav, missingv;
std::set<Graph::edge_descriptor> ae, be, re, samee, extrae, missinge;
BGL_FORALL_VERTICES_T(v, a, Graph) av.insert(v);
BGL_FORALL_VERTICES_T(v, b, Graph) bv.insert(v);
samev = (av / bv);
extrav = (bv - av);
missingv = (av - bv);
BGL_FORALL_EDGES_T(e, a, Graph) ae.insert(e);
BGL_FORALL_EDGES_T(e, b, Graph) be.insert(e);
samee = (ae / be);
extrae = (be - ae);
missinge = (ae - be);
// TODO(silgon): reverse_graph
// boost::reverse_graph<Graph> r(b);
// BGL_FORALL_EDGES_T(e, r, Graph) re.insert(e);
std::cout << "---- Vertices ----\n"
<< "same: " << samev.size() << std::endl
<< "extra: " << extrav.size() << std::endl
<< "missing: " << missingv.size() << std::endl;
std::cout << "---- Edges ----\n"
<< "same: " << samee.size() << std::endl
<< "extra: " << extrae.size() << std::endl
<< "missing: " << missinge.size() << std::endl;
}
int main()
{
Graph a;
{
boost::graph_traits<Graph>::vertex_descriptor v, u, y;
u = boost::vertex(1, a);
v = boost::vertex(2, a);
y = boost::vertex(3, a);
boost::add_edge(u, v, a);
}
Graph b;
{
boost::graph_traits<Graph>::vertex_descriptor v, u, y;
u = vertex(1, b);
v = vertex(2, b);
y = vertex(3, b);
boost::add_edge(u, v, b);
boost::add_edge(y, v, b);
}
const char* name = "123456";
std::cout << "---Graph1---" << "\n";
boost::print_graph(a);
std::cout << "Edges: ";
boost::print_edges(a,name);
std::cout << "---Graph2---" << "\n";
boost::print_graph(b);
std::cout << "Edges: ";
boost::print_edges(b,name);
compare(a,b);
}
至於程序的結果如下:
---Graph1---
0 <-->
1 <--> 2
2 <--> 1
Edges: (2,3)
---Graph2---
0 <-->
1 <--> 2
2 <--> 1 3
3 <--> 2
Edges: (2,3) (4,3)
---- Vertices ----
same: 3
extra: 1
missing: 0
---- Edges ----
same: 0
extra: 2
missing: 1
您可以看到邊之一相同(2,3),但是在執行操作時並未考慮到邊,因此在結果中將其說為same:0,並且多余或缺失的結果均不起作用。
深入研究Boost的edge_descriptor
實現(在boost/graph/detail/edge.hpp
,我們發現每個邊緣描述符都存儲源和目標vertex_descriptor
,還存儲着edge屬性的指針。該指針對於其vertex_descriptor
的兩個邊緣是不同的否則相同。
這意味着您需要定義自己的edge_descriptor
比較器,該比較器用於std::set
和set_intersection
/ set_difference
操作,例如:
struct edge_cmp
{
bool operator () (const Graph::edge_descriptor& a, const Graph::edge_descriptor& b) const
{
if (a.m_source < b.m_source) { return true; }
if (a.m_source > b.m_source) { return false; }
return (a.m_target < b.m_target);
}
};
必須將這種類型的比較器對象傳遞給您構造的所有集合以及交集/差異調用。
我已經在ideone上分叉了您的代碼,並進行了相應的修改。 輸出:
---- Vertices ----
same: 3
extra: 1
missing: 0
---- Edges ----
same: 1
extra: 1
missing: 0
作為我的問題的臨時解決方案,而不是:
std::set<Graph::edge_descriptor> ae
BGL_FORALL_EDGES_T(e, a, Graph) ae.insert(e)
我為邊緣做了以下代碼。
std::set<std::pair<unsigned long, unsigned long> > ae;
BGL_FORALL_EDGES_T(e, ga, Graph) gae.insert(std::make_pair(e.m_source,
e.m_target));
問題在於,boost圖形的邊緣具有“ m_eproperty”,我不知道為什么會出現它,並且包含諸如0x125d0c0
值。 然后,我基於邊的源和目標創建了一對,然后以與上面相同的方式(使用模板)進行了評估。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.