博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#133]Clone Graph
阅读量:4617 次
发布时间:2019-06-09

本文共 3460 字,大约阅读时间需要 11 分钟。

The problem:

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 #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. 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         / \         \_/

 

 

My analysis:
The implementation pattern used in this problem is very classic in cloning a graph. The key idea: use a hash map to store and retrieve the copy of a node. 
The basic idea is to use the invariant:1. we dequeue a node from the queue, then we scan the node's neighbors. we check if the neighbor node has already been traversaled by using the "containKeys()" method. 1.1 iff the node is in the hashmap, we would not make a copy for it.1.2 iff the node is not in the hashmap, we make a copy for this neighbor node. Put the
into the hashmap and enqueue the neighbor node.if (!map.containsKey(cur.neighbors.get(i))) {
//this node has not been traversaled copy = new UndirectedGraphNode(cur.neighbors.get(i).label); map.put(cur.neighbors.get(i), copy); queue.offer(cur.neighbors.get(i));}Note1: we only enqueue nodes from neighbors list, which means all neighbors list of remaining nodes in the queue(until the dequeue) has not been copyed yet. Thus, for each current node's neghbor list, we need to fully copy it. for (int i = 0; i < cur.neighbors.size(); i++) { if (!map.containsKey(cur.neighbors.get(i))) { ... } map.get(cur).neighbors.add(map.get(cur.neighbors.get(i)));}Note2: to use the invariant, we need to make necessary preparation for it (dummy layer). we should suppose there is dummy layer copy the starting node, and then put it into hashmap and enqueue the starting node.

My solution:

public class Solution {    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {                if (node == null)            return null;                HashMap
map = new HashMap
(); Queue
queue = new LinkedList
(); UndirectedGraphNode cur; UndirectedGraphNode copy = new UndirectedGraphNode(node.label); queue.offer(node); map.put(node, copy); while(!queue.isEmpty()) { cur = queue.poll(); for (int i = 0; i < cur.neighbors.size(); i++) { if (!map.containsKey(cur.neighbors.get(i))) {
//this node has not been traversaled copy = new UndirectedGraphNode(cur.neighbors.get(i).label); map.put(cur.neighbors.get(i), copy); queue.offer(cur.neighbors.get(i)); } map.get(cur).neighbors.add(map.get(cur.neighbors.get(i))); } } return map.get(node); }}

 

转载于:https://www.cnblogs.com/airwindow/p/4220170.html

你可能感兴趣的文章
Spring AOP 实现原理
查看>>
山东大学计算机方向自主招生
查看>>
[hdu5200]离线+标记
查看>>
Java JFrame图形界面 ----一个简单的窗口
查看>>
Win7 64位系统上Hadoop单机模式的安装及开发环境搭建
查看>>
C#中的委托
查看>>
如何渲染几万条数据并不卡住界面
查看>>
玩具装箱 BZOJ 1010
查看>>
iOS的主要框架介绍
查看>>
Python 动态语言
查看>>
linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 )...
查看>>
弹性盒布局
查看>>
Angular2 -- 生命周期
查看>>
重写与重载,背了八百遍终于明白了
查看>>
SQL逻辑查询处理顺序特别提醒
查看>>
HttpClient 教程 (一)
查看>>
【BZOJ】4671: 异或图
查看>>
【LOJ】#2115. 「HNOI2015」落忆枫音
查看>>
linux下open too many files错误Socket未正确关闭的处理方法
查看>>
chrome 命令
查看>>