Advertisement

Minimize Malware Spread - LeetCode 924 Solution

Minimize Malware Spread - Complete Solution Guide

Minimize Malware Spread is LeetCode problem 924, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 i

Detailed Explanation

The problem asks us to find the best node to remove from an initially infected set of nodes in a graph such that the final number of infected nodes after the malware spreads is minimized. The graph is represented as an adjacency matrix. Malware spreads from an infected node to all its directly connected neighbors, and this continues until no more nodes can be infected. We need to remove exactly one node from the initial infected set and return the node that results in the least number of infected nodes overall. If there are multiple such nodes, we return the node with the smallest index.

Solution Approach

The provided code uses the Disjoint Set Union (DSU) data structure to solve this problem. First, it builds the connected components of the graph using the DSU. Then, for each connected component, it counts the number of initially infected nodes belonging to it. Finally, it iterates through the initial infected nodes. For each node, it checks if removing it would prevent the spread of malware to a whole connected component (i.e., if the connected component has only one infected node). It chooses the node to remove that saves the largest connected component. If multiple nodes achieve the same savings, it picks the node with the smallest index.

Step-by-Step Algorithm

  1. Step 1: Initialize a DSU data structure (parent array and size array). The parent array initially points each node to itself (making it the root of its own set), and the size array is initialized to 1 for each node.
  2. Step 2: Iterate through the adjacency matrix and build the connected components using the `union` operation of the DSU. If `graph[i][j] == 1`, it means nodes `i` and `j` are connected, so we merge their sets.
  3. Step 3: Count the number of initially infected nodes in each connected component. Iterate through the `initial` array, find the root of each node using the `find` operation, and increment the count for that root in a `infected_counts` map.
  4. Step 4: Sort the `initial` array to ensure that if multiple nodes result in the same minimum infected nodes, we return the one with the smallest index.
  5. Step 5: Iterate through the sorted `initial` array. For each node, find its root and check if the `infected_counts` for that root is 1 (meaning it's the only infected node in that component). If it is, compare the size of that component with the current `max_saved_size`. If the component size is greater, update `max_saved_size` and the `result_node`.
  6. Step 6: Return the `result_node`.

Key Insights

  • Insight 1: The problem can be efficiently solved using the Disjoint Set Union (DSU) data structure to find connected components in the graph.
  • Insight 2: We need to count how many initially infected nodes belong to each connected component.
  • Insight 3: Removing a node from an initial set has a significant impact only if that node is the *only* infected node in its connected component. In that case, the entire connected component is saved from infection.
  • Insight 4: When choosing which node to remove, prioritize those within connected components having only one infected node and select the node associated with the largest component size.

Complexity Analysis

Time Complexity: O(N^2)

Space Complexity: O(N)

Topics

This problem involves: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Graph.

Companies

Asked at: DoorDash, Dropbox.