You are given a network of n nodes represented as an n × n adjacency matrix graph, where the i-th node is directly connected to the j-th node if graph[i][j] == 1.

Some nodes in 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: If a node was removed from the initial list of infected nodes, it might still be infected later due to the malware spread.

Input & Output

Example 1 — Basic Network
$ Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0
💡 Note: Nodes 0 and 1 are connected, so removing either still results in both getting infected. Node 2 is isolated. Since both removals give same result, return smaller index 0.
Example 2 — Unique Infection Source
$ Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,1]
Output: 0
💡 Note: Each node is isolated. Removing node 0 saves that component, removing node 1 saves its component. Both save 1 node, return smaller index 0.
Example 3 — Large Component
$ Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [0,1]
Output: 0
💡 Note: All nodes are connected. Removing either 0 or 1 still results in all 3 nodes infected via the remaining initial node. Return smaller index 0.

Constraints

  • n == graph.length
  • n == graph[i].length
  • 2 ≤ n ≤ 300
  • graph[i][j] is 0 or 1
  • graph[i][i] == 1
  • 1 ≤ initial.length < n
  • 0 ≤ initial[i] < n
  • All integers in initial are unique

Visualization

Tap to expand
Minimize Malware Spread - Union-Find Approach INPUT Network Graph (3 nodes) 0 infected 1 infected 2 clean Adjacency Matrix: 1 1 0 1 1 0 0 0 1 initial = [0, 1] ALGORITHM STEPS 1 Build Union-Find Union connected nodes {0,1} {2} 2 Count Component Size Comp A: 2, Comp B: 1 3 Count Infected/Component Comp A has 2 infected Node | Component | Infected Count 0 | A | 2 1 | A | 2 4 Select Best Node Both nodes in same comp with 2 infected: return 0 FINAL RESULT After removing node 0: 0 removed 1 infected 2 clean Remove 0: M = 1 node Remove 1: M = 1 node Tie: Return smallest = 0 Output: 0 Key Insight: Union-Find groups connected nodes into components. If a component has multiple infected nodes, removing one won't help (others will still spread). Prioritize removing the sole infected node in the largest component. If tied, return smallest index. TutorialsPoint - Minimize Malware Spread | Union-Find with Component Analysis
Asked in
Google 25 Facebook 18 Amazon 12
28.4K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen