- Main Page
- Modules
- Classes
- Namespaces
- Files
- Related Pages

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
List of all members | Public Types | Public Member Functions
UnionFind< IM > Class Template Reference
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. More...
int | find (const Item &a)
| Returns the index of the element's component. More...
void | clear ()
| Clears the union-find data structure. More...
int | insert (const Item &a)
| Inserts a new element into the structure. More...
bool | join (const Item &a, const Item &b)
| Joining the components of element a and element b. More...
int | size (const Item &a)
| Returns the size of the component of element a. More...

## Constructor & Destructor Documentation

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

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.

void clear | ( |  | ) |
| inline

Erase each item from the data structure.

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.

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.

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

Returns the size of the component of element a.