
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.
1using System;
2using System.Linq;
3
4public class UnionFind {
5 private int[] parent;
6 private int[] rank;
7
8 public UnionFind(int size) {
9 parent = new int[size];
10 rank = new int[size];
11 for (int i = 0; i < size; i++) {
12 parent[i] = i;
13 }
14 }
15
16 public int Find(int node) {
17 if (parent[node] != node) {
18 parent[node] = Find(parent[node]);
19 }
20 return parent[node];
21 }
22
23 public void Union(int node1, int node2) {
24 int root1 = Find(node1);
25 int root2 = Find(node2);
26 if (root1 != root2) {
27 if (rank[root1] > rank[root2]) {
28 parent[root2] = root1;
29 } else if (rank[root1] < rank[root2]) {
30 parent[root1] = root2;
31 } else {
32 parent[root2] = root1;
33 rank[root1]++;
34 }
35 }
36 }
37}
38
39public class Solution {
40 public int MinMalwareSpread(int[][] graph, int[] initial) {
41 int n = graph.Length;
42 var uf = new UnionFind(n);
43
44 for (int i = 0; i < n; i++) {
45 for (int j = 0; j < n; j++) {
46 if (graph[i][j] == 1) {
47 uf.Union(i, j);
48 }
49 }
50 }
51
52 Array.Sort(initial);
53
54 int[] infectedCount = new int[n];
55 foreach (var node in initial) {
56 infectedCount[uf.Find(node)]++;
57 }
58
59 int maxSaved = -1, bestNode = initial[0];
60 foreach (var node in initial) {
61 int root = uf.Find(node);
62 if (infectedCount[root] == 1) {
63 int saved = graph.Select((t, i) => uf.Find(i) == root ? 1 : 0).Sum();
64
65 if (saved > maxSaved) {
66 maxSaved = saved;
67 bestNode = node;
68 }
69 }
70 }
71
72 return bestNode;
73 }
74}The solution defines a C# implementation using union-find for directed components, sorted initial nodes, and infection status. Choosing the best removal node minimizes further infections, leveraging Array methods and LINQ for infection savings calculation.
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.
1function minMalwareSpread(graph, initial)
In JavaScript, this code employs DFS for component discovery. It tallies infective presence and decides best removal via infective reduction effect, while preserving ordering for minimum index assurance.