Minimize Malware Spread Problem
Description
LeetCode Problem 924.
You are given a network of n nodes represented as an n x n adjacency matrix graph, where the i^th node is directly connected to the j^th 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.
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 might still be infected later due to 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,2]
Output: 1
Constraints:
- n == graph.length
- n == graph[i].length
- 2 <= n <= 300
- graph[i][j] is 0 or 1.
- graph[i][j] == graph[j][i]
- graph[i][i] == 1
- 1 <= initial.length <= n
- 0 <= initial[i] <= n - 1
- All the integers in initial are unique.
Solution
Approach: Connected Components
Malware spreads to every node it can reach, so in the end each connected component is either fully infected (it contains at least one node of initial) or fully clean. Taking a node out of initial only matters if it makes its component clean, and that happens only when it is the only infected node there. If two infected nodes share a component, removing one changes nothing, because the other still infects all of it.
- Label every node with its component (DFS over the adjacency matrix) and record each component's size.
- Count how many nodes of
initialfall into each component. - For each node of
initialwhose component has exactly one infected node, removing it saves the whole component. Pick the node that saves the most, breaking ties by the smaller index. - If no node saves anything, every choice gives the same
M(initial), so return the smallest index ininitial.
The trap is step 4: initial is not sorted, so the default answer is min(initial), not initial[0].
Complexity: O(n2) time to scan the matrix and O(n) extra space, where n is the number of nodes.
class Solution:
def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
n = len(graph)
# Label every node with its connected component (iterative DFS).
component = [-1] * n
component_size = []
for start in range(n):
if component[start] != -1:
continue
label = len(component_size)
component[start] = label
size = 0
stack = [start]
while stack:
node = stack.pop()
size += 1
for neighbor in range(n):
if graph[node][neighbor] == 1 and component[neighbor] == -1:
component[neighbor] = label
stack.append(neighbor)
component_size.append(size)
# How many initially infected nodes each component contains.
infected_count = [0] * len(component_size)
for node in initial:
infected_count[component[node]] += 1
# Removing a node saves its component only if it is that component's only infected node.
best_node = min(initial)
best_saved = 0
for node in initial:
label = component[node]
if infected_count[label] == 1:
saved = component_size[label]
if saved > best_saved or (saved == best_saved and node < best_node):
best_node = node
best_saved = saved
return best_node
Now write it from memory
Close the write-up and solve it yourself against the tests.