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 }
  • 相关阅读:
    arcengine动态显示所需字段值
    arcengine标注转注记
    AreEngine 求最小面积的外接矩形,非IEnvelope,表达不清楚了
    数据库压缩(以mdb为测试对象,Arcengine——ILocalDataCompact接口与IDataCompact接口的使用)
    iMac环境下 配置flutter镜像
    微信小程序radio点选框,再次点击取消
    修改状态栏的颜色
    ios10 设置权限
    使用js动态更改:after的值
    canvas时钟
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6553610.html
Copyright © 2011-2022 走看看