Watch 10 video solutions for Minimize Malware Spread II, a hard level problem involving Array, Hash Table, Depth-First Search. This walkthrough by Pepcoding has 5,369 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node 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 exactly one node from initial, 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,1,0],[1,1,1],[0,1,1]], initial = [0,1] Output: 1
Example 3:
Input: graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1] Output: 1
Constraints:
n == graph.lengthn == graph[i].length2 <= n <= 300graph[i][j] is 0 or 1.graph[i][j] == graph[j][i]graph[i][i] == 11 <= initial.length < n0 <= initial[i] <= n - 1initial are unique.Problem Overview: You are given an undirected network represented by an adjacency matrix where some nodes are initially infected with malware. If two nodes are connected, infection spreads between them. You must remove exactly one initially infected node so the final number of infected nodes after the spread is minimized. If multiple choices give the same result, return the smallest index.
Approach 1: Brute Force Infection Simulation (O(k * n^2) time, O(n) space)
Try removing each node from the initial infected list one at a time. For every candidate removal, run a depth-first search or breadth-first search starting from the remaining infected nodes and simulate malware spread through the adjacency matrix. Count how many nodes end up infected. Track the removal that produces the smallest infected count. This approach directly models the spread but repeatedly traverses the graph, making it expensive when the number of infected nodes grows.
Approach 2: DFS Connected Components Analysis (O(n^2) time, O(n) space)
Instead of simulating spread repeatedly, analyze the structure of the graph. First identify connected components consisting only of clean nodes using DFS on the graph. For each component, track which infected nodes can reach it. If exactly one infected node connects to that component, removing that node prevents infection for the entire component. Sum the sizes of components uniquely controlled by each infected node and choose the node that saves the most vertices. This converts repeated spread simulation into a single component analysis pass.
Approach 3: Union-Find with Infected Tracking (O(n^2 * α(n)) time, O(n) space)
Use Union-Find to group all non-infected nodes into connected components. Iterate through the adjacency matrix and union clean nodes that share edges. Then examine edges between infected nodes and these clean components. For each component, count how many infected nodes can reach it. Components connected to exactly one infected node are "saved" if that infected node is removed. Accumulate the component sizes saved by each infected node and pick the best candidate. Union-Find keeps component size queries and merges efficient.
Recommended for interviews: The connected-component strategy (DFS or Union-Find) is the expected direction. Brute force simulation demonstrates understanding of the spread process, but interviewers typically look for the insight that only components uniquely infected by one node can be saved. Using DFS or Union-Find reduces repeated graph traversals and cleanly models the problem.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Infection Simulation | O(k * n^2) | O(n) | Good for understanding malware spread mechanics or small graphs |
| DFS Connected Components | O(n^2) | O(n) | Preferred when using graph traversal to analyze clean components |
| Union-Find with Infected Tracking | O(n^2 α(n)) | O(n) | Best when managing connected components efficiently with repeated merges |