Search⌘ K
AI Features

Minimize Malware Spread

Explore how to minimize malware spread in a network represented as a graph by applying the union find algorithm. Understand how connected components influence infection spread and learn to identify which infected node removal reduces total infections. This lesson guides you through solving this challenge with algorithm design and graph theory.

Statement

You’re given a network of nn nodes as an n×nn \times n adjacency matrix graph with the ithi^{th} node directly connected to the jthj^{th} node if graph[i][j] == 1.

A list of nodes, initial, is given, which contains nodes initially infected by malware. When two nodes are connected directly and at least one of them is infected by malware, both nodes will be infected by malware. This spread of malware will continue until every node in the connected component of nodes has been infected.

After the infection has stopped spreading, MM will represent the final number of nodes in the entire network that have been infected with malware.

Return a node from initial such that, when this node is removed from the graph, MM is minimized. If multiple nodes can be removed to minimize MM, return the node with the smallest index.

Note: If a node was removed from the initial list of infected nodes, it might still be infected later on due to the malware’s spread.

Constraints:

  • graph.length == graph[i].length
  • 22 \leq n 50\leq 50
  • graph[i][j] is 00 or 11.
  • graph[i][j] == graph[j][i]
  • graph[i][i] == 1
  • 11 \leq initial.length n\leq n
  • 00 \leq initial[i] n1\leq n - 1
  • All the integers in the initial are unique.

Examples

canvasAnimation-image
1 / 2

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Minimize Malware Spread

1.

Given the following graph and the initially infected nodes, which node will help minimize the malware spread?

graph = [[1, 1, 1],
                [1, 1, 0],
                [1, 0, 1]]

initial = [1, 2]

A.

0

B.

1

C.

2


1 / 2

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in MinimizeMalware.java in the following coding playground. You will need the provided supporting code to implement your solution.

Java
usercode > MinimizeMalware.java
/*
⬅️ We have provided a UnionFind.java file under the "Files" tab
of this widget. You can use this file to build your solution.
*/
import java.util.*;
class MinimizeMalware {
public static int minMalwareSpread(int[][] graph, int[] initial) {
// Replace this placeholder return statement with your code
return -1;
}
}
Minimize Malware Spread