题目:

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
/ \
\_/

链接: http://leetcode.com/problems/clone-graph/

题解:

拷贝图。图的遍历,主要就是DFS和BFS, 这道题考察基本功。需要注意的地点是如何建立visited数组,这道题因为lable unique,所以可以建立Map<Integer, UndirectedGraphNode>,假如lable有重复值,则建立Map的时候要使用Map<UndirectedGraphNode, UndirectedGraphNode>。 基本题目要多练习,这样拓展到难题以后才能借鉴思路。二刷的时候要注意recursive和iterative。

DFS:

Time Complexity - O(n), Space Complexity - O(n)

/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
HashMap<Integer, UndirectedGraphNode> visited = new HashMap<>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null)
return node;
if(visited.containsKey(node.label))
return visited.get(node.label); UndirectedGraphNode clone = new UndirectedGraphNode(node.label);
visited.put(clone.label, clone); for(UndirectedGraphNode neighbor : node.neighbors)
clone.neighbors.add(cloneGraph(neighbor)); return clone;
}
}

BFS:

Time Complexity - O(n), Space Complexity - O(n)

/**
* 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) {
if(node == null)
return node; Queue<UndirectedGraphNode> q = new LinkedList<>();
q.offer(node);
HashMap<Integer, UndirectedGraphNode> visited = new HashMap<>();
visited.put(node.label, new UndirectedGraphNode(node.label)); while(!q.isEmpty()) {
UndirectedGraphNode newNode = q.poll(); for(UndirectedGraphNode neighbor : newNode.neighbors) {
if(!visited.containsKey(neighbor.label)) {
q.offer(neighbor);
visited.put(neighbor.label, new UndirectedGraphNode(neighbor.label));
}
visited.get(newNode.label).neighbors.add(visited.get(neighbor.label));
}
} return visited.get(node.label);
}
}

二刷:

Java:

DFS:

/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
Map<Integer, UndirectedGraphNode> map = new HashMap<>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) return null;
if (map.containsKey(node.label)) return map.get(node.label);
UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
map.put(newNode.label, newNode); for (UndirectedGraphNode neighbor : node.neighbors) {
newNode.neighbors.add(cloneGraph(neighbor));
}
return newNode;
}
}

BFS:

/**
* 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) {
if (node == null) return null;
Queue<UndirectedGraphNode> q = new LinkedList<>();
q.offer(node);
Map<Integer, UndirectedGraphNode> visited = new HashMap<>();
visited.put(node.label, new UndirectedGraphNode(node.label)); while (!q.isEmpty()) {
UndirectedGraphNode oldNode = q.poll();
for (UndirectedGraphNode neighbor : oldNode.neighbors) {
if (!visited.containsKey(neighbor.label)) {
q.offer(neighbor);
visited.put(neighbor.label, new UndirectedGraphNode(neighbor.label));
}
visited.get(oldNode.label).neighbors.add(visited.get(neighbor.label));
}
}
return visited.get(node.label);
}
}

Reference:

http://www.cnblogs.com/springfor/p/3874591.html

http://blog.csdn.net/linhuanmars/article/details/22715747

http://blog.csdn.net/fightforyourdream/article/details/17497883

http://www.programcreek.com/2012/12/leetcode-clone-graph-java/

https://leetcode.com/discuss/26988/depth-first-simple-java-solution

https://leetcode.com/discuss/44330/java-bfs-solution

https://leetcode.com/discuss/14969/simple-java-iterative-bfs-solution-with-hashmap-and-queue

133. Clone Graph的更多相关文章

  1. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  2. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  3. 133. Clone Graph (3 solutions)——无向无环图复制

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  4. leetcode 133. Clone Graph ----- java

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  5. 133. Clone Graph(图的复制)

    Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains ...

  6. 133. Clone Graph (Graph, Map; DFS)

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  8. Java for LeetCode 133 Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  9. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

随机推荐

  1. viewpager双层嵌套,子viewpager无限循环无法手动滑动

    项目中首页是用viewpager+fragment集成的,第一个fragment有广告轮播图使用viewpager实现的,开始就遇到是广告图无法手动滑动,事件被外层的viewpager拦截响应切换到下 ...

  2. 我的MFC学习之路(一)

    因为项目需求,我开始应用MFC写程序.具体接触MFC的时间大概也有两个月了.现在的水平算是刚刚踏入了MFC大门的半只脚.目前能基本使用MFC Class Wizard,可以根据实例仿照完成需求,小范围 ...

  3. PRINTDLG 结构体

    //包含 PrintDlg 函数用来初始化Print Dialog Box的信息,在用户关闭窗口后,返回用户选择的信息typedef struct tagPD { DWORD lStructSize; ...

  4. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  5. C语言遍历一个文件夹下面的所有文件

    主要用到的函数/function. These should get you started: opendir() readdir() closedir() fopen() fread() fwrit ...

  6. Cannot connect to (local) sql server 2008

    Make following steps to solve the issue: Cannot connect to (local). ADDITIONAL INFORMATION: Login fa ...

  7. 创建线程的两种方式比较Thread VS Runnable

    1.首先来说说创建线程的两种方式 一种方式是继承Thread类,并重写run()方法 public class MyThread extends Thread{ @Override public vo ...

  8. apache 工作模式

    apache三种工作模式: prefork(2.4前默认)/worker/event(2.4默认)内容整理来自以下网站http://m.blog.csdn.net/article/details?id ...

  9. ASP.NET输入文本框自动提示功能

    在ASP.NET Web开发中会经常用到自动提示功能,比如百度搜索.我们只要输入相应的关键字,就可以自动得到相似搜索关键字的提示,方便我们快速的输入关键字进行查询. 那么在ASP.NET中,如果我们需 ...

  10. css3太极图效果+自动旋转

    主要使用border-radius属性实现圆,半圆,定位坐标覆盖部分模块. 半圆: width: 50%; height: 100%; border-radius:100% 0 0 100% /50% ...