并查集
先给出并查集接口(具体做算法题的时候可以不用定义)
package com.ffyc.algorithm.并查集;
public interface UF {
int getSize();
void union(int p, int q);
boolean isConnected(int p ,int q);
}
最原始的并查集 : 基于集合(数组)实现
package com.ffyc.algorithm.并查集;
public class UnionFind implements UF{
private int [] ids;
public UnionFind(int size) {
ids = new int[size];
for(int i = 0; i < size; i++) {
ids[i] = i;
}
}
@Override
public int getSize() {
return ids.length;
}
public int getIds(int index) {
return ids[index];
}
@Override
public void union(int p, int q) {
int pId = getIds(p);
int qId = getIds(q);
if(pId != qId) {
for(int i = 0; i < ids.length; i++) {
if(getIds(i) == pId) {
ids[i] = qId;
}
}
}
}
@Override
public boolean isConnected(int p, int q) {
return getIds(p) == getIds(q);
}
}
基于多叉树(树结构)实现的并查集 :
package com.ffyc.algorithm.并查集;
public class UnionFindPlus implements UF{
private int [] parents;
private int count;
public UnionFindPlus(int size){
parents = new int[size];
for(int i = 0; i < size; i++) {
parents[i] = i;
count++;
}
}
@Override
public int getSize() {
return this.count;
}
public int getParent(int index){
if(index == parents[index]) {
return index;
}
return getParent(parents[index]);
}
@Override
public void union(int p, int q) {
int pParent = getParent(p);
int qParent = getParent(q);
if(pParent != qParent) {
parents[pParent] = qParent;
}
}
@Override
public boolean isConnected(int p, int q) {
return getParent(p) == getParent(q);
}
}
基于size做一个优化:
size是指某个子树上结点的个数
package com.ffyc.algorithm.并查集;
public class UnionFindSZ implements UF{
private int [] parents;
private int [] sz;
private int count;
public UnionFindSZ(int size){
parents = new int[size];
sz = new int[size];
for(int i = 0; i < size; i++) {
count++;
parents[i] = i;
}
for(int i = 0; i < size; i++){
sz[i] = 1;
}
}
@Override
public int getSize() {
return this.count;
}
public int getParent(int index){
if(index == parents[index]) {
return index;
}
return getParent(parents[index]);
}
@Override
public void union(int p, int q) {
int pParent = getParent(p);
int qParent = getParent(q);
if(pParent != qParent) {
if(sz[pParent] > sz[qParent]) {
parents[qParent] = pParent;
sz[pParent] += sz[qParent];
}else {
parents[pParent] = qParent;
sz[qParent] += sz[pParent];
}
count--;
}
}
@Override
public boolean isConnected(int p, int q) {
return getParent(p) == getParent(q);
}
}
- 但是其实我们是要高度低, 而结点数量并不一定就反映了树的高度
基于某个子树高度rank的优化:
package com.ffyc.algorithm.并查集;
public class UnionFindRank implements UF{
private int [] parents;
private int [] rank;
private int count;
public UnionFindRank(int size){
parents = new int[size];
rank = new int[size];
for(int i = 0; i < size; i++) {
count++;
parents[i] = i;
}
for(int i = 0; i < size; i++){
rank[i] = 1;
}
}
@Override
public int getSize() {
return this.count;
}
public int getParent(int index){
if(index == parents[index]) {
return index;
}
return getParent(parents[index]);
}
@Override
public void union(int p, int q) {
int pParent = getParent(p);
int qParent = getParent(q);
if(pParent != qParent) {
if(rank[pParent] > rank[qParent]) {
parents[qParent] = pParent;
}else if(rank[pParent] < rank[qParent]){
parents[pParent] = qParent;
}else if(rank[pParent] == rank[qParent]) {
parents[pParent] = qParent;
rank[qParent]++;
}
count--;
}
}
@Override
public boolean isConnected(int p, int q) {
return getParent(p) == getParent(q);
}
}
基于Rank的路径压缩(分多次压缩, 一次压一点):
package com.ffyc.algorithm.并查集;
public class UnionFindPC implements UF{
private int [] parents;
private int [] rank;
private int count;
public UnionFindPC(int size){
parents = new int[size];
rank = new int[size];
for(int i = 0; i < size; i++) {
count++;
parents[i] = i;
}
for(int i = 0; i < size; i++){
rank[i] = 1;
}
}
@Override
public int getSize() {
return this.count;
}
public int getParent(int index){
if(index == parents[index]) {
return index;
}
parents[index] = parents[parents[index]];
return getParent(parents[index]);
}
@Override
public void union(int p, int q) {
int pParent = getParent(p);
int qParent = getParent(q);
if(pParent != qParent) {
if(rank[pParent] > rank[qParent]) {
parents[qParent] = pParent;
}else if(rank[pParent] < rank[qParent]){
parents[pParent] = qParent;
}else if(rank[pParent] == rank[qParent]) {
parents[pParent] = qParent;
rank[qParent]++;
}
count--;
}
}
@Override
public boolean isConnected(int p, int q) {
return getParent(p) == getParent(q);
}
}
基于Rank的路径压缩(全压缩, 一次直接压缩到根节点):
package com.ffyc.algorithm.并查集;
public class UnionFindPCPlus implements UF{
private int [] parents;
private int [] rank;
private int count;
public UnionFindPCPlus(int size){
parents = new int[size];
rank = new int[size];
for(int i = 0; i < size; i++) {
parents[i] = i;
count++;
}
for(int i = 0; i < size; i++){
rank[i] = 1;
}
}
@Override
public int getSize() {
return this.count;
}
public int getParent(int index){
int temp = index;
while(index != parents[index]) {
index = parents[index];
}
parents[temp] = index;
return index;
}
@Override
public void union(int p, int q) {
int pParent = getParent(p);
int qParent = getParent(q);
if(pParent != qParent) {
if(rank[pParent] > rank[qParent]) {
parents[qParent] = pParent;
}else if(rank[pParent] < rank[qParent]){
parents[pParent] = qParent;
}else if(rank[pParent] == rank[qParent]) {
parents[pParent] = qParent;
rank[qParent]++;
}
count--;
}
}
@Override
public boolean isConnected(int p, int q) {
return getParent(p) == getParent(q);
}
}