28 Mayıs 2017 Pazar

graph out_edges metodu

Giriş
in_edges() metodu ile kardeştir.

Bir grap içinde birden çok iterator tipi bulunur. Bunlar vertex_iterator ve out_edge_iterator olabilir.
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
  VertexProperty, EdgeProperty > GraphType;

typedef typename boost::graph_traits< GraphType >::vertex_descriptor VertexType;
typedef typename boost::graph_traits<GraphType>::vertex_iterator VertexIterator;

typedef typename boost::graph_traits< GraphType >::edge_descriptor   EdgeType;
typedef typename boost::graph_traits<GraphType>::out_edge_iterator EdgeIterator;

out_edges metodu
Açıklaması şöyle. Bir out_edge_iterator çifti döndürür.
Iterators relate to a logical range, not the graph. Iterators may not be valid between different ranges of the same graph

Instead, descriptors are graph-wide. Depending on graph model, descriptors may be more stable: if an operation invalidates iterators, it doesn't necessarily invalidate the descriptors corresponding to the same graph elements.

In other words, this makes descriptors more usable as vertex or edge "ID" - or, as Boost Graph would call it, vertex_index or edge_index properties.
Örnek
Şöyle yaparız.
auto range1 = out_edges(vertex1, g);
auto range2 = out_edges(vertex2, g);

assert(range1.first != range2.first); // unspecified or undefined
Örnek
Elimizde şöyle bir Graph olsun.
class VertexInfo
{
public:
    VertexInfo(int i) : id(i) {}
    int id;
};

typedef boost::adjacency_list< boost::setS,
                               boost::listS,    
                               boost::bidirectionalS,
                               VertexInfo,
                               boost::no_property,
                               boost::no_property,
                               boost::listS
                             > Graph;
Bu graph'e vertex ve edge ekleyelim.
Graph g;

Vertex src  = boost::add_vertex(VertexInfo(0), g);
Vertex tar1 = boost::add_vertex(VertexInfo(5), g);
Vertex tar2 = boost::add_vertex(VertexInfo(2), g);
Vertex tar3 = boost::add_vertex(VertexInfo(8), g);

boost::add_edge(src, tar1, g);
boost::add_edge(src, tar2, g);
boost::add_edge(src, tar3, g);
Sonra 0'daki tüm edge'leri dolaşalım.
typename boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
for(boost::tie(ei, ei_end) = boost::out_edges(src, g); ei != ei_end; ++ei)
{
  std::cout << g[boost::source(*ei, g)].id 
            << " --> " 
            << g[boost::target(*ei, g)].id 
            << std::endl;
}
Çıktı olarak şunu alırız.
0 --> 5
0 --> 2
0 --> 8

Hiç yorum yok:

Yorum Gönder