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?

sfrehse
  • 1,062
  • 9
  • 22
LoveMeow
  • 3,858
  • 9
  • 44
  • 66

2 Answers2

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
sfrehse
  • 1,062
  • 9
  • 22
  • 1
    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) – sehe Nov 11 '14 at 22:13
  • 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? – LoveMeow Nov 11 '14 at 22:51
  • Yes @LoveMeow 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: http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/graph_concepts.html – sehe Nov 11 '14 at 23:43
  • Thansk for the comments. I'll fix your issues. – sfrehse Nov 12 '14 at 07:32
  • what would happen in an undirectS graph, a friend of mine says it still works. – LoveMeow Nov 12 '14 at 09:24
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)