Algs4-1.5.16均摊成本的图像QuickFindUF、QuickUnionUF、WeightedQuickFindUF、WeightedQuickUnionUF

 1)QuickFindUF(1000点)

图片
2)QuickUnionUF(1000点)

图片
3)WeightedQuickFindUF(2000点)

图片
4)WeightedQuickUnionUF(1000点)
图片

CODE:
public class QuickFindUF
{
    private int[] id;
    private int count;
    //
    private int connTimes=0;
    private int cost=0;
    private int total=0;
   
    public int ConnTimes()
    {return connTimes;}
   
    public int Cost()
    {return cost;}
   
    public int Total()
    {return total;}
   
    //
    public QuickFindUF(int N)
    {
        count=N;
        id=new int[N];
        for (int i=0;i<N;i++)
             id[i]=i;
    }
   
    public int count()
    {return count;}
    
     public boolean connected(int p,int q)
     {
         connTimes++;
         cost=2;
         total=total+2;
         return find(p)==find(q);
     }

     public int find(int p)
     {return id[p];}
      
      public  void union(int p,int q)
       {
          int pID=find(p);
          int qID=find(q);
          //
          cost=cost+2;
          total=total+2;
          //
          if (pID==qID) return;
          for (int i=0;i<id.length;i++)
          {
              //
              cost++;
              total++;
              //
              if(id[i]==pID)
               {
                   id[i]=qID;
                   //
                   cost++;
                   total++;
                   //
              }
          }
          count--;
       }

    
       public static void main(String[] qrgs)
       {
           int N=StdIn.readInt();
           QuickFindUF uf=new QuickFindUF(N);
           StdDraw.setXscale(0,1000);
           StdDraw.setYscale(0,1000);
           StdDraw.setPenRadius(0.005);
           while (!StdIn.isEmpty())
           {
               //draw
               if (uf.ConnTimes()>0)
               {
               StdDraw.setPenColor(StdDraw.GRAY);
               StdDraw.point(uf.ConnTimes(),uf.Cost());
               //
               StdDraw.setPenColor(StdDraw.RED);
               StdDraw.point(uf.ConnTimes(),uf.Total()/uf.ConnTimes());
               }
               //
               int p=StdIn.readInt();
               int q=StdIn.readInt();
               if(uf.connected(p,q)) continue;

               StdOut.println(p+ " " +q);
               uf.union(p,q);
              
            }//end while
        }//end main
}//end class

public class QuickUnionUF
{
    private int[] id;
    private int count;
    //
    private int connTimes=0;
    private int cost=0;
    private int total=0;
   
    public int ConnTimes()
    {return connTimes;}
   
    public int Cost()
    {return cost;}
   
    public int Total()
    {return total;}
    //
    public QuickUnionUF(int N)
    {
        count=N;
        id=new int[N];
        for (int i=0;i<N;i++)
            id[i]=i;
   }
   
   public int count()
   {return count;}
    
   boolean connected(int p,int q)
   {
        connTimes++;
        cost=0;
       return find(p)==find(q);
   }


    public int find(int p)
     {
        cost++;
        total++;
        while(p!=id[p])
        {
            p=id[p];
            cost++;
            total++;
        }
         return p;
      }
     
      public void union(int p,int q)
      {
          int pRoot=find(p);
          int qRoot=find(q);
          if(pRoot==qRoot) return;
          id[pRoot]=qRoot;
          count--;
          cost++;
          total++;
      }

       public static void main(String[] qrgs)
       {
           int N=StdIn.readInt();
           QuickUnionUF uf=new QuickUnionUF(N);
           StdDraw.setXscale(0,1000);
           StdDraw.setYscale(0,1000);
           StdDraw.setPenRadius(0.005);
           while (!StdIn.isEmpty())
           {
               int p=StdIn.readInt();
               int q=StdIn.readInt();
                //draw
               if (uf.ConnTimes()>0)
               {
               StdDraw.setPenColor(StdDraw.GRAY);
               StdDraw.point(uf.ConnTimes(),uf.Cost());
               //
               StdDraw.setPenColor(StdDraw.RED);
               StdDraw.point(uf.ConnTimes(),uf.Total()/uf.ConnTimes());
               }
               //
               if(uf.connected(p,q)) continue;
               StdOut.println(p+ " " +q);
               uf.union(p,q);
              
            }//end while
        }//end main
}//end class


public class WeightedQuickFindUF
{
    private int[] id;
    private int[] sz;
    private int count;
     //
    private int connTimes=0;
    private int cost=0;
    private int total=0;
   
    public int ConnTimes()
    {return connTimes;}
   
    public int Cost()
    {return cost;}
   
