使用 Boost Graph 顶点属性进行动态分配
Posted
技术标签:
【中文标题】使用 Boost Graph 顶点属性进行动态分配【英文标题】:Dynamic allocation usign Boost Graph vertex properties 【发布时间】:2017-10-25 08:58:31 【问题描述】:我正在使用 Boost Graph 库来读取 GraphML 文件。我想做的是使用图管理的 Boost 功能来创建我自己的动态分配对象结构,以便我可以在其上运行我的自定义算法。
struct VertexProperties
std::string vertex_name;
bool defect;
bool node_logic;
custom_node * node;
;
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
但问题是,当使用深度优先搜索自定义访问者时,我似乎无法为 custom_node 指针分配内存。
node = new custom_node(g[v].vertex_name, 0, standby, normal);
当我收到“只读”编译错误时。
如果有办法将图表映射到可以使用动态分配重新创建图表结构的其他地方,我正在徘徊?
修剪过的主菜:
#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include "node.h"
using namespace boost;
struct VertexProperties
std::string vertex_name;
bool node_logic;
custom_node * node;
;
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;
class custom_dfs_visitor : public default_dfs_visitor
public:
void discover_vertex(custom_vertex v, const DirectedGraph& g) const
// Looking for adjacent vertices
DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;
if(true == g[v].node_logic)
g[v].node = new custom_node(g[v].vertex_name, 0, standby, normal);
std::cout << g[v].vertex_name << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
for (; neighbourIt != neighbourEnd; ++neighbourIt)
std::cout << g[*neighbourIt].vertex_name << " ";
std::cout << std::endl;
void examine_edge(custom_edge e, const DirectedGraph& g) const
std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> "
<< g[e.m_target].vertex_name << std::endl;
;
int main(int argc, char* argv[])
int i;
bool is_config = false;
DirectedGraph g;
int verbose;
std::ifstream infile;
dynamic_properties dp(ignore_other_properties);
custom_dfs_visitor vis;
dp.property("node_name", boost::get(&VertexProperties::vertex_name, g));
dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));
/* Argument check */
if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h'))
usage();
return 0;
/* Parse command line options */
for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++)
switch (argv[i][1])
case 'v': /* verbose */
verbose = 10;
break;
case 'c': /* read *.ini configuration file */
// d_printf(D_INFO, "parsing '%s'... \n", argv[++i]);
infile.open(argv[++i], std::ifstream::in);
if (!infile.is_open())
std::cout << "Loading file '" << argv[i] << "'failed" << std::endl;
throw "Could not load file";
else
boost::read_graphml(infile, g, dp);
is_config = true;
break;
case 's':
is_defect = true;
std::string temp_defect(argv[++i]);
defect = temp_defect;
std::cout << defect;
break;
default: /* something's wrong */
usage();
break;
if (true == is_config)
depth_first_search(g, boost::visitor(vis));
return 0;
感谢您的回答
【问题讨论】:
你能把样品做成独立的吗?我可以为您编写一个有效的示例,但它需要做很多工作,而且我已经知道它有效。 谢谢,我在邮件正文中添加了最重要的部分 【参考方案1】:好的,depht_first_visit 必须在常量图上工作。但是,显然不能修改常量图。
因此,您想告诉访问者对您的图表的非常量引用,以便您可以通过它进行修改。
我建议进行以下最小更改:
Live On Coliru
#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
//#include "cgfnode.h"
enum Sstandby;
enum Mnormal;
struct custom_node
custom_node(std::string, int, S, M)
;
void usage()
using namespace boost;
struct VertexProperties
std::string vertex_name;
bool node_logic;
std::unique_ptr<custom_node> node;
;
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;
struct custom_dfs_visitor : default_dfs_visitor
custom_dfs_visitor(DirectedGraph& g) : _mutable_graph(&g)
void discover_vertex(custom_vertex v, DirectedGraph const& g) const
assert(&g == _mutable_graph);
return discover_vertex(v, *_mutable_graph);
void discover_vertex(custom_vertex v, DirectedGraph& g) const
// Looking for adjacent vertices
DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;
if(g[v].node_logic)
g[v].node.reset(new custom_node(g[v].vertex_name, 0, standby, normal));
std::cout << g[v].vertex_name << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
for (; neighbourIt != neighbourEnd; ++neighbourIt)
std::cout << g[*neighbourIt].vertex_name << " ";
std::cout << "\n";
void examine_edge(custom_edge e, const DirectedGraph& g) const
std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> " << g[e.m_target].vertex_name << "\n";
private:
DirectedGraph* _mutable_graph;
;
int main()
DirectedGraph g;
dynamic_properties dp(ignore_other_properties);
dp.property("node_name", boost::get(&VertexProperties::vertex_name, g));
dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));
std::ifstream infile("input.xml");
boost::read_graphml(infile, g, dp);
custom_dfs_visitor vis (g);
depth_first_search(g, boost::visitor(vis));
【讨论】:
出于好奇,您如何使read_graphml
工作?我无法让它识别节点属性
哇,成功了。不过,我很难理解 unique_ptr 。非常感谢!
unique_ptr 是没有必要的,但我认为从代码中删除不必要的内存泄漏是一个原则问题。
您有我可以使用的示例 graphml 文档吗?我真的很想了解这一点,但是这些文档对我帮助不大。如果您不这样做,我将查看解析代码。
我从这里开始:link。但目前我还没有完成更改属性。我使用了 vertex_name,它基本上是 GraphML 文件中用于定义顶点和 '以上是关于使用 Boost Graph 顶点属性进行动态分配的主要内容,如果未能解决你的问题,请参考以下文章
使用Boost Graph库查找连接的组件,顶点和边缘类型为boost :: listS
删除顶点并再次添加它会导致 boost::graph 崩溃?
Boost Graph 通过 vertex_descriptor 访问属性