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. <老古董>线性支持向量机中的硬间隔(hard margin)和软间隔(soft margin)是什么

    _________________________________________________________________________________________________ Th ...

  2. require(): open_basedir restriction in effect. File(/www/wwwroot/xcx/zerg/thinkphp/start.php) is not within the allowed path(s): (/www/wwwroot/xcx/zerg/public/:/tmp/:/proc/) in /www/wwwroot/xcx/zerg/p

    解决方法: 在如下文件增加一项(如图所示) 在如下文件增加一项(如图所示): #php文件采用fastcgi解析并设置参数    location ~ \.php {        try_files ...

  3. 132-PHP子类和父类同名函数的调用

    <?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...

  4. 通过GlobalAddAtom,GlobalGetAtomName方式发送字符串

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. flutter文本简单实现

    import 'package:flutter/material.dart'; import 'package:flustars/flustars.dart'; import 'package:fl_ ...

  6. python练习(一)----打印九九乘法表

    打印九九乘法表 ,): ,i+): print("{0} x {1} = {2} \t".format(j,i,i*j),end='') //print默认end=‘\n’, pr ...

  7. P1036 跟奥巴马一起编程

    转跳点:

  8. 每天一点点之 taro 框架开发 - taro静态资源引入

    1.说明: taro中客园自由的引用静态资源,不需要安装任何的loader 引用样式文件 通过ES6的import引入 2.引用js文件 import { functionName } from '. ...

  9. UML概念

    UML UML:Unified Modeling Language是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明,可视化,结构和编写一个正在开发的,面向对象的,软件密集系统的制品开放 ...

  10. ImportError: dynamic module does not define init function (initcaffe)

    https://github.com/BVLC/caffe/issues/2770 $ python2 -c "import caffe" Traceback (most rece ...