Selected Reading

SciPy - DisjointSet() Method



The SciPy DisjointSet() method is used to manage the data partition set into a disjoint subsets. It is used to manage the clusters in a heirarchical clustering algorithms.

Syntax

Following is the syntax of the SciPy DisjointSet() method −

DisjointSet([item_1, item_2, ...])

Parameters

This method accept the list as a parameter.

Return value

This method doesn't return any value.

Example

Following is the example that shows the usage of SciPy DisjointSet() method.

from scipy.cluster.hierarchy import DisjointSet

# create a disjoint-set data structure with 5 elements
ds = DisjointSet([1, 2, 3, 'a', 'b'])

# union operations 
ds.merge(1, 2)
ds.merge(3, 'a')
ds.merge('a', 'b')

# find the root elements
print(ds[2]) 
print(ds['b'])  

# test connectivity
print(ds.connected(1, 2))  
print(ds.connected(1, 'b')) 

# list elements in disjoint set
print(list(ds))  

# get the subset containing 'a'
print(ds.subset('a'))  

# get the size of the subset containing 'a'
print(ds.subset_size('a')) 

# get all subsets in the disjoint set
print(ds.subsets()) 

Output

The above code produces the following output −

1
3
True
False
[1, 2, 3, 'a', 'b']
{'a', 3, 'b'}
3
[{1, 2}, {'a', 3, 'b'}]
scipy_reference.htm
Advertisements