
Sponsored
Sponsored
One way to solve the problem is by using the Union-Find algorithm (also known as Disjoint Set Union or DSU) to identify connected components of the graph. Each component can either have one or more initially infected nodes. The objective is to remove a node such that the size of the initially infected nodes is minimized after malware spreads. This approach groups nodes together and calculates the effect of each removal by considering the infection spread of each distinct component.
Time Complexity: O(n2) due to graph iteration, where n is the number of nodes. The union-find operations are nearly O(1) on average.
Space Complexity: O(n) for the union-find data structures.
1#include <vector>
2#include <algorithm>
3
4class UnionFind {
5public:
6 UnionFind(int size) : parent(size), rank(size, 0) {
7 for (int i = 0; i < size; ++i)
8 parent[i] = i;
9 }
10
11 int find(int node) {
12 if (parent[node] != node)
13 parent[node] = find(parent[node]);
14 return parent[node];
15 }
16
17 void unionSets(int node1, int node2) {
18 int root1 = find(node1), root2 = find(node2);
19 if (root1 != root2) {
20 if (rank[root1] > rank[root2])
21 parent[root2] = root1;
22 else if (rank[root1] < rank[root2])
23 parent[root1] = root2;
24 else {
25 parent[root2] = root1;
26 ++rank[root1];
27 }
28 }
29 }
30
31private:
32 std::vector<int> parent;
33 std::vector<int> rank;
34};
35
36class Solution {
37public:
38 int minMalwareSpread(std::vector<std::vector<int>>& graph, std::vector<int>& initial) {
39 int n = graph.size();
40 UnionFind uf(n);
41
42 for (int i = 0; i < n; ++i) {
43 for (int j = 0; j < n; ++j) {
44 if (graph[i][j] == 1)
45 uf.unionSets(i, j);
46 }
47 }
48
49 std::sort(initial.begin(), initial.end());
50
51 std::vector<int> infectedCount(n, 0);
52 for (int node : initial)
53 ++infectedCount[uf.find(node)];
54
55 int maxSaved = -1, bestNode = initial[0];
56 for (int node : initial) {
57 int root = uf.find(node);
58 if (infectedCount[root] == 1) {
59 int saved = 0;
60 for (int i = 0; i < n; ++i)
61 if (uf.find(i) == root) ++saved;
62
63 if (saved > maxSaved) {
64 maxSaved = saved;
65 bestNode = node;
66 }
67 }
68 }
69
70 return bestNode;
71 }
72};The C++ solution features a class UnionFind managing connected components and a solution class implementing the problem solution. The code sorts initial nodes, counts infections, and determines the node removal for minimal widespread infection.
This approach uses Depth-First Search (DFS) or Breadth-First Search (BFS) to analyze the graph, identify connected components, and determine component-wise infections. The solution calculates how many vertices are infected by each initial node within its component and selects a node for removal that would minimize the spread as much as possible.
Time Complexity: O(n2) given graph traversal and sorting; Space Complexity: O(n) for BFS structures and tracking components.
1#include <stdio.h>
2
The C implementation applies DFS to mark nodes into components, computes the infected and component sizes, determines a node to knock off minimizing max infected. Sorting the nodes ensures minimal index prioritization.