描述
克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors。数据中如何表示一个无向图?http://www.lintcode.com/help/graph/你的程序需要返回一个经过深度拷贝的新图。这个新图和原图具有同样的结构,并且对新图的任何改动不会对原图造成任何影响。
样例
比如,序列化图 {0,1,2#1,2#2,2} 共有三个节点, 因此包含两个个分隔符#。
第一个节点label为0,存在边从节点0链接到节点1和节点2
第二个节点label为1,存在边从节点1连接到节点2
第三个节点label为2,存在边从节点2连接到节点2(本身),从而形成自环。
我们能看到如下的图:
1
/ \
/ \
0 --- 2
/ \
\_/
代码
- 3steps
/**
* 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) {
if (node == null) {
return node;
}
// use bfs algorithm to traverse the graph and get all nodes.
ArrayList<UndirectedGraphNode> nodes = getNodes(node);
// copy nodes, store the old->new mapping information in a hash map
HashMap<UndirectedGraphNode, UndirectedGraphNode> mapping = new HashMap<>();
for (UndirectedGraphNode n : nodes) {
// 创建hashmap将原图和新图之间结点对应关系存在hashmap中
mapping.put(n, new UndirectedGraphNode(n.label));
}
// copy neighbors(edges)
for (UndirectedGraphNode n : nodes) {
// 复制的新的结点,不能写newNode = n这样复制的是引用,我们需要的是deep copy
UndirectedGraphNode newNode = mapping.get(n);
// neighbors是定义好的存储整型下标的数组
for (UndirectedGraphNode neighbor : n.neighbors) {
// 复制相邻结点,复制相邻结点和复制的结点建立边的关系
UndirectedGraphNode newNeighbor = mapping.get(neighbor);
newNode.neighbors.add(newNeighbor);
}
}
// 从输入的结点开始逐渐返回图中每个结点对应的信息
return mapping.get(node);
}
// 用bfs由点到面得到所有结点信息,返回一个包含所有结点的数组
private ArrayList<UndirectedGraphNode> getNodes(UndirectedGraphNode node) {
Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
// HashSet不允许有相同元素,如果b已是集合中元素,再执行set.add(b)无效
HashSet<UndirectedGraphNode> set = new HashSet<>();
queue.offer(node);
set.add(node);
// bfs利用队列来控制遍历图中结点的顺序
while (!queue.isEmpty()) {
UndirectedGraphNode head = queue.poll();
for (UndirectedGraphNode neighbor : head.neighbors) {
if(!set.contains(neighbor)){
set.add(neighbor);
queue.offer(neighbor);
}
}
}
// 所有结点都已添加到 set,此处用深度拷贝是因为外边要用ArrayList,而此处是hashset
return new ArrayList<UndirectedGraphNode>(set);
}
}
- two steps
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* ArrayList<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return null;
}
ArrayList<UndirectedGraphNode> nodes = new ArrayList<UndirectedGraphNode>();
HashMap<UndirectedGraphNode, UndirectedGraphNode> map
= new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
// clone nodes
nodes.add(node);
map.put(node, new UndirectedGraphNode(node.label));
int start = 0;
while (start < nodes.size()) {
UndirectedGraphNode head = nodes.get(start++);
for (int i = 0; i < head.neighbors.size(); i++) {
UndirectedGraphNode neighbor = head.neighbors.get(i);
if (!map.containsKey(neighbor)) {
map.put(neighbor, new UndirectedGraphNode(neighbor.label));
nodes.add(neighbor);
}
}
}
// clone neighbors
for (int i = 0; i < nodes.size(); i++) {
UndirectedGraphNode newNode = map.get(nodes.get(i));
for (int j = 0; j < nodes.get(i).neighbors.size(); j++) {
newNode.neighbors.add(map.get(nodes.get(i).neighbors.get(j)));
}
}
return map.get(node);
}
}
- Non-Recursion DFS
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* ArrayList<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
class StackElement {
public UndirectedGraphNode node;
public int neighborIndex;
public StackElement(UndirectedGraphNode node, int neighborIndex) {
this.node = node;
this.neighborIndex = neighborIndex;
}
}
public class Solution {
/**
* @param node: A undirected graph node
* @return: A undirected graph node
*/
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return node;
}
// use dfs algorithm to traverse the graph and get all nodes.
ArrayList<UndirectedGraphNode> nodes = getNodes(node);
// copy nodes, store the old->new mapping information in a hash map
HashMap<UndirectedGraphNode, UndirectedGraphNode> mapping = new HashMap<>();
for (UndirectedGraphNode n : nodes) {
mapping.put(n, new UndirectedGraphNode(n.label));
}
// copy neighbors(edges)
for (UndirectedGraphNode n : nodes) {
UndirectedGraphNode newNode = mapping.get(n);
for (UndirectedGraphNode neighbor : n.neighbors) {
UndirectedGraphNode newNeighbor = mapping.get(neighbor);
newNode.neighbors.add(newNeighbor);
}
}
return mapping.get(node);
}
private ArrayList<UndirectedGraphNode> getNodes(UndirectedGraphNode node) {
Stack<StackElement> stack = new Stack<StackElement>();
HashSet<UndirectedGraphNode> set = new HashSet<>();
stack.push(new StackElement(node, -1));
set.add(node);
while (!stack.isEmpty()) {
StackElement current = stack.peek();
current.neighborIndex++;
// there is no more neighbor to traverse for the current node
if (current.neighborIndex == current.node.neighbors.size()) {
stack.pop();
continue;
}
UndirectedGraphNode neighbor = current.node.neighbors.get(
current.neighborIndex
);
// check if we visited this neighbor before
if (set.contains(neighbor)) {
continue;
}
stack.push(new StackElement(neighbor, -1));
set.add(neighbor);
}
return new ArrayList<UndirectedGraphNode>(set);
}
}