Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use #
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
Solution:
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * List<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 11 if (node==null) return null; 12 13 Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap = new HashMap<UndirectedGraphNode,UndirectedGraphNode>(); 14 Set<UndirectedGraphNode> visited = new HashSet<UndirectedGraphNode>(); 15 cloneMap.put(node, new UndirectedGraphNode(node.label)); 16 17 return cloneGraphRecur(node,cloneMap,visited); 18 } 19 20 public UndirectedGraphNode cloneGraphRecur(UndirectedGraphNode cur, Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap, Set<UndirectedGraphNode> visited){ 21 UndirectedGraphNode curClone = cloneMap.get(cur); 22 visited.add(cur); 23 24 //Clone all the neighbors of cur node. 25 for (int i=0;i<cur.neighbors.size();i++){ 26 UndirectedGraphNode neigh = cur.neighbors.get(i); 27 if (cloneMap.containsKey(neigh)){ 28 curClone.neighbors.add(cloneMap.get(neigh)); 29 } else { 30 UndirectedGraphNode neighClone = new UndirectedGraphNode(neigh.label); 31 cloneMap.put(neigh,neighClone); 32 curClone.neighbors.add(neighClone); 33 } 34 } 35 36 //Visit all unvisited neighbors of cur node. 37 for (int i=0;i<cur.neighbors.size();i++) 38 if (!visited.contains(cur.neighbors.get(i))) 39 cloneGraphRecur(cur.neighbors.get(i),cloneMap,visited); 40 41 return curClone; 42 } 43 44 }
Solution (BFS):
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * List<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 11 if (node==null) return null; 12 13 Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap = new HashMap<UndirectedGraphNode,UndirectedGraphNode>(); 14 List<UndirectedGraphNode> visited = new ArrayList<UndirectedGraphNode>(); 15 cloneMap.put(node, new UndirectedGraphNode(node.label)); 16 visited.add(node); 17 int index = 0; 18 while (index<visited.size()){ 19 UndirectedGraphNode cur = visited.get(index); 20 UndirectedGraphNode curClone = cloneMap.get(cur); 21 22 //Clone all the neighbors of cur node. 23 for (int i=0;i<cur.neighbors.size();i++){ 24 UndirectedGraphNode neigh = cur.neighbors.get(i); 25 if (cloneMap.containsKey(neigh)){ 26 curClone.neighbors.add(cloneMap.get(neigh)); 27 } else { 28 UndirectedGraphNode neighClone = new UndirectedGraphNode(neigh.label); 29 cloneMap.put(neigh,neighClone); 30 curClone.neighbors.add(neighClone); 31 visited.add(neigh); 32 } 33 } 34 35 index++; 36 } 37 38 return cloneMap.get(node); 39 40 41 } 42 }