Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit822c91c

Browse files
authored
Merge pull requestTheAlgorithms#716 from MrYangxf/Development
Add an implementation of disjoint-set
2 parentsee8147c +01b0296 commit822c91c

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
packagesrc.main.java.com.dataStructures;
2+
3+
importjava.io.Serializable;
4+
importjava.util.*;
5+
6+
/**
7+
* An implementation of disjoint-set, represented by rooted trees.
8+
* <p>
9+
* Actually, the instance of {@link DisjointSet} is a disjoint-set forests.
10+
*
11+
* <p>
12+
* Disjoint-set operations:
13+
* <p>
14+
* 1. quickly unites two sets into a new set, requiring O(1) time.
15+
* <p>
16+
* 2. quickly query two elements whether contained in the same set, requiring about O(1) time.
17+
*
18+
* @author yangxf
19+
*/
20+
publicclassDisjointSet<T>implementsSerializable {
21+
privatestaticfinallongserialVersionUID =3134700471905625636L;
22+
23+
privateMap<T,Node<T>>nodeMap =newHashMap<>();
24+
25+
/**
26+
* Add an element to the disjoint-set forests as a set.
27+
*/
28+
publicvoidmakeSet(Telement) {
29+
checkNotNull(element,"element");
30+
nodeMap.putIfAbsent(element,newNode<>());
31+
}
32+
33+
/**
34+
* Unites the set that contains left and the set that contains right
35+
* into a new set with the union-by-rank heuristic.
36+
* <p>
37+
* Rank is an upper bound on the height of node.
38+
*/
39+
publicvoidunion(Tleft,Tright) {
40+
checkNotNull(left,"element");
41+
checkNotNull(right,"element");
42+
43+
Node<T>leftNode =nodeMap.get(left),
44+
rightNode =nodeMap.get(right);
45+
46+
if (leftNode ==null) {
47+
thrownewNoSuchElementException(left.toString());
48+
}
49+
50+
if (rightNode ==null) {
51+
thrownewNoSuchElementException(right.toString());
52+
}
53+
54+
Node<T>leftSet =findSet(leftNode),
55+
rightSet =findSet(rightNode);
56+
57+
if (leftSet ==rightSet) {
58+
return;
59+
}
60+
61+
if (leftSet.rank <rightSet.rank) {
62+
leftSet.parent =rightSet;
63+
}else {
64+
rightSet.parent =leftSet;
65+
if (leftSet.rank ==rightSet.rank) {
66+
leftSet.rank++;
67+
}
68+
}
69+
}
70+
71+
/**
72+
* Query two elements whether contained in the same set.
73+
*/
74+
publicbooleanisConnected(Tleft,Tright) {
75+
if (left ==null ||right ==null) {
76+
returnfalse;
77+
}
78+
79+
Node<T>leftNode =nodeMap.get(left);
80+
if (leftNode ==null) {
81+
returnfalse;
82+
}
83+
84+
Node<T>rightNode =nodeMap.get(right);
85+
if (rightNode ==null) {
86+
returnfalse;
87+
}
88+
89+
if (leftNode ==rightNode) {
90+
returntrue;
91+
}
92+
93+
returnfindSet(leftNode) ==findSet(rightNode);
94+
}
95+
96+
publicCollection<Set<T>>toSets() {
97+
Map<Node<T>,Set<T>>setMap =newHashMap<>();
98+
for (Map.Entry<T,Node<T>>entry :nodeMap.entrySet()) {
99+
setMap.computeIfAbsent(findSet(entry.getValue()),k ->newHashSet<>())
100+
.add(entry.getKey());
101+
}
102+
returnsetMap.values();
103+
}
104+
105+
publicvoidshow() {
106+
toSets().forEach(System.out::println);
107+
}
108+
109+
/**
110+
* Find the set that contains the node, actual is the first node of the set.
111+
* <p>
112+
* Backtracking with path compression.
113+
*/
114+
privateNode<T>findSet(Node<T>node) {
115+
if (node !=node.parent) {
116+
node.parent =findSet(node.parent);
117+
}
118+
returnnode.parent;
119+
}
120+
121+
privatestaticvoidcheckNotNull(Objectobj,Stringmsg) {
122+
if (obj ==null) {
123+
thrownewNullPointerException(msg +" must be not null");
124+
}
125+
}
126+
127+
staticclassNode<T> {
128+
intrank;
129+
Node<T>parent;
130+
131+
Node() {
132+
parent =this;
133+
}
134+
}
135+
136+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagesrc.test.java.com.dataStructures;
2+
3+
importorg.junit.Test;
4+
importsrc.main.java.com.dataStructures.DisjointSet;
5+
6+
importstaticorg.junit.Assert.*;
7+
8+
publicclassDisjointSetTest {
9+
@Test
10+
publicvoidtest() {
11+
DisjointSet<Object>set =newDisjointSet<>();
12+
13+
set.makeSet("flink");
14+
set.makeSet("c++");
15+
set.makeSet("java");
16+
set.makeSet("py");
17+
set.makeSet("spark");
18+
19+
set.union("java","c++");
20+
21+
assertTrue(set.isConnected("java","c++"));
22+
assertFalse(set.isConnected("java","py"));
23+
24+
set.union("c++","py");
25+
assertTrue(set.isConnected("java","py"));
26+
27+
set.makeSet("lisp");
28+
set.union("lisp","py");
29+
30+
assertTrue(set.isConnected("c++","lisp"));
31+
32+
set.show();
33+
}
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp