首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以graphml格式输出有向图?

如何以graphml格式输出有向图?
EN

Stack Overflow用户
提问于 2013-04-12 17:15:00
回答 2查看 827关注 0票数 0

我如何将下面的图形输出到graphml中?

代码语言:javascript
复制
typedef struct Vertex{ std::string name; std::string cmdb_id;

             Vertex& operator= (const Vertex& rhs)
             {
                     if (this == &rhs)
                             return *this;

                     name = rhs.name;
                     cmdb_id = rhs.cmdb_id;
             }

             bool operator< (const Vertex& rhs) const
             {
                     return cmdb_id < rhs.cmdb_id;
             };

             bool operator== (const Vertex& rhs) const
             {
                     return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name));
             };

     }vertex_container;

typedef struct Edge {std::string name;} edge_container;

boost::directed_graph<vertex_container, edge_container> Graph g;
EN

回答 2

Stack Overflow用户

发布于 2013-04-12 19:34:07

以下是不使用c++11功能的另一个答案的一个版本:

代码语言:javascript
复制
#include <iostream>

#include <boost/graph/directed_graph.hpp>
#include <boost/graph/graphml.hpp>



typedef struct Vertex
{ 
   std::string name; 
   std::string cmdb_id;

   Vertex& operator= (const Vertex& rhs)
   {
      if (this == &rhs)
        return *this;

      name = rhs.name;
      cmdb_id = rhs.cmdb_id;
   }

   bool operator< (const Vertex& rhs) const
   {
      return cmdb_id < rhs.cmdb_id;
   };

   bool operator== (const Vertex& rhs) const
   {
      return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name));
   };

}vertex_container;

typedef struct Edge {std::string name;} edge_container;

typedef boost::directed_graph<vertex_container, edge_container> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;

int main()
{
   Graph g;
   vertex_container A, B;
   edge_container AB;

   A.name="A";
   A.cmdb_id="1";
   B.name="B";
   B.cmdb_id="2";
   AB.name="A-B";

   vertex_descriptor v0 = g.add_vertex(A);
   vertex_descriptor v1 = g.add_vertex(B);
   g.add_edge(v0,v1,AB);

   boost::dynamic_properties dp;
   dp.property("vertex_name",get(&vertex_container::name,g));
   dp.property("vertex_cmdb_id",get(&vertex_container::cmdb_id,g));
   dp.property("edge_name",get(&edge_container::name,g));

   write_graphml(std::cout, g, dp);

}

Working on g++ 4.6.3 on LWS

票数 2
EN

Stack Overflow用户

发布于 2013-04-12 18:10:42

代码语言:javascript
复制
#include <iostream>

#include <boost/graph/directed_graph.hpp>
#include <boost/graph/graphml.hpp>



typedef struct Vertex
{ 
   std::string name; 
   std::string cmdb_id;

   Vertex& operator= (const Vertex& rhs)
   {
      if (this == &rhs)
        return *this;

      name = rhs.name;
      cmdb_id = rhs.cmdb_id;
   }

   bool operator< (const Vertex& rhs) const
   {
      return cmdb_id < rhs.cmdb_id;
   };

   bool operator== (const Vertex& rhs) const
   {
      return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name));
   };

}vertex_container;

typedef struct Edge {std::string name;} edge_container;

typedef boost::directed_graph<vertex_container, edge_container> Graph;

int main()
{
   Graph g;
   auto v0 = g.add_vertex({"A","1"});
   auto v1 = g.add_vertex({"B","2"});
   g.add_edge(v0,v1,{"A-B"});

   boost::dynamic_properties dp;
   dp.property("vertex_name",get(&vertex_container::name,g));
   dp.property("vertex_cmdb_id",get(&vertex_container::cmdb_id,g));
   dp.property("edge_name",get(&edge_container::name,g));

   write_graphml(std::cout, g, dp);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15967404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档