Example #1
0
        public UndirectedGraphNode CloneGraphMethod(UndirectedGraphNode node)
        {
            if (null == node)
            {
                return(null);
            }
            var queue = new Queue <UndirectedGraphNode>();
            var dic   = new Dictionary <UndirectedGraphNode, UndirectedGraphNode>();

            queue.Enqueue(node);
            dic.Add(node, new UndirectedGraphNode(node.label));

            while (queue.Count != 0)
            {
                var useNode = queue.Dequeue();

                foreach (var oneNode in useNode.neighbors)
                {
                    if (!dic.ContainsKey(oneNode))
                    {
                        dic.Add(oneNode, new UndirectedGraphNode(oneNode.label));
                        queue.Enqueue(oneNode);
                    }
                    dic[useNode].neighbors.Add(dic[oneNode]);
                }
            }

            return(dic[node]);
        }
Example #2
0
        public UndirectedGraphNode CloneGraph(UndirectedGraphNode node)
        {
            if (node == null)
            {
                return(null);
            }

            var dic       = new Dictionary <UndirectedGraphNode, UndirectedGraphNode>();
            var isVisited = new HashSet <UndirectedGraphNode>();
            var queue     = new Queue <UndirectedGraphNode>();

            var result = GetOrAddGraphNodeMapping(dic, node);

            queue.Enqueue(node);
            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                if (!isVisited.Contains(current))
                {
                    var cloneNode = GetOrAddGraphNodeMapping(dic, current);

                    foreach (UndirectedGraphNode neighbor in current.neighbors)
                    {
                        var cloneNeighbor = GetOrAddGraphNodeMapping(dic, neighbor);
                        cloneNode.neighbors.Add(cloneNeighbor);
                        queue.Enqueue(neighbor);
                    }
                    isVisited.Add(current);
                }
            }

            return(result);
        }
Example #3
0
        private UndirectedGraphNode CloneNode(UndirectedGraphNode node, Dictionary <UndirectedGraphNode, UndirectedGraphNode> graphMap)
        {
            if (node == null)
            {
                return(null);
            }

            UndirectedGraphNode clonedNode = new UndirectedGraphNode(node.label);

            graphMap.Add(node, clonedNode);

            UndirectedGraphNode neighborNode = null;

            for (int i = 0; i < node.neighbors.Count; i++)
            {
                if (node.neighbors[i] == null)
                {
                    clonedNode.neighbors.Add(node.neighbors[i]);
                    continue;
                }

                if (graphMap.ContainsKey(node.neighbors[i]))
                {
                    neighborNode = graphMap[node.neighbors[i]];
                }
                else
                {
                    neighborNode = CloneNode(node.neighbors[i], graphMap);
                }
                clonedNode.neighbors.Add(neighborNode);
            }

            return(clonedNode);
        }
Example #4
0
        public UndirectedGraphNode CloneGraph(UndirectedGraphNode node)
        {
            if (node == null)
            {
                return(node);
            }

            Dictionary <UndirectedGraphNode, UndirectedGraphNode> graphMap = new Dictionary <UndirectedGraphNode, UndirectedGraphNode>();
            UndirectedGraphNode startNode = CloneNode(node, graphMap);

            return(startNode);
        }
Example #5
0
 private UndirectedGraphNode GetOrAddGraphNodeMapping(
     Dictionary <UndirectedGraphNode, UndirectedGraphNode> dic,
     UndirectedGraphNode node)
 {
     if (dic.ContainsKey(node))
     {
         return(dic[node]);
     }
     else
     {
         var newNode = new UndirectedGraphNode(node.label);
         dic.Add(node, newNode);
         return(newNode);
     }
 }
Example #6
0
        private static UndirectedGraphNode DFSClone(UndirectedGraphNode node)
        {
            if (node == null)
            {
                return(null);
            }

            if (nodeMap.ContainsKey(node))
            {
                return(nodeMap[node]);
            }

            var newNode = new UndirectedGraphNode(node.label);

            nodeMap.Add(node, newNode);
            foreach (var n in node.neighbors)
            {
                var childNode = DFSClone(n);
                newNode.neighbors.Add(childNode);
            }

            return(newNode);
        }
Example #7
0
        private static void Main()
        {
            // "2[z]2[2[y]pq1[2[jk]e1[f]]]ef"
            var decoded = DecodeString("2[z]2[2[y]pq1[2[jk]e1[f]]]ef");
            var node    = new UndirectedGraphNode(0);

            node.neighbors = new List <UndirectedGraphNode> {
                node, node
            };
            var clone        = CloneGraph(node);
            var postfix      = EvalRPN(new string[] { "10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+" });
            var temperatures = new int[] { 73, 74, 75, 71, 69, 72, 76, 73 };
            var result       = DailyTemperatures(temperatures);

            var mStack = new MinStack();

            mStack.Push(2147483646);
            mStack.Push(2147483646);
            mStack.Push(2147483647);
            var top = mStack.Top();

            mStack.Pop();
            var getMin = mStack.GetMin();

            mStack.Pop();
            getMin = mStack.GetMin();
            mStack.Pop();
            mStack.Push(2147483647);
            top    = mStack.Top();
            getMin = mStack.GetMin();
            mStack.Push(-2147483648);
            top    = mStack.Top();
            getMin = mStack.GetMin();
            mStack.Pop();
            getMin = mStack.GetMin();
        }
Example #8
0
 public static UndirectedGraphNode CloneGraph(UndirectedGraphNode node)
 {
     return(DFSClone(node));
 }