Esempio n. 1
0
        private UndirectedGraphNode Dfs(UndirectedGraphNode node, Dictionary <int, UndirectedGraphNode> marked)
        {
            var clone = new UndirectedGraphNode(node.label);

            marked.Add(node.label, clone);
            foreach (var neighbor in node.neighbors)
            {
                UndirectedGraphNode n = null;
                if (!marked.ContainsKey(neighbor.label))
                {
                    n = Dfs(neighbor, marked);
                }
                else
                {
                    n = marked[neighbor.label];
                }
                clone.neighbors.Add(n);
            }
            return(clone);
        }
Esempio n. 2
0
        public UndirectedGraphNode CloneGraph(UndirectedGraphNode node)
        {
            Dictionary <int, UndirectedGraphNode> marked = new Dictionary <int, UndirectedGraphNode>();

            return(Dfs(node, marked));
        }