|
23 | 23 | \_/ |
24 | 24 | Hide Tags Depth-first Search Breadth-first Search Graph |
25 | 25 |
|
| 26 | + |
| 27 | +*/ |
| 28 | + |
| 29 | +/* |
| 30 | + //NEED TO RUN THIS ON LINT |
| 31 | + Thoughts: 12.12.2015 |
| 32 | + The original thoughs of using ArrayList, and using a index to track of which node has not been visited. |
| 33 | + It's alright, but it uses extra space, and basically copie all nodes again. |
| 34 | + It's similar to using a queue. |
| 35 | + At the end, it's doing O(m * n) |
| 36 | + Maybe can improve this. |
| 37 | +
|
| 38 | + Need a queue and process each element. and a hashmap to track duplicates. |
| 39 | + 1. make sure the node is no duplicate |
| 40 | + 2. make sure to all all child |
| 41 | +
|
| 42 | + border: case: node == nul, or node has not child, return a new instance of it'self? |
| 43 | +
|
| 44 | +*/ |
| 45 | + |
| 46 | +public class Solution { |
| 47 | + public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { |
| 48 | + if (node == null || node.neighbors.size() == 0) { |
| 49 | + return node; |
| 50 | + } |
| 51 | + |
| 52 | + HashMap<UndirectedGraphNode, UndirectedGraphNode> map = |
| 53 | + new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); |
| 54 | + Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); |
| 55 | + |
| 56 | + queue.offer(node); |
| 57 | + //process each node |
| 58 | + while (!queue.isEmpty()) { |
| 59 | + UndirectedGraphNode curr = queue.poll(); |
| 60 | + UndirectedGraphNode newNode; |
| 61 | + if (!map.containsKey(curr)) { |
| 62 | + map.put(curr, new UndirectedGraphNode(curr.label)); |
| 63 | + } |
| 64 | + UndirectedGraphNode newNode = map.get(curr); |
| 65 | + //Add neighbors for each node |
| 66 | + for (UndirectedGraphNode neighbor : curr.neighbors) { |
| 67 | + UndirectedGraphNode newNeighbor; |
| 68 | + if (!map.containsKey(neighbor)) { |
| 69 | + map.put(neighbor, new UndirectedGraphNode(neighbor.label)); |
| 70 | + } |
| 71 | + newNeighbor = map.get(neighbor); |
| 72 | + |
| 73 | + newNode.neighbors.add(newNeighbor); |
| 74 | + }//end for |
| 75 | + |
| 76 | + }//end while |
| 77 | + |
| 78 | + return map.get(node); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +/* |
| 85 | +
|
| 86 | + |
26 | 87 | Thinking process: |
27 | 88 | 1. Clone all nodes available: using HashMap to go through all possible query. No duplicates added using HashMap. |
28 | | - HashMap map has the list of all new nodes. No neighbors added yet |
29 | | - <key,value> = <original node, new node with just a label (without neighbor list)> |
30 | | - At same time, the arrayList nodes has all original nodes(with neighbors) in Breadth-first order. |
| 89 | + HashMap map has the list of all new nodes. No neighbors added yet |
| 90 | + <key,value> = <original node, new node with just a label (without neighbor list)> |
| 91 | + At same time, the arrayList nodes has all original nodes(with neighbors) in Breadth-first order. |
31 | 92 | 2. Add neighbor for nodes in map: |
32 | | - - Locate the 'newNode' from map by using the key: the original node |
33 | | - - loop through the original node's neighbor size |
34 | | - - use original neighbor as key to get the new neighbor instance from map |
35 | | - - add this new neighbor instance to the neighbor list of 'newNode' |
36 | | - |
| 93 | + - Locate the 'newNode' from map by using the key: the original node |
| 94 | + - loop through the original node's neighbor size |
| 95 | + - use original neighbor as key to get the new neighbor instance from map |
| 96 | + - add this new neighbor instance to the neighbor list of 'newNode' |
37 | 97 | */ |
38 | | - |
39 | 98 | /** |
40 | 99 | * Definition for undirected graph. |
41 | 100 | * class UndirectedGraphNode { |
|
0 commit comments