简体   繁体   English

路径压缩,这段代码是怎么路径压缩的?

[英]Path Compression , How does this code is path compression?

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package algorithm1;

/**
 *
* @author Navin
*/

public class QuickUnionWeighted {


private int [] id;
private int [] size;
int numberOfChild;
int j=0;

public QuickUnionWeighted(int N){

    id =  new int[N];
    size =  new int[N];

    for(int i=0;i<N;i++){

        id[i] = i;
        size[i]=1;
    }


}

 public int root(int i){

     while (i != id[i]){
         id[i] = id[id[i]];
         i=id[i];


     }

     return i;
 }   

 public boolean connected(int p,int q){

     return(root(p) == root(q));
}


 public void union(int p,int q){

     int i =  root(p);
     int j = root(q);

//         if(i == j) return;

     if(size[i] < size[j]){

         id[i] = j;
         size[j] += size[i];
     }else{
         id[j] = i;
         size[i] +=size[j];
     }


    for(int k=0;k<size.length;k++){
        System.out.print(size[k]+" ");

    } 

 }


public static void main(String [] args){


    QuickUnionWeighted quw = new QuickUnionWeighted(10);


    quw.union(3,0);
    quw.union(4, 0);
    quw.union(3, 5);
    quw.union(3, 6);
    quw.union(3,9);



}



}

Because as I examined the code, the id[i] = id[id[i]] is pointing to the parent of the node, and the examined node is not moved to its grandparents and the tree is not flattened.因为当我检查代码时, id[i] = id[id[i]]指向节点的父节点,并且检查的节点没有移动到它的祖父节点,树也没有展平。

Kindly Guide.请指导。

id[i] = id[id[i]];

This line is the path compression.这一行是路径压缩。 id[i] is the parent of node i . id[i]是节点i的父节点。 Hence, this line re-links node i to its grand-parent.因此,这条线将节点i重新链接到它的祖父节点。 Therefore, it skips the parent.因此,它跳过了父级。 Then, the same happens to the grand-parent and so on.然后,祖父母等也会发生同样的情况。 This flattens the tree.这会使树变平。

Here is a visualization of this step:这是这一步的可视化:

1                    1
^                   / \
|     root(3)      /   \
2    -------->   2       3
^
|
3

I know this is long due.我知道这是迟到的。 But in case anyone still needs this.但万一有人仍然需要这个。

Just as Nico said, this line is the path compression:正如 Nico 所说,这一行是路径压缩:

id[i] = id[id[i]];

But what it does is connect the node i directly to its grand-parent.但它所做的是将节点i直接连接到它的祖父节点。 It then skips the parent node, and the whole process is repeated for the grandparent onwards.然后它跳过父节点,并且从祖父节点开始重复整个过程。 Thereby flattening the tree.从而压平树。

However, to connect the node i directly to the root node, here is another implementation of path compression:但是,为了将节点i直接连接到根节点,这里是路径压缩的另一种实现:

id[i] = root(id[i]);

This creates a recursive function that goes down to the root node and sets every node on that path to point directly to the root node.这将创建一个递归函数,该函数向下到根节点,并将该路径上的每个节点设置为直接指向根节点。

Check https://algorithms.tutorialhorizon.com/disjoint-set-union-find-algorithm-union-by-rank-and-path-compression/ and page 9 of this material https://cseweb.ucsd.edu/classes/sp15/cse100-ab/lectures/Lec17_pdf.pdf检查https://algorithms.tutorialhorizo​​n.com/disjoint-set-union-find-algorithm-union-by-rank-and-path-compression/和本材料的第 9 页https://cseweb.ucsd.edu/classes /sp15/cse100-ab/lectures/Lec17_pdf.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM