示例#1
0
            // Union/Find Algorithm - The find routine.
            //
            // Implemented with Path Compression (inner loops are only
            // visited and collapsed once, however, deep nests would still
            // result in significant traversals).
            //
            public UnionFindNode findSet()
            {
                List <UnionFindNode> nodeList = new List <UnionFindNode>(2);

                UnionFindNode node = this;

                while (node != node.getParent())
                {
                    if (node.getParent() != node.getParent().getParent())
                    {
                        nodeList.Add(node);
                    }
                    node = node.getParent();
                }

                // Path Compression, all nodes' parents point to the 1st level parent.
                int len = nodeList.Count;

                for (int i = 0; i < len; i++)
                {
                    // for (UnionFindNode iter : nodeList)
                    UnionFindNode iter = nodeList[i];
                    iter.setParent(node.getParent());
                }
                return(node);
            }