2

I'm using Boost Graph library in order to read a GraphML file. What I want to do is to use Boost capabilities of graph management to create my own dynamically allocated objects structure so that I can run my custom algorithm on it.

struct VertexProperties {
    std::string vertex_name;
    bool defect;
    bool node_logic;
    custom_node * node;
};

typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;

But the problem is that I can't seem to allocate memory for the custom_node pointer when using a Deep First Search custom visitor.

node = new custom_node(g[v].vertex_name, 0, standby, normal);

As I am getting "read-only" compilation errors.

I was wandering if there was a way to map the graph to something else where I could use dynamic allocation to re-create my graph structure ?

Trimmed main :

#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;
}

Thanks for your answers

accpnt
  • 91
  • 10
  • Can you make the sample self contained. I can write you a sample that works but it's a lot of work, and I already know it works. – sehe Oct 25 '17 at 09:45
  • Thx, I added the most significant parts to the message body – accpnt Oct 25 '17 at 10:17

1 Answers1

2

Ok, depht_first_visit must work on constant graphs. However, obviously constant graphs cannot be modified.

So, you want to tell your visitor a non-constant reference to your graph so you can modify through that.

I'd propose the following minimal change:

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 S{standby};
enum M{normal};

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));
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Out of curiosity, how do you make `read_graphml` work? I can't get it to recognize the node attributes – sehe Oct 25 '17 at 12:37
  • Wow, that does the trick. I had a hard time understanding the unique_ptr though. Thanks a lot ! – accpnt Oct 25 '17 at 13:54
  • There was no need for the unique_ptr, but I make it a matter of principle to remove unnecessary memory leaks from the code. – sehe Oct 25 '17 at 14:09
  • Do you have a sample graphml document that I could have used? I would really like to learn that, but the docs don't help me much. I'll look at the parsing code instead if you don't. – sehe Oct 25 '17 at 14:19
  • I started from here : [link](http://graphml.graphdrawing.org/primer/attributes.graphml). But for the moment I'm not done with changing the attributes. I used vertex_name, which is basically a copy of the name used in GraphML file for defining vertex and ' 0 ' for the boolean attribute that I use to create an object to another class. – accpnt Oct 26 '17 at 07:30
  • Oh god. I should have known that nothing would be lightweight right after I saw it was XML :) And the tooling is primarily Java-based. Cheers, got it to work with those `key` declarations indeed. – sehe Oct 26 '17 at 08:03
  • I had to use "normal" pointers for node on Boost 1.58. unique_ptr doesn't seem to work on this version. I didn't have any problem with your code in the 1.65 library. Sorry for the memory leaks – accpnt Oct 27 '17 at 13:10
  • That's not a Boost issue ([compiles with 1.58](https://wandbox.org/permlink/QNok2E74r3wxf7SY)). It's a compiler config issue: [gcc 4.8 c++11](https://wandbox.org/permlink/QCOuKLlKX8k95rkT) or [gcc 4.8 c++03](https://wandbox.org/permlink/QCOuKLlKX8k95rkT). Just use c++11, upgrade your compiler or use some other smart pointer: [scoped_ptr](https://wandbox.org/permlink/9i317fdOr4sc3uZl) or [shared_ptr](https://wandbox.org/permlink/R4EWoyPjUI3NNLBS) – sehe Oct 27 '17 at 13:19
  • Don't apologize for memory leaks: they're _your_ problem, not mine. And they're a problem. Fix them! Golden rule: _"Don't use `new` or `delete` in C++"_. Use [smart pointers](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one) – sehe Oct 27 '17 at 13:20