zoukankan      html  css  js  c++  java
  • 133. Clone Graph

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

    这道题目还是比较难的,用DFS和BFS都可以做出来,首先说DFS,dfs的终止条件就是把第一个节点的所有邻居节点全部遍历到就算终止了,然后每次遍历邻居节点的时候,邻居节点的所有邻居也会
    遍历到,注意本题的判断是否node为空只是一种特殊情况,代码如下:
     1 /**
     2  * Definition for undirected graph.
     3  * class UndirectedGraphNode {
     4  *     int label;
     5  *     List<UndirectedGraphNode> neighbors;
     6  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
     7  * };
     8  */
     9 public class Solution {
    10     Map<Integer,UndirectedGraphNode> map = new HashMap<>();
    11     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
    12         if(node==null) return null;
    13         if(map.containsKey(node.label)){
    14             return map.get(node.label);
    15         }
    16         UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
    17         map.put(node.label,copy);
    18         for(UndirectedGraphNode un:node.neighbors){
    19             copy.neighbors.add(cloneGraph(un));
    20         }
    21         return copy;
    22     }
    23 }

     本题还可以用BFS来做,想法是使用一个queue来存储node以及neighbor,hashmap来存储copy的node和neighbor,代码如下:

     1 /**
     2  * Definition for undirected graph.
     3  * class UndirectedGraphNode {
     4  *     int label;
     5  *     List<UndirectedGraphNode> neighbors;
     6  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
     7  * };
     8  */
     9 public class Solution {
    10     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
    11         if(node==null) return null;
    12         Map<Integer,UndirectedGraphNode> map = new HashMap<>();
    13         UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
    14         map.put(copy.label,copy);
    15         LinkedList<UndirectedGraphNode> queue= new LinkedList<>();
    16         queue.add(node);
    17         while(!queue.isEmpty()){
    18             UndirectedGraphNode n = queue.pop();
    19             for(UndirectedGraphNode neighbor:n.neighbors){
    20                 if(!map.containsKey(neighbor.label)){
    21                     map.put(neighbor.label,new UndirectedGraphNode(neighbor.label));
    22                     queue.add(neighbor);
    23                 }
    24                 map.get(n.label).neighbors.add(map.get(neighbor.label));
    25             }
    26         }
    27         return copy;
    28     }
    29 }
  • 相关阅读:
    简单实现抽象工厂模式
    mongodb 最新版安装和配置(单机版)
    排序的三个基础算法 (python实现)
    关于学习,关于工具
    嵌入式linux教程
    用软件工程分析开源项目octave的移植
    C++高质量编程笔记
    从高级软件工程角度分析毕业设计项目存在的问题
    史话未完待续。。。
    乔治布尔
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6553610.html
Copyright © 2011-2022 走看看