2

I want to export a vector which is storing sequence of string values to boost graph(dot file). . The last four lines can explain the problem/help required. I know the code is wrong, but need guidlines to address this issue. I want to store vector toponodedist2 to graph dot file.Here I want store an array/vector which will store the sequence of values at indexes like (image01, image02, iamge 03 ....). later I will export this vector via write_graphviz_dp to dot file. Thanks

#include<iostream>
#include<boost/graph/adjacency_list.hpp>
#include<boost/graph/graphviz.hpp>
#include<boost/graph/properties.hpp>
#include<boost/graph/property_maps/container_property_map.hpp>
#include<boost/graph/named_function_params.hpp>
#include <cstdlib>
#include<fstream>

struct VertexData
{

    std:: string image_path;
        int id;
    int image_num;
        std:: vector<std::vector<std::string>> toponodedist2;

};



int main(int,char*[])
{

    VertexData v11;


    std:: vector<std::vector<std::string>> toponodedist;
    std:: vector<std::string> toponodedist1;
    toponodedist1.push_back("this");
    toponodedist1.push_back("This is first node2");
    toponodedist1.push_back("This is first node3");
    v11.toponodedist2.push_back(toponodedist1);
    toponodedist.push_back(toponodedist1);
    toponodedist1.clear();
    toponodedist1.push_back("This is first node1");
    toponodedist1.push_back("This is first node2");
    toponodedist1.push_back("This is first node3");
    toponodedist.push_back(toponodedist1);
     v11.toponodedist2.push_back(toponodedist1);

    for(int i=0;i<=2;i++)
        for(int j=0;j<=2;j++)
        std::cout<< "this is "<<v11.toponodedist2[i][j]<<std::endl;

    /// mention vertex data in declaring adjacency list
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexData,boost::no_property> MyGraphType;

    MyGraphType G;
    auto v1 =add_vertex(G);
    auto v2 =add_vertex(G);
    auto v3 =add_vertex(G);
    auto v4 =add_vertex(G);
    auto v5 =add_vertex(G);
    auto v6 =add_vertex(G);
    auto v7 =add_vertex(G);
    auto v8 =add_vertex(G);
    auto v9 =add_vertex(G);
        auto e1 =add_edge(v1,v2,G);
        auto e2 =add_edge(v2,v3,G);
        auto e3 =add_edge(v3,v4,G);
    auto e4 =add_edge(v4,v5,G);
        auto e5 =add_edge(v5,v6,G);
        auto e6 =add_edge(v7,v7,G);
        auto e7 =add_edge(v5,v2,G);
        auto e8 =add_edge(v1,v4,G);
        auto e9 =add_edge(v3,v7,G);
        auto e10 =add_edge(v2,v7,G);
        auto e11 =add_edge(v2,v6,G);
        auto vpair=vertices(G);
        int numberOfInEdges = boost::out_degree(8,G);

        std::cout<< "The number of vertices are  "<<numberOfInEdges; 
      ;

      std::ofstream dotfile1;
     dotfile1.open("dotgraph1.txt", std::ios::app);

      boost::dynamic_properties dp;

    dp.property("node_id", get(&VertexData::id, G));
// this is the place where I need help to export toponodeist2 to graph dot
 // file  ditfile1, but I am not able to do it. thanks    
dp.property("path_Ismage", get(&VertexData::toponodedist2, G));
/// the line need to be addressed

    boost::write_graphviz_dp(dotfile1,G,dp);
    dotfile1.close();

  return 0;
}
Khan
  • 55
  • 11

1 Answers1

1

Just transform the property to a string in some form, e.g.:

dp.property("toponodedist", boost::make_transform_value_property_map(topo_attr, get(boost::vertex_index, g)));

with e.g.

