
Sponsored
Sponsored
One way to solve the problem is by using the Union-Find algorithm (also known as Disjoint Set Union or DSU) to identify connected components of the graph. Each component can either have one or more initially infected nodes. The objective is to remove a node such that the size of the initially infected nodes is minimized after malware spreads. This approach groups nodes together and calculates the effect of each removal by considering the infection spread of each distinct component.
Time Complexity: O(n2) due to graph iteration, where n is the number of nodes. The union-find operations are nearly O(1) on average.
Space Complexity: O(n) for the union-find data structures.
1import java.util.List;
2import java.util.ArrayList;
3import java.util.Collections;
4
5class UnionFind {
6 private int[] parent;
7 private int[] rank;
8
9 public UnionFind(int size) {
10 parent = new int[size];
11 rank = new int[size];
12 for (int i = 0; i < size; ++i)
13 parent[i] = i;
14 }
15
16 public int find(int node) {
17 if (parent[node] != node)
18 parent[node] = find(parent[node]);
19 return parent[node];
20 }
21
22 public void unionSets(int node1, int node2) {
23 int root1 = find(node1);
24 int root2 = find(node2);
25 if (root1 != root2) {
26 if (rank[root1] > rank[root2])
27 parent[root2] = root1;
28 else if (rank[root1] < rank[root2])
29 parent[root1] = root2;
30 else {
31 parent[root2] = root1;
32 rank[root1]++;
33 }
34 }
35 }
36}
37
38class Solution {
39 public int minMalwareSpread(int[][] graph, int[] initial) {
40 int n = graph.length;
41 UnionFind uf = new UnionFind(n);
42
43 for (int i = 0; i < n; i++) {
44 for (int j = 0; j < n; j++) {
45 if (graph[i][j] == 1) {
46 uf.unionSets(i, j);
47 }
48 }
49 }
50
51 List<Integer> initialList = new ArrayList<>();
52 for (int i : initial) initialList.add(i);
53 Collections.sort(initialList);
54
55 int[] infectedCount = new int[n];
56 for (int node : initialList) {
57 infectedCount[uf.find(node)]++;
58 }
59
60 int maxSaved = -1, bestNode = initialList.get(0);
61 for (int node : initialList) {
62 int root = uf.find(node);
63 if (infectedCount[root] == 1) {
64 int saved = 0;
65 for (int i = 0; i < n; ++i) {
66 if (uf.find(i) == root) ++saved;
67 }
68
69 if (saved > maxSaved) {
70 maxSaved = saved;
71 bestNode = node;
72 }
73 }
74 }
75
76 return bestNode;
77 }
78}Java implementation uses a Union-Find strategy to handle connected components. The sorting of initial infectivity nodes aids in choosing smaller indices. Counting infections per root, the function chooses a node to yield maximum saving nodes.
This approach uses Depth-First Search (DFS) or Breadth-First Search (BFS) to analyze the graph, identify connected components, and determine component-wise infections. The solution calculates how many vertices are infected by each initial node within its component and selects a node for removal that would minimize the spread as much as possible.
Time Complexity: O(n2) given graph traversal and sorting; Space Complexity: O(n) for BFS structures and tracking components.
1from collections import defaultdict, deque
2
This Python code uses BFS to find connected components and tags them. It processes components comparing infection counts and decides which node removal saves the most others from infection. Initial nodes are sorted for minimum index resolution on equal counts.