我正在用Boost.Graph做一个有向图(实际上是一个双向图)。我想使用现有的布局算法( Kamada-Kawai或Fruchterman-Reingold),但它们只接受无向图作为参数。
使用这些布局算法最简单的方法是什么?更广泛地说,什么是引诱算法认为有向图实际上是无向图的正确方法?
谢谢,伯努瓦
发布于 2009-02-16 14:40:28
你确定Fruchterman-Reingold算法只接受无向图吗?我尝试使用双向图而不是无向图来运行Boost documentation中的小示例,它编译并运行得很好。
为了回答你的问题,我不确定BGL中是否有任何工具可以将有向图转换为无向图。我找到的唯一解决方案是创建一个新的图,并添加来自原始图的所有边:
typedef adjacency_list<vecS, vecS, bidirectionalS> BidirectionalGraph;
typedef adjacency_list<setS, vecS, bidirectionalS> UndirectedGraph;
// UndirectedGraph uses a set to avoid parallel edges
BidirectionalGraph bdg;
// use bdg
// create an undirected graph with the edges of the first one
typedef graph_traits<BidirectionalGraph>::vertex_iterator vi_beg, vi_end;
tie(vbeg, vend) = vertices(bdg);
UndirectedGraph ug(std::distance(vbeg, vend));
typedef graph_traits<BidirectionalGraph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(bdg) ; ei != ei_end ; ++ei)
{
add_edge(source(*ei,bdg), target(*ei,bdg), ug);
}然而,我猜这个解决方案在处理巨大的图形时可能会引起一些性能问题。可能有更好的方法来实现你的目标,但我不是BGL专家,所以这就是我能给你的:-)!
正如伯努瓦在评论中指出的那样,BGL提供了一个函数copy_graph,它可以将一个图的所有顶点和边复制到另一个图中。因此,上面的代码可以归结为:
#include <boost/graph/copy.hpp>
Bidirectional bdg;
// use bdg
// create an undirected graph with the vertices and edges of the first one
UndirectedGraph g;
copy_graph(bdg, g);https://stackoverflow.com/questions/552854
复制相似问题