Imagine a computer network under cyber attack! You have a network of n nodes represented as an n x n adjacency matrix graph, where nodes are connected if graph[i][j] == 1.

The Attack Scenario: Some nodes in the initial array are already infected with malware. The malware spreads through direct connections - whenever two nodes are connected and at least one is infected, both become infected. This continues until no more nodes can be infected.

Your Mission: You can completely remove exactly one infected node (and all its connections) from the network before the malware spreads. Your goal is to minimize the final number of infected nodes M(initial).

Return: The index of the node to remove that minimizes the spread. If multiple nodes give the same minimum result, return the smallest index.

Input & Output

example_1.py โ€” Basic network
$ Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
โ€บ Output: 0
๐Ÿ’ก Note: Removing node 0 leaves only node 1 infected, which can infect node 2 through their connection, resulting in 1 infected node total. Removing node 1 would leave node 0 infected, which can still reach node 2, also resulting in 1 infected node. We return 0 (smaller index).
example_2.py โ€” Isolated components
$ Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
โ€บ Output: 0
๐Ÿ’ก Note: Nodes are isolated, so removing either infected node results in 1 remaining infected node. Return the smaller index (0).
example_3.py โ€” Bridge node removal
$ Input: graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
โ€บ Output: 1
๐Ÿ’ก Note: Node 1 acts as a bridge. Removing node 1 isolates node 0, leaving only node 0 infected (1 total). Removing node 0 leaves node 1 connected to nodes 2,3, resulting in 3 infected nodes total. Better to remove node 1.

Constraints

  • n == graph.length == 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 values in initial are unique

Visualization

Tap to expand
๐ŸงŸโ€โ™‚๏ธ Zombie Outbreak Prevention StrategyOriginal Outbreak๐ŸงŸโ€โ™‚๏ธ๐ŸงŸโ€โ™‚๏ธ๐Ÿ ๐Ÿ 4 buildings at risk!Strategy A: Demolish Building 0๐Ÿ’ฅ๐ŸงŸโ€โ™‚๏ธ๐ŸงŸโ€โ™‚๏ธ๐ŸงŸโ€โ™‚๏ธResult: 3 infectedStrategy B: Demolish Building 1๐ŸงŸโ€โ™‚๏ธ๐Ÿ’ฅ๐Ÿ ๐Ÿ Result: 1 infected (isolated!)๐ŸŽฏ Optimal StrategyDemolish Building 1โ€ข Breaks the bridge connectionโ€ข Isolates Building 0โ€ข Minimizes total infected: 1๐Ÿ’ก Key Insight: Strategic removal of bridge nodes can isolate infected areas!
Understanding the Visualization
1
Identify Infected Buildings
Start with multiple infected buildings (nodes) connected by bridges (edges)
2
Analyze Demolition Impact
For each infected building, simulate what happens if we demolish it completely
3
Calculate Spread
See how zombies spread from remaining infected buildings through connected bridges
4
Choose Optimal Demolition
Select the building whose removal results in minimum total infected buildings
Key Takeaway
๐ŸŽฏ Key Insight: The optimal strategy focuses on identifying and removing bridge nodes that connect multiple components. By analyzing the graph structure with Union-Find, we can efficiently determine which infected node's removal will minimize the total spread.
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 25
28.5K Views
Medium-High Frequency
~25 min Avg. Time
875 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