1

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)

user1443778
  • 581
  • 1
  • 5
  • 20

2 Answers2

2

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)));
}
sehe
  • 374,641
  • 47
  • 450
  • 633
1

Found the answer:

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

Where _reverse_graph is type Graph

user1443778
  • 581
  • 1
  • 5
  • 20