user1443778
user1443778

Reputation: 591

Get a copy of a boost graph that is reversed

I have a

typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, VertexInfo, EdgeInfo > Graph;

That I want a copy of with all edges reversed. I want a copy and not a view since I want to change the edge weights of this graph without modifying the original data structure.

boost::reverse_graph gives a view (with a different type)

Upvotes: 1

Views: 690

Answers (2)

sehe
sehe

Reputation: 393084

You can use make_reverse_graph:

E.g. input

Graph read() {
    Graph g;

    using namespace boost;
    dynamic_properties dp;
    dp.property("node_id", get(&VertexInfo::label, g));
    read_graphviz("digraph { a -> b; a -> c; b -> d; b -> e; e -> f; e -> c; }", g, dp);

    return g;
}

Graph:

enter image description here

Reversal:

int main() {
    using namespace boost;

    auto g = read();
    auto r = make_reverse_graph(g);

    write_graphviz(std::cout, r, make_label_writer(get(&VertexInfo::label, r)));
}

Graph

enter image description here

Full Demo

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <libs/graph/src/read_graphviz_new.cpp>

struct VertexInfo {
    std::string label;
};

struct EdgeInfo {
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexInfo, EdgeInfo> Graph;

Graph read() {
    Graph g;

    using namespace boost;
    dynamic_properties dp;
    dp.property("node_id", get(&VertexInfo::label, g));
    read_graphviz("digraph { a -> b; a -> c; b -> d; b -> e; e -> f; e -> c; }", g, dp);

    return g;
}

int main() {
    using namespace boost;

    auto g = read();
    auto r = make_reverse_graph(g);

    write_graphviz(std::cout, r, make_label_writer(get(&VertexInfo::label, r)));
}

Upvotes: 2

user1443778
user1443778

Reputation: 591

Found the answer:

boost::copy_graph(boost::make_reverse_graph(_graph), _reverse_graph);

Where _reverse_graph is type Graph

Upvotes: 1

Related Questions