Степень итератора вершин в BGL

Я пытаюсь удалить все узлы из моего графика (используя шаблон, определенный здесь), которые не имеют соединительных ребер. Мой (MWE) код до сих пор выглядит следующим образом:

//g++ -O3 question.cpp -o question.exe
#include <iostream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>

typedef long long node_id_t;

typedef boost::adjacency_list<
  boost::listS,           // Store out-edges of each vertex in a std::list
  boost::listS,           // Store vertex set in a std::list
  boost::bidirectionalS,  // The file dependency graph is directed
  boost::no_property,     // vertex properties
  boost::no_property      // edge properties
> AdjGraph;

typedef boost::labeled_graph<
  AdjGraph,
  node_id_t          // Node ID
> LabeledGraph;

int main(){
  LabeledGraph g;

  add_vertex( 10, g );
  add_vertex( 20, g );
  add_vertex( 30, g );
  add_vertex( 40, g );
  add_vertex( 50, g );

  boost::graph_traits<LabeledGraph>::vertex_iterator vi, vi_end, next;
  boost::tie(vi, vi_end) = boost::vertices(g);
  for (next = vi; vi != vi_end; vi = next) {
    ++next;
    if(boost::degree(*vi)==0)
      boost::remove_vertex(*vi, g);
  }
}

К сожалению, код выдает ошибку при компиляции с жалобой:

question.cpp:36:25: error: no matching function for call to ‘degree(void*&)’
 if(boost::degree(*vi)==0)

Я ожидаю, что vi будет vertex_iterator, и разыменование его должно дать мне действительный дескриптор. Я не уверен, почему этого не происходит.

Как я могу этого добиться?


person Richard    schedule 17.06.2015    source источник


Ответы (1)


Вам нужно передать дополнительный аргумент g в degree().

Кроме того, поскольку адаптер LabeledGraph не моделирует MutableGraph, вызов remove_vertex не может работать.

К счастью, вы можете получить базовый граф и изменить его. Я должен был бы прочитать о LabeledGraph, чтобы увидеть, есть ли побочные эффекты:

Жить на Coliru

// g++ -O3 question.cpp -o question.exe
#include <iostream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>

typedef long long node_id_t;

typedef boost::adjacency_list<boost::listS,          // Store out-edges of each vertex in a std::list
                              boost::listS,          // Store vertex set in a std::list
                              boost::bidirectionalS, // The file dependency graph is directed
                              boost::no_property,    // vertex properties
                              boost::no_property     // edge properties
                              > AdjGraph;

typedef boost::labeled_graph<AdjGraph,
                             node_id_t // Node ID
                             > LabeledGraph;

int main() {
    LabeledGraph g;

    add_vertex(10, g);
    add_vertex(20, g);
    add_vertex(30, g);
    add_vertex(40, g);
    add_vertex(50, g);

    boost::graph_traits<LabeledGraph>::vertex_iterator vi, vi_end, next;

    AdjGraph& underlying = g.graph();

    boost::tie(vi, vi_end) = boost::vertices(underlying);
    for (next = vi; vi != vi_end; vi = next) {
        ++next;
        if (boost::degree(*vi, underlying) == 0)
            boost::remove_vertex(*vi, underlying);
    }
}

Может быть, вы можете обойтись без LabeledGraph:

Жить на Coliru

// g++ -O3 question.cpp -o question.exe
#include <iostream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>

typedef long long node_id_t;

typedef boost::adjacency_list<boost::listS,          // Store out-edges of each vertex in a std::list
                              boost::listS,          // Store vertex set in a std::list
                              boost::bidirectionalS, // The file dependency graph is directed
                              node_id_t,             // vertex properties
                              boost::no_property     // edge properties
                              > AdjGraph;

int main() {
    AdjGraph g;

               add_vertex(10, g);
    auto v20 = add_vertex(20, g);
               add_vertex(30, g);
    auto v40 = add_vertex(40, g);
               add_vertex(50, g);

    add_edge(v40, v20, g);

    std::cout << "BEFORE:\n";
    print_graph(g, boost::get(boost::vertex_bundle, g));

    boost::graph_traits<AdjGraph>::vertex_iterator vi, vi_end, next;

    boost::tie(vi, vi_end) = boost::vertices(g);
    for (next = vi; vi != vi_end; vi = next) {
        ++next;
        if (boost::degree(*vi, g) == 0)
            boost::remove_vertex(*vi, g);
    }

    std::cout << "\n---\nAFTER:\n";
    print_graph(g, boost::get(boost::vertex_bundle, g));
}

Отпечатки:

BEFORE:
10 --> 
20 --> 
30 --> 
40 --> 20 
50 --> 

---
AFTER:
20 --> 
40 --> 20 
person sehe    schedule 17.06.2015