2

BGL を使用してグラフの計算を実行する関数を作成しています。計算方法はグラフが有向か無向かによって異なりますが、無向グラフ用と有向グラフ用の 2 つの異なる関数を記述することは避けたいと思います。両方のタイプのグラフは次のように定義されます

using namespace boost;
// Undirected
typedef adjacency_list<listS, vecS, undirectedS> UGraph;
// Directed
typedef adjacency_list<listS, vecS, bidirectionalS> DGraph;

グラフ オブジェクト自体からグラフが指示されているかどうかを確認する方法はありますか? 言い換えれば、グラフ オブジェクトから、使用されている「有向性」プロパティ (つまり、undirectedS、bidiretionalS、またはdirectedS) を知る方法はありますか?

4

1 に答える 1

0

graph_utility.hppの関数の定義を見ているうちに答えが見つかりました。これにより、 graph_traits.hppで定義されている関数print_graph()にたどり着きました。is_directed()

これを行うにはもっとエレガントな方法があると思いますが、最小限の作業例を次に示します。

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>


template <typename graph_t>
void print_directedness(graph_t &g)
{
  // Typedef of an object whose constructor returns "directedness" of the graph object.
  typedef typename boost::graph_traits<graph_t>::directed_category Cat;
  // The function boost::detail::is_directed() returns "true" if the graph object is directed.
  if(boost::detail::is_directed(Cat()))
    std::cout << "The graph is directed." << std::endl;
  else
    std::cout << "The graph is undirected." << std::endl;
}


int main()
{

  // Creates an undirected graph and tests whether it is directed of not.
  boost::adjacency_list< boost::setS, boost::vecS, boost::undirectedS > UnGraph;
  print_directedness(UnGraph);

  // Creates a directed graph and tests whether it is directed of not.
  boost::adjacency_list< boost::setS, boost::vecS, boost::directedS > DiGraph;
  print_directedness(DiGraph);

  // Creates a bidirectional graph and tests whether it is directed of not.
  boost::adjacency_list< boost::setS, boost::vecS, boost::bidirectionalS > BidiGraph;
  print_directedness(BidiGraph);

  return 0;
}

正しく返す

>> g++ minimal_working_example.cpp -o minimal_working_example
>> ./minimal_working_example
The graph is undirected.
The graph is directed.
The graph is directed.
于 2016-05-17T16:34:57.870 に答える