928. Minimize Malware Spread II
928. Minimize Malware Spread II
(This problem is the same as Minimize Malware Spread, with the differences bolded.)
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, **completely removing it and any connections from this node to any other node.** 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.
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.
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,
**completely removing it and any connections from this node to any other node.**
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.
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
The key is to search on a graph with all bad nodes removed. Then count the bad neighbors of each group, and the group with a unique bad neighbors is a valid group. Add all valid groups of a bad node together. The bad node with the largest elements count is the result.
- Search BFS
public int minMalwareSpreadBFS(int[][] graph, int[] initial) {
int[] eCnt = new int[graph.length];
HashSet<Integer> vSet = new HashSet<>();
for(int v : initial) vSet.add(v);
boolean[] visited = new boolean[graph.length];
for(int i = 0; i < graph.length; i++) {
if(visited[i] || vSet.contains(i)) continue;
ArrayList<Integer> vs = new ArrayList<>();
Queue<Integer> q = new ArrayDeque<>();
int cnt = 0;
q.offer(i);
visited[i] = true;
while(!q.isEmpty()) {
int curr = q.poll();
if(vSet.contains(curr)) {
vs.add(curr);
continue; // don't add virus's neighbors
}
cnt++;
for(int j = 0; j < graph.length; j++) {
if(!visited[j] && graph[curr][j] == 1) {
visited[j] = true; // both good and virus nodes are visited
q.offer(j);
}
}
}
if(vs.size() == 1)
eCnt[vs.get(0)] += cnt; // find a valid group
for(int v : vs) // recover removed virus node (even with only one virus,
// example: [[1,1,0,0,0],[1,1,1,1,1],[0,1,1,0,0],[0,1,0,1,0],[0,1,0,0,1]], [0,1])
visited[v] = false;
}
System.out.println(Arrays.toString(eCnt));
int result = graph.length;
for(int i : initial) result = Math.min(result, i);
for(int i = result; i < graph.length; i++) {
if(eCnt[i] > eCnt[result])
result = i;
}
return result;
}
- 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, eCnt, vIndex;
HashSet<Integer>[] vs;
HashSet<Integer> vSet = new HashSet<>();
public UnionFind(int n, int[] initial) {
arr = new int[n];
eCnt = new int[n];
vs = new HashSet[n];
for(int v : initial) vSet.add(v);
for(int i = 0; i < n; i++) {
arr[i] = i;
eCnt[i] = 1;
vs[i] = new HashSet();
}
}
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);
boolean vi = vSet.contains(i), vj = vSet.contains(j);
if(vi && vj) return; // two virus
else if(!vi && !vj && i != j) {
arr[j] = i;
eCnt[i] += eCnt[j];
for(int v : vs[j]) vs[i].add(v);
} else if(vi) { // i is virus, j is not
vs[j].add(i);
} else if(vj) {
vs[i].add(j);
}
}
public int calculate() {
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < arr.length; i++) {
if(arr[i] == i && vs[i] != null && vs[i].size() == 1) {
Iterator it = vs[i].iterator();
int v = (int)it.next();
int cnt = map.getOrDefault(v, 0);
map.put(v, cnt+eCnt[i]);
}
}
int index = arr.length, cnt = 0;
for(Map.Entry<Integer, Integer> e : map.entrySet()) {
if(e.getValue() > cnt || (e.getValue() == cnt && e.getKey() < index)) {
index = e.getKey();
cnt = e.getValue();
}
}
if(index < arr.length) return index;
for(int v : vSet) index = Math.min(index, v);
return index;
}