Algorithm


Problem Name: 924. Minimize Malware Spread

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.

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const minMalwareSpread = function (graph, initial) {
  const l = graph.length
  const p = []
  const children = []
  for (let i = 0; i  <  l; i++) {
    p[i] = i
    children[i] = [i]
  }

  for (let i = 0; i  <  l; i++) {
    for (let j = i + 1; j  <  l; j++) {
      if (graph[i][j] === 1) {
        const pi = find(i)
        const pj = find(j)
        if (pi !== pj) {
          union(pi, pj)
        }
      }
    }
  }

  initial.sort((a, b) => (a > b ? 1 : -1))

  const count = {}

  let index = initial[0]
  let max = 0
  // find the index that not unioned with other indexes and with the most number of children
  initial.forEach((e) => {
    const pe = find(e)
    if (!count[pe]) count[pe] = 0
    count[pe] += 1
  })
  initial.forEach((e, i) => {
    const pe = find(e)
    if (count[pe] === 1 && children[pe].length > max) {
      max = children[pe].length
      index = e
    }
  })

  return index

  function find(x) {
    while (p[x] !== x) {
      p[x] = p[p[x]]
      x = p[x]
    }
    return x
  }

  function union(pi, pj) {
    p[pj] = pi
    //also move the children to the new parent
    children[pi] = children[pi].concat(children[pj])
    children[pj] = []
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def minMalwareSpread(self, graph, initial):
        def dfs(i):
            nodes.add(i)
            for j in range(len(graph[i])):
                if graph[i][j] and j not in nodes:
                    dfs(j)
        rank, initial = collections.defaultdict(list), set(initial)
        for node in sorted(initial):
            nodes = set()
            dfs(node)
            if nodes & initial == {node}:
                rank[len(nodes)].append(node)
        return rank[max(rank)][0] if rank else min(initial)'
Copy The Code & Try With Live Editor

Input

x
+
cmd
graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Advertisements

Demonstration


Previous
#923 Leetcode 3Sum With Multiplicity Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#925 Leetcode Long Pressed Name Solution in C, C++, Java, JavaScript, Python, C# Leetcode