算法

朋友圈问题

言七墨 · 11月1日 · 2020年 · · 1532次已读

Dhttps://qimok.cnFS

七墨博客
public int findCircleNum1(int[][] M) {
    // 是否访问过
    int[] visited = new int[M.length];
    int count = 0;
    for (int i = 0; i < M.length; i++) {
        if (visited[i] == 0) {
            // 遍历没有被访问过的横坐标,继续进行 dfs 扫描
            dfs(M, visited, i);
            count++;
        }
    }
    return count;
}

private void dfs(int[][] M, int[] visited, int i) {
    visited[i] = 1;
    for (int j = 0; j < M.length; j++) {
        if (M[i][j] == 1 && visited[j] == 0) {
            // 假如 i、j 是朋友且以 j 为横坐标的边的节点没有被访问过,直接进入 dfs
            dfs(M, visited, j);
        }
        // ...此时已经把能扩散的在同一个朋友圈的元素遍历完了
    }
}

BFhttps://qimok.cnS

public int findCircleNum2(int[][] M) {
    int[] visited = new int[M.length];
    int count = 0;
    Queue<Integer> queue = new LinkedList<>();
    for (int i = 0; i < M.length; i++) {
        if (visited[i] == 0) {
            queue.add(i);
            while (!queue.isEmpty()) {
                Integer x = queue.remove();
                visited[x] = 1;
                for (int j = 0; j < M.length; j++) {
                    if (M[x][j] == 1 && visited[j] == 0) {
                        queue.add(j);
                    }
                }
            }
            count++;
        }
    }
    return count;
}

并查集https://qimok.cn

class UF {

    // 连通分量个数
    private int count;
    // 存储一颗树(用 parent 数组记录每个节点的父节点,相当于指向父节点的指针,所以 parent 数组内实际存储着一个森林(若干棵多叉树))
    private int[] parent;
    // 记录树的`重量`(通过`重量`,可以将小树接到大树下面,以此来维持树的平衡性,防止在极端情况下退化成链表)
    private int[] size;

    public UF(int n) {
        this.count = n;
        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            // 初始化时,每个节点的父节点是自己本身,即自己指向自己
            parent[i] = i;
            size[i] = 1;
        }
    }

    public void union(int p, int q) {
        int rootP = findRoot(p);
        int rootQ = findRoot(q);
        if (rootP == rootQ) return;
        // 小树接到大树下面,较平衡
        if (size[rootP] > size[rootQ]) {
            parent[rootQ] = rootP;
            size[rootP] += size[rootQ];
        } else {
            parent[rootP] = rootQ;
            size[rootQ] += size[rootP];
        }
        count--;
    }

    public boolean connected(int p, int q) {
        int rootP = findRoot(p);
        int rootQ = findRoot(q);
        return rootP == rootQ;
    }

    private int findRoot(int x) {
        while (parent[x] != x) {
            // 在 findRoot 函数中进行路径压缩,保证任意树的高度保持在常数,使得 union 和 connected 方法的时间复杂度为 O(1)
            // 进行路径压缩(只要树的高度达到 3,就会进行路径压缩)
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

    public int count() {
        return count;
    }

}
public int findCircleNum3(int[][] M) {
    int n = M.length;
    UF uf = new UF(n);
    for (int i = 0; i < n; i++) {
        // 优化点:由于连接矩阵的朋友关系是沿左斜线对称的,故 j 只需要计算小于 i 的情况即可
        // i != j 的原因:因为自己跟自己肯定是朋友,即肯定连通,故不需要重复计算
        for (int j = 0; j < i; j++) {
            if (M[i][j] == 1) {
                uf.union(i, j);
            }
        }
    }
    return uf.count();
}
七墨博客

0 条回应