算法(Algorithms)第4版 练习 1.5.14

package com.qiusongde;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class UFWQuickUnionByHeight {

    private int[] id;//parent link(site indexed)
    private int[] treeheight;//size of component for roots(site indexed)
    private int count;//number of components
    
    public UFWQuickUnionByHeight(int N) {
        
        count = N;
        
        id = new int[N];
        for(int i = 0; i < N; i++) 
            id[i] = i;
        
        treeheight = new int[N];
        for(int i = 0; i < N; i++)
            treeheight[i] = 0;
        
    }
    
    public int count() {
        return count;
    }
    
    public boolean connected(int p, int q) {
        return find(p) == find(q);
    }
    
    public int find(int p) {
        
        int root = p;//initialize root
        
        //find root(id[p] save the parent of p)
        while(root != id[root])
            root = id[root];
        
        return root;
        
    }
    
    public void union(int p, int q) {
        
        int pRoot = find(p);
        int qRoot = find(q);
        
        if(pRoot == qRoot)
            return;
        
        //make smaller root point to larger one
        if(treeheight[pRoot] < treeheight[qRoot]) {
            id[pRoot] = qRoot;
        }
        else if(treeheight[pRoot] == treeheight[qRoot]) {
            id[qRoot] = pRoot;
            treeheight[pRoot]++; 
        }
        else {
            //treeheight[pRoot] > treeheight[qRott]
            id[qRoot] = pRoot;
        }
        
        count--;
        
    }
    
    @Override
    public String toString() {
        String s = "";
        
        for(int i = 0; i < id.length; i++) {
            s += id[i] + " ";
        }
        s += "\n";
        
        for(int i = 0; i < treeheight.length; i++) {
            s += treeheight[i] + " ";
        }
        s += "\n" + count + " components";
        
        return s;
    }
    
    public static void main(String[] args) {
        
        //initialize N components
        int N = StdIn.readInt();
        UFWQuickUnionByHeight uf = new UFWQuickUnionByHeight(N);
        StdOut.println(uf);
        
        while(!StdIn.isEmpty()) {
            
            int p = StdIn.readInt();
            int q = StdIn.readInt();
            
            if(uf.connected(p, q)) {//ignore if connected
                StdOut.println(p + " " + q + " is connected");
                StdOut.println(uf);
                continue;
            }
            
            uf.union(p, q);//connect p and q
            StdOut.println(p + " " + q);
            StdOut.println(uf);
        }
        
    }
    
}

 

 

运行结果:

0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
10 components
4 3
0 1 2 4 4 5 6 7 8 9
0 0 0 0 1 0 0 0 0 0
9 components
3 8
0 1 2 4 4 5 6 7 4 9
0 0 0 0 1 0 0 0 0 0
8 components
6 5
0 1 2 4 4 6 6 7 4 9
0 0 0 0 1 0 1 0 0 0
7 components
9 4
0 1 2 4 4 6 6 7 4 4
0 0 0 0 1 0 1 0 0 0
6 components
2 1
0 2 2 4 4 6 6 7 4 4
0 0 1 0 1 0 1 0 0 0
5 components
8 9 is connected
0 2 2 4 4 6 6 7 4 4
0 0 1 0 1 0 1 0 0 0
5 components
5 0
6 2 2 4 4 6 6 7 4 4
0 0 1 0 1 0 1 0 0 0
4 components
7 2
6 2 2 4 4 6 6 2 4 4
0 0 1 0 1 0 1 0 0 0
3 components
6 1
6 2 6 4 4 6 6 2 4 4
0 0 1 0 1 0 2 0 0 0
2 components
1 0 is connected
6 2 6 4 4 6 6 2 4 4
0 0 1 0 1 0 2 0 0 0
2 components
6 7 is connected
6 2 6 4 4 6 6 2 4 4
0 0 1 0 1 0 2 0 0 0
2 components

 

证明可参照P229页的Proposition H的证明。

posted @ 2017-03-17 11:11  我是老邱  阅读(294)  评论(0编辑  收藏  举报