1
- # Disjoint-Set
1
+ # Disjoint-Sets
2
2
3
3
Some algorithms, such as finding connected components in undirected
4
4
graph and Kruskal's method of finding minimum spanning tree, require a
5
5
data structure that can efficiently represent a collection of disjoint
6
6
subsets. A widely used data structure for this purpose is the * Disjoint
7
- set forest* .
7
+ set forest* (disjoint sets) .
8
8
9
9
Usage:
10
10
11
11
``` julia
12
- a = IntDisjointSet (10 ) # creates a forest comprised of 10 singletons
12
+ a = IntDisjointSets (10 ) # creates a forest comprised of 10 singletons
13
13
union! (a, 3 , 5 ) # merges the sets that contain 3 and 5 into one and returns the root of the new set
14
14
root_union! (a, x, y) # merges the sets that have root x and y into one and returns the root of the new set
15
15
find_root! (a, 3 ) # finds the root element of the subset that contains 3
@@ -21,14 +21,14 @@ elem = push!(a) # adds a single element in a new set; returns the new e
21
21
One may also use other element types:
22
22
23
23
``` julia
24
- a = DisjointSet {AbstractString} ([" a" , " b" , " c" , " d" ])
24
+ a = DisjointSets {AbstractString} ([" a" , " b" , " c" , " d" ])
25
25
union! (a, " a" , " b" )
26
26
in_same_set (a, " c" , " d" )
27
27
push! (a, " f" )
28
28
```
29
29
30
- Note that the internal implementation of ` IntDisjointSet ` is based on
31
- vectors, and is very efficient. ` DisjointSet {T}` is a wrapper of
32
- ` IntDisjointSet ` , which uses a dictionary to map input elements to an
33
- internal index. Note for ` DisjointSet ` , ` union! ` , ` root_union! ` and
30
+ Note that the internal implementation of ` IntDisjointSets ` is based on
31
+ vectors, and is very efficient. ` DisjointSets {T}` is a wrapper of
32
+ ` IntDisjointSets ` , which uses a dictionary to map input elements to an
33
+ internal index. Note for ` DisjointSets ` , ` union! ` , ` root_union! ` and
34
34
` find_root! ` return the index of the root.
0 commit comments