[英]How can I implement the Union-Find algorithm?
我正在嘗試實現Union-Find算法,但我查找的所有實現都使用整數。 我需要實現算法,以便我可以用這種方式調用union()和connected()方法:union(Vertex v,Vertex,w) - connected(Vertex v,Vertex w)
我已經嘗試調整我的算法來處理頂點,但我不知道如何替換父類並對屬性進行排名以使其工作。 請幫忙 :(
公共課UF {
private int[] parent; // parent[i] = parent of i
private byte[] rank; // rank[i] = rank of subtree rooted at i (never more than 31)
private int count; // number of components
/**
* Initializes an empty union–find data structure with {@code n} sites
* {@code 0} through {@code n-1}. Each site is initially in its own
* component.
*
* @param n the number of sites
* @throws IllegalArgumentException if {@code n < 0}
*/
public UF(int n) {
if (n < 0) throw new IllegalArgumentException();
count = n;
parent = new int[n];
rank = new byte[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
/**
* Returns the component identifier for the component containing site {@code p}.
*
* @param p the integer representing one site
* @return the component identifier for the component containing site {@code p}
* @throws IllegalArgumentException unless {@code 0 <= p < n}
*/
public int find(int p) {
validate(p);
while (p != parent[p]) {
parent[p] = parent[parent[p]]; // path compression by halving
p = parent[p];
}
return p;
}
/**
* Returns the number of components.
*
* @return the number of components (between {@code 1} and {@code n})
*/
public int count() {
return count;
}
/**
* Returns true if the the two sites are in the same component.
*
* @param p the integer representing one site
* @param q the integer representing the other site
* @return {@code true} if the two sites {@code p} and {@code q} are in the same component;
* {@code false} otherwise
* @throws IllegalArgumentException unless
* both {@code 0 <= p < n} and {@code 0 <= q < n}
*/
public boolean connected(int p, int q) {
return find(p) == find(q);
}
/**
* Merges the component containing site {@code p} with the
* the component containing site {@code q}.
*
* @param p the integer representing one site
* @param q the integer representing the other site
* @throws IllegalArgumentException unless
* both {@code 0 <= p < n} and {@code 0 <= q < n}
*/
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make root of smaller rank point to root of larger rank
if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ;
else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP;
else {
parent[rootQ] = rootP;
rank[rootP]++;
}
count--;
}
// validate that p is a valid index
private void validate(int p) {
int n = parent.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));
}
}
}
在標准算法中,每個頂點都有一個int
id,表示它在數組中的位置。 所以這意味着parent[0]
包含頂點0的父節點的id等等。
真的,你可以認為數組只是一個從int
到其他東西的非常有效的映射。 如果用更復雜的類型替換int
,則需要開始使用Map
而不是數組。
因此,如果您想使用名為Vertex
的類來表示頂點,那么您需要以不同方式聲明父級和排名:
Map<Vertex,Vertex> parent = new HashMap<>();
Map<Vertex,Rank> rank = new HashMap<>();
如果你想堅持當前的方案,你可以用Byte
替換Rank
- 盡管使用類可能是更好的封裝。
然后,您將得到類似於以下內容的代碼:
while (!vertex.equals(parent.get(vertex))) {
parent.put(vertex, parent.get(parent.get(vertex)));
vertex = parent.get(vertex);
}
return vertex;
需要注意的一點是,如果您要使用Vertex
作為地圖的關鍵(正如我所推薦的那樣),那么您必須實現equals
和hashCode
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.