Loading...
Searching...
No Matches
UnionFind< IM > Class Template Reference
Data Structures » Auxiliary Data Structures

## Detailed Description

template<typename IM>
class lemon::UnionFind< IM >

The class implements the Union-Find data structure. The union operation uses rank heuristic, while the find operation uses path compression. This is a very simple but efficient implementation, providing only four methods: join (union), find, insert and size. For more features, see the UnionFindEnum class.

It is primarily used in Kruskal algorithm for finding minimal cost spanning tree in a graph.

See also
  kruskal()

Precondition
  You need to add all the elements by the insert() method.

`#include <lemon/unionfind.h>`

## Public Types

typedef IM | ItemIntMap
typedef ItemIntMap::Key | Item

## Public Member Functions

| UnionFind (ItemIntMap &m)
| Constructor.
int | find (const Item &a)
| Returns the index of the element's component.
void | clear ()
| Clears the union-find data structure.
int | insert (const Item &a)
| Inserts a new element into the structure.
bool | join (const Item &a, const Item &b)
| Joining the components of element a and element b.
int | size (const Item &a)
| Returns the size of the component of element a.

## Constructor & Destructor Documentation

## UnionFind()

template<typename IM>

UnionFind | ( | ItemIntMap & | m | ) |

inline

Constructor of the UnionFind class. You should give an item to integer map which will be used from the data structure. If you modify directly this map that may cause segmentation fault, invalid data structure, or infinite loop when you use again the union-find.

## Member Function Documentation

## find()

template<typename IM>

int find | ( | const Item & | a | ) |

inline

The method returns the index of the element's component. This is an integer between zero and the number of inserted elements.

## clear()

template<typename IM>

void clear | ( |  | ) |

inline

Erase each item from the data structure.

## insert()

template<typename IM>

int insert | ( | const Item & | a | ) |

inline

This method inserts a new element into the data structure.

The method returns the index of the new component.

## join()

template<typename IM>

bool join | ( | const Item & | a,
|  | const Item & | b )

inline

This is the union operation of the Union-Find structure. Joins the component of element a and component of element b. If a and b are in the same component then it returns false otherwise it returns true.

## size()

template<typename IM>

int size | ( | const Item & | a | ) |

inline

Returns the size of the component of element a.