コード例 #1
0
        public static UndirectedGraphNode cloneGraph(UndirectedGraphNode node)
        {
            if (node == null)
            {
                return(null);
            }
            Dictionary <UndirectedGraphNode, UndirectedGraphNode> dic = new Dictionary <UndirectedGraphNode, UndirectedGraphNode> ();
            Queue <UndirectedGraphNode> q       = new Queue <UndirectedGraphNode> ();
            UndirectedGraphNode         newNode = new UndirectedGraphNode(node.label);

            q.Enqueue(node);
            dic.Add(node, newNode);
            while (q.Count != 0)
            {
                UndirectedGraphNode        currNode  = q.Dequeue();
                List <UndirectedGraphNode> neighbors = currNode.neighbors;
                foreach (UndirectedGraphNode neighbor in neighbors)
                {
                    if (!dic.ContainsKey(neighbor))
                    {
                        q.Enqueue(neighbor);
                        UndirectedGraphNode copyNeighbor = new UndirectedGraphNode(neighbor.label);
                        dic [currNode].neighbors.Add(copyNeighbor);
                        dic.Add(neighbor, copyNeighbor);
                    }
                    else
                    {
                        dic [currNode].neighbors.Add(dic [neighbor]);
                    }
                }
            }
            return(newNode);
        }
コード例 #2
0
    		public UndirectedGraphNode CloneGraph(UndirectedGraphNode node) {
        		if (node == null)
				return null;
			Dictionary<UndirectedGraphNode, UndirectedGraphNode> dic = new Dictionary<UndirectedGraphNode, UndirectedGraphNode>();			
			Queue<UndirectedGraphNode> q = new Queue<UndirectedGraphNode>();
			UndirectedGraphNode newNode = new UndirectedGraphNode (node.label);
			q.Enqueue(node);
			dic.Add(node, newNode);
			while (q.Count != 0) {
				UndirectedGraphNode currNode = q.Dequeue();
				foreach (UndirectedGraphNode neighbor in currNode.neighbors) {
					if (!dic.ContainsKey (neighbor)) {
						q.Enqueue(neighbor);
						UndirectedGraphNode copyNeighbor = new UndirectedGraphNode (neighbor.label);
						dic[currNode].neighbors.Add(copyNeighbor);
						dic.Add(neighbor, copyNeighbor);
					} else
						dic[currNode].neighbors.Add (dic[neighbor]);
				}
			}
			return newNode;
    }