C++ 如何读取Boost图形库中邻接矩阵的图形?

C++ 如何读取Boost图形库中邻接矩阵的图形?,c++,boost,graph,adjacency-matrix,boost-graph,C++,Boost,Graph,Adjacency Matrix,Boost Graph,在boost图形库中,有两个常用函数可以从文件中读入图形: ,和,分别用于和 现在,它们都通俗地阅读任何类型的boost::adjacence_list,因为它们是该概念的模型: #包括 #包括 #包括 #包括 #包括 #包括 #包括 模板 图形类型加载(std::字符串文件名,std::字符串格式){ 图形类型g(0); std::ifstream t(filename.c_str()); boost::动态属性dp(boost::忽略其他属性); 如果(格式==“图形”) boost::re

在boost图形库中,有两个常用函数可以从文件中读入图形: ,和,分别用于和

现在,它们都通俗地阅读任何类型的
boost::adjacence_list
,因为它们是该概念的模型:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
模板
图形类型加载(std::字符串文件名,std::字符串格式){
图形类型g(0);
std::ifstream t(filename.c_str());
boost::动态属性dp(boost::忽略其他属性);
如果(格式==“图形”)
boost::read_graphml(t,g,dp);
其他的
boost::read_graphviz(t,g,dp);
返回g;
}
如果你要测试

load<boost::adjacency_matrix<boost::undirectedS> >("my_file.gv", "graphviz");
因此,我如何能够包括读取
boost::adjacency_矩阵
的可能性,最好不必从中间邻接列表复制图形,如前所述(图形可能非常大)

我不明白的是,对于复制,(复制目标)图也必须是可变图,那么我们如何才能复制到邻接矩阵?而不是读入其中

谢谢你的帮助

boost/graph/graphml.hpp
库不仅仅是标题,还需要链接,例如在直接从CLI编译/链接时,添加
-lboost\u graph

g++-lboost\u graph my\u file.cc
关于
复制图表
我不明白的是,对于复制,(复制目标)图显然也必须是可变图,那么我们如何才能复制到邻接矩阵?而不是读入其中

我同意。那没有道理。尽管如此,
copy_graph
似乎不起作用,或者记录的概念要求已经过时

也许它总是受到过度约束,或者专门为邻接矩阵添加了专门化

粗略地看一眼,它可能受到了过度约束,因为邻接矩阵显然不存在专门化/重载

在测试之前,让我们看看断言

关于断言 断言源于此:

template < typename D, typename VP, typename EP, typename GP, typename A >
inline typename adjacency_matrix< D, VP, EP, GP, A >::vertex_descriptor
add_vertex(adjacency_matrix< D, VP, EP, GP, A >& g)
{
    // UNDER CONSTRUCTION
    BOOST_ASSERT(false);
    return *vertices(g).first;
}
(我测试过,方向性实际上没有什么区别)

这就是我们谜题的答案。我们实际上无法复制

它确实可以编译,但在没有断言的情况下不会运行

现在,您可能认为可以定义NDEBUG并使断言静音。但是,从上面的代码可以清楚地看出,不能指望它工作,因为它总是返回vertex
0

:

这显然是不正确的,尽管这是您从阅读代码中所期望的

结束语 手动编写一些解析代码可能更容易。若你们必须,你们可以先读入一个高度优化的邻接列表。例如,您可以完全映射它的内存

< > >代码> Boo::SouthyLyList< <代码>不允许,可以考虑提供自己的数据结构来建模可变图概念。这听起来可能让人望而生畏,但实际上比你想象的要少:

  • 接下来,它将使用更多的算法:
  • 关于在映射内存中存储复杂的数据结构,我使用Boost Interprocess有很多答案

坦率地说,这些答案没有我刚才就你的另一个问题所作的回答那么复杂。一定要给他们一个阅读的灵感(从第一个开始)

“因为他们是从可变图概念中衍生出来的”应该读“因为他们是可变图概念的模型”,这是一个明显不同的东西。
template < typename D, typename VP, typename EP, typename GP, typename A >
inline typename adjacency_matrix< D, VP, EP, GP, A >::vertex_descriptor
add_vertex(adjacency_matrix< D, VP, EP, GP, A >& g)
{
    // UNDER CONSTRUCTION
    BOOST_ASSERT(false);
    return *vertices(g).first;
}
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/copy.hpp>
using boost::vecS;
using D = boost::undirectedS¹;
using L = boost::adjacency_list<vecS, vecS, D>;
using M = boost::adjacency_matrix<D>;

int main() {
    { L list(0); M matrix(20); copy_graph(matrix, list);   } // okay
    { L list(0); M matrix(20); copy_graph(list,   matrix); } // okay
    { L list(1); M matrix(1);  copy_graph(list,   matrix); } // ASSERTS
    { L list(1); M matrix(20); copy_graph(list,   matrix); } // ASSERTS
}
#define NDEBUG
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/graph_utility.hpp>
using boost::vecS;
using D = boost::undirectedS;
using L = boost::adjacency_list<vecS, vecS, D>;
using M = boost::adjacency_matrix<D>;

int main() {
    L list(5);
    M matrix(10);

    add_edge(1, 4, list);
    add_edge(3, 1, list);

    copy_graph(list, matrix);

    print_graph(list, std::cout << "\nlist:");
    print_graph(matrix, std::cout << "\nmatrix:");
}
list:0 --> 
1 --> 4 
2 --> 
3 --> 1 
4 --> 

matrix:0 --> 0 
1 --> 
2 --> 
3 --> 
4 -->