2008-09-24

How to use boost::disjoint_sets

The disjoint set is used in MST and many other algorithms. The boost library has implemented a convenient for us. It can be used like the following example,
typedef std::vector<SizeType> RankVector;
typedef RankVector::iterator RankRAIter;
typedef std::vector<VertexDescriptor> ParentVector;
typedef ParentVector::iterator ParentRAIter;

typedef boost::iterator_property_map<RankRAIter, IndexMap,
std::iterator_traits<RankRAIter>::value_type,
std::iterator_traits<RankRAIter>::reference> RankMap;

typedef boost::iterator_property_map<ParentRAIter, IndexMap,
std::iterator_traits<ParentRAIter>::value_type,
std::iterator_traits<ParentRAIter>::reference> ParentMap;

RankVector ranks( d_numOfVertices );
ParentVector parents( d_numOfVertices );
boost::disjoint_sets<RankMap, ParentMap> dset(
boost::make_iterator_property_map(
anks.begin(), boost::get( boost::vertex_index, g ), ranks[0] ),
boost::make_iterator_property_map(
parents.begin(), boost::get( boost::vertex_index, g ),
parents[0] )
);


After the disjoint set dset is construct, we can use
dset.make_set( u ); // make u a set
dset.link(u, v); // u and v are belonging to the same set.
dset.find_set( u ); // find the set owning u. A representative of the set is returned

沒有留言:

張貼留言