{"id":4177,"date":"2018-10-13T19:58:42","date_gmt":"2018-10-14T02:58:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4177"},"modified":"2018-10-13T19:59:03","modified_gmt":"2018-10-14T02:59:03","slug":"leetcode-924-minimize-malware-spread","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-924-minimize-malware-spread\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 924. Minimize Malware Spread"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>In a network of nodes, each node\u00a0<code>i<\/code>\u00a0is directly connected to another node\u00a0<code>j<\/code>\u00a0if and only if\u00a0<code>graph[i][j] = 1<\/code>.<\/p>\n<p>Some nodes\u00a0<code>initial<\/code>\u00a0are initially infected by malware.\u00a0 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.\u00a0 This spread of malware will continue until no more nodes can be infected in this manner.<\/p>\n<p>Suppose\u00a0<code>M(initial)<\/code>\u00a0is the final number of nodes infected with malware in the entire network, after the spread of malware stops.<\/p>\n<p>We will\u00a0remove one node from the initial list.\u00a0 Return the node that if removed, would minimize\u00a0<code>M(initial)<\/code>.\u00a0 If multiple nodes could be removed to minimize\u00a0<code>M(initial)<\/code>, return such a node with the smallest index.<\/p>\n<p>Note that if a node was removed from the\u00a0<code>initial<\/code>\u00a0list of infected nodes, it may still be infected later as a result of the malware spread.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>graph = <span id=\"example-input-1-1\">[[1,1,0],[1,1,0],[0,0,1]]<\/span>, initial = <span id=\"example-input-1-2\">[0,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">0<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>graph = <span id=\"example-input-2-1\">[[1,0,0],[0,1,0],[0,0,1]]<\/span>, initial = <span id=\"example-input-2-2\">[0,2]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">0<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>graph = <span id=\"example-input-3-1\">[[1,1,1],[1,1,1],[1,1,1]]<\/span>, initial = <span id=\"example-input-3-2\">[1,2]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">1<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt; graph.length = graph[0].length &lt;= 300<\/code><\/li>\n<li><code>0 &lt;= graph[i][j] == graph[j][i] &lt;= 1<\/code><\/li>\n<li><code>graph[i][i] = 1<\/code><\/li>\n<li><code>1 &lt;= initial.length &lt; graph.length<\/code><\/li>\n<li><code>0 &lt;= initial[i] &lt; graph.length<\/code><\/li>\n<\/ol>\n<h1>Solution: BFS<\/h1>\n<p>Time complexity: O(n^3)<\/p>\n<p>Space complexity: O(n^2)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int minMalwareSpread(vector&lt;vector&lt;int&gt;&gt;&amp; graph, vector&lt;int&gt;&amp; initial) {\r\n    int min_affected = INT_MAX;\r\n    int min_node = -1;\r\n    sort(begin(initial), end(initial));\r\n    for (int t : initial) {\r\n      vector&lt;int&gt; bad(graph.size(), 0);\r\n      queue&lt;int&gt; q;\r\n      for (int n : initial)\r\n        if (n != t) {\r\n          bad[n] = 1;\r\n          q.push(n);\r\n        }\r\n      int affected = initial.size() - 1;\r\n      while (!q.empty()) {\r\n        int size = q.size();\r\n        while (size--) {\r\n          int n = q.front(); q.pop();\r\n          for (int i = 0; i &lt; graph[n].size(); ++i) {            \r\n            if (graph[n][i] == 0 || bad[i]) continue;\r\n            ++affected;            \r\n            bad[i] = 1;\r\n            q.push(i);\r\n          }\r\n        }\r\n      }      \r\n      if (affected &lt; min_affected) {\r\n        min_affected = affected;\r\n        min_node = t;\r\n      }\r\n    }    \r\n    return min_node;    \r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem In a network of nodes, each node\u00a0i\u00a0is directly connected to another node\u00a0j\u00a0if and only if\u00a0graph[i][j] = 1. Some nodes\u00a0initial\u00a0are initially infected by malware.\u00a0 Whenever&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[34,77,217],"class_list":["post-4177","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4177"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4177\/revisions"}],"predecessor-version":[{"id":4179,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4177\/revisions\/4179"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}