You are given a network of n
nodes represented as an n x n
adjacency matrix graph
, where the ith
node is directly connected to the jth
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 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 that if a node was removed from the initial
list of infected nodes, it might still be infected later due to the malware spread.
Example 1:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] Output: 0
Example 2:
Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2] Output: 0
Example 3:
Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2] Output: 1
Constraints:
n == graph.length
n == graph[i].length
2 <= n <= 300
graph[i][j]
is 0
or 1
.graph[i][j] == graph[j][i]
graph[i][i] == 1
1 <= initial.length <= n
0 <= initial[i] <= n - 1
initial
are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for minimizing malware spread involves checking every possible way to isolate one of the initially infected computers. We essentially try removing each infected computer one by one and then simulate how the malware spreads from the remaining infected computers to see how bad the outbreak becomes.
Here's how the algorithm would work step-by-step:
def minimize_malware_spread_brute_force(graph, initial):
number_of_nodes = len(graph)
min_infected = number_of_nodes + 1
best_node = -1
for node_to_remove in initial:
# Try removing each initially infected node
infected_count = 0
visited = [False] * number_of_nodes
remaining_infected = [node for node in initial if node != node_to_remove]
queue = remaining_infected[:]
for infected_node in remaining_infected:
visited[infected_node] = True
while queue:
current_node = queue.pop(0)
infected_count += 1
# Standard BFS
for neighbor in range(number_of_nodes):
if graph[current_node][neighbor] == 1 and not visited[neighbor]:
visited[neighbor] = True
queue.append(neighbor)
# Compare current infection count and choose the best
if infected_count < min_infected:
min_infected = infected_count
best_node = node_to_remove
elif infected_count == min_infected and node_to_remove < best_node:
best_node = node_to_remove
# Handle the case where the initial list is empty
if best_node == -1:
return min(initial) if initial else 0
return best_node
The goal is to minimize the spread of malware by strategically vaccinating computers. The core idea is to identify groups of computers that are only connected to each other and then vaccinate the most crucial computer in each group to prevent the spread of malware within that group.
Here's how the algorithm would work step-by-step:
def minimize_malware_spread(graph, initial_infected): number_of_nodes = len(graph)
def depth_first_search(start_node, visited_nodes):
visited_nodes.add(start_node)
for neighbor in range(number_of_nodes):
if graph[start_node][neighbor] == 1 and neighbor not in visited_nodes:
depth_first_search(neighbor, visited_nodes)
components = []
visited = set()
for node in range(number_of_nodes):
if node not in visited:
component = set()
depth_first_search(node, component)
components.append(component)
visited |= component
influence = {}
for initial in initial_infected:
for component in components:
if initial in component:
for node in initial_infected:
if node not in component:
influence[initial] = influence.get(initial, 0) + 1
max_influence = 0
candidates = []
for initial in influence:
if influence[initial] > max_influence:
max_influence = influence[initial]
candidates = [initial]
elif influence[initial] == max_influence:
candidates.append(initial)
# Remove infected nodes that have a tie in influence
if len(candidates) == 1:
return candidates[0]
# Count nodes that are initially infected
single_node_components_count = 0
for initial in initial_infected:
is_single_node_component = True
for component in components:
if initial in component and len(component) > 1:
is_single_node_component = False
break
if is_single_node_component:
single_node_components_count += 1
# Ensure we return the smallest initial node
return min(initial_infected)
Case | How to Handle |
---|---|
Empty graph (no nodes or edges) | Return the initial infected node if graph is empty; if the initial infected is also empty return -1 |
No initial infected nodes | If no initial nodes are infected, return the node that would minimize spread by calculating its connected component size. |
All nodes are initially infected | If all nodes are initially infected, no further spread can occur, so return the smallest index initial infected node. |
Single connected component in the graph | If all nodes are reachable from each other, infecting any node minimizes spread to zero beyond the initially infected set. |
Graph with multiple disjoint connected components | The algorithm should correctly identify and analyze each connected component separately to minimize the overall spread. |
Large graph with many nodes and edges exceeding memory limits | Consider alternative data structures or algorithms (e.g., iterative deepening DFS) to reduce memory footprint or optimize space complexity. |
Integer overflow when calculating the size of connected components | Use a data type with sufficient capacity (e.g., long) to store the size of the components, or validate sizes to avoid overflow. |
Cycles present in the graph | The DFS or BFS implementation should correctly handle cycles to prevent infinite loops or incorrect connected component size calculation using a 'visited' set. |