【发布时间】:2021-08-26 22:46:32
【问题描述】:
我有一个无向的 boost::adjacency_graph。给定一个顶点,我想找到所有以该顶点为源的边对。
例如,如果我有
0 --- 1 --- 2
0 和 2 的对将是空集,1 将是 ((1,0), (1,2))
为
0 --- 1 --- 2
\--- 3
0、2 和 3 的对将是空集,而 1 将是
((1,0), (1,2))
((1,0), (1,3))
((1,2), (1,3))
我在考虑类似的事情
std::vector<std::pair<edge, edge> edges;
for (const auto vertex : boost::make_iterator_range(boost::vertices(graph))) {
for (const auto outEdge1 : boost::make_iterator_range(boost::out_edges(vertex, graph))) {
for (const auto outEdge2 : boost::make_iterator_range(boost::out_edges(vertex, graph))) {
if (outEdge != outEdge2) edges.push_back(outEdge1, outEdge2);
}
}
}
但这会失败,因为它会添加例如((1,0), (1, 2)) 和((1,2), (1, 0))
我怎样才能避免这种情况?我认为这只是没有重复顶点外边的组合
考虑到边缘
(1,0)
(1,2)
(1,3)
如何获得组合?
【问题讨论】:
-
所以您只需要一种方法来消除当前答案集中的重复项?
-
嗯...没有看到我所说的这个问题的重复,但STL + Ordered set + without duplicates 可能会为您指明一个好的方向。
-
使用迭代器而不是 for 范围,类似于 (
if (!out.empty()) for (auto it1 = out.begin(); std::next(it1) != out.end(); ++it1) { for (auto it2 = std::next(it1); it2 != out.end(); ++it2) { foo(*it1, *it2); } })。
标签: c++ boost graph combinations