zoukankan      html  css  js  c++  java
  • LeetCode-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
             / 
             \_/
    

    Solution:
     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 
    13         Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
    14         Set<UndirectedGraphNode> visited = new HashSet<UndirectedGraphNode>();
    15         cloneMap.put(node, new UndirectedGraphNode(node.label)); 
    16         
    17         return cloneGraphRecur(node,cloneMap,visited);
    18     }
    19 
    20     public UndirectedGraphNode cloneGraphRecur(UndirectedGraphNode cur, Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap, Set<UndirectedGraphNode> visited){
    21         UndirectedGraphNode curClone = cloneMap.get(cur);
    22         visited.add(cur);
    23 
    24         //Clone all the neighbors of cur node.
    25         for (int i=0;i<cur.neighbors.size();i++){
    26             UndirectedGraphNode neigh = cur.neighbors.get(i);
    27             if (cloneMap.containsKey(neigh)){
    28                 curClone.neighbors.add(cloneMap.get(neigh));
    29             } else {
    30                 UndirectedGraphNode neighClone = new UndirectedGraphNode(neigh.label);
    31                 cloneMap.put(neigh,neighClone);
    32                 curClone.neighbors.add(neighClone);
    33             }
    34         }
    35 
    36         //Visit all unvisited neighbors of cur node.
    37         for (int i=0;i<cur.neighbors.size();i++)
    38             if (!visited.contains(cur.neighbors.get(i)))
    39                 cloneGraphRecur(cur.neighbors.get(i),cloneMap,visited);
    40 
    41         return curClone;
    42     }
    43             
    44 }

    Solution (BFS):

     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 
    13         Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
    14         List<UndirectedGraphNode> visited = new ArrayList<UndirectedGraphNode>();
    15         cloneMap.put(node, new UndirectedGraphNode(node.label)); 
    16         visited.add(node);
    17         int index = 0;
    18         while (index<visited.size()){
    19             UndirectedGraphNode cur = visited.get(index);
    20             UndirectedGraphNode curClone = cloneMap.get(cur);
    21 
    22             //Clone all the neighbors of cur node.
    23             for (int i=0;i<cur.neighbors.size();i++){
    24                 UndirectedGraphNode neigh = cur.neighbors.get(i);
    25             if (cloneMap.containsKey(neigh)){
    26                     curClone.neighbors.add(cloneMap.get(neigh));
    27                 } else {
    28                     UndirectedGraphNode neighClone = new UndirectedGraphNode(neigh.label);
    29                     cloneMap.put(neigh,neighClone);
    30                 curClone.neighbors.add(neighClone);
    31                 visited.add(neigh);
    32                 }
    33             }
    34 
    35             index++;
    36         }
    37         
    38         return cloneMap.get(node);
    39 
    40 
    41     }            
    42 }
  • 相关阅读:
    how to uninstall devkit
    asp.net中bin目录下的 dll.refresh文件
    查找2个分支的共同父节点
    Three ways to do WCF instance management
    WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
    检查string是否为double
    How to hide TabPage from TabControl
    获取当前系统中的时区
    git svn cygwin_exception
    lodoop打印控制具体解释
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4202653.html
Copyright © 2011-2022 走看看