0

I'm trying to use C++ boost to make a graph from an input file and currently I have vectors with vertex names and edge weights stored in them.

I know how to add vertices using:

typedef boost::graph_traits < Graph >::vertex_descriptor Vertex

Vertex v1 = add_vertex(string("v1"), g);
Vertex v2 = add_vertex(string("v2"), g);
Vertex v3 = add_vertex(string("v3"), g);

But how can I make it so that a new vertex will be created and added to the graph for each element in my vector?

  • psst. what vector? `for(auto s : v) { add_vertex(s, g); }`? – sehe Dec 12 '14 at 13:56
  • There's also quite some examples reading graphs from text files on this site. I know, because I've written quite some answers with them – sehe Dec 12 '14 at 13:58
  • @sehe, actually I may have worded my question poorly, but I was mainly wondering how to use the add_edge function which requires 2 vertex descriptors as parameters. – user3543260 Dec 12 '14 at 17:22
  • yeah that's surprising as you hardly mention edges. Also, you don't show any relevant types, so I hope you find my _pro-forma_ answer helpful. – sehe Dec 12 '14 at 19:52

1 Answers1

2

I'm going to have to do some guessing, but here goes:

for (auto& edge : my_edges_vector)
{
     vertex_descriptor sd = g[edge.source_vertex_id];
     vertex_descriptor td = g[edge.target_vertex_id];
     double weight = edge.weight;

     add_edge(g, sd, td, weight);
}
sehe
  • 374,641
  • 47
  • 450
  • 633