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

2

7

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?

Phelps answered 11/11, 2014 at 20:30 Comment(2)
(vi+a) simply chooses a certain node.Passade
why not just write a instead of vi+a?Phelps
P
13

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.

Passade answered 11/11, 2014 at 20:38 Comment(5)
Gah. Can you be slightly less vague about "the edges"? Even in the sample you supposedly copied from somewhere, you fail to define where u came from, or what it might be. I can't think of anything besides this that the OP doesn't immediately understand (optionally mention different Graph representations)Cooker
u is the vertex from which you want to find edge iteratoes ei,and ei_end,ei will iterate till ei_end,and hence supply all the iterators to all edges coming from vertex 'u'.This is a way to access all edges from a vertex,am I right?Phelps
Yes @Phelps that's correct. Perhaps you want a documentation page that adds ("yes, you've read that correctly") to each page :) It's worth noting though, that out_edges are not all edges connected to a vertex, instead, it's the outgoing edges. This has everything to with the way edges are represented. You need an IncidenceGraph as documented here: boost.org/doc/libs/1_55_0/libs/graph/doc/graph_concepts.htmlCooker
Thansk for the comments. I'll fix your issues.Passade
what would happen in an undirectS graph, a friend of mine says it still works.Phelps
F
1

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)
Followup answered 3/4, 2019 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.