Make a deep copy of an undirected graph, there could be cycles in the original graph.

Assumptions

  • The given graph is not null

DFS

/*
* class GraphNode {
* public int key;
* public List<GraphNode> neighbors;
* public GraphNode(int key) {
* this.key = key;
* this.neighbors = new ArrayList<GraphNode>();
* }
* }
*/
public class Solution {
public List<GraphNode> copy(List<GraphNode> graph) {
// Write your solution here.
Map<GraphNode, GraphNode> map = new HashMap<>();
for (GraphNode node : graph) {
if (!map.containsKey(node)) {
map.put(node, new GraphNode(node.key));
}
dfs(node, map);
}
return new ArrayList<>(map.values());
} private void dfs(GraphNode node, Map<GraphNode, GraphNode> map) {
GraphNode newNode = map.get(node);
for (GraphNode gNode : node.neighbors) {
if (!map.containsKey(gNode)) {
map.put(gNode, new GraphNode(gNode.key));
}
newNode.neighbors.add(map.get(gNode));
}
}
}

BFS

/*
* class GraphNode {
* public int key;
* public List<GraphNode> neighbors;
* public GraphNode(int key) {
* this.key = key;
* this.neighbors = new ArrayList<GraphNode>();
* }
* }
*/
public class Solution {
public List<GraphNode> copy(List<GraphNode> graph) {
// Write your solution here.
Map<GraphNode, GraphNode> map = new HashMap<>();
for (GraphNode gNode: graph) {
map.put(gNode, new GraphNode(gNode.key));
}
for (GraphNode node : graph) {
GraphNode cpNode = map.get(node);
for (GraphNode nei: node.neighbors) {
cpNode.neighbors.add(map.get(nei));
}
}
return new ArrayList<>(map.values());
}
}

[Algo] 132. Deep Copy Undirected Graph的更多相关文章

  1. [Algo] 131. Deep Copy Linked List With Random Pointer

    Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...

  2. Shallow copy and Deep copy

    Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...

  3. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. C# Bitmap deep copy

    今天在研究一个关于 Bitmap deep copy 的问题, 经过一系列的查询,在StackOverFlow上面找到了答案,遂记录下来: public static Bitmap DeepCopyB ...

  5. shallow copy 和 deep copy 的示例

    本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481874.html   (Robin) Student package base; impo ...

  6. python deep copy and shallow copy

    Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...

  7. Deep Copy cv::StereoBM 深度拷贝

    在使用OpenCV的三维立体重建的库时,一个重要的步骤就是生成左右视图的差异图Disparity,而控制生成disparity的参数的类是cv::StereoBM,我们有时候需要拷贝一份cv::Ste ...

  8. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  9. Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

随机推荐

  1. jQuery省市联动(XML/JSON)

    准备: 导包 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 新建js文件夹导入jQuery配置文件 NO01:创建city.jsp页面 <%@ page lan ...

  2. 常用Java工具类

    一. org.apache.commons.io.IOUtils closeQuietly:关闭一个IO流.socket.或者selector且不抛出异常,通常放在finally块 toString: ...

  3. C#无边框应用

    引用using System.Runtime.InteropServices; 在窗体的全局模式下输入 [DllImport("user32.dll")] public stati ...

  4. 我们是如何将 ToB 服务的交付能力优化 75%?

    ToB 服务交付的方式分为公有云部署和私有化部署两种.其中,对成本敏感的中小企业往往采用公有云部署的方式,从而尽量减少成本.客单价较高的大型企业.政府.银行和事业单位,考虑到数据隐私.安全.合规等要求 ...

  5. JavaScript获取当前日期前后7天的日期

    function fun_date(aa){ var date1 = new Date(), time1=date1.getFullYear()+"-"+(date1.getMon ...

  6. Linux基础命令层级图-01

    Linux基础命令层级图-01:

  7. ZOJ - 3870 Team Formation(异或)

    题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...

  8. apache2+django+virtualenv 服务器部署实战

    目录 基本配置 配置python环境 安装 python.pip 安装 virtualenv 配置python虚拟环境 配置 apache2 安装 apache2 安装 mod-wsgi 服务 部署d ...

  9. Django学习路线

  10. 六、CI框架之分配变量

    一.在controllers里面添加 $this->load->vars('m_Str1','我是一个字符串变量'); 二.在View中添加相应代码 界面显示效果如下: 不忘初心,如果您认 ...