{"id":9775,"date":"2022-08-14T09:33:26","date_gmt":"2022-08-14T16:33:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9775"},"modified":"2022-08-14T20:41:29","modified_gmt":"2022-08-15T03:41:29","slug":"leetcode-133-clone-graph-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-133-clone-graph-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 133. Clone Graph"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 133. Clone Graph - \u5237\u9898\u627e\u5de5\u4f5c EP400\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/NCQMg-WMRZc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given a reference of a node in a\u00a0<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Connectivity_(graph_theory)#Connected_graph\" target=\"_blank\">connected<\/a><\/strong>\u00a0undirected graph.<\/p>\n\n\n\n<p>Return a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Object_copying#Deep_copy\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>deep copy<\/strong><\/a>&nbsp;(clone) of the graph.<\/p>\n\n\n\n<p>Each node in the graph contains a value (<code>int<\/code>) and a list (<code>List[Node]<\/code>) of its neighbors.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">class Node {\n    public int val;\n    public List<Node> neighbors;\n}\n<\/pre>\n\n\n\n<p><strong>Test case format:<\/strong><\/p>\n\n\n\n<p>For simplicity, each node&#8217;s value is the same as the node&#8217;s index (1-indexed). For example, the first node with&nbsp;<code>val == 1<\/code>, the second node with&nbsp;<code>val == 2<\/code>, and so on. The graph is represented in the test case using an adjacency list.<\/p>\n\n\n\n<p><strong>An adjacency list<\/strong>&nbsp;is a collection of unordered&nbsp;<strong>lists<\/strong>&nbsp;used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.<\/p>\n\n\n\n<p>The given node will always be the first node with&nbsp;<code>val = 1<\/code>. You must return the&nbsp;<strong>copy of the given node<\/strong>&nbsp;as a reference to the cloned graph.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/11\/04\/133_clone_graph_question.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> adjList = [[2,4],[1,3],[2,4],[1,3]]\n<strong>Output:<\/strong> [[2,4],[1,3],[2,4],[1,3]]\n<strong>Explanation:<\/strong> There are 4 nodes in the graph.\n1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).\n2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).\n3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).\n4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/01\/07\/graph.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> adjList = [[]]\n<strong>Output:<\/strong> [[]]\n<strong>Explanation:<\/strong> Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> adjList = []\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> This an empty graph, it does not have any nodes.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the graph is in the range&nbsp;<code>[0, 100]<\/code>.<\/li><li><code>1 &lt;= Node.val &lt;= 100<\/code><\/li><li><code>Node.val<\/code>&nbsp;is unique for each node.<\/li><li>There are no repeated edges and no self-loops in the graph.<\/li><li>The Graph is connected and all nodes can be visited starting from the given node.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS + Hashtable<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-1.png\" alt=\"\" class=\"wp-image-9776\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-2.png\" alt=\"\" class=\"wp-image-9777\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-3.png\" alt=\"\" class=\"wp-image-9778\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/08\/133-ep400-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\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\nclass Solution {\npublic:\n  Node* cloneGraph(Node* node) {\n    if (!node) return nullptr;\n    unordered_map<Node*, Node*> m;\n    function<void(Node*)> dfs = [&](Node* u) {\n      m[u] = new Node(u->val);\n      for (Node* v : u->neighbors) {\n        if (!m.count(v)) dfs(v);\n        m[u]->neighbors.push_back(m[v]);\n      }\n    };\n    dfs(node);\n    return m[node];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a reference of a node in a\u00a0connected\u00a0undirected graph. Return a&nbsp;deep copy&nbsp;(clone) of the graph. Each node in the graph contains a value (int) and&#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":[780,77,177],"class_list":["post-9775","post","type-post","status-publish","format-standard","hentry","category-graph","tag-clone","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9775","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=9775"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9775\/revisions"}],"predecessor-version":[{"id":9782,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9775\/revisions\/9782"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}