|
| 1 | +/* |
| 2 | +Clone Graph |
| 3 | +https://leetcode.com/problems/clone-graph/description/ |
| 4 | +
|
| 5 | +Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the given node and each of the nodes in its neighbors. |
| 6 | +
|
| 7 | +
|
| 8 | +OJ's undirected graph serialization (so you can understand error output): |
| 9 | +Nodes are labeled uniquely. |
| 10 | +
|
| 11 | +We use # as a separator for each node, and , as a separator for node label and each neighbor of the node. |
| 12 | + |
| 13 | +
|
| 14 | +As an example, consider the serialized graph {0,1,2#1,2#2,2}. |
| 15 | +
|
| 16 | +The graph has a total of three nodes, and therefore contains three parts as separated by #. |
| 17 | +
|
| 18 | +First node is labeled as 0. Connect node 0 to both nodes 1 and 2. |
| 19 | +Second node is labeled as 1. Connect node 1 to node 2. |
| 20 | +Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle. |
| 21 | + |
| 22 | +
|
| 23 | +Visually, the graph looks like the following: |
| 24 | +
|
| 25 | + 1 |
| 26 | + / \ |
| 27 | + / \ |
| 28 | + 0 --- 2 |
| 29 | + / \ |
| 30 | + \_/ |
| 31 | +Note: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. |
| 32 | +You don't need to understand the serialization to solve the problem. |
| 33 | +*/ |
| 34 | + |
| 35 | +/** |
| 36 | + * Definition for undirected graph. |
| 37 | + * function UndirectedGraphNode(label) { |
| 38 | + * this.label = label; |
| 39 | + * this.neighbors = []; // Array of UndirectedGraphNode |
| 40 | + * } |
| 41 | + */ |
| 42 | + |
| 43 | + |
| 44 | +// SOLUTION 1 Using DFS |
| 45 | +/** |
| 46 | + * @param {UndirectedGraphNode} graph |
| 47 | + * @return {UndirectedGraphNode} |
| 48 | + */ |
| 49 | +var cloneGraph = function(graph) { |
| 50 | + if(!graph) |
| 51 | + return graph; |
| 52 | + |
| 53 | + return dfs(graph, {}); |
| 54 | +}; |
| 55 | + |
| 56 | +var dfs = function(graph, visited) { |
| 57 | + if(visited[graph.label]) |
| 58 | + return visited[graph.label]; |
| 59 | + |
| 60 | + var newNode = new UndirectedGraphNode(graph.label); |
| 61 | + visited[newNode.label] = newNode; |
| 62 | + |
| 63 | + for(var i = 0; i < graph.neighbors.length; i++) { |
| 64 | + const neighbor = dfs(graph.neighbors[i], visited); |
| 65 | + newNode.neighbors.push(neighbor); |
| 66 | + } |
| 67 | + |
| 68 | + return newNode; |
| 69 | +} |
| 70 | + |
| 71 | +// SOLUTION 2 Using DFS |
| 72 | +var cloneGraphBFS = function(graph) { |
| 73 | + if(graph === null) |
| 74 | + return graph; |
| 75 | + |
| 76 | + var visitedMap = {}; |
| 77 | + var queue = [graph]; |
| 78 | + var copyReturn = new UndirectedGraphNode(graph.label); |
| 79 | + visitedMap[graph.label] = copyReturn; |
| 80 | + |
| 81 | + while(queue.length > 0) { |
| 82 | + var node = queue.shift(); |
| 83 | + var nodeCopied = visitedMap[node.label]; |
| 84 | + |
| 85 | + for(var i = 0; i < node.neighbors.length; i++) { |
| 86 | + var neighbor = node.neighbors[i]; |
| 87 | + |
| 88 | + if(!visitedMap[neighbor.label]) { |
| 89 | + var copyNeighbor = new UndirectedGraphNode(neighbor.label); |
| 90 | + visitedMap[neighbor.label] = copyNeighbor; |
| 91 | + queue.push(neighbor); |
| 92 | + } |
| 93 | + |
| 94 | + nodeCopied.neighbors.push(visitedMap[neighbor.label]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return copyReturn; |
| 99 | +} |
0 commit comments