Skip to content

Latest commit

 

History

History

924

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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
  • All the integers in initial are unique.

Companies: Google, Dropbox

Related Topics:
Array, Depth-First Search, Breadth-First Search, Union Find, Matrix

Solution 1. Union Find

Use UnionFind to count the infected nodes given initial array and a skipped initial node. Find the skipped initial node that results in minimum infected count.

// OJ: https://leetcode.com/problems/minimize-malware-spread/
// Author: github.com/lzl124631x
// Time: O(N^2 * logN)
// Space: O(N)
class UnionFind {
    vector<int> id, size;
public:
    UnionFind(int n) : id(n), size(n, 1) {
        for (int i = 0; i < n; ++i) id[i] = i;
    }
    void connect(int a, int b) {
        int x = find(a), y = find(b);
        if (x == y) return;
        id[x] = y;
        size[y] += size[x];
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
    int getSize(int a) {
        return size[find(a)];
    }
};
class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int N = graph.size(), ans = initial[0], minInfected = INT_MAX;
        sort(initial.begin(), initial.end());
        UnionFind uf(N);
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j < N; ++j) {
                if (graph[i][j]) uf.connect(i, j);
            }
        }
        for (int i = 0; i < initial.size(); ++i) {
            int cnt = 0; // M(initial) if we skip initial[i]
            unordered_set<int> s;
            for (int j = 0; j < initial.size(); ++j) {
                if (j == i) continue;
                int r = uf.find(initial[j]);
                if (s.count(r)) continue;
                s.insert(r);
                cnt += uf.getSize(r);
            }
            if (cnt < minInfected) {
                ans = initial[i];
                minInfected = cnt;
            }
        }
        return ans;
    }
};

Solution 2. Union Find

Find the initial node that belongs to the largest connected component with only one initial node in it.

If there is no such a node, return the smallest index of initial nodes.

// OJ: https://leetcode.com/problems/minimize-malware-spread/
// Author: github.com/lzl124631x
// Time: O(N^2 * logN)
// Space: O(N)
class UnionFind {
    vector<int> id, size;
public:
    UnionFind(int n) : id(n), size(n, 1) {
        iota(begin(id), end(id), 0);
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
    void connect(int a, int b) {
        int x = find(a), y = find(b);
        if (x == y) return;
        id[y] = x; // make sure find(a), i.e. x, is always used as the root
        size[x] += size[y];
    }
    int getSize(int a) { return size[find(a)]; }
};
class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& G, vector<int>& A) {
        sort(begin(A), end(A));
        int N = G.size(), ans = A[0], maxSize = 0;
        UnionFind uf(N);
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j < N; ++j) {
                if (G[i][j]) uf.connect(i, j); // make sure the smaller index is always used as root
            }
        }
        // Find the largest graph component with a single initial node
        unordered_map<int, int> m; // graph component root -> number of initial nodes in this graph component
        for (int i : A) m[uf.find(i)]++;
        for (int i : A) {
            int s = uf.getSize(i);
            if (m[uf.find(i)] == 1 && s > maxSize) {
                ans = i;
                maxSize = s;
            }
        }
        return ans;
    }
};

Solution 3. Component Coloring (DFS)

// OJ: https://leetcode.com/problems/minimize-malware-spread/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N)
class Solution {
    void dfs(vector<vector<int>> &graph, int start, int c, vector<int> &color) {
        color[start] = c;
        for (int i = 0; i < graph.size(); ++i) {
            if (graph[start][i] && !color[i]) dfs(graph, i, c, color);
        }
    }
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        unordered_set<int> init(initial.begin(), initial.end());
        sort(initial.begin(), initial.end());
        int N = graph.size(), c = 0, ans = initial[0], maxSize = 0;
        vector<int> color(N);
        for (int n : initial) {
            if (color[n]) continue;
            dfs(graph, n, ++c, color);
        }
        for (int i = 1; i <= c; ++i) {
            int cnt = 0, mal = INT_MAX, malCnt = 0;
            for (int j = 0; j < N; ++j) {
                if (color[j] == i) {
                    cnt++;
                    if (init.count(j)) {
                        mal = min(mal, j);
                        ++malCnt;
                    }
                }
            }
            if (malCnt == 1 && cnt > maxSize) {
                maxSize = cnt;
                ans = mal;
            }
        }
        return ans;
    }
};