    public int Total()
    {return total;}
    //
    public WeightedQuickFindUF(int N)
    {
        count=N;
        id=new int[N];
        for (int i=0;i<N;i++)
             id[i]=i;
        //
        sz=new int[N];
        for (int i=0;i<N;i++)
            sz[i]=1;
    }
   
    public int count()
    {return count;}
    
     boolean connected(int p,int q)
     {
         connTimes++;
         cost=2;
         total=total+2;
         return find(p)==find(q);
     }

     public int find(int p)
     {
         cost++;
         total++;
         return id[p];
     }
      
      public  void union(int p,int q)
       {
          int pID=find(p);
          int qID=find(q);
         
          if (pID==qID) return;
          int totalSize=sz[p]+sz[q];

          int oldID;
          int newID;
          cost=cost+4;
          total=total+4;
          if(sz[p]<sz[q])
            {
              oldID=pID;
              newID=qID;
           }
          else
           {
              oldID=qID;
              newID=pID;
          }
         for (int i=0;i<id.length;i++)
             {
                cost=cost+2;
                total=total+2;
                if(id[i]==oldID)
                 {
                    id[i]=newID;
                    cost++;
                    total++;
                }
                 if(id[i]==newID)
                 {
                     sz[i]=totalSize;
                     cost++;
                     total++;
                 }
              }
           count--;
      }
         //

       public static void main(String[] qrgs)
       {
           int N=StdIn.readInt();
           WeightedQuickFindUF uf=new WeightedQuickFindUF(N);
           //
           StdDraw.setXscale(0,2000);
           StdDraw.setYscale(0,2000);
           StdDraw.setPenRadius(0.005);
           while (!StdIn.isEmpty())
           {
               int p=StdIn.readInt();
               int q=StdIn.readInt();
                //draw
               if (uf.ConnTimes()>0)
               {
               StdDraw.setPenColor(StdDraw.GRAY);
               StdDraw.point(uf.ConnTimes(),uf.Cost());
               //
               StdDraw.setPenColor(StdDraw.RED);
               StdDraw.point(uf.ConnTimes(),uf.Total()/uf.ConnTimes());
               }
               //
               if(uf.connected(p,q)) continue;
               StdOut.println(p+ " " +q);
               uf.union(p,q);
            }//end while
        }//end main
}//end class


public class WeightedQuickUnionUF
{
    private int[] id;
    private int[] sz;
    private int count;
      //
    private int connTimes=0;
    private int cost=0;
    private int total=0;
   
    public int ConnTimes()
    {return connTimes;}
   
    public int Cost()
    {return cost;}
   
    public int Total()
    {return total;}
    //
    public WeightedQuickUnionUF(int N)
    {
        count=N;
        id=new int[N];
        for (int i=0;i<N;i++)
         {
            id[i]=i;
            StdOut.printf("%3d",i);
         }
         StdOut.println();
        //
        sz=new int[N];
        for (int i=0;i<N;i++)
            sz[i]=1;
        //
    }
   
     public int count()
     {return count;}
    
      boolean connected(int p,int q)
      {
         connTimes++;
         cost=0;
          return find(p)==find(q);
      }
    
      public int find(int p)
      {
          cost++;
          total++;
          while(p!=id[p])
          {
              cost++;
              total++;
              p=id[p];
          }
          return p;
      }
      
    
      public void union(int p,int q)
      {
          int i=find(p);
          int j=find(q);
          StdOut.printf(" i=%d j=%d\n",i,j);
          if(i==j) return;
          cost=cost+6;
          total=total+6;
          if(sz[i]<sz[j])
          {
              id[i]=j;
              sz[j]=sz[j]+sz[i];
           }
          else
          {
              id[j]=i;
              sz[i]=sz[i]+sz[j];
          }
          count--;
         }
      
       public static void main(String[] qrgs)
       {
           int N=StdIn.readInt();
           WeightedQuickUnionUF uf=new WeightedQuickUnionUF(N);
            //
           StdDraw.setXscale(0,1000);
           StdDraw.setYscale(0,1000);
           StdDraw.setPenRadius(0.005);
           //
           while (!StdIn.isEmpty())
           {
               int p=StdIn.readInt();
               int q=StdIn.readInt();
                 //draw
               if (uf.ConnTimes()>0)
               {
               StdDraw.setPenColor(StdDraw.GRAY);
               StdDraw.point(uf.ConnTimes(),uf.Cost());
               //
               StdDraw.setPenColor(StdDraw.RED);
               StdDraw.point(uf.ConnTimes(),uf.Total()/uf.ConnTimes());
               }
               //
               if(uf.connected(p,q)) continue;
                StdOut.printf("p=%d q=%d",p,q);
               uf.union(p,q);
            }//end while
        }//end main
}//end class

posted @ 2018-10-26 10:16  修电脑的龙生  阅读(432)  评论(0编辑  收藏  举报