2016-03-25 15 views
1

BGLを使用してグラフの計算を行う関数を書いています。計算がどのように行われるかは、グラフが指示されているかどうかによって異なりますが、無向グラフと有向グラフの2つの異なる関数を書くことは避けたいと思います。グラフは、グラフオブジェクト自体から導かれているか否かを確認する方法はありブーストグラフライブラリ:グラフが指示されているかどうかの確認

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

次のようにグラフの両方のタイプが定義されていますか?言い換えれば、グラフオブジェクトから、使用される "directedness"特性(すなわち、無向S、双対Sまたは有向S)を知る方法がありますか?

答えて

0

print_graph()の定義を見て答えを見つけました。is_directed()graph_traits.hppで定義したgraph_utility.hppにつながりました。

私はこれを行うには、よりエレガントな方法があるかもしれないと仮定しますが、ここでは最小限の作業例です:

正しく

>> g++ minimal_working_example.cpp -o minimal_working_example 
>> ./minimal_working_example 
The graph is undirected. 
The graph is directed. 
The 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; 
} 

関連する問題