简体   繁体   中英

Having trouble with weighted and unweighted quick union finds

TLDR at bottom

I've been assigned a programming project at school to build a percolation model and i've come across an issue which has given me quite some confusion. First off, we were supposed to build an api to run a percolation simulation

public class Percolation{
private int grid[][];
public int size;
QuickFindUF unionFind;
//WeightedQuickUnionUF unionFind;


public Percolation(int n)
{

    if(n<1){
        throw new IllegalArgumentException ("grid must be larger than 0");
    }

    grid=new int[n][n];
    size=n;
    unionFind=new QuickFindUF(size*size);
    //unionFind=new WeightedQuickUnionUF(size*size);
    //initially set all to blocked
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            grid[i][j]=1;
        }
    }
}

public void open(int x, int y)
{
    grid[x][y]=0;

            //Check below to see if you can
            //if you are not on the bottom row
            if(y>0)
            {
                if(grid[x][y]==0 && grid[x][y-1]==0){unionFind.union(x+y*size,x+(y-1)*size);}
            }
            //check to see to the right (x->)
            if(x<size-1){
                if(grid[x][y]==0 && grid[x+1][y]==0){unionFind.union(x+y*size,x+1+y*size);}
            }
            //check if can union to the left
            if(x>0)
            {
                if(grid[x][y]==0 && grid[x-1][y]==0){unionFind.union(x+y*size,x-1+y*size);}

            }
            //check for above 
            if(y<size-1){
                if(grid[x][y]==0 && grid[x][y+1]==0){unionFind.union(x+y*size,x+(y+1)*size);}
            }

}

public boolean isOpen(int x, int y)
{
    if(x>=size || y>=size){return false;}
    if(grid[x][y]==0){return true;}
    return false;
}

public boolean isFull(int x, int y)
{
    if(x>=size || y>=size){return false;}//if input is out of bounds


    for(int i=0;i<size;i++){
        if(unionFind.connected(x+y*size,i+((size-1)*size)))
            return true;
    }


    return false;
}

public boolean percolates()
{
    for(int i=0;i<size;i++){
        for(int j=0;j<size;j++){
            if(unionFind.connected(i,(size-1)*size+j)){
                //System.out.println(i+" "+((size-1)*size+j));

                return true;
            }
        }
    }
    return false;
}
}

Now, the book kindly provides the quickfindUF and WeightedQuickUnionUF . All the classmates i've talked to have gotten the expected results when timing with a PercolationStats class which we've been instructed to make but my results very greatly. Here's the class

class PercolationStats{

private Percolation perc;
private double[] array;
private int expCount;

public PercolationStats(int gridSize, int numOfExperiments){
    if(gridSize <= 0 || numOfExperiments <=0)
        throw new IllegalArgumentException("gridSize and numOfExperiments needs to be more than 0");
    array=new double[numOfExperiments];
    expCount=numOfExperiments;

    for(int i=0;i<numOfExperiments;i++){
        perc=new Percolation(gridSize);
        int count=0;
        while(!perc.percolates()){
            int x=StdRandom.uniform(gridSize),y=StdRandom.uniform(gridSize);
            if(!perc.isOpen(x,y)){
                perc.open(x,y);
                count++;
            }
        }
        array[i]=(double) count/(gridSize*gridSize);
    }

}

public double mean(){
    return StdStats.mean(array);
}

public double stddev(){
    return StdStats.stddev(array);
}

public double confidenceLo(){
    return mean() - ((1.96 * stddev()) / Math.sqrt(expCount));
}

public double confidenceHi(){
    return mean()+((1.96 * stddev()) / Math.sqrt(expCount));
}

public static void main(String[] args){
    Stopwatch timer=new Stopwatch();
    PercolationStats percStats=new PercolationStats(200,100);
    System.out.println("mean: "+ percStats.mean() +"stddev: "+percStats.stddev()+" confidence Lo: "+percStats.confidenceLo()+" confidence hi: "+percStats.confidenceHi());
    System.out.println(timer.elapsedTime());
    percStats=new PercolationStats(200,100);
    System.out.println("mean: "+ percStats.mean() +"stddev: "+percStats.stddev()+" confidence Lo: "+percStats.confidenceLo()+" confidence hi: "+percStats.confidenceHi());
    percStats=new PercolationStats(2,100000);
    System.out.println("mean: "+ percStats.mean() +"stddev: "+percStats.stddev()+" confidence Lo: "+percStats.confidenceLo()+" confidence hi: "+percStats.confidenceHi());
}

}

When I run this with the QuickFindUF, at percStats(200,100), it takes me about 7 seconds, and if I run it at the same 200,100 with WeightedQuickUnionUF, It takes about 50+ seconds?? I was quite certain that the weighted quick union was supposed to be faster, and it's not just a matter of getting unlucky with my horrendous worst case random number generator. I ran it quite a few times and still the results were about the same and I've been staring here at the code for quite a while and can't figure out why my code is so wrong..

TLDR

Correct results, incorrect timing. Slower api is faster for some reason and I can't figure out why. QuickFindUF faster than WeightedQuickUnionUF. (about 7-8 times faster). What am I doing wrong?

Haha I'm dumb. I saw online that others were using a virtual top so I added one and now it works fine :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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