C++ Boost

topological_sort

template <typename VertexListGraph, typename OutputIterator,
          typename P, typename T, typename R>
void topological_sort(VertexListGraph& g, OutputIterator result,
    const bgl_named_params<P, T, R>& params = all defaults)

トポロジカルソート・アルゴリズムは、例えば辺 (u,v) がグラフ中に現れる と、順序において uv の前に来るように頂点の線形の順序付けを 行う。グラフは非循環有向グラフ (DAG) でなければならない。実装は主に depth_first_search() への呼び出し によって成り立っている。

Where Defined:

boost/graph/topological_sort.hpp

Parameters

IN: VertexListGraph& g
非循環有向グラフ (DAG)。グラフの型は Vertex List Graph のモデルでなければならない。もしグラフが DAG でなければ、not_a_dag 例外が送出され、ユーザは result の範囲の内容を捨てるべきである。
OUT: OutputIterator result
グラフの頂点記述子はトポロジカル順序によって result 出力イテレータに 出力される。イテレータの型は Output Iterator のモデルでなければならない。

Named Parameters

UTIL/OUT: color_map(ColorMap color)
これはグラフを通る進行過程を保持するためにアルゴリズムによって使われる。 ColorMap 型は Read/Write Property Map のモデルでなければならず、かつキー型はグラフの頂点記述子型 でなければならず、またカラー・マップの値型は ColorValue をモデルとしなければならない。
デフォルト: サイズ num_vertices(g)default_color_typestd::vector から作られた iterator_property_mapで、添え字マップには i_map を用いる。
IN: vertex_index_map(VertexIndexMap i_map)
これは各頂点を [0, num_vertices(g)) の範囲において整数に マップする。 このパラメータはデフォルトのカラー・プロパティ・マップが使われた時にのみ必要である。 VertexIndexMap の型は Readable Property Map のモデルでなければならない。マップの値型は汎整数型でなければならない。 グラフの頂点記述子型はマップのキー型として使用できる必要がある。
デフォルト: get(vertex_index, g)

Complexity

時間複雑性は O(V + E) である。

Example

頂点のトポロジカル順序を計算する。

  typedef adjacency_list< vecS, vecS, directedS, color_property<> > Graph;
  typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
  Pair edges[7] = { Pair(0,1), Pair(2,4),
                    Pair(2,5),
                    Pair(0,3), Pair(1,4),
                    Pair(4,3), Pair(5,5) };
  Graph G(6, edges, edges + 7);

  typedef std::vector< Vertex > container;
  container c;
  topological_sort(G, std::back_inserter(c));

  cout << "A topological ordering: ";
  for ( container::reverse_iterator ii=c.rbegin(); ii!=c.rend(); ++ii)
    cout << index(*ii) << " ";
  cout << endl;
出力:
  A topological ordering: 2 5 0 1 4 3


Copyright © 2000-2001 Jeremy Siek, Indiana University (jsiek@osl.iu.edu)

Japanese Translation Copyright © 2003 Takashi Itou
オリジナルの、及びこの著作権表示が全ての複製の中に現れる限り、この文書の複製、利用、変更、販売そして配布を認める。このドキュメントは「あるがまま」に提供されており、いかなる明示的、暗黙的保証も行わない。また、いかなる目的に対しても、その利用が適していることを関知しない。

このドキュメントの対象: Boost Version 1.29.0
最新版ドキュメント (英語)