Advertisement

Minimize Malware Spread II - LeetCode 928 Solution

Minimize Malware Spread II - Complete Solution Guide

Minimize Malware Spread II is LeetCode problem 928, 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 presents a network of nodes represented by an adjacency matrix, where `graph[i][j] = 1` indicates a direct connection between nodes `i` and `j`. Some nodes are initially infected with malware. The malware spreads through connected nodes until no further nodes can be infected. The goal is to find the single node from the `initial` set of infected nodes that, when removed, minimizes the total number of infected nodes after the spread stops. If multiple such nodes exist, the node with the smallest index should be returned.

Solution Approach

The provided solution iterates through each node in the 'initial' array, considering each as a candidate to be removed. For each candidate, it simulates the spread of malware from the remaining infected nodes using Breadth-First Search (BFS). It then counts the number of infected nodes after the spread. The candidate node that results in the minimum number of infected nodes is selected as the node to remove.

Step-by-Step Algorithm

  1. Step 1: Sort the 'initial' array to ensure that if multiple nodes result in the same minimum infected count, the smallest index will be chosen.
  2. Step 2: Initialize 'min_infected_count' to infinity and 'result_node' to the first element of the sorted 'initial' array. These variables will store the minimum infected count found so far and the corresponding node to remove.
  3. Step 3: Iterate through each node 'node_to_remove' in the 'initial' array.
  4. Step 4: For each 'node_to_remove', create a set 'initial_sources' containing the remaining initial infected nodes (excluding 'node_to_remove').
  5. Step 5: Perform a BFS starting from the nodes in 'initial_sources'. During the BFS, ensure that 'node_to_remove' is not visited or considered as a potential source of infection.
  6. Step 6: Keep track of the infected nodes in a 'infected' set. Each time a node is infected during the BFS, add it to this set.
  7. Step 7: After the BFS completes, count the number of nodes in the 'infected' set. This is the 'current_infected_count'.
  8. Step 8: Compare 'current_infected_count' with 'min_infected_count'. If 'current_infected_count' is smaller, update 'min_infected_count' with 'current_infected_count' and update 'result_node' with 'node_to_remove'.
  9. Step 9: After iterating through all nodes in 'initial', return 'result_node'.

Key Insights

  • Insight 1: Removing a node from 'initial' can prevent the spread of malware from that node, potentially reducing the total number of infected nodes.
  • Insight 2: Breadth-First Search (BFS) or Depth-First Search (DFS) can be used to simulate the malware spread effectively within the graph.
  • Insight 3: Sorting the 'initial' array allows for consistent selection of the smallest-indexed node when multiple nodes lead to the same minimum infection count.

Complexity Analysis

Time Complexity: O(n^2*m)

Space Complexity: O(n+m)

Topics

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

Companies

Asked at: Dropbox.