What does boost::out_edges( v, g ) in Boost.Graph do?

6.5k views Asked by At

I am not able to comprehend the documentation for this function, I have seen several times the following

tie (ei,ei_end) = out_edges(*(vi+a),g);

**g**<-graph
**vi**<-beginning vertex of graph
**a**<- a node
**ei and ei_end** <- edge iterators

What does the function return,and what does it do,when could I use?

Can I find all edges from a node for example?

2

There are 2 answers

5
sfrehse On BEST ANSWER

Provides iterators to iterate over the out-going edges of node u from graph g, e.g.:

  typename graph_traits < Graph >::out_edge_iterator ei, ei_end;
  for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
    auto source = boost::source ( *ei, g );
    auto target = boost::target ( *ei, g );
    std::cout << "There is an edge from " << source <<  " to " << target << std::endl;
  }

where Graph is your type definition of the graph an g is an instance of that. However, out_edges is only applicable for graphs with directed edges. The opposite of out_edges is in_edges that provides you iterators to compute in-coming edges of a node.

In an undirected graph both out_edges and in_edges will return all the edges connecting to the node in question.

However, more information can be easily found on http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/graph_concepts.html or just in the Boost.Graph examples/tests.

0
Rohin Mohanadas On

As explained above, for a directed graph, out_edges accepts a "vertex_descriptor and the graph(adjacency list) to be examined" and returns "all the edges that emanate (directed from) the given vertex_descriptor", by means of an iterator-range.

As described in https://www.boost.org/doc/libs/1_69_0/libs/graph/doc/adjacency_list.html

std::pair<out_edge_iterator, out_edge_iterator>
out_edges(vertex_descriptor u, const adjacency_list& g)

Returns an iterator-range providing access to the out-edges of vertex u in graph g. If the graph is undirected, this iterator-range provides access to all edges incident on vertex u. For both directed and undirected graphs, for an out-edge e, source(e, g) == u and target(e, g) == v where v is a vertex adjacent to u.

In short, to answer some of your questions,

  1. Yes, you can use it to find all edges from a node.
  2. For undirected graphs, the behavior is as explained in the link above, it returns all the edges incident on the vertex (all edges connected to it)