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.
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.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.The key idea in #924 Minimize Malware Spread is to determine which initially infected node should be removed to minimize the final number of infected nodes in a graph. The network is represented as an adjacency matrix, meaning every node may belong to a connected component. If multiple infected nodes exist in the same component, removing one of them will not stop the infection because others will still spread it.
A common strategy is to identify connected components using Union-Find or Depth-First Search (DFS). After grouping nodes into components, compute the size of each component and track how many initially infected nodes belong to it. If a component has exactly one infected node, removing that node prevents the entire component from being infected.
The optimal candidate is the infected node that uniquely infects the largest component. If there is a tie, choose the node with the smallest index. These graph traversal and component grouping techniques help efficiently evaluate the infection impact. The overall complexity is typically O(n^2) due to the adjacency matrix traversal.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Union-Find (Disjoint Set) | O(n^2) | O(n) |
| DFS/BFS Connected Components | O(n^2) | O(n) |
NeetCodeIO
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.
1class UnionFind:
2 def __init__(self, size):
3 self.parent = list(range(size))
4 self
The solution defines a Union-Find class with find and union methods to manage node groups efficiently. The minMalwareSpread method initiates a Union-Find instance, groups nodes, counts infections per component, sorts initial infections, and finds the best node to remove based on the unique infections that save the most nodes from being infected.
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.
1function minMalwareSpread(graph, initial)
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, graph-based problems like Minimize Malware Spread are common in FAANG-style interviews. They test knowledge of graph traversal, connected components, and Union-Find data structures.
The optimal approach is to identify connected components using Union-Find or DFS. Then count how many initially infected nodes appear in each component and determine which removal prevents the largest component from being infected.
If a component has more than one infected node, removing just one will not stop the infection because the others will still spread malware. Only components with exactly one infected node can be fully protected by removing that node.
Union-Find (Disjoint Set Union) is commonly used to group nodes into connected components efficiently. It allows quick tracking of component sizes and helps determine which infected node uniquely affects a component.
In JavaScript, this code employs DFS for component discovery. It tallies infective presence and decides best removal via infective reduction effect, while preserving ordering for minimum index assurance.