Union Find Set, Kotlin

private var parents_: IntArray? = null
    //记录触点的个数
    private var ranks_: IntArray? = null
    private var count = 0

    init {
        count = N
        //初始化分量数组
        parents_ = IntArray(count + 1)
        for (i in 0 until parents_!!.size) {
            parents_?.set(i,i)
        }
        ranks_ = IntArray(count + 1)
        //每个触点都是独立的一个分量,每个分量的大小都是1
        ranks_?.fill(1)
    }


    /**
     * merge sets that contains u and v
     * return true if merged, false if v and v are already in one set
     * */
    fun union(u: Int, v: Int): Boolean {
        val pu = find(u)
        val pv = find(v)
        if (pu == pv) {
            return false
        }
        var rankPV = ranks_?.get(pv) ?: -1
        var rankPU = ranks_?.get(pu) ?: -1
        //merge low ranks_ tree into high ranks_ tree
        if (rankPV > rankPU) {
            parents_?.set(pu, pv)
        } else if (rankPV < rankPU) {
            parents_?.set(pv, pu)
        } else {
            parents_?.set(pv, pu)
            rankPU++
            ranks_?.set(pu, rankPU)
        }
        return true
    }

    fun find(i: Int): Int {
        var index = i
        while (index != parents_?.get(index)) {
            //将i节点链接到其爷爷触点
            val p = parents_?.get(parents_?.get(index) ?: -1)
            if (p != null) {
                parents_?.set(index, p)
            }
            index = parents_?.get(index) ?: -1
        }
        return index
    }

 

posted @ 2020-02-14 01:05  johnny_zhao  阅读(159)  评论(0编辑  收藏  举报