【问题标题】:How to set and get graph property from boost graph library?如何从 boost 图形库中设置和获取图形属性?
【发布时间】:2014-04-09 00:17:16
【问题描述】:

我不知道如何从 adjacency_list 图中设置和获取 graph_name 作为属性。我能够放置和获取顶点和边属性。

任何帮助将不胜感激。

【问题讨论】:

    标签: boost graph boost-graph


    【解决方案1】:

    首先要定义类来保存顶点、边和图形的属性

    class cVertexProps {
    ...
    }
    class cEdgeProps {
    ...
    }
    class cGraphProps {
    public:
        std::string myName;
        ...
    };
    

    现在定义你的图表

    typedef boost::adjacency_list <
        boost::vecS, boost::vecS, boost::undirectedS,
        cVertexProps, cEdgeProps, cGraphProps  >
                graph_t;
    graph_t myGraph;
    

    ...然后设置你的图表名称

    myGraph[graph_bundle].myName= "My First Graph";
    

    这使用“捆绑属性”,此处描述:http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/bundles.html

    【讨论】:

      【解决方案2】:

      我有 boost subgraph 的解决方案(只是尝试它肯定会工作)属性获取和设置如下:-

      #include <QtCore/QCoreApplication>
      
      #include <boost/config.hpp>
      #include <iostream>
      #include <algorithm>
      #include <boost/graph/adjacency_list.hpp>
      #include <boost/property_map/property_map.hpp>
      #include <string>
      #include <boost/graph/subgraph.hpp>
      #include <QMap>
      
      using namespace std;
      using namespace boost;
      
      enum graph_IDproperty_t
      {
          graph_IDproperty
      };
      
      namespace boost
      {
          BOOST_INSTALL_PROPERTY(graph,IDproperty);
      }
      
      struct GraphProperties {
          std::string strName;
          std::string id;
      };
      
      typedef boost::subgraph<
          boost::adjacency_list<
              boost::listS, boost::vecS, boost::bidirectionalS,
              boost::property<boost::vertex_index_t, int,
                              property< boost::vertex_color_t, boost::default_color_type > >,
              boost::property<boost::edge_index_t, int,
                              property<boost::edge_color_t, default_color_type> >,
              boost::property<graph_IDproperty_t,GraphProperties> 
          > 
      > Graph;
      
      Graph gMainGraph;
      
      typedef QMap<Graph*,GraphProperties*> mapGraphToProperty;
      mapGraphToProperty getMap(Graph& graph);
      void graphMapRecur(mapGraphToProperty& map, Graph& graph);
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          Graph& subG = gMainGraph.create_subgraph();
          Graph& subG1 = gMainGraph.create_subgraph();
      
          boost::ref_property_map<Graph*, GraphProperties>
              graph_propt1(boost::get_property(subG1,graph_IDproperty));
          graph_propt1[&subG1].id = "SubG1";
          cout << graph_propt1[&subG1].id << endl;
      
          boost::ref_property_map<Graph*, GraphProperties>
              graph_propt(boost::get_property(subG,graph_IDproperty));
          graph_propt[&subG].id = "SubG";
          cout << graph_propt[&subG].id << endl;
      
          boost::ref_property_map<Graph*, GraphProperties>
              graph_proptMain(boost::get_property(gMainGraph,graph_IDproperty));
          graph_proptMain[&gMainGraph].id = "gMain";
          cout << graph_proptMain[&gMainGraph].id << endl;
      
          mapGraphToProperty map = getMap(gMainGraph);
      
          boost::ref_property_map<Graph*, GraphProperties>
              graph_proptMain1(*(map.value(&gMainGraph)));
      
          boost::ref_property_map<Graph*, GraphProperties>
              graph_proptsubG(*(map.value(&subG)));
      
          boost::ref_property_map<Graph*, GraphProperties>
              graph_proptsubG1(*(map.value(&subG1)));
      
          cout << "Main G Value : " << graph_proptMain1[&gMainGraph].id << endl;
          cout << "Sub G Value : " << graph_proptsubG[&subG].id << endl;
          cout << "Sub G1 Value : " << graph_proptsubG1[&subG1].id << endl;
      
      
          cout << "Map Value Main: " << (map.value(&gMainGraph)) << endl;
          cout << "Map Value SubG: " << (map.value(&subG)) << endl;
          cout << "Map Value SubG1b: " << (map.value(&subG1)) << endl;
          return a.exec();
      }
      
      mapGraphToProperty getMap(Graph &graph)
      {
          mapGraphToProperty map;
          graphMapRecur(map,graph);
          return map;
      }
      
      void graphMapRecur(mapGraphToProperty &map, Graph &graph)
      {
          Graph::children_iterator itrSubgraph, itrSubgraph_end;
      
          for (boost::tie(itrSubgraph, itrSubgraph_end) = (graph).children();
               itrSubgraph != itrSubgraph_end; ++itrSubgraph)
          {
              graphMapRecur(map,(*itrSubgraph));
          }
      
          GraphProperties* gp = &(get_property(graph,graph_IDproperty));
      
          map.insert(&graph,gp);
          cout << "Recurrr" << endl;
      
      }
      

      【讨论】:

      • 建议添加一些cmets来解释你在做什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      相关资源
      最近更新 更多