コード例 #1
0
        public override DecompositionTree Construct(Graph graph, WidthParameter widthparameter)
        {
            DecompositionTree tree = new DecompositionTree(graph, widthparameter);

            // Create the leaves.
            DecompositionNode[] nodes = new DecompositionNode[graph.Vertices.Count];
            for (int i = 0; i < nodes.Length; i++)
            {
                tree.Nodes[i] = nodes[i] = new DecompositionNode(new BitSet(nodes.Length, graph.Vertices[i].Index), i, tree);
            }

            int size = nodes.Length;

            while (size > 1)
            {
                // Find the pair of nodes whose combination is of minimal width.
                double min = double.PositiveInfinity;
                int    first = -1, second = -1;
                for (int i = 0; i < size; i++)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        double width = widthparameter.GetWidth(graph, nodes[i].Set | nodes[j].Set);
                        if (width < min)
                        {
                            min    = width;
                            first  = i;
                            second = j;
                        }
                    }
                }
                // Create the parent and connect it to its children.
                DecompositionNode node = new DecompositionNode(nodes[first].Set | nodes[second].Set, nodes.Length * 2 - size, tree);
                tree.Attach(node, nodes[first], Branch.Left);
                tree.Attach(node, nodes[second], Branch.Right);
                tree.Nodes[node.Index] = node;

                // Update the active set of nodes.
                nodes[first]  = node;
                nodes[second] = nodes[size - 1];

                size--;
            }

            tree.Attach(null, nodes[0], Branch.Left);
            tree.ComputeWidth();
            return(tree);
        }
コード例 #2
0
        public override DecompositionTree Construct(Graph graph, WidthParameter widthparameter)
        {
            DecompositionTree tree = new DecompositionTree(graph, widthparameter);

            // Create the leaves.
            DecompositionNode[] nodes = new DecompositionNode[graph.Vertices.Count];
            for (int i = 0; i < nodes.Length; i++)
            {
                tree.Nodes[i] = nodes[i] = new DecompositionNode(new BitSet(nodes.Length, graph.Vertices[i].Index), i, tree);
            }

            int size = nodes.Length;

            while (size > 1)
            {
                int first  = this.rng.Next(size);
                int second = this.rng.Next(size - 1);
                if (second <= first)
                {
                    second++;
                }

                // Create the parent and connect it to its children.
                DecompositionNode node = new DecompositionNode(nodes[first].Set | nodes[second].Set, nodes.Length * 2 - size, tree);
                tree.Attach(node, nodes[first], Branch.Left);
                tree.Attach(node, nodes[second], Branch.Right);
                tree.Nodes[node.Index] = node;

                // Update the active set of nodes.
                nodes[first]  = node;
                nodes[second] = nodes[size - 1];

                size--;
            }

            tree.Attach(null, nodes[0], Branch.Left);
            tree.ComputeWidth();
            return(tree);
        }
コード例 #3
0
        public override DecompositionTree Construct(Graph graph, WidthParameter widthparameter)
        {
            int index = 0;
            DecompositionTree tree = new DecompositionTree(graph, widthparameter);
            DecompositionNode root = new DecompositionNode(~(new BitSet(tree.VertexCount)), index, tree);

            tree.Nodes[index++] = root;
            tree.Attach(null, root, Branch.Left);

            List <DecompositionNode> candidates = new List <DecompositionNode>();

            candidates.Add(root);

            while (candidates.Count > 0)
            {
                // Select a random childless internal node.
                DecompositionNode parent = candidates[this.rng.Next(candidates.Count)];
                candidates.Remove(parent);

                // Shuffle its set of vertices.
                int[] indices = parent.Set.ToArray();
                indices.Shuffle(this.rng);

                // Split the set randomly into two non-empty sets.
                int split = 1 + this.rng.Next(indices.Length - 2);

                // Create its children.
                DecompositionNode left = null;
                if (split == 1)
                {
                    left = new DecompositionNode(graph.Vertices[indices[0]], index, tree);
                }
                else
                {
                    BitSet leftset = new BitSet(tree.VertexCount);
                    for (int i = 0; i < split; i++)
                    {
                        leftset[indices[i]] = true;
                    }
                    left = new DecompositionNode(leftset, index, tree);
                    candidates.Add(left);
                }
                tree.Nodes[index++] = left;
                tree.Attach(parent, left, Branch.Left);

                DecompositionNode right = null;
                if (split == indices.Length - 1)
                {
                    right = new DecompositionNode(graph.Vertices[indices[indices.Length - 1]], index, tree);
                }
                else
                {
                    BitSet rightset = new BitSet(tree.VertexCount);
                    for (int i = split; i < indices.Length; i++)
                    {
                        rightset[indices[i]] = true;
                    }
                    right = new DecompositionNode(rightset, index, tree);
                    candidates.Add(right);
                }
                tree.Nodes[index++] = right;
                tree.Attach(parent, right, Branch.Right);
            }

            tree.ComputeWidth();
            return(tree);
        }