|
| 1 | +# Clone Graph |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/clone-graph/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +Given a reference of a node in a connected undirected graph. |
| 8 | + |
| 9 | +Return a deep copy (clone) of the graph. |
| 10 | + |
| 11 | +Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors. |
| 12 | + |
| 13 | +``` |
| 14 | +class Node { |
| 15 | + public int val; |
| 16 | + public List<Node> neighbors; |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +**Test case format:** |
| 21 | + |
| 22 | +For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list. |
| 23 | + |
| 24 | +An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph. |
| 25 | + |
| 26 | +The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph. |
| 27 | + |
| 28 | +### Constraints |
| 29 | + |
| 30 | +- The number of nodes in the graph is in the range [0, 100]. |
| 31 | +- 1 <= Node.val <= 100 |
| 32 | +- Node.val is unique for each node. |
| 33 | +- There are no repeated edges and no self-loops in the graph. |
| 34 | +- The Graph is connected and all nodes can be visited starting from the given node. |
| 35 | + |
| 36 | +### Example |
| 37 | + |
| 38 | +``` |
| 39 | +Input: adjList = [[2,4],[1,3],[2,4],[1,3]] |
| 40 | +Output: [[2,4],[1,3],[2,4],[1,3]] |
| 41 | +Explanation: There are 4 nodes in the graph. |
| 42 | +1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). |
| 43 | +2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). |
| 44 | +3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). |
| 45 | +4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). |
| 46 | +``` |
| 47 | + |
| 48 | +## Solution |
| 49 | + |
| 50 | +- Traverse all nodes |
| 51 | +- Make copies of nodes (use spread syntax?) |
| 52 | +- Return new node 1 |
| 53 | +- No nodes then return node? |
| 54 | +- Single node still need to make a copy |
| 55 | +- BFS with queue, will it get stuck in a loop? |
| 56 | + |
| 57 | +### Initial Pseudocode |
| 58 | + |
| 59 | +1. Make empty queue |
| 60 | +2. Create copy of first node |
| 61 | +3. Add copy to queue |
| 62 | +4. While queue isn't empty |
| 63 | +5. Shift from queue |
| 64 | +6. Create copy of node and add to stack |
| 65 | + |
| 66 | +### Optimized Solution |
| 67 | + |
| 68 | +The below is a recursive "DFS" approach to solving the problem with a time and space complexity of O(n). You can see an explanation of this approach here: https://www.youtube.com/watch?v=mQeF6bN8hMk A discussion about using DFS and BFS can be viewed here: https://leetcode.com/problems/clone-graph/discuss/1793212/C%2B%2Bor-Detailed-Explanation-w-DFS-and-BFS-or-Commented-code-with-extra-Test-Case |
| 69 | + |
| 70 | +```javascript |
| 71 | +const cloneGraph = function (node) { |
| 72 | + // Empty map for tracking which nodes are copied |
| 73 | + const map = {}; |
| 74 | + |
| 75 | + function dfs(node) { |
| 76 | + // Null case, covers the no-node case in the constraints |
| 77 | + if (!node) { |
| 78 | + return node; |
| 79 | + } |
| 80 | + // If the node does exist in the map, skip this block and return the already copied node |
| 81 | + if (!map[node.val]) { |
| 82 | + // If it's not in the map then make a new deep copy of the node |
| 83 | + map[node.val] = new Node(node.val); |
| 84 | + // Make copies of each of the neighbor nodes (recursively) |
| 85 | + map[node.val].neighbors = node.neighbors.map((n) => dfs(n)); |
| 86 | + } |
| 87 | + // After neighbors are processed on the node return it |
| 88 | + return map[node.val]; |
| 89 | + } |
| 90 | + |
| 91 | + return dfs(node); |
| 92 | +}; |
| 93 | +``` |
0 commit comments