1

I use the following code to initialize the graph denoted below:

enter image description here

#include <boost/config.hpp>
#include <iostream>
#include <fstream>

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;

typedef adjacency_list_traits<vecS, vecS, directedS> Traits;

typedef adjacency_list<
    vecS, vecS, directedS,

    property<
    vertex_name_t, std::string,
    property<vertex_index_t, int,
    property<vertex_color_t, boost::default_color_type,
    property<vertex_distance_t, double,
    property<vertex_predecessor_t, Traits::edge_descriptor>
    > > > >,

    property<
    edge_index_t, int,
    property<edge_capacity_t, double,
    property<edge_weight_t, double,
    property<edge_residual_capacity_t, double,
    property<edge_reverse_t, Traits::edge_descriptor>
> > > > >
Graph;

int main() {
    Graph g(4);

    property_map<Graph, edge_index_t>::type             E = get(edge_index, g);
    property_map<Graph, edge_weight_t>::type            cost = get(edge_weight, g);



    // Create edges
    Traits::edge_descriptor ed;

    int eindex = 0;

    ed = (add_edge(0, 1, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;


    ed = (add_edge(0, 2, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;

    ed = (add_edge(1, 3, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;

    ed = (add_edge(2, 3, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;

    //At this point, how can I change the cost of the second edge, (0,2), to 2 most efficiently?

}

After adding the four edges, initially with cost of 1, I would like to change the cost of the second edge, (0,2), to 2.

How do I obtain the appropriate edge descriptor most efficiently?

One method I tried was the following:

graph_traits < Graph >::out_edge_iterator ei, e_end;
for (tie(ei, e_end) = out_edges(0, g); ei != e_end; ++ei) {//search through out edges from vertex 0
    if (target(*ei, g) == 2) {//if the edge ends at vertex 2
        cost[*ei] = 2;
    }
}

This seems to be somewhat inefficient since it has to iterate through all edges starting at a node. Is there any method that is more efficient?

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • That's most efficient. The linked answer really beat it to death, even showing the adjacency matrix model which has the "direct" lookup. – sehe Sep 27 '17 at 08:09
  • @sehe, I saw your linked answer on my earlier thread that you posted just now. I am yet to fully go through and understand it. So, if I understand correctly, the method indicated in this post of iterating through the out edge iterators is the best possible? – Tryer Sep 27 '17 at 08:11
  • "That's most efficient". Seem clear to me. You can [read the older answer](https://stackoverflow.com/questions/45937710/accessing-specific-edges-in-boostgraph-with-integer-index) for more nuance. And examples – sehe Sep 27 '17 at 08:14
  • alright. I was confused since I could not fully understand your next sentence. "The linked answer really beat it to death, even showing the adjacency matrix model which has the "direct" lookup." Thanks again. – Tryer Sep 27 '17 at 08:15
  • I meant the topic was "beaten to death" as in treated exhaustively, at length, and with multiple approaches and backgrounds – sehe Sep 27 '17 at 08:20

0 Answers0