924. Minimize Malware Spread

In a network of nodes, each node i is directly connected to another node j if
and only if graph[i][j] = 1.

Some nodes initial are initially infected by malware.  Whenever two nodes are
directly connected and at least one of those two nodes is infected by malware,
both nodes will be infected by malware.  This spread of malware will continue
until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the
entire network, after the spread of malware stops.

We will remove one node from the initial list.  Return the node that if removed,
would minimize M(initial).  If multiple nodes could be removed to minimize M(initial),
return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it may
still be infected later as a result of the malware spread.



Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1]
Output: 1
  • Search BFS
public int minMalwareSpread(int[][] graph, int[] initial) {
    HashSet<Integer> vs = new HashSet<>();
    for(int v : initial) vs.add(v);
    int[] result = new int[graph.length];
    boolean[] visited = new boolean[graph.length];
    Arrays.fill(result, -1);
    for(int v : initial) {
        if(visited[v]) continue;
        Queue<Integer> q = new ArrayDeque<>();
        q.offer(v);
        visited[v] = true;
        int cnt = 0;
        ArrayList<Integer> virus = new ArrayList<>();
        while(!q.isEmpty()) {
            int curr = q.poll();
            if(vs.contains(curr)) {
                virus.add(curr);
            }
            cnt++;
            for(int j = 0; j < graph.length; j++) {
                if(graph[curr][j] == 1 && !visited[j]) {
                    q.offer(j);
                    visited[j] = true;
                }
            }
        }
        if(virus.size() == 1) result[v] = cnt;
    }
    int i = graph.length;
    for(int index : initial) i = Math.min(index, i);
    int maxCnt = result[i];
    for(int index : initial) {
        if(result[index] > maxCnt) {
            maxCnt = result[index];
            i = index;
        }
    }
    return i;
}
  • UnionFind
public int minMalwareSpread(int[][] graph, int[] initial) {
    UnionFind uf = new UnionFind(graph.length, initial);
    for(int i = 0; i < graph.length; i++) {
        for(int j = i + 1; j < graph.length; j++) {
            if(graph[i][j] == 1)
                uf.union(i, j);
        }
    }
    return uf.calculate();
}

class UnionFind {
    int[] arr; // the parent index of an element
    int[] vIndex; // -1 if no virus in this group, -2 if multiple virus, others are
                  // virus index of the only virus in this group
    int[] eCnt; // element count of a group
    HashSet<Integer> virus = new HashSet<>();
    public UnionFind(int n, int[] initial) {
        arr = new int[n];
        vIndex = new int[n];
        eCnt = new int[n];
        Arrays.fill(vIndex, -1);

        for(int i : initial) {
            virus.add(i);
            vIndex[i] = i; // virus element itself is a group with a single virus
        }
        for(int i = 0; i < n; i++) {
            arr[i] = i;
            eCnt[i] = 1;
        }
    }

    public int find(int x) {
        if(x != arr[x]) arr[x] = find(arr[x]);
        return arr[x];
    }

    public void union(int x, int y) {
        int i = find(x), j = find(y);
        if(i != j) {
            if(vIndex[j] == -2 || vIndex[i] == -2 || (vIndex[i] >= 0 && vIndex[j] >= 0)) vIndex[i] = -2; // -2 represents multiple virus in the combined group
            // this is caused by either of the two groups has multiple virus or each of them has only one virus
            else if(vIndex[j] >= 0) vIndex[i] = vIndex[j]; // only one virus in the two groups
            eCnt[i] += eCnt[j];
            arr[j] = i;
        }
    }

    public int calculate() {
        int index = -1;
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == i && vIndex[i] >= 0) {
                if(index == -1) index = i;
                else index = eCnt[i] > eCnt[index] ? i : index;
            }
        }

        if(index != -1) return vIndex[index]; // find a group with only one virus

        int min = arr.length;
        for(int i : virus) min = Math.min(min, i); // return the virus with min index
        return min;
    }
}