之所以把这道题单独拿出来,是因为通过它我们可以了解到图的结构,以及如何处理,我们分别用递归,广搜和深搜来完成这道题。
Clone Graph
复制一个无向图,图中每个节点都有一个label和一个neighbors集合。
解决图的题,因为图中存在环,我们要判断哪些点已经访问过了,做上标记,以防止进入死循环。这里我们用哈希函数来判断一个顶点是否被访问过,如果没被访问过就加入到哈希表中。
首先我们通过广搜来完成它。广搜用到队列,代码如下:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
if(node == null) return null;
UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
hm.put(node,copy);
queue.offer(node);
while(!queue.isEmpty()) {
UndirectedGraphNode cur = queue.poll();
for(UndirectedGraphNode neighbor : cur.neighbors) {
if(!hm.containsKey(neighbor)) {
copy = new UndirectedGraphNode(neighbor.label);
hm.put(neighbor, copy);
queue.offer(neighbor);
}
hm.get(cur).neighbors.add(hm.get(neighbor));
}
}
return hm.get(node);
}
}
深度搜索用到堆栈,代码如下:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
Stack<UndirectedGraphNode> stack = new Stack<UndirectedGraphNode>();
if(node == null) return null;
stack.push(node);
UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
hm.put(node,copy);
while(!stack.isEmpty()) {
UndirectedGraphNode cur = stack.pop();
for(UndirectedGraphNode neighbor : cur.neighbors) {
if(!hm.containsKey(neighbor)) {
copy = new UndirectedGraphNode(neighbor.label);
hm.put(neighbor, copy);
stack.push(neighbor);
}
hm.get(cur).neighbors.add(hm.get(neighbor));
}
}
return hm.get(node);
}
}
递归实现:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
return DFS(hm, node);
}
private UndirectedGraphNode DFS(HashMap<UndirectedGraphNode, UndirectedGraphNode> hm, UndirectedGraphNode node) {
if(node == null) return null;
UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
hm.put(node, copy);
for(UndirectedGraphNode neighbor : node.neighbors) {
if(!hm.containsKey(neighbor)) {
UndirectedGraphNode neighborcopy = DFS(hm, neighbor);
}
copy.neighbors.add(hm.get(neighbor));
}
return copy;
}
}