Minimize Malware Spread
Master this topic with zero to advance depth.
Minimize Malware Spread
In a network of nodes, some nodes are initially infected with malware. Malware spreads through connected components. You can remove exactly one node from the initial list to minimize total infected nodes. If multiple nodes lead to the same result, return the node with the smallest index.
Optimization Strategy
Only removing an infected node in a component with exactly one initially infected node will actually reduce the total infection (by the size of that component).
Examples
Level II: DFS (Component Analysis)
Intuition
Use DFS to find all connected components in the graph. For each component, count how many nodes in initial are part of it. If only one node is infected, removing it saves all nodes in that component. Track the best node to remove.
Level III: Union-Find (Component Size Analysis)
Intuition
Build components using Union-Find and record the size of each. For each component, count how many nodes from initial are in it. If a component has exactly one initial node, moving that node saves size nodes. Pick the one that saves the most.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.