auto topo_attr = [&g](MyGraphType::vertex_descriptor const& v) {
    auto& vd = g[v];
    std::string s;
    for (auto& row : vd.topo) {
        for (auto& el : row) s += el + " ";
        s += '\n';
    }
    return s;
};

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <fstream>
#include <iostream>
#include <sstream>

using string_table = std::vector<std::vector<std::string>>;

struct VertexData {
    std::string image_path;
    int id;
    int image_num;
    string_table topo;
};

int main(int, char *[]) {

    /// mention vertex data in declaring adjacency list
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexData>
        MyGraphType;

    MyGraphType g(9);
    add_edge(0, 1, g);
    add_edge(1, 2, g);
    add_edge(2, 3, g);
    add_edge(3, 4, g);
    add_edge(4, 5, g);
    add_edge(6, 6, g);
    add_edge(4, 1, g);
    add_edge(0, 3, g);
    add_edge(2, 6, g);
    add_edge(1, 6, g);
    add_edge(1, 5, g);

    std::srand(time(0));
    auto gen_topo = []() -> string_table {
        auto ranch = [] { return std::string(1, rand()%26 + 'a'); };
        return {
            { ranch(), ranch(), ranch() },
            { ranch(), ranch(), ranch() },
            { ranch(), ranch(), ranch() },
        };
    };

    for (auto v : boost::make_iterator_range(boost::vertices(g))) {
        auto& data = g[v];
        data.id = v;
        data.topo = gen_topo();
    }

    std::cout << "out-degree: " << boost::out_degree(8, g);

    {
        std::ofstream dotfile;
        dotfile.open("dotgraph.txt");

        boost::dynamic_properties dp;

        dp.property("node_id", get(&VertexData::id, g));

        auto topo_attr = [&g](MyGraphType::vertex_descriptor const& v) {
            auto& vd = g[v];
            std::string s;
            for (auto& row : vd.topo) {
                for (auto& el : row) s += el + " ";
                s += '\n';
            }
            return s;
        };

        dp.property("toponodedist", boost::make_transform_value_property_map(topo_attr, get(boost::vertex_index, g)));

        boost::write_graphviz_dp(dotfile, g, dp);
    }
}

The dot-graph looks like:

graph G {
0 [toponodedist="q c i 
z a e 
o x c 
"];
1 [toponodedist="i u t 
c i l 
s k w 
"];
2 [toponodedist="z q m 
q j l 
n o t 
"];
3 [toponodedist="u k q 
g d u 
o c v 
"];
4 [toponodedist="u t s 
w d n 
r g x 
"];
5 [toponodedist="e y h 
b x z 
n q i 
"];
6 [toponodedist="a f y 
w z j 
m f o 
"];
7 [toponodedist="g v s 
d r l 
w o p 
"];
8 [toponodedist="l h x 
i m x 
s n v 
"];
0--1 ;
1--2 ;
2--3 ;
3--4 ;
4--5 ;
6--6 ;
4--1 ;
0--3 ;
2--6 ;
1--6 ;
1--5 ;
}

Demo Viz

Just using the label attribute instead of toponodedest there to make it show on a standard dot render:

enter image description here

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for such nice amendment in code. This code is very useful and will help me. But I am trying to store a vector(2 D) whose each index will store some variable string. The rows and columns of vector would not be predictable as these can varies accoding to data. Plz can you guide me that, is this possible to export/map 2D vector with its all values to dotfile of BGL. Because later I will try to read/import this vector (stored in dot file) to C program and use it for my purpose. I am trying best to get answer by myself. – Khan Oct 07 '17 at 21:22
  • @Khan there were really very few assumptions, but maaaaaybe you just want something that you can parse back. I'd suggest using JSON or something. Here's a proof of concept that checks roundtrips too, [not running on Coliru](http://coliru.stacked-crooked.com/a/98c030d89124158a), but here's an example of the output on my box: http://paste.ubuntu.com/25695854/ (or have 100 others: http://paste.ubuntu.com/25695872/) – sehe Oct 07 '17 at 23:03