{"id":4844,"date":"2019-02-13T08:34:14","date_gmt":"2019-02-13T16:34:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4844"},"modified":"2019-02-13T08:40:27","modified_gmt":"2019-02-13T16:40:27","slug":"leetcode-133-clone-graph","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-133-clone-graph\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 133. Clone Graph"},"content":{"rendered":"\n<p>Given the head of a&nbsp;graph, return a deep copy (clone) of the graph. Each node in the graph contains a&nbsp;<code>label<\/code>&nbsp;(<code>int<\/code>) and a list (<code>List[UndirectedGraphNode]<\/code>) of its&nbsp;<code>neighbors<\/code>. There is an edge between the given node and each of the nodes in its neighbors.<br><strong>OJ&#8217;s undirected graph serialization (so you can understand error output):<\/strong><\/p>\n\n\n\n<p>Nodes are labeled uniquely.We use&nbsp;<code>#<\/code>&nbsp;as a separator for each node, and&nbsp;<code>,<\/code>&nbsp;as a separator for node label and each neighbor of the node.<\/p>\n\n\n\n<p>As an example, consider the serialized graph&nbsp;<code>{0,1,2#1,2#2,2}<\/code>.<\/p>\n\n\n\n<p>The graph has a total of three nodes, and therefore contains three parts as separated by&nbsp;<code>#<\/code>.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>First node is labeled as&nbsp;<code>0<\/code>. Connect node&nbsp;<code>0<\/code>&nbsp;to both nodes&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>2<\/code>.<\/li><li>Second node is labeled as&nbsp;<code>1<\/code>. Connect node&nbsp;<code>1<\/code>&nbsp;to node&nbsp;<code>2<\/code>.<\/li><li>Third node is labeled as&nbsp;<code>2<\/code>. Connect node&nbsp;<code>2<\/code>&nbsp;to node&nbsp;<code>2<\/code>&nbsp;(itself), thus forming a self-cycle.<\/li><\/ol>\n\n\n\n<p>Visually, the graph looks like the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\">       1\n      \/ \\\n     \/   \\\n    0 --- 2\n         \/ \\\n         \\_\/\n<\/pre>\n\n\n\n<p><strong>Note<\/strong>: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. You don&#8217;t need to understand the serialization to solve the problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Queue + Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(V+E)<br>Space complexity: O(V+E)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, 44ms, 16.8MB\nclass Solution {\npublic:\n  UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {\n    if (!node) return nullptr;\n    typedef UndirectedGraphNode Node;\n    unordered_set<Node*> done;\n    unordered_map<Node*, Node*> m;    \n    queue<Node*> q;\n    q.push(node);\n    while (!q.empty()) {\n      Node* s = q.front(); q.pop();\n      if (done.count(s)) continue;\n      done.insert(s);\n      if (!m.count(s))\n        m[s] = new Node(s->label);\n      Node* t = m[s];\n      for (Node* ss : s->neighbors) {\n        if (!m.count(ss))\n          m[ss] = new Node(ss->label);\n        q.push(ss);        \n        t->neighbors.push_back(m[ss]);\n      }\n    }    \n    return m[node];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the head of a&nbsp;graph, return a deep copy (clone) of the graph. Each node in the graph contains a&nbsp;label&nbsp;(int) and a list (List[UndirectedGraphNode]) of&#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":[77,82,213],"class_list":["post-4844","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-hashtable","tag-queue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4844","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=4844"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4844\/revisions"}],"predecessor-version":[{"id":4847,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4844\/revisions\/4847"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}