您的位置:首页 > 其它

Clone Graph

2017-09-28 00:00 253 查看
问题:

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 node
0
to both nodes
1
and
2
.

Second node is labeled as
1
. Connect node
1
to node
2
.

Third node is labeled as
2
. Connect node
2
to node
2
(itself), thus forming a self-cycle.

Visually, the graph looks like the following:

1
/ \
/   \
0 --- 2
/ \
\_/

解决:

【题意】克隆无向图。图中的每一个节点包含一个label和 一组neighbors。

本题中的无向图的定义为:

每个节点都是唯一标记的。

我们通过#来划分每一个节点, 通过,来划分节点标记和节点的每一个邻居。

举个例子,给定一个图{0,1,2#1,2#2,2}.

这个图中共3个节点,因此包含了通过#划分的三个部分.

1.第一个节点被标记为 0.连接节点0和节点 1and 2.

2.第二个节点被标记为 1.将节点1和 节点2相连接.

3.第三个节点被标记为 2. 将节点2和节点2(它自身)想连接,这样就形成了自环.

这个图就如下面所示:

1
/ \
/   \
0 --- 2
/ \
\_/

① dfs,使用map保存已经遍历过的节点。

/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution { //7ms
Map<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<>();//必须放在外面,否则会栈溢出。
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return null;
}
if (map.containsKey(node)) {
return map.get(node);
}
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
map.put(node,newHead);
for (UndirectedGraphNode aNeighbor : node.neighbors ) {
newHead.neighbors.add(cloneGraph(aNeighbor));
}
return newHead;
}
}

② 将递归方法提取出来。

public class Solution {//7ms
HashMap<Integer, UndirectedGraphNode> map = new HashMap<>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
return dfs(node);
}
private UndirectedGraphNode dfs(UndirectedGraphNode node) {
if (node == null) return null;
if (map.containsKey(node.label)) {
return map.get(node.label);
}
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
map.put(newHead.label, newHead);
for (UndirectedGraphNode neighbor : node.neighbors) {
newHead.neighbors.add(dfs(neighbor));
}
return newHead;
}
}

③ BFS。

public class Solution { //8ms
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return null;
}
Queue<UndirectedGraphNode> queue = new LinkedList<>();
HashMap<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<>();
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
queue.add(node);
map.put(node,newHead);
while(! queue.isEmpty()){
UndirectedGraphNode cur = queue.poll();
for (UndirectedGraphNode aNeighbor : cur.neighbors ) {
if (! map.containsKey(aNeighbor)) {
UndirectedGraphNode copy = new UndirectedGraphNode(aNeighbor.label);
map.put(aNeighbor,copy);
map.get(cur).neighbors.add(copy);
queue.add(aNeighbor);
}else{
map.get(cur).neighbors.add(map.get(aNeighbor));
}
}
}
return newHead;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: