Clone Graph
2017-09-13 21:51
295 查看
Clone an undirected graph. Each node in the graph contains a
a list of its
How we serialize an undirected graph:
Nodes are labeled uniquely.
We use
a separator for node label and each neighbor of the node.
As an example, consider the serialized graph
The graph has a total of three nodes, and therefore contains three parts as separated by
First node is labeled as
both nodes
Second node is labeled as
node
Third node is labeled as
node
java
python
labeland
a list of its
neighbors.
How we serialize an undirected graph:
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 node
0to
both nodes
1and
2.
Second node is labeled as
1. Connect node
1to
node
2.
Third node is labeled as
2. Connect node
2to
node
2(itself), thus forming a self-cycle
java
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * ArrayList<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { /** * @param node: A undirected graph node * @return: A undirected graph node */ public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { // write your code here if (node == null) { return node; } Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>(); // find all node HashSet<UndirectedGraphNode> set = findNode(node); // copy node for (UndirectedGraphNode root : set) { map.put(root, new UndirectedGraphNode(root.label)); } // copy node for (UndirectedGraphNode root : set) { UndirectedGraphNode newNode = map.get(root); for (UndirectedGraphNode neighbor : root.neighbors) { UndirectedGraphNode newNeighbor = map.get(neighbor); newNode.neighbors.add(newNeighbor); } } return map.get(node); } private HashSet<UndirectedGraphNode> findNode(UndirectedGraphNode node) { Queue<UndirectedGraphNode> queue = new LinkedList<>(); queue.offer(node); HashSet<UndirectedGraphNode> set = new HashSet<>(); set.add(node); while(!queue.isEmpty()) { UndirectedGraphNode root = queue.poll(); for (UndirectedGraphNode index : root.neighbors) { if (set.contains(index)) { continue; } else { queue.offer(index); set.add(index); } } } return set; } }
python
from Queue import Queue # Definition for a undirected graph node # class UndirectedGraphNode: # def __init__(self, x): # self.label = x # self.neighbors = [] class Solution: # @param node, a undirected graph node # @return a undirected graph node def __init__(self): self.dict = {} def cloneGraph(self, node): # write your code here if node is None: return node arr = self.findNode(node) mapping = {} for root in arr: mapping[root] = UndirectedGraphNode(root.label) for root in arr: for nei in root.neighbors: mapping[root].neighbors.append(mapping[nei]) return mapping[node] def findNode(self, node): queue = Queue() queue.put(node) arr = set([node]) while not queue.empty(): node = queue.get() for nei in node.neighbors: if nei in arr: continue else: arr.add(nei) queue.put(nei) return arr
相关文章推荐
- 算法分析与设计课程(18):【leetcode】Clone Graph
- LeetCode:Clone Graph
- Clone Graph [LeetCode]
- Leetcode: Clone Graph
- Leetcode: Clone Graph
- 图的深拷贝 Clone Graph
- 算法(六)Clone Graph
- leetCode解题报告之Clone Graph
- leetcode之Clone Graph
- LeetCode_Clone Graph
- Clone Graph
- Clone Graph leetcode java(DFS and BFS 基础)
- [LeetCode] Clone Graph
- Clone Graph
- leetcode [Clone Graph] Java
- [Leetcode]Clone Graph
- * Clone Graph
- LeetCode Clone Graph
- lintcode-medium-Clone Graph
- leetcode:clone graph